?
Solved

.exe as a service

Posted on 2003-03-24
14
Medium Priority
?
370 Views
Last Modified: 2010-04-01
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
Comment
Question by:life_drake
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
14 Comments
 

Author Comment

by:life_drake
ID: 8197987
Can I do this with the SERVICE_INTERACTIVE_PROCESS - Flag?
0
 
LVL 2

Expert Comment

by:corduroy9
ID: 8198164

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


0
 
LVL 1

Expert Comment

by:yukapapa
ID: 8199489
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 2

Expert Comment

by:bkrahmer
ID: 8200633
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
 
LVL 1

Expert Comment

by:yukapapa
ID: 8200717
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
 

Author Comment

by:life_drake
ID: 8202889
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
 

Author Comment

by:life_drake
ID: 8203074
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
 

Author Comment

by:life_drake
ID: 8203576
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
 

Author Comment

by:life_drake
ID: 8203925
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
 

Author Comment

by:life_drake
ID: 8204188
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
 

Author Comment

by:life_drake
ID: 8204406
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
 
LVL 1

Accepted Solution

by:
yukapapa earned 300 total points
ID: 8206917
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
 
LVL 11

Expert Comment

by:bcladd
ID: 9558733
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question