Creating a Process from a windows service

G

Guest

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?
 
C

Chris Dunaway

Noam said:
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?
 
G

Guest

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
 
G

Guest

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
 
B

Ben Voigt

Noam said:
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?
 
W

Willy Denoyette [MVP]

| 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.
 
G

Guest

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.
 
G

Guest

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.
 
J

John Vottero

[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.
 
G

Guest

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

John Vottero said:
[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.
 
W

Willy Denoyette [MVP]

|
| 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:
|
| >
| > | >
| > [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.
| >
| >
| >
| >
 
G

Guest

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 said:
How about running the service as the local system account and checking
"interact with desktop"?

John Vottero said:
[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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top