Windows服务编程Demo

时间:2022-05-23 08:46:43

实现一个开机自动启动的关机程序,具体代码如下:

 #include <Windows.h>

 void ServiceMain();
void ControlHandler(DWORD request);
void RunService();
void Shutdown();
BOOL IsInstall();
BOOL Install(); SERVICE_STATUS_HANDLE hServiceStatus;
SERVICE_STATUS status; int WINAPI WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in_opt LPSTR lpCmdLine, __in int nShowCmd )
{
SERVICE_TABLE_ENTRY st[] =
{
{TEXT("Shutdown"), (LPSERVICE_MAIN_FUNCTION)ServiceMain},
{NULL, NULL}
}; if (!IsInstall())
{
Install();
} StartServiceCtrlDispatcher(st); RunService(); return ;
} void RunService()
{
SC_HANDLE schSCManager;
SC_HANDLE schService; schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (!schSCManager)
{
MessageBox(NULL, TEXT("OpenSCManager error"), TEXT("error"), MB_OK | MB_ICONERROR);
return;
} schService = OpenService(schSCManager, TEXT("Shutdown"), SERVICE_ALL_ACCESS);
if (!schService)
{
MessageBox(NULL, TEXT("OpenService error"), TEXT("error"), MB_OK | MB_ICONERROR);
} BOOL b = StartService(schService, , NULL);
} void ControlHandler(DWORD request)
{
switch (request)
{
case SERVICE_CONTROL_STOP:
case SERVICE_CONTROL_SHUTDOWN:
status.dwWin32ExitCode = ;
status.dwCurrentState = SERVICE_STOPPED;
break;
default:
break;
}
SetServiceStatus(hServiceStatus, &status);
} void ServiceMain()
{
status.dwServiceType = SERVICE_WIN32;
status.dwCurrentState = SERVICE_START_PENDING;
status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
status.dwWin32ExitCode = ;
status.dwServiceSpecificExitCode = ;
status.dwCheckPoint = ;
status.dwWaitHint = ; hServiceStatus = RegisterServiceCtrlHandler(TEXT("Shutdown"), (LPHANDLER_FUNCTION)ControlHandler);
if ((SERVICE_STATUS_HANDLE) == hServiceStatus)
{
return;
} status.dwCurrentState = SERVICE_RUNNING;
SetServiceStatus(hServiceStatus, &status);
Shutdown();
} void Shutdown()
{
OSVERSIONINFO os;
os.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&os);
if (VER_PLATFORM_WIN32_NT == os.dwPlatformId)
{
HANDLE handle;
TOKEN_PRIVILEGES tkp; OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &handle);
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[].Luid); tkp.PrivilegeCount = ;
tkp.Privileges[].Attributes = SE_PRIVILEGE_ENABLED; AdjustTokenPrivileges(handle, FALSE, &tkp, , (PTOKEN_PRIVILEGES)NULL, ); if (GetLastError() != ERROR_SUCCESS)
{
MessageBox(NULL, TEXT("AdjustTokenPrivileges error"), TEXT("error"), MB_OK | MB_ICONERROR);
}
} ExitWindowsEx(EWX_SHUTDOWN, );
} BOOL IsInstall()
{
BOOL bResult = FALSE; SC_HANDLE schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (schSCManager)
{
SC_HANDLE schService = OpenService(schSCManager, TEXT("Shutdown"), SERVICE_ALL_ACCESS);
if (schService)
{
bResult = TRUE;
CloseServiceHandle(schService);
}
CloseServiceHandle(schSCManager);
}
else
{
MessageBox(NULL, TEXT("OpenSCManager error"), TEXT("error"), MB_OK | MB_ICONERROR);
} return bResult;
} BOOL Install()
{
SC_HANDLE schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (!schSCManager)
{
MessageBox(NULL, TEXT("OpenSCManager error"), TEXT("error"), MB_OK | MB_ICONERROR);
return FALSE;
} TCHAR szFilePath[MAX_PATH];
GetModuleFileName(NULL, szFilePath, MAX_PATH); SC_HANDLE schService = CreateService(schSCManager, TEXT("Shutdown"), TEXT("Shutdown"), SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, szFilePath, NULL, NULL, TEXT(""), NULL, NULL);
if (!schService)
{
CloseServiceHandle(schSCManager);
MessageBox(NULL, TEXT("CreateService error"), TEXT("error"), MB_OK | MB_ICONERROR);
return FALSE;
} CloseServiceHandle(schService);
CloseServiceHandle(schSCManager); return TRUE;
}

本程序是开机自动关机的恶搞程序,本人亲自在物理机上测试,在windows server 2008 r2 enterprise版本的系统下测试通过。要解除本恶搞程序对你造成的不便,可以在开机时按F8进入安全模式下删除名字为Shutdown的服务即可。 输入命令: sc delete Shutdown