把一個程式註冊成系統服務
//註冊並啟動/終止服務
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;
}
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;
}
相關文章
- exe程式註冊成windows系統服務Windows
- 把程式做成系統服務
- mysql註冊服務MySql
- ElasticSearch註冊Windows服務ElasticsearchWindows
- nacos 服務註冊原理
- Android Binder原理(三)系統服務的註冊過程Android
- windows驅動註冊中斷服務程式Windows
- 建立一個spring alibaba nacos Discovery 服務註冊demoSpring
- SpringCloud之服務註冊SpringGCCloud
- lms框架服務註冊中心框架
- C# 註冊Windows服務C#Windows
- Linux下注冊系統服務Linux
- 將windows應用程式註冊為windows服務Windows
- golang 同時(一個專案)註冊http和GRPC服務GolangHTTPRPC
- 微服務架構之「 服務註冊 」微服務架構
- netty叢集(一)-服務註冊發現Netty
- Spring Cloud 實戰一:服務註冊中心SpringCloud
- 分散式(一) 搞定服務註冊與發現分散式
- spring cloud alibaba系列(一) 服務註冊SpringCloud
- go微服務系列(二) - 服務註冊/服務發現Go微服務
- 自定義元件服務註冊配置元件
- 服務治理平臺-註冊中心
- 將IoTdb註冊為Windows服務Windows
- Nacos服務註冊與發現
- 【SpringBoot】服務對註冊中心的註冊時機Spring Boot
- 微服務4:服務註冊與發現微服務
- 【微服務之Eureka服務註冊發現】微服務
- 《springcloud 一》搭建註冊中心,服務提供者,服務消費者SpringGCCloud
- 服務治理->搭建服務註冊中心: Spring Cloud EurSpringCloud
- NodeJs服務註冊與服務發現實現NodeJS
- etcd套路(九)註冊服務中心程式碼封裝封裝
- 小白入門微服務(4) – 服務註冊與服務發現微服務
- 小白入門微服務(4) - 服務註冊與服務發現微服務
- 微服務之服務註冊和服務發現篇微服務
- 第一章:SpringCloud服務發現和服務註冊SpringGCCloud
- Zookeeper實現服務註冊/發現
- Nacos服務註冊與發現原理
- apisix~整合服務發現註冊中心API