Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 371
  • Last Modified:

.exe as a service

Hello,
I've got an .exe - File and I wanted it to start as a Windows 2000 service application.
My file only works if a network connection is available. And that's my problem.
I don't know how to write my program (start the service) so that it only starts after the network connection is available. I used the code from the msdn to write my service. Can anyone help me??
0
life_drake
Asked:
life_drake
1 Solution
 
life_drakeAuthor Commented:
Can I do this with the SERVICE_INTERACTIVE_PROCESS - Flag?
0
 
corduroy9Commented:

Can you check for the network connection and go into a sleep mode until it can connect?


0
 
yukapapaCommented:
The NT service program can be configured with its dependency. If your service program depends on the network connection, you can tell the system to start the service after network connection has been done.


Use "Service" console GUI to configure the dependency of the service. You can also use the command line utility netsvc.exe included in Windows 2000 Resource Kit. If you need to do this by programmatically, the dependency can be specified as a argument of the CreateService API function in your service configuration program.


To start the service, use "net start" command or the netsvc.exe utility. If your need to do this by programmatically, you can use the StartService API.


The configuration program can be written as follows:

//Establish a connection to the service control manager.

SC_HANDLE h_scm = OpenSCManager(
0, //connect to the SCM on the local machine
0, //use active database of the SCM
SC_MANAGER_CREATE_SERVICE //allow service creation
);

//Configure your exe as a service program.

SC_HANDLE h_svc = CreateService(
h_scm, //the handle to the SCM
service_name, //your service name
display_name, //display name for the service
GENERIC_READ, //access right flag
SERVICE_WIN32_OWN_PROCES, //service type
SERVICE_DEMAND_START, //start the service on demand, use SERVICE_AUTO_START to start the service on system startup
SERVICE_ERROR_NORMAL, //normal error control
path, //path to the your service program
0, //indicate your service does not belong to any group
0, //not used
dependency, //the dependency. see below
0); //the service runs under the built-in LocalSystem account.

//Close the handles

CloseServiceHandle(h_svc);
CloseServiceHandle(h_scm);

The dependency is a list of service names separated by a '\0' character and terminated by two '\0's such like "dependent_service_1\0dependent_service_2\0"

If your service has GUI and need to access the desktop
, you should include SERVICE_INTERACTIVE_PROCESS flag in the service type.


To start the service, you can use the StartService API as follows:

//Establish a connection to the service control manager.

SC_HANDLE h_scm = OpenSCManager(
0, //connect to the SCM on the local machine
0, //use active database of the SCM
SC_MANAGER_CONNECT
);

//Open the service

SC_HANDLE h_svc = OpenService(
h_scm, //the handle to the SCM
service_name, //name of the service
SERVICE_START //open the service to start it
);

//Start the service.

StartService(
h_svc,
0, //number of arguments for the service
0  //pointer to the array of arguments
);

//Close the handles

CloseServiceHandle(h_svc);
CloseServiceHandle(h_scm);





0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
bkrahmerCommented:
The SCM code looks pretty complete.  You may run into a problem, though.  By default, services run as LocalSystem.  Depending on what you're trying to do with the network, this account may not have enough privileges.  Let us know if you run into that.

brian
0
 
yukapapaCommented:
Hi bkrahmer.

The LocalSystem account has no rights to access network resources as you indicated, but WinSock is not included in the such resources. See

http://www.microsoft.com/msj/0398/service2.htm

for details.

Bye.
0
 
life_drakeAuthor Commented:
Hello!
Thanks for your answers.
@yukapapa:
I have done the same way in my program, as you in yours.
But how can I tell the system to start the service after network connection has been done? I wanted to do this in my program.
0
 
life_drakeAuthor Commented:
Hello!
Thanks for your answers.
@yukapapa:
I have done the same way in my program, as you in yours.
But how can I tell the system to start the service after network connection has been done? I wanted to do this in my program.
0
 
life_drakeAuthor Commented:
Hello!
Thanks for your answers.
@yukapapa:
I have done the same way in my program, as you in yours.
But how can I tell the system to start the service after network connection has been done? I wanted to do this in my program.
0
 
life_drakeAuthor Commented:
Hello!
Thanks for your answers.
@yukapapa:
I have done the same way in my program, as you in yours.
But how can I tell the system to start the service after network connection has been done? I wanted to do this in my program.
0
 
life_drakeAuthor Commented:
Hello!
Thanks for your answers.
@yukapapa:
I have done the same way in my program, as you in yours.
But how can I tell the system to start the service after network connection has been done? I wanted to do this in my program.
0
 
life_drakeAuthor Commented:
Hello!
Thanks for your answers.
@yukapapa:
I have done the same way in my program, as you in yours.
But how can I tell the system to start the service after network connection has been done? I wanted to do this in my program.
0
 
yukapapaCommented:
Good morning, life.
It is morning in my locale.

Setting the service start-up type to AUTOMATIC, the system start the service on every system start-up.

And you can start your service manually.
From command line: "net start your_service_name"
or use Windows 2000 Service console GUI to start the service.

Setting the dependency of your service, the system automatically runs the all dependent services before your service runs.

To start your service after the network connection has been done, tell the system the service dependent on "Workstation" service. You can do this by the Service Console GUI or programatically as I wrote before.

Make any questions, I'm ready to help you.
Bye.
0
 
bcladdCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered: Points to yukapapa

Please leave any comments here within the next seven days. Experts: Silence
means you don't care.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now