---
title: "vc++搭建一个NT服务框架"
author: "winzenghua"
date: "2009-03-29T18:15:00Z"
tags: []
slug: "vc-nt"
draft: false
lastmod: "2009-03-29T18:15:00Z"
id: 948155
url: "https://java4gis.iteye.com/blog/948155"
---

#include &lt;stdio.h&gt;  
#include &lt;windows.h&gt;  
SERVICE\_STATUS m\_ServiceStatus;  
SERVICE\_STATUS\_HANDLE m\_ServiceStatusHandle;  
BOOL bRunning=true;  
void WINAPI ServiceMain(DWORD argc, LPTSTR \*argv);//服务主函数  
void WINAPI ServiceCtrlHandler(DWORD Opcode);//服务控制函数  
void WINAPI CmdStart(void);//要启动的程序函数  
BOOL InstallService(); //安装服务的函数  
BOOL DeleteService(); //删除服务的函数

int main(int argc, char* argv\[])  
{  
printf("\\twindows based service demo\\n");  
printf("\\tgxisone@hotmail.com\\n");  
if(argc!=3)  
{  
printf("usage: %s -install\[remove]",argv\[0]);  
return 0;  
}  
if(strcmp(argv\[1],"-install")==0) /// install  
{  
if(InstallService())  
printf("\\n\\nService Installed Sucessfully\\n");  
else  
printf("\\n\\nError Installing Service\\n");  
}  
else if(strcmp(argv\[1],"-remove")==0) // remove  
{  
if(DeleteService())  
printf("\\n\\nService remove sucessfully\\n");  
else  
printf("\\n\\nError removing Service\\n");  
}  
else  
{  
printf("\\nusage: %s -install\[remove]\\n",argv\[0]);  
return 0;  
}  
//在进入点函数里面要完成ServiceMain的初始化，  
//准确点说是初始化一个SERVICE\_TABLE\_ENTRY结构数组，  
//这个结构记录了这个服务程序里面所包含的所有服务的名称  
//和服务的进入点函数  
SERVICE\_TABLE\_ENTRY  
DispatchTable\[]={{"WindowsMgr",ServiceMain},{NULL,NULL}};  
//最后的NULL指明数组的结束  
StartServiceCtrlDispatcher(DispatchTable);  
return 0;  
}

