服務(wù)創(chuàng)建步驟
- 設(shè)置服務(wù)入口函數(shù)ServiceMain,
StartServiceCtrlDispatcher - 注冊服務(wù)控制處理器念秧,接收SCM的命令强霎。
RegisterServiceCtrlHandler - 服務(wù)執(zhí)行,等待停止膏燃,反饋狀態(tài)刷允。
代碼
#define SERVICE_NAME _T("MyXzmService")
#define LOGFILE _T("E:\\memstatus.txt")
SERVICE_STATUS ServiceStatus;
SERVICE_STATUS_HANDLE hStatus;
void WINAPI ServiceMain(int argc, char** argv);
void ControlHandler(DWORD request);
int WriteToLog(TCHAR *str);
int InitService();
// 文章: https://blog.csdn.net/nodeathphoenix/article/details/24181509
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
OutputDebugString(_T("-------_tWinMain--------"));
SERVICE_TABLE_ENTRY ServiceTable[2];
ServiceTable[0].lpServiceName = SERVICE_NAME; // 指向表示服務(wù)名稱字符串的指針;當(dāng)定義了多個(gè)服務(wù)時(shí)奶躯,那么這個(gè)域必須指定;
ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain; // 指向服務(wù)主函數(shù)的指針(服務(wù)入口點(diǎn))亿驾;
ServiceTable[1].lpServiceName = NULL;
ServiceTable[1].lpServiceProc = NULL;
StartServiceCtrlDispatcher(ServiceTable);
return 0;
}
// 該函數(shù)是服務(wù)的入口點(diǎn)嘹黔。它運(yùn)行在一個(gè)單獨(dú)的線程當(dāng)中,這個(gè)線程是由控制分派器創(chuàng)建的
void WINAPI ServiceMain(int argc, char** argv){
OutputDebugString(_T("ServiceMain start"));
ServiceStatus.dwServiceType = SERVICE_WIN32; // 指示服務(wù)類型颊乘,創(chuàng)建 Win32 服務(wù)参淹。賦值 SERVICE_WIN32 ;
ServiceStatus.dwCurrentState = SERVICE_START_PENDING; // 指定服務(wù)的當(dāng)前狀態(tài)
ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN; // 這個(gè)域通知 SCM 服務(wù)接受哪個(gè)域
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwCheckPoint = 0;
ServiceStatus.dwWaitHint = 0;
// 為服務(wù)注冊控制處理器,兩個(gè)參數(shù)傳遞給此函數(shù):服務(wù)名和指向 ControlHandlerfunction 的指針乏悄。
// 注冊完控制處理器之后浙值,獲得狀態(tài)句柄(hStatus)。通過調(diào)用 SetServiceStatus 函數(shù)檩小,用 hStatus 向 SCM 報(bào)告服務(wù)的狀態(tài)开呐。
hStatus = RegisterServiceCtrlHandler(SERVICE_NAME, (LPHANDLER_FUNCTION)ControlHandler);
if (hStatus == (SERVICE_STATUS_HANDLE)0)
{
// Registering Controll Handler failed;
return;
}
OutputDebugString(_T("ServiceMain InitService"));
int error = InitService();
if (error)
{
OutputDebugString(_T("ServiceMain InitService fail."));
// 初始化失敗,終止服務(wù)
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
ServiceStatus.dwWin32ExitCode = -1;
SetServiceStatus(hStatus, &ServiceStatus);
// 退出ServiceMan
return;
}
ServiceStatus.dwCurrentState = SERVICE_RUNNING;
SetServiceStatus(hStatus, &ServiceStatus);
//
// 不斷獲取內(nèi)存大小寫入文件
while (ServiceStatus.dwCurrentState == SERVICE_RUNNING)
{
TCHAR buffer[100];
MEMORYSTATUS memory;
GlobalMemoryStatus(&memory);
_stprintf_s(buffer, _T("memory.dwAvailPhys=%d"), memory.dwAvailPhys);
int result = WriteToLog(buffer);
if (result)
{
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
ServiceStatus.dwWin32ExitCode = -1;
SetServiceStatus(hStatus, &ServiceStatus);
return;
}
// 等待10s
Sleep(10000);
}
}
void ControlHandler(DWORD request){
switch (request)
{
case SERVICE_CONTROL_STOP:
WriteToLog(_T("Monitoring stoppend."));
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
ServiceStatus.dwWin32ExitCode = 0;
SetServiceStatus(hStatus, &ServiceStatus);
break;
case SERVICE_CONTROL_SHUTDOWN:
WriteToLog(_T("Monitoring shutdown."));
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
ServiceStatus.dwWin32ExitCode = 0;
SetServiceStatus(hStatus, &ServiceStatus);
break;
default:
break;
}
}
int InitService(){
int result;
result = WriteToLog(_T("Monitoring started."));
return result;
}
int WriteToLog(TCHAR *str){
FILE * log;
_tfopen_s(&log, LOGFILE, _T("a+"));
if (log == NULL)
{
return -1;
}
_ftprintf(log,_T("%s\n"), str);
fclose(log);
return 0;
}
SC.exe管理Service
sc命令的語法格式:
sc create MyService binpath= e:/xxx/MyService.exe ;創(chuàng)建服務(wù)MyService
sc start MyService ;啟動(dòng)服務(wù)
sc stop MyService ;停止服務(wù)
sc delete MyService ; 刪除服務(wù)
sc config 服務(wù)名 start= auto|demand|disabled ;修改服務(wù)啟動(dòng)類型规求。start參數(shù)的值可以是demand(手動(dòng))筐付、disabled(禁用),auto(自動(dòng))阻肿。
參考
C++編寫Windows服務(wù)
https://blog.csdn.net/nodeathphoenix/article/details/24181509