469,582 Members | 2,525 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,582 developers. It's quick & easy.

Creating a Process from a windows service

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?

Jun 20 '06 #1
12 6262
Noam wrote:
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?


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?

Jun 20 '06 #2
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 wrote:
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?


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?

Jun 20 '06 #3
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...
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?

Jun 20 '06 #4
I'm not sure i understand your answer, when you say specify the full path are
you referring to the path to the CreateProcess api (that i am passing) or for
the reference that i am recieving the System.IO.FileNotFoungException? If it
is for the reference my understanding is that all references (if not in the
GAC) must be in the same directory as the .exe... Also I have other refernces
in that directory that the program is picking up just fine.

Here is some informtaion on the c++ referance that is throwing the error.
Configuration type: Dynamic Libray
MFC: Use MFC in a Shared DLL
Runtime Library: /MDd

every other value is the default
"chanmm" wrote:
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...
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?


Jun 20 '06 #5
"Noam" <No**@discussions.microsoft.com> wrote in message
news:EC**********************************@microsof t.com...
I'm not sure i understand your answer, when you say specify the full path
are
you referring to the path to the CreateProcess api (that i am passing) or
for
the reference that i am recieving the System.IO.FileNotFoungException? If
it
is for the reference my understanding is that all references (if not in
the
GAC) must be in the same directory as the .exe... Also I have other
refernces
in that directory that the program is picking up just fine.
Can you somehow run your process through depends.exe, possibly by changing
your CreateProcess call? Depends.exe will watch the process and tell you
more about why the DLL failed to load. Although you will have trouble
getting the window to be visible so you can interact and inspect it....
services running as user accounts run in a separate desktop by default.

Here is some informtaion on the c++ referance that is throwing the error.
Configuration type: Dynamic Libray
MFC: Use MFC in a Shared DLL
Runtime Library: /MDd
Does your DLL have a DllMain? Doing anything significant there?

every other value is the default
"chanmm" wrote:
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...
>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?
>


Jun 20 '06 #6


"Noam" <No**@discussions.microsoft.com> wrote in message
news:EC**********************************@microsof t.com...
| I'm not sure i understand your answer, when you say specify the full path
are
| you referring to the path to the CreateProcess api (that i am passing) or
for
| the reference that i am recieving the System.IO.FileNotFoungException? If
it
| is for the reference my understanding is that all references (if not in
the
| GAC) must be in the same directory as the .exe... Also I have other
refernces
| in that directory that the program is picking up just fine.
|
| Here is some informtaion on the c++ referance that is throwing the error.
| Configuration type: Dynamic Libray
| MFC: Use MFC in a Shared DLL
| Runtime Library: /MDd
|

As far as I see you have a MFC DLL as "reference", right.
Questions are:
- what version of the C compiler was used to build this DLL? If it's VC 8
(vs2005), the DLL must contain an embedded manifest if the CRT library is
not statically linked.
- this DLL is in the same path as the exe?
- this DLL is built using the /clr option, right?

Willy.


Jun 20 '06 #7
Again thank you so much for the reply I really apreciate everyones help. I am
using Visual Studios 2003 Verssion 7.1.3088. The dll is in the same path as
the exe and the /clr option was used.

"Willy Denoyette [MVP]" wrote:


"Noam" <No**@discussions.microsoft.com> wrote in message
news:EC**********************************@microsof t.com...
| I'm not sure i understand your answer, when you say specify the full path
are
| you referring to the path to the CreateProcess api (that i am passing) or
for
| the reference that i am recieving the System.IO.FileNotFoungException? If
it
| is for the reference my understanding is that all references (if not in
the
| GAC) must be in the same directory as the .exe... Also I have other
refernces
| in that directory that the program is picking up just fine.
|
| Here is some informtaion on the c++ referance that is throwing the error.
| Configuration type: Dynamic Libray
| MFC: Use MFC in a Shared DLL
| Runtime Library: /MDd
|

As far as I see you have a MFC DLL as "reference", right.
Questions are:
- what version of the C compiler was used to build this DLL? If it's VC 8
(vs2005), the DLL must contain an embedded manifest if the CRT library is
not statically linked.
- this DLL is in the same path as the exe?
- this DLL is built using the /clr option, right?

