16,472
社区成员
发帖
与我相关
我的任务
分享
#include <windows.h>
#include <winsvc.h>
#include <iostream>
void ShowErr(char* perror);
void ServiceMain(DWORD argc, LPTSTR *argv);
void ServiceCtrlHandler(DWORD nControlCode);
BOOL UpdateServiceStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode,
DWORD dwServiceSpecificExitCode, DWORD dwCheckPoint,
DWORD dwWaitHint);
BOOL StartServiceThread();
DWORD ServiceExecutionThread(LPDWORD param);
HANDLE hServiceThread;
void KillService();
char *strServiceName = "aaa";
SERVICE_STATUS_HANDLE nServiceStatusHandle;
HANDLE killServiceEvent;
BOOL nServiceRunning;
DWORD nServiceCurrentStatus;
int InstallService();
int StartStopService(int nflag);
int ExecuteService();
int main(int argc, char* argv[])
{
if(argc<2)
{
printf("Usage:- StartStopService install|starting|stop|exe\r\n");
return 1;
}
if(strcmp(argv[1],"install")==0)
{
InstallService();
}
if(strcmp(argv[1],"starting")==0)
{
StartStopService(1);
}
if(strcmp(argv[1],"stoping")==0)
{
StartStopService(2);
}
if(strcmp(argv[1],"exe")==0)
{
ExecuteService();
}
return 0;
}
void ShowErr(char* perror)
{
printf(perror);
}
int InstallService()
{
printf("InstallService\r\n");
TCHAR szPath[MAX_PATH];
if( !GetModuleFileName( NULL, szPath, MAX_PATH ) )
{
printf("Cannot install service (%d)\n", GetLastError());
return 1;
}
SC_HANDLE NishService,scm;
scm=OpenSCManager(0,0,SC_MANAGER_CREATE_SERVICE);
if(!scm)
{
ShowErr("OpenSCManager Error\r\n");
return 1;
}
NishService=CreateService(scm,strServiceName,
strServiceName,
SERVICE_ALL_ACCESS,SERVICE_WIN32_OWN_PROCESS,SERVICE_DEMAND_START,//,SERVICE_AUTO_START
SERVICE_ERROR_NORMAL,
szPath,
NULL, NULL, "", NULL, NULL);
if(!NishService)
{
CloseServiceHandle(scm);
ShowErr("CreateService Error\r\n");
return 1;
}
CloseServiceHandle(NishService);
CloseServiceHandle(scm);
return 0;
}
int StartStopService(int nflag)// flag 1 start 2 stop
{
printf("StartStopService\r\n");
SC_HANDLE NishService,scm;
scm=OpenSCManager(0,0,SC_MANAGER_CREATE_SERVICE);
if(!scm)
{
ShowErr("OpenSCManager Error\r\n");
return 1;
}
NishService=OpenService(scm,strServiceName,SERVICE_ALL_ACCESS);
if(!NishService)
{
CloseServiceHandle(scm);
ShowErr("OpenService Error\r\n");
return 1;
}
if(nflag==1)
{
printf("starting...\r\n");
BOOL blnret = StartService(NishService,0,NULL);
if(!blnret)
{
printf("StartService Error(%d)\n", GetLastError());
}
}
if(nflag==2)
{
printf("stopping...\r\n");
SERVICE_STATUS m_SERVICE_STATUS;
ControlService(NishService,SERVICE_CONTROL_STOP,&m_SERVICE_STATUS);
}
CloseServiceHandle(NishService);
CloseServiceHandle(scm);
return 0;
}
int ExecuteService()
{
printf("ExecuteService\r\n");
SERVICE_TABLE_ENTRY servicetable[]=
{
{strServiceName,(LPSERVICE_MAIN_FUNCTION)ServiceMain},
{NULL,NULL}
};
BOOL success;
success=StartServiceCtrlDispatcher(servicetable);
if(!success)
{
//error occured
}
return 0;
}
void ServiceMain(DWORD argc, LPTSTR *argv)
{
BOOL success;
nServiceStatusHandle=RegisterServiceCtrlHandler(strServiceName,
(LPHANDLER_FUNCTION)ServiceCtrlHandler);
if(!nServiceStatusHandle)
{
printf("RegisterServiceCtrlHandler Error\r\n");
return;
}
success=UpdateServiceStatus(SERVICE_START_PENDING,NO_ERROR,0,1,3000);
if(!success)
{
printf("UpdateServiceStatus 1 Error\r\n");
return;
}
killServiceEvent=CreateEvent(0,TRUE,FALSE,0);
if(killServiceEvent==NULL)
{
printf("CreateEvent Error\r\n");
return;
}
success=UpdateServiceStatus(SERVICE_START_PENDING,NO_ERROR,0,2,1000);
if(!success)
{
printf("SERVICE_START_PENDING 2 Error\r\n");
return;
}
success=StartServiceThread();
if(!success)
{
printf("StartServiceThread Error\r\n");
return;
}
nServiceCurrentStatus=SERVICE_RUNNING;
success=UpdateServiceStatus(SERVICE_RUNNING,NO_ERROR,0,0,0);
if(!success)
{
printf("UpdateServiceStatus 0 Error\r\n");
return;
}
WaitForSingleObject(killServiceEvent,INFINITE);
CloseHandle(killServiceEvent);
}
BOOL UpdateServiceStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode,
DWORD dwServiceSpecificExitCode, DWORD dwCheckPoint,
DWORD dwWaitHint)
{
BOOL success;
SERVICE_STATUS nServiceStatus;
nServiceStatus.dwServiceType=SERVICE_WIN32_OWN_PROCESS;
nServiceStatus.dwCurrentState=dwCurrentState;
if(dwCurrentState==SERVICE_START_PENDING)
{
nServiceStatus.dwControlsAccepted=0;
}
else
{
nServiceStatus.dwControlsAccepted=SERVICE_ACCEPT_STOP
|SERVICE_ACCEPT_SHUTDOWN;
}
if(dwServiceSpecificExitCode==0)
{
nServiceStatus.dwWin32ExitCode=dwWin32ExitCode;
}
else
{
nServiceStatus.dwWin32ExitCode=ERROR_SERVICE_SPECIFIC_ERROR;
}
nServiceStatus.dwServiceSpecificExitCode=dwServiceSpecificExitCode;
nServiceStatus.dwCheckPoint=dwCheckPoint;
nServiceStatus.dwWaitHint=dwWaitHint;
success=SetServiceStatus(nServiceStatusHandle,&nServiceStatus);
if(!success)
{
KillService();
return success;
}
else
return success;
}
BOOL StartServiceThread()
{
DWORD id;
hServiceThread=CreateThread(0,0,
(LPTHREAD_START_ROUTINE)ServiceExecutionThread,
0,0,&id);
if(hServiceThread==0)
{
return false;
}
else
{
nServiceRunning=true;
return true;
}
}
DWORD ServiceExecutionThread(LPDWORD param)
{
while(nServiceRunning)
{
MessageBox(NULL,"run","tip",MB_OK);
Beep(450,150);
Sleep(1000);
}
return 0;
}
void KillService()
{
nServiceRunning=false;
SetEvent(killServiceEvent);
UpdateServiceStatus(SERVICE_STOPPED,NO_ERROR,0,0,0);
}
void ServiceCtrlHandler(DWORD nControlCode)
{
BOOL success;
switch(nControlCode)
{
case SERVICE_CONTROL_SHUTDOWN:
case SERVICE_CONTROL_STOP:
nServiceCurrentStatus=SERVICE_STOP_PENDING;
success=UpdateServiceStatus(SERVICE_STOP_PENDING,NO_ERROR,0,1,3000);
KillService();
return;
default:
break;
}
UpdateServiceStatus(nServiceCurrentStatus,NO_ERROR,0,0,0);
}