473,394 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Starting an exe from within a service

Hi all,

I need to start an executable from within a windows service. I tried
the following code in the windows service:

ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "filename.exe";
psi.Arguments = "arg1 arg2";
psi.UseShellExecute = false;

psi.Domain = "domain";
psi.UserName = "username";

string password = "password";
SecureString secureString = new SecureString();
foreach (char c in password) { secureString.AppendChar(c); }
psi.Password = secureString;

Process process = new Process();
process.StartInfo = psi;
process.Start();

However, when the Start() method is executed I always get the error
"Access is denied".

Can someone tell me what could be wrong or what the reason may be for
the error?

Thanks!

Jun 1 '06 #1
6 9139

lu***********@coders-lab.be schreef:
Hi all,

I need to start an executable from within a windows service. I tried
the following code in the windows service:

ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "filename.exe";
psi.Arguments = "arg1 arg2";
psi.UseShellExecute = false;

psi.Domain = "domain";
psi.UserName = "username";

string password = "password";
SecureString secureString = new SecureString();
foreach (char c in password) { secureString.AppendChar(c); }
psi.Password = secureString;

Process process = new Process();
process.StartInfo = psi;
process.Start();

However, when the Start() method is executed I always get the error
"Access is denied".

Can someone tell me what could be wrong or what the reason may be for
the error?

Thanks!


Extra information: if I don't specify the domain, username and
password, the exe is started under the SYSTEM account; but I need the
exe to be started under the currently logged on user.

Jun 1 '06 #2
And you are hard coding the username/password of the current user into the
code?

I would imagine this may not work, try using LogonUser
http://msdn.microsoft.com/library/en...asp?frame=true
and CreateProcessAsUser
http://msdn.microsoft.com/library/de...cessasuser.asp

If you google on the API function names with "C#" it is pretty easy to get
their definitions as well as some articles on usage.

Cheers,

Greg Young
MVP - C#
http://geekswithblogs.net/gyoung
<lu***********@coders-lab.be> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...

lu***********@coders-lab.be schreef:
Hi all,

I need to start an executable from within a windows service. I tried
the following code in the windows service:

ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "filename.exe";
psi.Arguments = "arg1 arg2";
psi.UseShellExecute = false;

psi.Domain = "domain";
psi.UserName = "username";

string password = "password";
SecureString secureString = new SecureString();
foreach (char c in password) { secureString.AppendChar(c); }
psi.Password = secureString;

Process process = new Process();
process.StartInfo = psi;
process.Start();

However, when the Start() method is executed I always get the error
"Access is denied".

Can someone tell me what could be wrong or what the reason may be for
the error?

Thanks!


Extra information: if I don't specify the domain, username and
password, the exe is started under the SYSTEM account; but I need the
exe to be started under the currently logged on user.

Jun 1 '06 #3

<lu***********@coders-lab.be> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
| Hi all,
|
| I need to start an executable from within a windows service. I tried
| the following code in the windows service:
|
| ProcessStartInfo psi = new ProcessStartInfo();
| psi.FileName = "filename.exe";
| psi.Arguments = "arg1 arg2";
| psi.UseShellExecute = false;
|
| psi.Domain = "domain";
| psi.UserName = "username";
|
| string password = "password";
| SecureString secureString = new SecureString();
| foreach (char c in password) { secureString.AppendChar(c); }
| psi.Password = secureString;
|
| Process process = new Process();
| process.StartInfo = psi;
| process.Start();
|
| However, when the Start() method is executed I always get the error
| "Access is denied".
|
| Can someone tell me what could be wrong or what the reason may be for
| the error?
|
| Thanks!
|

Using alternate credentials to start a process from a 'localsystem' logon
session (SYSTEM) does not work on XP SP2 and W2K3. This is because the
underlying API used (CreateProcessWithLogonW() ) needs the Logon SID of the
parent in order to add it to the access token of the client process (this it
grants the client process access to the parents desktop), however, a SYSTEM
logon token does no longer contain a Logon SID, so the API fails.

One way to solve this issue (provided that the logon user and the one used
in the ProcessStartInfo are the same) is to impersonate (calling LogonUser
using the alternate credentials) before calling process.Start().
If both "login" and "alternate" user are different, you will have to to
modify the DACLs for the windowstation/desktop, before you call
Process.Start.

Willy.
Jun 1 '06 #4

Greg Young schreef:
And you are hard coding the username/password of the current user into the
code?

I would imagine this may not work, try using LogonUser
http://msdn.microsoft.com/library/en...asp?frame=true
and CreateProcessAsUser
http://msdn.microsoft.com/library/de...cessasuser.asp

If you google on the API function names with "C#" it is pretty easy to get
their definitions as well as some articles on usage.

Cheers,

Greg Young
MVP - C#
http://geekswithblogs.net/gyoung

Thank you, I managed to launch a process with a specified account using
CreateProcessAsUser.

First I do an impersonation of a specific user, then I create a token
based on the impersonated windows identity (DuplicateTokenEx) and then
I use this token in CreateProcessAsUser to start a proces.

However, I need to explicitely specify the username, domain and
password to impersonate.

