C++ StartService失败,错误代码为1053
我正试图在远程机器上启动一个进程。我正在使用OpenSCManager()、CreateService()、QueryServiceStatusEx()和StartService()API-s。我能够成功地将我的流程作为服务安装,但当我尝试启动它时,StartService返回错误代码1053(“该服务没有及时响应启动或控制请求”)。有人能帮我解决这个问题吗 提前谢谢 坎皮 更新:这是我到目前为止的代码(几乎来自msdn,但我已经编辑了一点)C++ StartService失败,错误代码为1053,c++,windows,remoting,C++,Windows,Remoting,我正试图在远程机器上启动一个进程。我正在使用OpenSCManager()、CreateService()、QueryServiceStatusEx()和StartService()API-s。我能够成功地将我的流程作为服务安装,但当我尝试启动它时,StartService返回错误代码1053(“该服务没有及时响应启动或控制请求”)。有人能帮我解决这个问题吗 提前谢谢 坎皮 更新:这是我到目前为止的代码(几乎来自msdn,但我已经编辑了一点) #包括 VOID svcsinstall(); VO
#包括
VOID svcsinstall();
VOID uu stdcall DoStartSvc();
#pragma注释(lib,“Advapi32.lib”)
SCU经理;
SC_处理服务;
int _tmain(int argc,_TCHAR*argv[]
{
SvcInstall();
DoStartSvc();
返回0;
}
VOID SvcInstall()
{
SCU经理;
SC_处理服务;
TCHAR szPath[MAX_PATH];
如果(!GetModuleFileName(NULL,szPath,MAX_PATH))
{
printf(“无法安装服务(%d)\n”,GetLastError());
返回;
}
//获取SCM数据库的句柄。
SChManager=OpenSCManager(
_T(“\\\\kampimachine”),//远程计算机
NULL,//ServicesActive数据库
SC_MANAGER_ALL_ACCESS);//完全访问权限
if(NULL==SCHManager)
{
printf(“OpenSCManager失败(%d)\n”,GetLastError());
返回;
}
//创建服务
schService=CreateService(
schSCManager,//SCM数据库
_T(“kampi”),//服务名称
_T(“kampi”),//要显示的服务名称
服务\u所有\u访问,//所需访问
服务\u WIN32\u自己的\u进程,//服务类型
服务需求启动,//启动类型
服务\u错误\u正常,//错误控制类型
_T(“%SystemRoot%\\system32\\notepad.exe”),//服务二进制文件的路径
NULL,//无加载顺序组
NULL,//没有标记标识符
NULL,//没有依赖项
_T(“域\\用户”),//本地系统帐户
_T(“密码”);//没有密码
if(schService==NULL)
{
printf(“CreateService失败(%d)\n”,GetLastError());
CloseServiceHandle(schSCManager);
返回;
}
else printf(“服务安装成功\n”);
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
}
VOID uu stdcall DoStartSvc()
{
服务状态\流程状态;
德沃德·德沃德检查站;
DWORD dwStartTickCount;
德沃德·德怀特泰姆;
德沃德·德比特斯;
//获取SCM数据库的句柄。
SChManager=OpenSCManager(
_T(“\\\\kampimachine”),//远程计算机
NULL,//servicesActive数据库
SC_MANAGER_ALL_ACCESS);//完全访问权限
if(NULL==SCHManager)
{
printf(“OpenSCManager失败(%d)\n”,GetLastError());
返回;
}
//获得服务的句柄。
schService=OpenService(
schSCManager,//SCM数据库
_T(“kampi”),//服务名称
服务_所有访问);//完全访问
if(schService==NULL)
{
printf(“OpenService失败(%d)\n”,GetLastError());
CloseServiceHandle(schSCManager);
返回;
}
//如果服务未停止,请检查状态。
如果(!QueryServiceStatusEx(
schService,//句柄到服务
SC_状态_进程_信息,//信息级别
(LPBYTE)&ssStatus,//结构的地址
sizeof(服务状态进程),//结构的大小
&dwBytesRequired))//如果缓冲区太小,则需要大小
{
printf(“QueryServiceStatusEx失败(%d)\n”,GetLastError());
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
返回;
}
//检查服务是否已在运行。这是可能的
//在这里停止服务,但为了简单起见,此示例仅返回。
if(ssStatus.dwCurrentState!=服务已停止和&ssStatus.dwCurrentState!=服务已停止
{
printf(“无法启动服务,因为它已在运行\n”);
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
返回;
}
//保存勾号计数和初始检查点。
dwStartTickCount=GetTickCount();
dwOldCheckPoint=ssStatus.dwCheckPoint;
//等待服务停止,然后再尝试启动它。
while(ssStatus.dwCurrentState==SERVICE\u STOP\u PENDING)
{
//等待的时间不要超过等待提示的时间。最好间隔一段时间
//等待提示的十分之一,但不少于1秒
//不超过10秒。
dwWaitTime=ssStatus.dwWaitHint/10;
如果(dwWaitTime<1000)
dwWaitTime=1000;
否则如果(dwWaitTime>10000)
dwWaitTime=10000;
睡眠时间;
//检查状态,直到服务不再处于停止挂起状态。
如果(!QueryServiceStatusEx(
schService,//句柄到服务
SC_状态_进程_信息,//信息级别
(LPBYTE)&ssStatus,//结构的地址
sizeof(服务状态进程),//结构的大小
&dwBytesRequired))//如果缓冲区太小,则需要大小
{
printf(“QueryServiceStatusEx失败(%d)\n”,GetLastError());
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
返回;
}
if(ssStatus.dwCheckPoint>dwOldCheckPoint)
{
//继续等待并检查。
dwStartTickCount=GetTickCount();
dwOldCheckPoint=ssStatus.dwCheckPoint;
}
其他的
{
if(GetTickCount()-dwStartTickCount>ssStatus.dwWaitHint)
{
printf(“等待服务停止的超时\n”);
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
返回;
}
}
}
//尝试启动服务。
如果(!StartService(
schService,//句柄到服务
0,//参数数
N
#include <windows.h>
VOID SvcInstall();
VOID __stdcall DoStartSvc();
#pragma comment(lib, "Advapi32.lib")
SC_HANDLE schSCManager;
SC_HANDLE schService;
int _tmain(int argc, _TCHAR* argv[])
{
SvcInstall();
DoStartSvc();
return 0;
}
VOID SvcInstall()
{
SC_HANDLE schSCManager;
SC_HANDLE schService;
TCHAR szPath[MAX_PATH];
if( !GetModuleFileName( NULL, szPath, MAX_PATH ) )
{
printf("Cannot install service (%d)\n", GetLastError());
return;
}
// Get a handle to the SCM database.
schSCManager = OpenSCManager(
_T("\\\\kampimachine"), // remote computer
NULL, // ServicesActive database
SC_MANAGER_ALL_ACCESS); // full access rights
if (NULL == schSCManager)
{
printf("OpenSCManager failed (%d)\n", GetLastError());
return;
}
// Create the service
schService = CreateService(
schSCManager, // SCM database
_T("kampi"), // name of service
_T("kampi"), // 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
_T("%SystemRoot%\\system32\\notepad.exe"),// path to service's binary
NULL, // no load ordering group
NULL, // no tag identifier
NULL, // no dependencies
_T("domain\\user"), // LocalSystem account
_T("password")); // no password
if (schService == NULL)
{
printf("CreateService failed (%d)\n", GetLastError());
CloseServiceHandle(schSCManager);
return;
}
else printf("Service installed successfully\n");
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
}
VOID __stdcall DoStartSvc()
{
SERVICE_STATUS_PROCESS ssStatus;
DWORD dwOldCheckPoint;
DWORD dwStartTickCount;
DWORD dwWaitTime;
DWORD dwBytesNeeded;
// Get a handle to the SCM database.
schSCManager = OpenSCManager(
_T("\\\\kampimachine"), // remote computer
NULL, // servicesActive database
SC_MANAGER_ALL_ACCESS); // full access rights
if (NULL == schSCManager)
{
printf("OpenSCManager failed (%d)\n", GetLastError());
return;
}
// Get a handle to the service.
schService = OpenService(
schSCManager, // SCM database
_T("kampi"), // name of service
SERVICE_ALL_ACCESS); // full access
if (schService == NULL)
{
printf("OpenService failed (%d)\n", GetLastError());
CloseServiceHandle(schSCManager);
return;
}
// Check the status in case the service is not stopped.
if (!QueryServiceStatusEx(
schService, // handle to service
SC_STATUS_PROCESS_INFO, // information level
(LPBYTE) &ssStatus, // address of structure
sizeof(SERVICE_STATUS_PROCESS), // size of structure
&dwBytesNeeded ) ) // size needed if buffer is too small
{
printf("QueryServiceStatusEx failed (%d)\n", GetLastError());
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return;
}
// Check if the service is already running. It would be possible
// to stop the service here, but for simplicity this example just returns.
if(ssStatus.dwCurrentState != SERVICE_STOPPED && ssStatus.dwCurrentState != SERVICE_STOP_PENDING)
{
printf("Cannot start the service because it is already running\n");
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return;
}
// Save the tick count and initial checkpoint.
dwStartTickCount = GetTickCount();
dwOldCheckPoint = ssStatus.dwCheckPoint;
// Wait for the service to stop before attempting to start it.
while (ssStatus.dwCurrentState == SERVICE_STOP_PENDING)
{
// Do not wait longer than the wait hint. A good interval is
// one-tenth of the wait hint but not less than 1 second
// and not more than 10 seconds.
dwWaitTime = ssStatus.dwWaitHint / 10;
if( dwWaitTime < 1000 )
dwWaitTime = 1000;
else if ( dwWaitTime > 10000 )
dwWaitTime = 10000;
Sleep( dwWaitTime );
// Check the status until the service is no longer stop pending.
if (!QueryServiceStatusEx(
schService, // handle to service
SC_STATUS_PROCESS_INFO, // information level
(LPBYTE) &ssStatus, // address of structure
sizeof(SERVICE_STATUS_PROCESS), // size of structure
&dwBytesNeeded ) ) // size needed if buffer is too small
{
printf("QueryServiceStatusEx failed (%d)\n", GetLastError());
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return;
}
if ( ssStatus.dwCheckPoint > dwOldCheckPoint )
{
// Continue to wait and check.
dwStartTickCount = GetTickCount();
dwOldCheckPoint = ssStatus.dwCheckPoint;
}
else
{
if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint)
{
printf("Timeout waiting for service to stop\n");
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return;
}
}
}
// Attempt to start the service.
if (!StartService(
schService, // handle to service
0, // number of arguments
NULL) ) // no arguments
{
printf("StartService failed (%d)\n", GetLastError());
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return;
}
else printf("Service start pending...\n");
// Check the status until the service is no longer start pending.
if (!QueryServiceStatusEx(
schService, // handle to service
SC_STATUS_PROCESS_INFO, // info level
(LPBYTE) &ssStatus, // address of structure
sizeof(SERVICE_STATUS_PROCESS), // size of structure
&dwBytesNeeded ) ) // if buffer too small
{
printf("QueryServiceStatusEx failed (%d)\n", GetLastError());
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return;
}
// Save the tick count and initial checkpoint.
dwStartTickCount = GetTickCount();
dwOldCheckPoint = ssStatus.dwCheckPoint;
while (ssStatus.dwCurrentState == SERVICE_START_PENDING)
{
// Do not wait longer than the wait hint. A good interval is
// one-tenth the wait hint, but no less than 1 second and no
// more than 10 seconds.
dwWaitTime = ssStatus.dwWaitHint / 10;
if( dwWaitTime < 1000 )
dwWaitTime = 1000;
else if ( dwWaitTime > 10000 )
dwWaitTime = 10000;
Sleep( dwWaitTime );
// Check the status again.
if (!QueryServiceStatusEx(
schService, // handle to service
SC_STATUS_PROCESS_INFO, // info level
(LPBYTE) &ssStatus, // address of structure
sizeof(SERVICE_STATUS_PROCESS), // size of structure
&dwBytesNeeded ) ) // if buffer too small
{
printf("QueryServiceStatusEx failed (%d)\n", GetLastError());
break;
}
if ( ssStatus.dwCheckPoint > dwOldCheckPoint )
{
// Continue to wait and check.
dwStartTickCount = GetTickCount();
dwOldCheckPoint = ssStatus.dwCheckPoint;
}
else
{
if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint)
{
// No progress made within the wait hint.
break;
}
}
}
// Determine whether the service is running.
if (ssStatus.dwCurrentState == SERVICE_RUNNING)
{
printf("Service started successfully.\n");
}
else
{
printf("Service not started. \n");
printf(" Current State: %d\n", ssStatus.dwCurrentState);
printf(" Exit Code: %d\n", ssStatus.dwWin32ExitCode);
printf(" Check Point: %d\n", ssStatus.dwCheckPoint);
printf(" Wait Hint: %d\n", ssStatus.dwWaitHint);
}
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
}