0

请帮我解决这个问题:

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

#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 failed w/err 0x000001e7(我只知道它是:ERROR_INVALID_ADDRESS) - 但我不知道究竟是什么意思,以及如何修复。

任何人请帮助我。

4

1 回答 1

2

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

你需要初始化你的变量!

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

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

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

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

pszAccount需要为 NULL 或指向包含运行服务的所需用户帐户的以 null 结尾的字符串。

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

编辑:您最好完全摆脱变量并将所需的值直接传递给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;
    }
} 
于 2013-11-15T03:10:53.167 回答