void WINAPI ServiceMain(DWORD argc, LPTSTR \*argv)  
{  
m\_ServiceStatus.dwServiceType = SERVICE\_WIN32;  
m\_ServiceStatus.dwCurrentState = SERVICE\_START\_PENDING;  
m\_ServiceStatus.dwControlsAccepted = SERVICE\_ACCEPT\_STOP;  
m\_ServiceStatus.dwWin32ExitCode = 0;  
m\_ServiceStatus.dwServiceSpecificExitCode = 0;  
m\_ServiceStatus.dwCheckPoint = 0;  
m\_ServiceStatus.dwWaitHint = 0;  
m\_ServiceStatusHandle = RegisterServiceCtrlHandler("WindowsMgr",ServiceCtrlHandler);  
if (m\_ServiceStatusHandle == (SERVICE\_STATUS\_HANDLE)0)return;  
m\_ServiceStatus.dwCurrentState = SERVICE\_RUNNING; //设置服务状态  
m\_ServiceStatus.dwCheckPoint = 0;  
m\_ServiceStatus.dwWaitHint = 0;  
//SERVICE\_STATUS结构含有七个成员，它们反映服务的现行状态。  
//所有这些成员必须在这个结构被传递到SetServiceStatus之前正确的设置  
SetServiceStatus (m\_ServiceStatusHandle, &m\_ServiceStatus);  
bRunning=true;  
//*  
CmdStart(); //启动我们的服务程序  
//*  
return;  
}  
void WINAPI ServiceCtrlHandler(DWORD Opcode)//服务控制函数  
{  
switch(Opcode)  
{  
case SERVICE\_CONTROL\_PAUSE: // we accept the command to pause it  
m\_ServiceStatus.dwCurrentState = SERVICE\_PAUSED;  
break;  
case SERVICE\_CONTROL\_CONTINUE: // we got the command to continue  
m\_ServiceStatus.dwCurrentState = SERVICE\_RUNNING;  
break;  
case SERVICE\_CONTROL\_STOP: // we got the command to stop this service  
m\_ServiceStatus.dwWin32ExitCode = 0;  
m\_ServiceStatus.dwCurrentState = SERVICE\_STOPPED;  
m\_ServiceStatus.dwCheckPoint = 0;  
m\_ServiceStatus.dwWaitHint = 0;  
SetServiceStatus (m\_ServiceStatusHandle,&m\_ServiceStatus);  
bRunning=false;  
break;  
case SERVICE\_CONTROL\_INTERROGATE: //  
break;  
}  
return;  
}  
BOOL InstallService() //安装服务函数  
{  
char strDir\[1024];  
SC\_HANDLE schSCManager,schService;  
GetCurrentDirectory(1024,strDir);  
GetModuleFileName(NULL,strDir,sizeof(strDir));

char chSysPath\[1024];  
GetSystemDirectory(chSysPath,sizeof(chSysPath));

strcat(chSysPath,"[\\\\WindowsMgr.exe]()");  
if(!CopyFile(strDir,chSysPath,FALSE))printf("Copy file OK\\n"); // 把我们的服务程序复制到系统根目录

strcpy(strDir,chSysPath);  
schSCManager = OpenSCManager(NULL,NULL,SC\_MANAGER\_ALL\_ACCESS);  
if (schSCManager == NULL)  
{  
printf("open scmanger failed,maybe you do not have the privilage to do this\\n");  
return false;  
}

LPCTSTR lpszBinaryPathName=strDir;

schService = CreateService(schSCManager,"WindowsMgr","Windows Manger Control", //将服务的信息添加到SCM的数据库  
SERVICE\_ALL\_ACCESS, // desired access  
SERVICE\_WIN32\_OWN\_PROCESS, // service type  
SERVICE\_AUTO\_START, // start type  
SERVICE\_ERROR\_NORMAL, // error control type  
lpszBinaryPathName, // service's binary  
NULL, // no load ordering group  
NULL, // no tag identifier  
NULL, // no dependencies  
NULL, // LocalSystem account  
NULL); // no password

if (schService == NULL)  
{  
printf("faint,we failed just because we invoke createservices failed\\n");  
return false;  
}  
CloseServiceHandle(schService);  
return true;  
}  
BOOL DeleteService()  
{  
SC\_HANDLE schSCManager;  
SC\_HANDLE hService;  
schSCManager = OpenSCManager(NULL,NULL,SC\_MANAGER\_ALL\_ACCESS);

char chSysPath\[1024];  
GetSystemDirectory(chSysPath,sizeof(chSysPath));  
strcat(chSysPath,"[\\\\WindowsMgr.exe]()");

if (schSCManager == NULL)  
{  
printf("faint,open scmanger failed\\n");  
return false;  
}  
hService=OpenService(schSCManager,"WindowsMgr",SERVICE\_ALL\_ACCESS);  
if (hService == NULL)  
{  
printf("faint,open services failt\\n");  
return false;  
}  
if(DeleteFile(chSysPath)==0)  
{  
printf("Dell file Failure !\\n");  
return false;  
}  
else printf("Delete file OK!\\n");  
if(DeleteService(hService)==0)  
return false;

if(CloseServiceHandle(hService)==0)  
return false;  
else  
return true;  
}

void WINAPI CmdStart(void)  
{  
//--------------------------------  
//把你的要做成服务启动的程序代码添加到这里  
//那么你的代码就可以作为NT服务启动了  
//--------------------------------  
}