从Windows服务创建进程 [英] Creating a Process from a windows service

查看:109
本文介绍了从Windows服务创建进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最初编写的程序是一个c#控制台应用程序。程序

使用了我用c ++编写的引用。后来我被告知重新编写

应用程序作为Windows服务。当服务启动时,只要

它访问了引用(通过实例化引用中的类),

服务就会崩溃。在事件查看器中,我看到它抛出一个

System.IO.FileNotFoundException,表示缺少c ++引用

(即使它与...相同的目录中)。可执行程序)。我决定写一个

windows服务,只需启动控制台应用程序。在

a ?? OnStarta ??方法我调用了win32 API CreateProcess(其中IP / Invoked)。

当服务启动时,它成功启动了控制台应用程序,然后在控制台应用程序中提供
被使用的通过

控制台应用程序,控制台应用程序崩溃说

File.IO.FileNotFoundException。作为完整性检查我首先运行控制台应用程序

本身,它运行得很好。然后我创建了第二个c#控制台应用程序

并且在主要中我使用了完全相同的代码来自a ?? OnStarta?前面提到的
提到的服务(CreateProcess api)它启动了我的原始程序

就好了。


为什么有一个如果我使用来自ac#service和c#

控制台应用程序的CreateProcess,会有什么不同?还不是服务只是一个控制台应用程序,

与服务控制管理器交互?

I had originally written a program as a c# console application. The program
used a reference that I wrote in c++. Later I was told to re-write the
application as a windows service. When the service would start up, as soon as
it accessed the reference (by instantiating a class within the reference),
the service would crash. In the event viewer I saw that it throw a
System.IO.FileNotFoundException saying that the c++ reference was missing
(even though it was in the same directory as the .exe). I decided to write a
windows service that would simply launch the console application. In the
a??OnStarta?? method I called the win32 API CreateProcess (which I P/Invoked).
When the service started it launched the console application successfully,
the console application then as soon as the reference was "used" by the
console application the console application crashes saying
File.IO.FileNotFoundException. As sanity check first I ran the console app by
itself and it ran just fine. Then I created a second c# console application
and in the main I used the exact same code from the a??OnStarta?? of the afore
mentioned service (CreateProcess api) and it launched my original program
just fine.

Why is there a difference if I use CreateProcess from a c# service vs. c#
console application? Also isn''t a service simply a console application that
interacts with service control manager?

推荐答案

Noam写道:
Noam wrote:
如果我使用来自ac#service和c#
控制台应用程序的CreateProcess,为什么会有区别?还不是服务只是一个与服务控制管理器交互的控制台应用程序吗?
Why is there a difference if I use CreateProcess from a c# service vs. c#
console application? Also isn''t a service simply a console application that
interacts with service control manager?




手动运行控制台应用程序时,您已登录到

Windows。当你的服务执行时,它以不同的方式登录,使用

受限制的LocalSystem帐户。


如果不知道你想要启动的进程是什么,我不能确定你为什么得到FileNotFound异常的确切原因,但可能是因为LocalSystem帐户没有必要的权限来支持$ / $ $ $ $
访问该过程所需的一些资源。


当您将服务设置为自己登录时会发生什么,是否可以运行?b $ b然后正确运行?



When you ran the console application manually, you were logged in to
Windows. When your service executed, it logged in differently, using
the LocalSystem account which is restricted.

Without knowing what the process you are trying to start does, I can''t
say exactly why you got the FileNotFound exception, but it may be that
the LocalSystem account does not have the necessary permissions to
access some resource needed by the process.

What happens when you set the service to log in as yourself, can it
then run correctly?


非常感谢你这么快回到我身边我真的很感激。

在登录选项卡下服务我将它设置为我的domain \ namename和

密码(因此该服务未在本地系统帐户下设置)。我的

公司也使用活动目录,我是计算机的管理员。仍然

没有骰子