Willy.


Jun 20 '06 #8
Thank you so much for your reply. My dll is not a regular dll it is a visual
studios assembly. As such there is no dll main. I am trying depends.exe now
but I haven’t seen anything useful.

"Ben Voigt" wrote:
"Noam" <No**@discussions.microsoft.com> wrote in message
news:EC**********************************@microsof t.com...
I'm not sure i understand your answer, when you say specify the full path
are
you referring to the path to the CreateProcess api (that i am passing) or
for
the reference that i am recieving the System.IO.FileNotFoungException? If
it
is for the reference my understanding is that all references (if not in
the
GAC) must be in the same directory as the .exe... Also I have other
refernces
in that directory that the program is picking up just fine.


Can you somehow run your process through depends.exe, possibly by changing
your CreateProcess call? Depends.exe will watch the process and tell you
more about why the DLL failed to load. Although you will have trouble
getting the window to be visible so you can interact and inspect it....
services running as user accounts run in a separate desktop by default.

Here is some informtaion on the c++ referance that is throwing the error.
Configuration type: Dynamic Libray
MFC: Use MFC in a Shared DLL
Runtime Library: /MDd


Does your DLL have a DllMain? Doing anything significant there?

every other value is the default
"chanmm" wrote:
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...
>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?
>


Jun 20 '06 #9

"Noam" <No**@discussions.microsoft.com> wrote in message
news:0C**********************************@microsof t.com...

[snip]
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?


One key difference between running a console app and a windows service is
the default directory. Try changing your directory to C:\Windows\System32
then run your console app from there (by entering the full path to the
console exe). Does that work? I think it will fail the same way that the
service is failing. If it does, make your service change it's directory via
System.Environment.CurrentDirectory.

Jun 20 '06 #10

How about running the service as the local system account and checking
"interact with desktop"?

"John Vottero" wrote:

"Noam" <No**@discussions.microsoft.com> wrote in message
news:0C**********************************@microsof t.com...

[snip]
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?


One key difference between running a console app and a windows service is
the default directory. Try changing your directory to C:\Windows\System32
then run your console app from there (by entering the full path to the
console exe). Does that work? I think it will fail the same way that the
service is failing. If it does, make your service change it's directory via
System.Environment.CurrentDirectory.

Jun 30 '06 #11

"Mike" <Mi**@discussions.microsoft.com> wrote in message
news:35**********************************@microsof t.com...
|
| How about running the service as the local system account and checking
| "interact with desktop"?
|

No, this doesn't have anything to do with the OP's issue, and, this is
something you should only consider when "debugging" a service, but not in
production.

Willy.

| "John Vottero" wrote:
|
| >
| > "Noam" <No**@discussions.microsoft.com> wrote in message
| > news:0C**********************************@microsof t.com...
| >
| > [snip]
| >
| > > 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?
| >
| > One key difference between running a console app and a windows service
is
| > the default directory. Try changing your directory to
C:\Windows\System32
| > then run your console app from there (by entering the full path to the
| > console exe). Does that work? I think it will fail the same way that
the
| > service is failing. If it does, make your service change it's directory
via
| > System.Environment.CurrentDirectory.
| >
| >
| >
| >
Jun 30 '06 #12
Thank you so much kind sir, I think it must be some sort of path issues...
but as soon as i ran the service with the local system and cheked interact
with desktop everything started running perfectly

"Mike" wrote:
>
How about running the service as the local system account and checking
"interact with desktop"?

"John Vottero" wrote:

"Noam" <No**@discussions.microsoft.comwrote in message
news:0C**********************************@microsof t.com...

[snip]
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?
One key difference between running a console app and a windows service is
the default directory. Try changing your directory to C:\Windows\System32
then run your console app from there (by entering the full path to the
console exe). Does that work? I think it will fail the same way that the
service is failing. If it does, make your service change it's directory via
System.Environment.CurrentDirectory.


Jul 5 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by BobHutch | last post: by
4 posts views Thread by Kavitha | last post: by
3 posts views Thread by Chris | last post: by
2 posts views Thread by Diego F. | last post: by
2 posts views Thread by =?Utf-8?B?U2Vhbk1hYw==?= | last post: by
reply views Thread by Harry Bellafonte | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.