软件编写完了,创建了一个内核服务,提示
[SC] StartService 失败 1053:
服务没有及时响应启动或控制请求。
但是已经有了响应启动程序,如何解决?
代码如下:
#include <Windows.h>
#include <stdio.h>
#include <iostream>
using namespace std;
SERVICE_STATUS_HANDLE g_ServiceStatusHandle;
SERVICE_STATUS g_ServiceStatus;
HANDLE g_StopEvent = NULL;
VOID WINAPI ServiceCtrlHandler(DWORD dwControl) {
switch (dwControl) {
case SERVICE_CONTROL_STOP:
g_ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
SetServiceStatus(g_ServiceStatusHandle, &g_ServiceStatus);
SetEvent(g_StopEvent);
break;
default:
break;
}
}
VOID WINAPI ServiceMain(DWORD dwArgc, LPSTR* lpszArgv) {
// 初始化服务状态
g_ServiceStatusHandle = RegisterServiceCtrlHandler("YourServiceName", ServiceCtrlHandler);
g_ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
g_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
g_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
SetServiceStatus(g_ServiceStatusHandle, &g_ServiceStatus);
//stop
g_StopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (g_StopEvent == NULL) {
return;
}
// 更新服务状态为运行中
g_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
SetServiceStatus(g_ServiceStatusHandle, &g_ServiceStatus);
WaitForSingleObject(g_StopEvent, INFINITE);
CloseHandle(g_StopEvent);
g_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(g_ServiceStatusHandle, &g_ServiceStatus);
}
int main() {
SERVICE_TABLE_ENTRY DispatchTable[] = {
{ "C++ Calc Legend Kernel", (LPSERVICE_MAIN_FUNCTION)ServiceMain },
{ NULL, NULL }
};
if (!StartServiceCtrlDispatcher(DispatchTable)) {
DWORD error = GetLastError();
return 1;
}
HMODULE dll=LoadLibrary("C:\\Windows\\System32\\drivers\\CalcKernel32.dll");
if(dll==NULL) cout<<"<error>";
while(1){
Sleep(1000);
}
return 0;
返回值:1 没有输出
相似问题