把一個程式註冊成系統服務
//註冊並啟動/終止服務
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
- 把程式做成系統服務
- 建立一個spring alibaba nacos Discovery 服務註冊demoSpring
- Android Binder原理(三)系統服務的註冊過程Android
- mysql註冊服務MySql
- 服務註冊-Eureka
- spring cloud alibaba系列(一) 服務註冊SpringCloud
- windows驅動註冊中斷服務程式Windows
- Linux下注冊系統服務Linux
- nacos 服務註冊原理
- consul 服務註冊中心
- SpringCloud之服務註冊SpringGCCloud
- golang 同時(一個專案)註冊http和GRPC服務GolangHTTPRPC
- netty叢集(一)-服務註冊發現Netty
- Spring Cloud 實戰一:服務註冊中心SpringCloud
- 【SpringBoot】服務對註冊中心的註冊時機Spring Boot
- 將windows應用程式註冊為windows服務Windows
- C# 註冊Windows服務C#Windows
- lms框架服務註冊中心框架
- 分散式(一) 搞定服務註冊與發現分散式
- 微服務架構之「 服務註冊 」微服務架構
- etcd套路(九)註冊服務中心程式碼封裝封裝
- 《springcloud 一》搭建註冊中心,服務提供者,服務消費者SpringGCCloud
- go微服務系列(二) - 服務註冊/服務發現Go微服務
- NodeJs服務註冊與服務發現實現NodeJS
- 服務治理->搭建服務註冊中心: Spring Cloud EurSpringCloud
- 服務治理平臺-註冊中心
- 自定義元件服務註冊配置元件
- nacos服務註冊與發現
- OpenFeign 服務註冊和呼叫原理
- Eureka服務註冊與發現
- 服務註冊中心之Eureka使用
- 將IoTdb註冊為Windows服務Windows
- 微服務4:服務註冊與發現微服務
- Spring Cloud 系列(一)Eureka 服務註冊與發現SpringCloud
- 簡單使用spring cloud 服務註冊做一個請求轉發中心SpringCloud
- consul服務註冊與服務發現的巨坑
- Java Spring Cloud 與響應式微服務(一)服務註冊中心JavaSpringCloud微服務