StartService失败,错误代码为1053

时间:2009-10-28 21:06:54

标签: c++ windows remoting

我正在尝试在远程计算机上启动进程。我正在使用OpenSCManager(),CreateService(),QueryServiceStatusEx()和StartService()API-s。我能够成功安装我的进程作为serice,但是当我尝试启动它时,StartService返回错误代码1053(“服务没有及时响应启动或控制请求。”)。任何人都可以帮我解决这个问题吗?

提前致谢!

kampi

更新:到目前为止这是我的代码(几乎来自msdn,但我已编辑了一下)

#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);
}

3 个答案:

答案 0 :(得分:1)

您的服务很可能会立即关闭或崩溃。我建议您在服务中添加一些日志记录信息,以便了解它的原因。

答案 1 :(得分:1)

我认为您缺少一个关键组件:启动服务控制调度程序。如documentation中所述,您必须启动服务控制调度程序才能启用Windows的“服务控制管理器(SCM)”以向您的服务发送命令(例如SERVICE_CONTROL_STOP例如)

启动调度程序的步骤如下:

  1. 创建一个SERVICE_TABLE_ENTRYlook at MSDN library),其中包含您的服务名称及其服务主要功能(与您的普通主要方法不同!)
  2. 使用此服务表条目
  3. 启动服务控制调度程序

    背景是每个服务都是通过SCM启动的,它等待服务启动Service Control Dispatcher以便接收命令......

    我在您的代码中发现了另一个问题:您每次启动时都会尝试安装服务!您最好检查适当的命令行参数,以确定是要安装还是启动服务!

答案 2 :(得分:0)

实际上你试着做的是冒充远程电脑。如果电脑使用的是Windows XP,则需要管理员以外的权限才能创建服务。要调整权限,请参阅远程PC中的本地系统策略。顺便说一下,你可以从你的远程电脑中打开ADMIN $共享吗?如果没有,那就是问题所在。

进一步参考检查sysinternals中的psexec。

没有BSOD,没有Rootkit,没有乐趣 - Biswanath Chowdhury(Win32内核开发人员)