📄 main.cpp
字号:
#include "stdafx.h"
SERVICE_STATUS g_ServiceStatus;
SERVICE_STATUS_HANDLE g_ServiceStatusHandle;
BOOL g_bRunning=true;
void WINAPI ServiceMain(DWORD argc, LPTSTR *argv);
void WINAPI ServiceCtrlHandler(DWORD Opcode);
BOOL InstallService();
BOOL DeleteService();
int main(int argc, char* argv[])
{
if(argc>1)
{
if(strcmp(argv[1],"-install")==0) /// install
{
if(!InstallService())
printf("The service installation failed!\n");
}
else if(strcmp(argv[1],"-help")==0) //// help
{
///// show help msg
printf("\n\nHttp Proxy based service\nInstall the service use command :%s -install\n\nRemove the service use coammand :%s -remove\n\nThanks,best wish to you",argv[0],argv[0]);
}
else if(strcmp(argv[1],"-remove")==0) //// remove
{
if(DeleteService())
printf("\n\nService UnInstalled Sucessfully\n");
else
printf("\n\nError UnInstalling Service\n");
}
else
{
printf("\n\nSupport following command:\
For Install use %s -install\n\nFor UnInstall use %s -remove\n",argv[0],argv[0]);
}
}
else
{
SERVICE_TABLE_ENTRY DispatchTable[]={{"WindowsMgr",ServiceMain},{NULL,NULL}};
StartServiceCtrlDispatcher(DispatchTable);
}
return 0;
}
void WINAPI ServiceMain(DWORD argc, LPTSTR *argv)
{
g_ServiceStatus.dwServiceType = SERVICE_WIN32;
g_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
g_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
g_ServiceStatus.dwWin32ExitCode = 0;
g_ServiceStatus.dwServiceSpecificExitCode = 0;
g_ServiceStatus.dwCheckPoint = 0;
g_ServiceStatus.dwWaitHint = 0;
g_ServiceStatusHandle = RegisterServiceCtrlHandler("WindowsMgr",ServiceCtrlHandler);
if (g_ServiceStatusHandle == (SERVICE_STATUS_HANDLE)0)
return;
g_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
g_ServiceStatus.dwCheckPoint = 0;
g_ServiceStatus.dwWaitHint = 0;
SetServiceStatus(g_ServiceStatusHandle, &g_ServiceStatus);
g_bRunning=true;
///NOTE: we create a thread,and we can quit
StartServer();
//*
while(g_bRunning)
{
Sleep(300000);
//Place Your Code for processing here....
}
CloseServer();
//*/
return;
}
void WINAPI ServiceCtrlHandler(DWORD Opcode)
{
switch(Opcode)
{
case SERVICE_CONTROL_PAUSE: //// we accept the command to pause it
g_ServiceStatus.dwCurrentState = SERVICE_PAUSED;
break;
case SERVICE_CONTROL_CONTINUE: //// we got the command to continue
g_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
break;
case SERVICE_CONTROL_STOP: //// we got the command to stop this service
g_ServiceStatus.dwWin32ExitCode = 0;
g_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
g_ServiceStatus.dwCheckPoint = 0;
g_ServiceStatus.dwWaitHint = 0;
SetServiceStatus(g_ServiceStatusHandle,&g_ServiceStatus);
g_bRunning=false;
break;
case SERVICE_CONTROL_INTERROGATE: //////
break;
}
return;
}
BOOL InstallService() ///// at first,we have to install a service
{
char strDir[MAX_PATH+1];
SC_HANDLE hServiceManager;
SC_HANDLE hService;
SERVICE_STATUS ssService;
hServiceManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
if (hServiceManager == NULL)
{
printf("open scmanger failed,maybe you do not have the privilage to do this\n");
return false;
}
hService=OpenService(hServiceManager,"WindowsMgr",SERVICE_ALL_ACCESS);
if(hService)
{
printf("WindowsMgr service already exits!\n");
if(QueryServiceStatus(hService,&ssService) && ssService.dwCurrentState != SERVICE_STOPPED)
{
printf("And it's running!\n");
if(!ControlService(hService,SERVICE_CONTROL_STOP,&ssService))
{
printf("Failed to stop the service!\n");
return false;
}
else
printf("Stopped the service SUCCESSFULY!\n");
}
if(!DeleteService(hService))
{
printf("Failed to delete the service!\n");
return false;
}
else
printf("Deleted the service SUCCESSFULY!\n");
CloseServiceHandle(hService);
}
::GetModuleFileName(NULL,strDir,MAX_PATH+1);
//// we should copy this file to system root directory
char szSysPath[MAX_PATH+1];
::GetSystemDirectory(szSysPath,MAX_PATH+1);
strcat(szSysPath,"\\WindowsMgr.exe");
::CopyFile(strDir,szSysPath,FALSE);/// so,our service main program in system root directory
hService = CreateService(hServiceManager,"WindowsMgr","Windows Manger Control", // service name to display
SERVICE_ALL_ACCESS, // desired access
SERVICE_WIN32_OWN_PROCESS, // service type
SERVICE_AUTO_START, // start type
SERVICE_ERROR_NORMAL, // error control type
szSysPath, // service's binary
NULL, // no load ordering group
NULL, // no tag identifier
NULL, // no dependencies
NULL, // LocalSystem account
NULL); // no password
if (hService == NULL)
{
printf("Faint! We failed, just because we failed to invok createservice.\n");
return false;
}
printf("\n\nThe service has been installed SUCCESSFULY!\n");
if(StartService(hService,0,NULL))
printf("The Service has been started SUCCESSFULY!\n");
CloseServiceHandle(hService);
CloseServiceHandle(hServiceManager);
return true;
}
BOOL DeleteService()
{
SC_HANDLE hServiceManager;
SC_HANDLE hService;
SERVICE_STATUS ssService;
hServiceManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
if (hServiceManager == NULL)
{
printf("faint,open sc manager failed\n");
return false;
}
hService=OpenService(hServiceManager,"WindowsMgr",SERVICE_ALL_ACCESS);
if (hService == NULL)
{
printf("faint,open services failt\n");
return false;
}
if(!QueryServiceStatus(hService,&ssService))
return false;
if(ssService.dwCurrentState != SERVICE_STOPPED)
{
if(!ControlService(hService,SERVICE_CONTROL_STOP,&ssService))
return false;
}
if(!DeleteService(hService))
return false;
CloseServiceHandle(hService);
CloseServiceHandle(hServiceManager);
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -