创建windows服务

1. 新建console程序

2. 将如下代码拷到程序中

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

class CWinService
{
public:
static void RunService(LPCTSTR lpszSeriveName);
private:
enum { SLEEP_TIME = 5000};
static SERVICE_STATUS          ServiceStatus; 
static SERVICE_STATUS_HANDLE   hStatus; 
static TCHAR serviceName[MAX_PATH];

private:
static void  ServiceMain(int argc, char** argv); 
static void  ControlHandler(DWORD request); 
static int InitService();
};

SERVICE_STATUS          CWinService::ServiceStatus; 
SERVICE_STATUS_HANDLE   CWinService::hStatus; 
TCHAR CWinService::serviceName[MAX_PATH];

void CWinService::RunService(LPCTSTR lpszSeriveName)
{
_tcscpy(serviceName, lpszSeriveName);

SERVICE_TABLE_ENTRY ServiceTable[2];
ServiceTable[0].lpServiceName = new TCHAR[MAX_PATH];

_tcscpy(ServiceTable[0].lpServiceName, lpszSeriveName);
ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)CWinService::ServiceMain;

ServiceTable[1].lpServiceName = NULL;
ServiceTable[1].lpServiceProc = NULL;


// Start the control dispatcher thread for our service
StartServiceCtrlDispatcher(ServiceTable);
delete []ServiceTable[0].lpServiceName;
}


int CWinService::InitService() 

OutputDebugString(L"Monitoring started.");
return 0; 
}


// Control Handler
void CWinService::ControlHandler(DWORD request) 

switch(request) 

case SERVICE_CONTROL_STOP: 
OutputDebugString(L"Monitoring stopped.");
ServiceStatus.dwWin32ExitCode = 0; 
ServiceStatus.dwCurrentState = SERVICE_STOPPED; 
SetServiceStatus (hStatus, &ServiceStatus);
return; 

case SERVICE_CONTROL_SHUTDOWN: 
OutputDebugString(L"Monitoring stopped.");
ServiceStatus.dwWin32ExitCode = 0; 
ServiceStatus.dwCurrentState = SERVICE_STOPPED; 
SetServiceStatus (hStatus, &ServiceStatus);
return;         
default:
break;
}  
// Report current status
SetServiceStatus (hStatus, &ServiceStatus); 
return; 
}

void CWinService::ServiceMain(int argc, char** argv) 

int error; 

ServiceStatus.dwServiceType = SERVICE_WIN32; 
ServiceStatus.dwCurrentState = SERVICE_START_PENDING; 
ServiceStatus.dwControlsAccepted   =SERVICE_ACCEPT_STOP |SERVICE_ACCEPT_SHUTDOWN;
ServiceStatus.dwWin32ExitCode = 0; 
ServiceStatus.dwServiceSpecificExitCode = 0; 
ServiceStatus.dwCheckPoint = 0; 
ServiceStatus.dwWaitHint = 0; 

hStatus = RegisterServiceCtrlHandler(serviceName,(LPHANDLER_FUNCTION)ControlHandler); 
if (hStatus == (SERVICE_STATUS_HANDLE)0) 

return; 
}  


// Initialize Service 
error = InitService(); 
if (error) 
{
// Initialization failed
ServiceStatus.dwCurrentState = SERVICE_STOPPED; 
ServiceStatus.dwWin32ExitCode = -1; 
SetServiceStatus(hStatus, &ServiceStatus); 
return; 





// We report the running status to SCM. 
ServiceStatus.dwCurrentState = SERVICE_RUNNING; 
SetServiceStatus (hStatus, &ServiceStatus);


MEMORYSTATUS memory;
// The worker loop of a service
while (ServiceStatus.dwCurrentState == SERVICE_RUNNING)
{
Sleep(SLEEP_TIME );
}
return; 
}

void main(int argc, char* argv[])

CWinService::RunService(L"AcebyteAutoCare");
}


注册服务

sc create xxxx binpath= "c:\xxxx.exe" displayname= "xxxxx"  start= auto

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值