Is it possible to retrieve the token of the currently logged on user
(which will be null if no user is logged on) so that I can impersonate
the currently logged on user from within the windows service?

Jun 1 '06 #5

lu***********@coders-lab.be schreef:
Greg Young schreef:
And you are hard coding the username/password of the current user into the
code?

I would imagine this may not work, try using LogonUser
http://msdn.microsoft.com/library/en...asp?frame=true
and CreateProcessAsUser
http://msdn.microsoft.com/library/de...cessasuser.asp

If you google on the API function names with "C#" it is pretty easy to get
their definitions as well as some articles on usage.

Cheers,

Greg Young
MVP - C#
http://geekswithblogs.net/gyoung

Thank you, I managed to launch a process with a specified account using
CreateProcessAsUser.

First I do an impersonation of a specific user, then I create a token
based on the impersonated windows identity (DuplicateTokenEx) and then
I use this token in CreateProcessAsUser to start a proces.

However, I need to explicitely specify the username, domain and
password to impersonate.

Is it possible to retrieve the token of the currently logged on user
(which will be null if no user is logged on) so that I can impersonate
the currently logged on user from within the windows service?


Question: what are the consequences of starting an application from a
windows service so that it runs under the SYSTEM account? I ask this
because I noticed that the processes that correspond to the tray icons
also run under the SYSTEM account...

Jun 1 '06 #6

<lu***********@coders-lab.be> wrote in message
news:11*********************@j55g2000cwa.googlegro ups.com...
|
| lu***********@coders-lab.be schreef:
|
| > Greg Young schreef:
| >
| > > And you are hard coding the username/password of the current user into
the
| > > code?
| > >
| > > I would imagine this may not work, try using LogonUser
| > >
http://msdn.microsoft.com/library/en...asp?frame=true
| > > and CreateProcessAsUser
| > >
http://msdn.microsoft.com/library/de...cessasuser.asp
| > >
| > > If you google on the API function names with "C#" it is pretty easy to
get
| > > their definitions as well as some articles on usage.
| > >
| > > Cheers,
| > >
| > > Greg Young
| > > MVP - C#
| > > http://geekswithblogs.net/gyoung
| >
| >
| > Thank you, I managed to launch a process with a specified account using
| > CreateProcessAsUser.
| >
| > First I do an impersonation of a specific user, then I create a token
| > based on the impersonated windows identity (DuplicateTokenEx) and then
| > I use this token in CreateProcessAsUser to start a proces.
| >
| > However, I need to explicitely specify the username, domain and
| > password to impersonate.
| >
| > Is it possible to retrieve the token of the currently logged on user
| > (which will be null if no user is logged on) so that I can impersonate
| > the currently logged on user from within the windows service?
|
| Question: what are the consequences of starting an application from a
| windows service so that it runs under the SYSTEM account? I ask this
| because I noticed that the processes that correspond to the tray icons
| also run under the SYSTEM account...
User processes that have a UI should never run in the TCB, that is they
should never run as SYSTEM. Even windows services should (preferably) not
run as SYSTEM, use one of the service accounts like LOCAL_SERVICE or
NETWORK_SERVICE to run a Service if you care about security.

Willy.
Jun 1 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Rajesh Abraham | last post by:
I have a Windows Service and in the OnStart, I am initializing the BusinessLayer Object and calling a method of the object, which normally takes about 10 mts to finish execution. Now when I...
1
by: Tom O'Brien | last post by:
I have a windows service project as part of a multi-project solution. To step through some of the code, I need to debug the service. 1) How do I start the service for debuging. A production...
8
by: Kevin D. | last post by:
Please note, I already posted this on the MySQL official forum, but received no response. I thought I'd try again in another location. My apologies to anyone reading this twice... Despite...
2
by: Soren S. Jorgensen | last post by:
Hi, How do I see, from within my main method, in what context my assembly, containing my service program, is executed ?? I want to be able to detect if the assembly is executed by the service...
3
by: placid | last post by:
Hi all, Using Tim Golden's wmi module you can get the service names import wmi c = wmi.WMI () stopped_services = c.Win32_Service (StartMode="Auto", State="Stopped") if stopped_services: for...
5
by: Benzi Eilon | last post by:
I have written a C# application which should run as a Windows Service. I must avoid having multiple instances of the application on one machine so I inserted the following code at the beginning of...
3
by: Cenarius | last post by:
Hi, I need some help, i don't know much about programming other than executing them get getting them from scripting sites. My program is it won't start a service if the startup type is Disabled. it...
0
by: s.kapitza | last post by:
Hi, try M$S servicebug 3 regards s.kapitza alghaden@alumni.fdu.edu (Ahmad) wrote in message news:<4a75f4b7.0308080649.49105373@posting.google.com>...
4
by: rottmanj | last post by:
I have written an application that installs a service and then is supposed to start the service. Everything works great except for the service starting(either with AfterIntall or starting it from the...
5
by: mwizniki | last post by:
I'm trying to start the MSFTPSVC, and I need to add a user/password. This has to be Windows 7 friendly. Starting the service is the easy part, but I'm having difficulty locating in the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.