![]() |
Platforms, Frameworks & Libraries »
Win32/64 SDK & OS »
General
Intermediate
Become a serviceBy fatoThis article introduces a set of C functions you can use in you projects to write windows services in few lines of code |
VC6, VC7Win2K, WinXP, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
Write a service isn't allways easy, specialy when you have a lot of existing code to deal with. The OSR_Service toolkit is a set of C files you can include into your C/C++ projects to write window services functionalities in few lines.
OSR_ERROR OSR_CALL ServiceStart(OSR_SERVICE_PARAMS * Params)
Pointer to the OSR_SEVICE_PARAMS structure the contains the servis start parameters and preferences.
If the function succeeds, the return value is OSR_SUCCESS.
WinMain) before the the code the application may execute when the service is running.int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance, LPSTR lpCmdLine,int nCmdShow) { OSR_SERVICE_PARAMS params; params.ServiceDependencies[0] = 0; params.ServiceDependencies[1] = 0; strcpy((char *)params.ServiceLogin,".\\Administrateur"); strcpy((char *)params.ServicePassword,"AdminPassword"); strcpy((char *)params.ServiceName,"OSR_SAPMPLE_SERVICE"); strcpy((char *)params.ServiceNameDisplay,"OSR sample service"); params.ServiceAcceptedCommands = SERVICE_ACCEPT_STOP| SERVICE_ACCEPT_PAUSE_CONTINUE|SERVICE_ACCEPT_SHUTDOWN; params.ServiceStartType = SERVICE_DEMAND_START; if(strcmp(lpCmdLine,"install") == 0) { // Install the service //********************** ServiceInstall(¶ms); } else { if(strcmp(lpCmdLine,"uninstall") == 0) { // Uninstall the service //********************** ServiceUninstall(¶ms); } else { // Start the service //********************** ServiceStart(¶ms); // The service is now running //*************************** while(!stop_request) { // Do something //******************* printf("I'm a running service\n"); Sleep(1000); } } } return 0; }
int OSR_CALL OSR_APP_START(int step); int OSR_CALL OSR_APP_STOP(int step); int OSR_CALL OSR_APP_PAUSE(int step); int OSR_CALL OSR_APP_CONTINUE(int step); int OSR_CALL OSR_APP_SHUTDOWN(int step);
When the service receives a message form the service manager, requisting for a new status (eg. the service is running, and we want to stop it), the OSR_APP_STOP is called one time. According to the returned value by this function, the function will be called as long as is necesary to complete the stop process.
STOPED,RUNNING,PAUSED) and 4 pending states (START PENDING,PAUSE PENDING,CONTINUE PENDING,STOP PENDING) .
The step is an incremential value sent to the function every time the function is called under the same status change.
Value Meaning OSR_SERVICE_WAITThe init process is not completed, the service stay in a pending state, and this function will be called again and the step value increased. OSR_SERVICE_DONEThe init process is completed. The service can be in the requested state. OSR_SERVICE_ABORTThe init process can't be done, or fail. The status can't be changed.
int OSR_CALL OSR_APP_START(int step) { switch(step) { case 0: OpenLogFile(); return OSR_SERVICE_WAIT break; case 1: InitSocket(); return OSR_SERVICE_WAIT; break; case 2: return OSR_SERVICE_DONE; break; }
typedef struct _OSR_SERVICE_PARAMS { // Name of the service UCHAR ServiceName[STR_CHAR32]; // Service name displayed in the service manager UCHAR ServiceNameDisplay[STR_CHAR32]; // Dependencies name array UCHAR ServiceDependencies[STR_CHAR64]; // Service start mode U32BIT ServiceStartType; // Login used by the service UCHAR ServiceLogin[STR_CHAR32]; // Password used by the service UCHAR ServicePassword[STR_CHAR32]; // Commands you can use with this service U32BIT ServiceAcceptedCommands; }OSR_SERVICE_PARAMS;
Null-terminated string that names the service. "\" and "/" characters are invalid in the service name.
Null-terminated string that is to be used by user interface programs to identify the service.
Double null-terminated array of null-separated names of services or load ordering groups that the system must start before this service. Specify NULL if no dependencies are required.
- "NetworkService\0LogService\0\0" means NetworkService and LogService may be started before allow your service to start.
- "\0\0" means no dependencied are defined.
Specifies when to start the service. You must specify one of the following start types
Value Meaning SERVICE_AUTO_STARTSpecifies a service to be started automatically by the service control manager during system startup. SERVICE_DEMAND_STARTSpecifies a service to be started by the service control manager when a process calls the StartService function. SERVICE_DISABLEDSpecifies a service that can no longer be started.
Null-terminated string that names the service. You must use an account name in the form DomainName\UserName. Because the account belongs to the built-in domain, you can specify .\UserName. You also must specify the LocalSystem account. If you specify an empty string ("\0") LocalSystem account is used.
Null-terminated string that contains the password to the account name specified by the ServiceLogin parameter. If the string is empty, the service has no password.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 15 May 2003 Editor: Nick Parker |
Copyright 2003 by fato Everything else Copyright © CodeProject, 1999-2009 Web22 | Advertise on the Code Project |