CreateService get error:ERROR_INVALID_ADDRESS(0x000001e7)

时间:2013-11-15 02:40:59

标签: c++ winapi service

PLZ帮我解决这个问题:

我在此代码中创建了一个基本服务:

#include "stdafx.h"

PWSTR pszServiceName;
PWSTR pszDisplayName; 
DWORD dwStartType;
PWSTR pszDependencies; 
PWSTR pszAccount;
PWSTR pszPassword;

#define MAX_PATH 100

void __cdecl _tmain(int argc, TCHAR *argv[]) 
{ 
    wchar_t szPath[MAX_PATH];
    SC_HANDLE schSCManager = NULL;
    SC_HANDLE schService = NULL;

    if (GetModuleFileName(NULL, szPath, ARRAYSIZE(szPath)) == 0)
    {
        wprintf(L"GetModuleFileName failed w/err 0x%08lx\n", GetLastError());
        goto Cleanup;
    }

    // Open the local default service control manager database
    schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT | 
        SC_MANAGER_CREATE_SERVICE);
    if (schSCManager == NULL)
    {
        wprintf(L"OpenSCManager failed w/err 0x%08lx\n", GetLastError());
        goto Cleanup;
    }

    // Install the service into SCM by calling CreateService
    schService = CreateService(
        schSCManager,                   // SCManager database
        pszServiceName,                 // Name of service
        pszDisplayName,                 // Name to display
        SERVICE_QUERY_STATUS,           // Desired access
        SERVICE_WIN32_OWN_PROCESS,      // Service type
        dwStartType,                    // Service start type
        SERVICE_ERROR_NORMAL,           // Error control type
        szPath,                         // Service's binary
        NULL,                           // No load ordering group
        NULL,                           // No tag identifier
        pszDependencies,                // Dependencies
        pszAccount,                     // Service running account
        pszPassword                     // Password of the account
        );
    if (schService == NULL)
    {
        wprintf(L"CreateService failed w/err 0x%08lx\n", GetLastError());
        goto Cleanup;
    }

    wprintf(L"%s is installed.\n", pszServiceName);

Cleanup:
    // Centralized cleanup for all allocated resources.
    if (schSCManager)
    {
        CloseServiceHandle(schSCManager);
        schSCManager = NULL;
    }
    if (schService)
    {
        CloseServiceHandle(schService);
        schService = NULL;
    }
} 

当我运行它时,我收到错误:CreateService失败w / err 0x000001e7(我只知道它是:ERROR_INVALID_ADDRESS) - 但我不知道究竟是什么意思,以及如何修复。

任何人都帮助我。

1 个答案:

答案 0 :(得分:2)

除了schSCManagerszPath变量之外,您传递给CreateService()的其他变量的所有尚未初始化,它们包含随机数值。这对于psz...变量尤其重要,因为它们是指针,因此您实际上将随机内存地址传递给CreateService()。这就是您收到ERROR_INVALID_ADDRESS错误的原因。

您需要初始化变量!

pszServiceName需要指向包含所需服务名称的以null结尾的字符串。

pszDisplayName需要指向包含所需服务显示名称的以null结尾的字符串。

dwStartType需要包含有效的起始类型整数值。

pszDependencies需要为NULL,或者指向一个双重结尾的字符串,其中包含服务所依赖的空值分隔的服务名称列表。

pszAccount需要为NULL或指向以空字符结尾的字符串,该字符串包含服务所在的所需用户帐户。

pszPassword需要为NULL或指向包含pszAccount帐户密码的以空字符结尾的字符串。

编辑:您最好完全摆脱变量并将所需的值直接传递给CreateService()。试试这个:

#include "stdafx.h"

void __cdecl _tmain(int argc, TCHAR *argv[]) 
{ 
    wchar_t szPath[MAX_PATH+1];
    SC_HANDLE schSCManager = NULL;
    SC_HANDLE schService = NULL;

    if (GetModuleFileName(NULL, szPath, ARRAYSIZE(szPath)) == 0)
    {
        wprintf(L"GetModuleFileName failed w/err 0x%08lx\n", GetLastError());
        goto Cleanup;
    }

    // Open the local default service control manager database
    schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT | SC_MANAGER_CREATE_SERVICE);
    if (schSCManager == NULL)
    {
        wprintf(L"OpenSCManager failed w/err 0x%08lx\n", GetLastError());
        goto Cleanup;
    }

    // Install the service into SCM by calling CreateService
    schService = CreateService(
        schSCManager,                   // SCManager database
        L"Win32_Service,                // Name of service
        L"My Service,                   // Name to display
        SERVICE_QUERY_STATUS,           // Desired access
        SERVICE_WIN32_OWN_PROCESS,      // Service type
        SERVICE_DEMAND_START,           // Service start type
        SERVICE_ERROR_NORMAL,           // Error control type
        szPath,                         // Service's binary
        NULL,                           // No load ordering group
        NULL,                           // No tag identifier
        NULL,                           // No Dependencies
        L"NT AUTHORITY\\LocalService",  // Service running account
        NULL                            // No Password of the account
        );
    if (schService == NULL)
    {
        wprintf(L"CreateService failed w/err 0x%08lx\n", GetLastError());
        goto Cleanup;
    }

    wprintf(L"Service is installed.\n");

Cleanup:
    // Centralized cleanup for all allocated resources.
    if (schService)
    {
        CloseServiceHandle(schService);
        schService = NULL;
    }
    if (schSCManager)
    {
        CloseServiceHandle(schSCManager);
        schSCManager = NULL;
    }
}