gpt4 book ai didi

启动时出现 C++ 服务错误 : Could not Start the Service on Local Computer

转载 作者:可可西里 更新时间:2023-11-01 12:08:52 34 4
gpt4 key购买 nike

我已经使用 C++ 创建了一个 Win32 服务并成功安装到服务中。

现在,当我尝试从 Services.msc 启动服务时,出现错误:无法在本地计算机上启动服务。错误2:系统找不到指定的文件。

这是我在服务入口点中定义的代码片段:

#include "stdafx.h"
#include "iostream"
#include "Windows.h"
#include "Winsvc.h"
#include "time.h"


SERVICE_STATUS m_ServiceStatus;
SERVICE_STATUS_HANDLE m_ServiceStatusHandle;
BOOL bRunning=true;
void WINAPI ServiceMain(DWORD argc, LPTSTR *argv);
void WINAPI ServiceCtrlHandler(DWORD Opcode);
BOOL InstallService();
BOOL DeleteService();
void main(int argc, char* argv[])
{

if(argc>1)
{
if(strcmp(argv[1],"-i")==0)
{
if(InstallService())
printf("\n\nService Installed Sucessfully\n");
else
printf("\n\nError Installing Service\n");
}
else if(strcmp(argv[1],"-u")==0)
{
if(DeleteService())
printf("\n\nService UnInstalled Sucessfully\n");
else
printf("\n\nError UnInstalling Service\n");
}
else
{
printf("\n\nUnknown Switch Usage\n\nFor Install use WindowService -i\n\nFor UnInstall use WindowService -u\n");
}
}
else
{
SERVICE_TABLE_ENTRY DispatchTable[]=
{{"myService",ServiceMain},{NULL,NULL}};
StartServiceCtrlDispatcher(DispatchTable);
}
// DeleteService();
//return 0;
}

void WINAPI ServiceMain(DWORD argc, LPTSTR *argv)
{
DWORD status;
DWORD specificError;
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("myService",
ServiceCtrlHandler);
if (m_ServiceStatusHandle == (SERVICE_STATUS_HANDLE)0)
{
return;
}
m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
m_ServiceStatus.dwCheckPoint = 0;
m_ServiceStatus.dwWaitHint = 0;
if (!SetServiceStatus (m_ServiceStatusHandle, &m_ServiceStatus))
{
}
system("start c:\\progra~1\\intern~1\\iexplore.exe http://www.google.com");
//strcat("Win1", "1.exe");
/*bRunning=true;
while(bRunning)
{
//Sleep(3000);
//Place Your Code for processing here....
//printf("\nThe Service is Running Now...\n");


}*/
return;
}

void WINAPI ServiceCtrlHandler(DWORD Opcode)
{
switch(Opcode)
{
case SERVICE_CONTROL_PAUSE:
m_ServiceStatus.dwCurrentState = SERVICE_PAUSED;
break;
case SERVICE_CONTROL_CONTINUE:
m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
break;
case SERVICE_CONTROL_STOP:
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];
HANDLE schSCManager,schService;
GetCurrentDirectory(1024,strDir);
strcat(strDir,"\\Debug\\WindowService.exe");
schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);

if (schSCManager == NULL)
return false;
LPCTSTR lpszBinaryPathName=strDir;

schService = CreateService(schSCManager,"myService",
"my_Service", // service name to display

SERVICE_ALL_ACCESS, // desired access

SERVICE_WIN32_OWN_PROCESS, // service type

SERVICE_DEMAND_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)
return false;

CloseServiceHandle(schService);
return true;
}

BOOL DeleteService()
{
HANDLE schSCManager;
SC_HANDLE hService;
schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);

if (schSCManager == NULL)
return false;
hService=OpenService(schSCManager,"myService",SERVICE_ALL_ACCESS);
if (hService == NULL)
return false;
if(DeleteService(hService)==0)
return false;
if(CloseServiceHandle(hService)==0)
return false;

return true;
}

我从 codeproject 中获取了这段代码示例.

最佳答案

在 services.msc 中检查您的服务属性以查看您的 exe 应该从哪里运行,并确保这与 EXE 的物理路径相匹配。确保 EXE 确实存在。检查事件查看器以查看是否存在有关该错误的更多信息。确保代码在不作为服务运行时正常运行 - 让您的代码可选地作为服务运行而不是作为服务在这方面可能非常有帮助。在调试器中运行服务安装,以确保您没有在某处遗漏错误代码 - 服务安装是否真的端到端正常工作?

关于启动时出现 C++ 服务错误 : Could not Start the Service on Local Computer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3625692/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com