// server.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Windows.h"
#define SZAPPNAME "serverSample"
#define SZSERVICENAME "serviceSample"
bool bDebugServer=false;
SERVICE_STATUS ssStatus;
SERVICE_STATUS_HANDLE sshStatusHandle;
DWORD dwErr=0;
TCHAR szErr[256];
void WINAPI Service_Main(DWORD dwArgc, LPTSTR *lpszArgv);
void WINAPI Service_Ctrl(DWORD dwCtrlCode);
void installService();
void removeService();
void debugService(int argc,char** argv);
bool ReportStatusToSCMgr(DWORD dwCurrentState,DWORD dwWin32ExitCode,DWORD dwWaitHint);
void AddToMessageLog(LPTSTR lpszMsg);
void ServiceStart(DWORD dwArgc,LPTSTR* lpszArgv);
int _tmain(int argc, _TCHAR* argv[])
{
SERVICE_TABLE_ENTRY dispatchTable[]=
{
{TEXT(SZSERVICENAME),(LPSERVICE_MAIN_FUNCTION)Service_Main},
{ NULL,NULL}
};
// if((argc>1)&&((*argv[1]=='-')||(argv[1]==L"/")))
// {
// if(_stricmp(L"install",argv[1]+1)==0)
// {
// installService();
// }
// else if(_stricmp(L"remove",argv[1]+1)==0)
// {
// removeService();
// }
// else if(_stricmp(L"debug",argv[1]+1)==0)
// {
// bDebugServer=true;
// debugService(argc,argv);
// }
// else
// { installService();
printf("%s - install to install the service \n",SZAPPNAME);
printf("%s - remove to remove the service \n",SZAPPNAME);
printf("%s - debug to debug the service \n",SZAPPNAME);
printf("\n StartServiceCtrlDispatcher being called.\n");
printf("This may take several seconds.Please wait.\n");
if(!StartServiceCtrlDispatcher(dispatchTable))
AddToMessageLog(TEXT("StartServiceCtrlDispatcher failed."));
else
AddToMessageLog(TEXT("StartServiceCtrlDispatcher OK."));
// }
//exit(0);
//}
return 0;
}
void WINAPI Service_Main(DWORD dwArgc, LPTSTR *lpszArgv)
{
sshStatusHandle=RegisterServiceCtrlHandler(TEXT(SZSERVICENAME),Service_Ctrl);
if(!sshStatusHandle)
{
goto cleanup;
return;
}
ssStatus.dwServiceType=SERVICE_WIN32_OWN_PROCESS;
ssStatus.dwServiceSpecificExitCode=0;
if(!ReportStatusToSCMgr(
SERVICE_START_PENDING,
NO_ERROR,
3000))
goto cleanup;
ServiceStart(dwArgc,lpszArgv);
return;
cleanup:
if(sshStatusHandle)
(void)ReportStatusToSCMgr(SERVICE_STOPPED,dwErr,0);
}
void WINAPI Service_Ctrl(DWORD dwCtrlCode)
{
switch(dwCtrlCode)
{
//先更新服务状态为 SERVICDE_STOP_PENDING,再停止服务。
case SERVICE_CONTROL_STOP:
ReportStatusToSCMgr(SERVICE_STOP_PENDING,NO_ERROR,500);
ServiceStop(); //由具体的服务程序实现
return;
//暂停服务
case SERVICE_CONTROL_PAUSE:
ReportStatusToSCMgr(SERVICE_STOP_PENDING,NO_ERROR,500);
ServicePause(); //由具体的服务程序实现
ssStatus.dwCurrentState=SERVICE_PAUSED;
return;
//继续服务
case SERVICE_CONTROL_CONTINUE:
ReportStatusToSCMgr(SERVICE_STOP_PENDING,NO_ERROR,500);
ServiceContinue(); //由具体的服务程序实现
ssStatus.dwCurrentState=SERVICE_RUNNING;
return;
//更新服务状态
case SERVICE_CONTROL_INTERROGATE:
break;
//无效控制码
default:
break;
}
ReportStatusToSCMgr(ssStatus.dwCurrentState,NO_ERROR,0);
}
void installService()
{
SC_HANDLE schService;
SC_HANDLE schSCManager;
TCHAR szPath[512];
//得到程序磁盘文件的路径
if(GetModuleFileName(NULL,szPath,512)==0)
{
_tprintf(TEXT("Unable to install %s - %s \n"),
TEXT(SZAPPNAME),
GetLastError());//@1获取调用函数返回的最后错误码
return;
}
//打开服务管理数据库
schSCManager=OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if(schSCManager)
{
//登记服务程序
schService=CreateService(
schSCManager, //服务管理数据库句柄
TEXT(SZSERVICENAME), //服务名
TEXT(SZAPPNAME), //用于显示服务的标识
SERVICE_ALL_ACCESS, //响应所有的访问请求
SERVICE_WIN32_OWN_PROCESS, //服务类型
SERVICE_DEMAND_START, //启动类型
SERVICE_ERROR_NORMAL, //错误控制类型
szPath, //服务程序磁盘文件的路径
NULL, //服务不属于任何组
NULL, //没有tag标识符
NULL, //启动服务所依赖的服务或服务组,这里仅仅是一个空字符串
NULL, //LocalSystem 帐号
NULL);
if(schService)
{
_tprintf(TEXT("%s installed. \n"),TEXT(SZAPPNAME));
CloseServiceHandle(schService);
}
else
{
_tprintf(TEXT("CreateService failed - %s \n"),GetLastError());
}
CloseServiceHandle(schSCManager);
}
else
_tprintf(TEXT("OpenSCManager failed - %s \n"),GetLastError());
}
//停止和删除已安装的服务程序
void removeService()
{
SC_HANDLE schService;
SC_HANDLE schSCManager;
//打开服务管理数据库
schSCManager=OpenSCManager(
NULL, //本地计算机
NULL, //默认的数据库
SC_MANAGER_ALL_ACCESS //要求所有的访问权
);
if(schSCManager)
{
//获取服务程序句柄
schService=OpenService(
schSCManager, //服务管理数据库句柄
TEXT(SZSERVICENAME), //服务名
SERVICE_ALL_ACCESS //响应所有的访问请求
);
if(schService)
{
//试图停止服务
if(ControlService(
schService, //服务程序句柄
SERVICE_CONTROL_STOP, //停止服务请求码
&ssStatus //接收最后的服务状态信息
))
{
_tprintf(TEXT("Stopping %s."),TEXT(SZAPPNAME));
Sleep(1000);
//等待服务停止
//
while(QueryServiceStatus(schService,&ssStatus))
{
if(SERVICE_STOP_PENDING==ssStatus.dwCurrentState)
{
_tprintf(TEXT("."));
Sleep(1000);
}
else
break;
}
if(SERVICE_STOPPED==ssStatus.dwCurrentState)
_tprintf(TEXT("\n %s stopped. \n"),TEXT(SZAPPNAME));
else
_tprintf(TEXT("\n %s failed to stopp. \n"),TEXT(SZAPPNAME));
}
//删除已安装的服务程序安装
if(DeleteService(schService))
_tprintf(TEXT("%s removed. \n"),TEXT(SZAPPNAME));
else
_tprintf(TEXT("DeleteService failed - %s. \n"), GetLastError());
CloseServiceHandle(schService);
}
else
_tprintf(TEXT("OpenService failed - %s \n"),GetLastError());
CloseServiceHandle(schSCManager);
}
else
_tprintf(TEXT("OpenSCManager failed - %s \n"),GetLastError());
}