把一個程式註冊成系統服務

Mobidogs發表於2020-04-04
//註冊並啟動/終止服務
BOOL RegisterServer(LPCTSTR lpServiceName,LPCTSTR lpDisplayName,LPCTSTR lpBinaryPathName)
{
SC_HANDLE hSCManager = OpenSCManager(0,0,SC_MANAGER_CREATE_SERVICE);
if(hSCManager == NULL)
{
return FALSE;
}

if(CreateService(hSCManager,lpServiceName,lpDisplayName,
SERVICE_ALL_ACCESS,SERVICE_WIN32_OWN_PROCESS,SERVICE_DEMAND_START,
SERVICE_ERROR_NORMAL,lpBinaryPathName,NULL,NULL,NULL,NULL,NULL))
{
printf("RegisterServer.../r/n");

CloseServiceHandle(hSCManager);
return TRUE;
}
else
{
printf("RegisterServer Error Code = %d/r/n",GetLastError());

CloseServiceHandle(hSCManager);
return FALSE;
}
}

BOOL ExecuteService(LPCTSTR lpServiceName)
{
SC_HANDLE hSCManager,hService;
hSCManager = OpenSCManager(0,0,SC_MANAGER_ALL_ACCESS);
if(hSCManager == NULL)
{
return FALSE;
}

hService = OpenService(hSCManager,lpServiceName,SERVICE_ALL_ACCESS);
if(hService == NULL)
{
CloseServiceHandle(hSCManager);
return FALSE;
}

if(StartService(hService,0,NULL))
{
printf("StartService.../r/n");

CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
return TRUE;
}
else
{
printf("StartService Error Code = %d/r/n",GetLastError());

CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
return FALSE;
}
}

BOOL TerminateService(LPCTSTR lpServiceName)
{
SC_HANDLE hSCManager,hService;
hSCManager = OpenSCManager(0,0,SC_MANAGER_ALL_ACCESS);
if(hSCManager == NULL)
{
return FALSE;
}

hService = OpenService(hSCManager,lpServiceName,SERVICE_ALL_ACCESS);
if(hService == NULL)
{
CloseServiceHandle(hSCManager);
return FALSE;
}

if(StartService(hService,0,NULL))
{
SERVICE_STATUS ServerStatus;
ControlService(hService,SERVICE_CONTROL_STOP,&ServerStatus);

printf("TerminateService.../r/n");

CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
return TRUE;
}
else
{
printf("TerminateService Error Code = %d/r/n",GetLastError());

CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
return FALSE;
}
}


////////////////////////////////////////////////////////////////
 //要做成服務的EXE必須這樣寫

#include <stdio.h>
#include <windows.h>
#include <winsvc.h>

#define SERVERNAME "EnochServer"

void WINAPI ServiceMain(DWORD argc, LPTSTR *argv);
void WINAPI ServiceControl(DWORD nControlCode);
BOOL ExecuteService();
void TerminateService();
DWORD WINAPI ServiceProc(LPVOID lpParameter);
BOOL UpdateServiceStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode,
DWORD dwServiceSpecificExitCode, DWORD dwCheckPoint,DWORD dwWaitHint);

SERVICE_STATUS_HANDLE hServiceStatus;
DWORD ServiceCurrentStatus;
HANDLE hServiceThread;
BOOL bServiceRunning;
HANDLE hServiceEvent;

void main(int argc, char* argv[])
{
SERVICE_TABLE_ENTRY ServiceTable[] =
{
{SERVERNAME,(LPSERVICE_MAIN_FUNCTION)ServiceMain},
{NULL,NULL}
};

if(!StartServiceCtrlDispatcher(ServiceTable))
{
printf("RegisterServer First");
}
}

void WINAPI ServiceMain(DWORD argc, LPTSTR *argv)
{
hServiceStatus = RegisterServiceCtrlHandler(SERVERNAME,(LPHANDLER_FUNCTION)ServiceControl);
if(!hServiceStatus || !UpdateServiceStatus(SERVICE_START_PENDING,NO_ERROR,0,1,3000))
{
return;
}

hServiceEvent = CreateEvent(0,TRUE,FALSE,0);
if(!hServiceEvent || !UpdateServiceStatus(SERVICE_START_PENDING,NO_ERROR,0,2,1000))
{
return;
}

if(!ExecuteService())
{
return;
}

ServiceCurrentStatus = SERVICE_RUNNING;

if(!UpdateServiceStatus(SERVICE_RUNNING,NO_ERROR,0,0,0))
{
return;
}

WaitForSingleObject(hServiceEvent,INFINITE);
CloseHandle(hServiceEvent);
}

void WINAPI ServiceControl(DWORD dwControlCode)
{
switch(dwControlCode)
{
case SERVICE_CONTROL_SHUTDOWN:
case SERVICE_CONTROL_STOP:
ServiceCurrentStatus = SERVICE_STOP_PENDING;
UpdateServiceStatus(SERVICE_STOP_PENDING,NO_ERROR,0,1,3000);
TerminateService();
return;
default:
break;
}

UpdateServiceStatus(ServiceCurrentStatus,NO_ERROR,0,0,0);
}

BOOL ExecuteService()
{
DWORD dwThreadID;
hServiceThread = CreateThread(0,0,(LPTHREAD_START_ROUTINE)ServiceProc,0,0,&dwThreadID);

if(hServiceThread != NULL)
{
bServiceRunning = TRUE;
return TRUE;
}
else
{
return FALSE;
}
}

void TerminateService()
{
bServiceRunning = FALSE;
SetEvent(hServiceEvent);
UpdateServiceStatus(SERVICE_STOPPED,NO_ERROR,0,0,0);
}

DWORD WINAPI ServiceProc(LPVOID lpParameter)//在這個函式裡面新增你想要的操作
{
while(bServiceRunning)
{
Beep(450,150);
Sleep(4000);
}

return 0;
}

BOOL UpdateServiceStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode,
DWORD dwServiceSpecificExitCode, DWORD dwCheckPoint,DWORD dwWaitHint)
{
SERVICE_STATUS ServiceStatus;
ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
ServiceStatus.dwCurrentState = dwCurrentState;

if(dwCurrentState == SERVICE_START_PENDING)
{
ServiceStatus.dwControlsAccepted = 0;
}
else
{
ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
}

if(dwServiceSpecificExitCode == 0)
{
ServiceStatus.dwWin32ExitCode = dwWin32ExitCode;
}
else
{
ServiceStatus.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR;
}

ServiceStatus.dwServiceSpecificExitCode = dwServiceSpecificExitCode;
ServiceStatus.dwCheckPoint = dwCheckPoint;
ServiceStatus.dwWaitHint = dwWaitHint;

if(!SetServiceStatus(hServiceStatus,&ServiceStatus))
{
TerminateService();
return FALSE;
}

return TRUE;


相關文章