Installing a Windows Service programmatically

S

sunil

Hi all,
I read an article from
http://www.c-sharpcorner.com/Code/2003/Sept/InstallingWinServiceProgrammatically.asp
about how to install a windows service programmatically.
Based on the code sample, it provides the feature to install
service
under LocalSystem account.
What I need is to install service under NT
AUTHORITY\NetworkService, so that my service can access the shared
network resources.
What I had done for this is to call
CreateService(sc_handle, serviceName, serviceDisplayName,
SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
servicePath, null, 0, null, @NT AUTHORITY\NetworkService, "");

But the problem is that, the service is installed under LocalSystem
account inspite of being passing the parameter value as non-null.
Please help me.
 
T

Tom Spink

sunil said:
Hi all,
I read an article from
http://www.c-sharpcorner.com/Code/2003/Sept/...
about how to install a windows service programmatically.
Based on the code sample, it provides the feature to install
service
under LocalSystem account.
What I need is to install service under NT
AUTHORITY\NetworkService, so that my service can access the shared
network resources.
What I had done for this is to call
CreateService(sc_handle, serviceName, serviceDisplayName,
SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
servicePath, null, 0, null, @NT AUTHORITY\NetworkService, "");

But the problem is that, the service is installed under LocalSystem
account inspite of being passing the parameter value as non-null.
Please help me.

Hi,

I believe there's a property in your Service object or ServiceInstaller or
something that specifies which account the service should run under, have
you got this set to "NetworkService"?
 
W

Willy Denoyette [MVP]

| Hi all,
| I read an article from
|
http://www.c-sharpcorner.com/Code/2003/Sept/InstallingWinServiceProgrammatically.asp
| about how to install a windows service programmatically.
| Based on the code sample, it provides the feature to install
| service
| under LocalSystem account.
| What I need is to install service under NT
| AUTHORITY\NetworkService, so that my service can access the shared
| network resources.
| What I had done for this is to call
| CreateService(sc_handle, serviceName, serviceDisplayName,
| SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
| SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
| servicePath, null, 0, null, @NT AUTHORITY\NetworkService, "");
|
| But the problem is that, the service is installed under LocalSystem
| account inspite of being passing the parameter value as non-null.
| Please help me.
|

If by NT you mean NT4, you won't be able to do it, LocalService and
NetworkService service accounts, were introduced in W2K. Note also that
NetworkService (just like LocalSystem) has network access only in a W2K
domain (AD) realm.
Your options are:
- impersonate while accessing the network resource,
- run your service as a domain user (NT4 domain),
- or get rid of NT4.


Willy.
 
S

sunil

Hi,

I believe there's a property in your Service object or ServiceInstaller or
something that specifies which account the service should run under, have
you got this set to "NetworkService"?

Hi Tom,
I have passed the value @NT AUTHORITY\NetworkService for the
lpServiceStartName and the password is empty as can be seen in the
call:
CreateService(sc_handle, serviceName, serviceDisplayName,
SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
servicePath, null, 0, null, @NT AUTHORITY\NetworkService, "");

Thanks for the reply
 
S

sunil

Willy said:
If by NT you mean NT4, you won't be able to do it, LocalService and
NetworkService service accounts, were introduced in W2K. Note also that
NetworkService (just like LocalSystem) has network access only in a W2K
domain (AD) realm.
Your options are:
- impersonate while accessing the network resource,
- run your service as a domain user (NT4 domain),
- or get rid of NT4.


Willy.
Hi,
I am not asking about NT4. I am asking about running the service on
WInXP service pack2 prof.
I thought that, to create a NetworkService programmatically, i have to
call CreateService as I have written in my previous post. Am I correct?
Thanks
 
W

Willy Denoyette [MVP]

|
| Willy Denoyette [MVP] wrote:
| > If by NT you mean NT4, you won't be able to do it, LocalService and
| > NetworkService service accounts, were introduced in W2K. Note also that
| > NetworkService (just like LocalSystem) has network access only in a W2K
| > domain (AD) realm.
| > Your options are:
| > - impersonate while accessing the network resource,
| > - run your service as a domain user (NT4 domain),
| > - or get rid of NT4.
| >
| >
| > Willy.
| Hi,
| I am not asking about NT4. I am asking about running the service on
| WInXP service pack2 prof.
| I thought that, to create a NetworkService programmatically, i have to
| call CreateService as I have written in my previous post. Am I correct?
| Thanks
|

Yes, but you don't even have to write a custom installer yourself, all
system higher than W2K come with a commandline utility, called sc.exe, which
offers all you need to install configure and control your service
applications. Just start sc /help from the command prompt and check the
options.


Willy.
 
S

sunil

Yes, but you don't even have to write a custom installer yourself, all
system higher than W2K come with a commandline utility, called sc.exe, which
offers all you need to install configure and control your service
applications. Just start sc /help from the command prompt and check the
options.
I know about that utility.That will do in case of a standalone service.
But in my case, I have to interact with the user.
For this purpose, I have 2 projects in my application. One project is a
console application and should run as a service, whereas the other is a
Windows forms application that interacts with the user and uses the
newly installed service.
Considering this scenario, how can I make the console application as a
network service rather than running under LocalSystem account. Please
tell me how to do this?
 
W

Willy Denoyette [MVP]

|
| >
| > Yes, but you don't even have to write a custom installer yourself, all
| > system higher than W2K come with a commandline utility, called sc.exe,
which
| > offers all you need to install configure and control your service
| > applications. Just start sc /help from the command prompt and check the
| > options.
| >
| I know about that utility.That will do in case of a standalone service.
| But in my case, I have to interact with the user.
| For this purpose, I have 2 projects in my application. One project is a
| console application and should run as a service, whereas the other is a
| Windows forms application that interacts with the user and uses the
| newly installed service.
| Considering this scenario, how can I make the console application as a
| network service rather than running under LocalSystem account. Please
| tell me how to do this?
|

Ok, first of all, a console application is not a windows service and can
never run as such. If you need a windows service you have to create a
"service project" from VS. You can include a service installer component by
which you can set the service account to run the service.
If you don't include a service installer component into your service, you
will have to use separate installer like sc.exe.
Second, while you can arrange to run a service from the commandline, you
have to keep in mind that the context the console 'service' runs in is not
the same as it would run as a 'Windows service'.
For instance, while a service can run as 'localsystem', network service' or
'local service', a console application cannot, it always runs in the context
of the interactive user.

Willy.
 

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