一段简单的服务代码,在vc6,xpsp2上跑过去了。想了解如何写服务的朋友可以拿来看看,不明白可以查msdn。
it's al what i do today : code a very simple services on windows xp sp2
that's the code below:
///filename :svohost.c
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define SLEEP_TIME 10000
SERVICE_STATUS ServiceStatus;
SERVICE_STATUS_HANDLE hStatus;
char szFullPath[MAX_PATH];
void ServiceMain(int argc, char** argv);
void ControlHandler(DWORD request);
int InitService();
int do_ur_things()
{
//MessageBox(NULL,"service is running","",0);
//do any thing u like...
return 0;
}
//
//
//
//
void ServiceMain(int argc, char** argv)
{
int error;
ServiceStatus.dwServiceType = SERVICE_WIN32;
ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwServiceSpecificExitCode = 0;
ServiceStatus.dwCheckPoint = 0;
ServiceStatus.dwWaitHint = 0;
//
hStatus = RegisterServiceCtrlHandler(
"svohost",
(LPHANDLER_FUNCTION)ControlHandler);
//
if (hStatus == (SERVICE_STATUS_HANDLE)0)
{
// Registering Control Handler failed
return;
}
// Initialize Service
error = InitService();
if (error)
{
// Initialization failed
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
ServiceStatus.dwWin32ExitCode = -1;
SetServiceStatus(hStatus, &ServiceStatus);
return;
}
// We report the running status to SCM.
ServiceStatus.dwCurrentState = SERVICE_RUNNING;
SetServiceStatus (hStatus, &ServiceStatus);
// The worker loop of a service
while (ServiceStatus.dwCurrentState == SERVICE_RUNNING)
{
do_ur_things();
Sleep(SLEEP_TIME);
}
return;
}
//
//
//
int InitService()
{
//do anything?
return 0;
}
//
//
//
void ControlHandler(DWORD request)
{
switch(request)
{
case SERVICE_CONTROL_STOP:
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus (hStatus, &ServiceStatus);
return;
case SERVICE_CONTROL_SHUTDOWN:
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus (hStatus, &ServiceStatus);
return;
default:
break;
}
// Report current status
SetServiceStatus (hStatus, &ServiceStatus);
return;
}
//
// add the service and start it
//
VOID install_service(char* pPath, char* pName)
{
SC_HANDLE schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_CREATE_SERVICE);
if (schSCManager==0)
{
long nError = GetLastError();
printf("OpenSCManager failed, error code = %d", nError);
}
else
{
SC_HANDLE schService = CreateService
(
schSCManager, /* SCManager database */
pName, /* name of service */
pName, /* service name to display */
SERVICE_ALL_ACCESS, /* desired access */
SERVICE_WIN32_OWN_PROCESS|SERVICE_INTERACTIVE_PROCESS , /* service type */
SERVICE_AUTO_START, /* start type */
SERVICE_ERROR_NORMAL, /* error control type */
pPath, /* service's binary */
NULL, /* no load ordering group */
NULL, /* no tag identifier */
NULL, /* no dependencies */
NULL, /* LocalSystem account */
NULL
); /* no password */
if (schService==0)
{
long nError = GetLastError();
printf( "Failed to create service %s, error code = %d", pName, nError);
}
else
{
StartService(schService,0,0);
//printf("Service %s installed", pName);
CloseServiceHandle(schService);
}
CloseServiceHandle(schSCManager);
}
}
//
//win32 application
//
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
SERVICE_TABLE_ENTRY ServiceTable[]=
{
{"svohost",(LPSERVICE_MAIN_FUNCTION)ServiceMain},
{NULL,NULL}
};
GetModuleFileName(NULL,szFullPath,MAX_PATH);
StartServiceCtrlDispatcher(ServiceTable);
install_service(szFullPath,"svohost");
return 0;
}