" Chris Dunaway"写道:
Thank you so much for getting back to me so quickly I really appreciate it.
In the Log On tab under the service I had it set to my domain\username and
password (so the service is not set under Local System account). Also my
company uses active directory and I am an administrator on my computer. Still
no dice

"Chris Dunaway" wrote:
Noam写道:
Noam wrote:
如果我使用来自ac#service和c#
控制台的CreateProcess,为什么会有区别?应用?还不是服务只是一个与服务控制管理器交互的控制台应用程序吗?
Why is there a difference if I use CreateProcess from a c# service vs. c#
console application? Also isn''t a service simply a console application that
interacts with service control manager?



手动运行控制台应用程序时,您已登录
视窗。当您的服务执行时,它使用受限制的LocalSystem帐户以不同的方式登录。

在不知道您尝试启动的过程的情况下,我不能
确切地说你为什么得到FileNotFound异常,但可能是因为LocalSystem帐户没有必要的权限来访问进程所需的一些资源。

什么当您将服务设置为自己登录时,是否可以正确运行?



When you ran the console application manually, you were logged in to
Windows. When your service executed, it logged in differently, using
the LocalSystem account which is restricted.

Without knowing what the process you are trying to start does, I can''t
say exactly why you got the FileNotFound exception, but it may be that
the LocalSystem account does not have the necessary permissions to
access some resource needed by the process.

What happens when you set the service to log in as yourself, can it
then run correctly?



您能指定完整的吗?路径而不是依赖于默认的exe文件夹?


chanmm


" Noam" <否** @ discussions.microsoft.com>在留言中写道

新闻:0C ********************************** @ microsof t.com ...
Can you specifiy the full path rather than depend on the default exe folder?

chanmm

"Noam" <No**@discussions.microsoft.com> wrote in message
news:0C**********************************@microsof t.com...
我最初编写了一个程序作为ac#console应用程序。该程序
使用了我用c ++编写的引用。后来我被告知重新编写
应用程序作为Windows服务。当服务启动时,尽快
它访问引用(通过实例化引用中的类),服务将崩溃。在事件查看器中,我看到它抛出一个
System.IO.FileNotFoundException,表示缺少c ++引用
(即使它与.exe位于同一目录中)。我决定写一个
Windows服务,只需启动控制台应用程序。在
OnStart中方法我调用了win32 API CreateProcess(我P / Invoked)。
当服务启动时,它成功启动了控制台应用程序,然后一旦引用控制台应用程序使用"通过控制台应用程序控制台应用程序崩溃说文件.IO.FileNotFoundException。作为健全检查,我首先运行控制台应用程序
本身,它运行得很好。然后我创建了第二个c#console
应用程序
在主要内容中我使用了与OnStart完全相同的代码。上面提到的服务(CreateProcess api)和它启动了我的原始程序
就好了。

如果我使用ac#service和c#中的CreateProcess,为什么会有区别?
控制台应用程序?也不仅仅是一个服务器应用程序
与服务控制管理器交互?
I had originally written a program as a c# console application. The program
used a reference that I wrote in c++. Later I was told to re-write the
application as a windows service. When the service would start up, as soon
as
it accessed the reference (by instantiating a class within the reference),
the service would crash. In the event viewer I saw that it throw a
System.IO.FileNotFoundException saying that the c++ reference was missing
(even though it was in the same directory as the .exe). I decided to write
a
windows service that would simply launch the console application. In the
"OnStart" method I called the win32 API CreateProcess (which I
P/Invoked).
When the service started it launched the console application successfully,
the console application then as soon as the reference was "used" by the
console application the console application crashes saying
File.IO.FileNotFoundException. As sanity check first I ran the console app
by
itself and it ran just fine. Then I created a second c# console
application
and in the main I used the exact same code from the "OnStart" of the afore
mentioned service (CreateProcess api) and it launched my original program
just fine.

Why is there a difference if I use CreateProcess from a c# service vs. c#
console application? Also isn''t a service simply a console application
that
interacts with service control manager?



这篇关于从Windows服务创建进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