CreateService get error: ERROR_INVALID_ADDRESS (0x000001e7)

0

plz help me this problem:

I create a basic service in this code:

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

When I run it, I get error: CreateService failed w/err 0x000001e7 (I only know it is: ERROR_INVALID_ADDRESS) - but I don't known what is exactly mean, and how to fix.

Anyone plz help me.

c++
winapi
service
asked on Stack Overflow Nov 15, 2013 by cristiano

1 Answer

2

With the exception of the schSCManager and szPath variables, all of the other variables you are passing to CreateService() have not been initialized, they contain random values. That is especially important for the psz... variables, because they are pointers, so you are effectively passing random memory addresses to CreateService(). That is why you are getting an ERROR_INVALID_ADDRESS error.

You need to initialize your variables!

pszServiceName needs to point at a null-terminated string containing the desired service name.

pszDisplayName needs to point at a null-terminated string containing the desired service display name.

dwStartType needs to contain a valid start type integer value.

pszDependencies needs to either be NULL, or point at a double-null-terminated string containing a list of null-separated service names that your service depends on.

pszAccount needs to either be NULL or point at a null-terminated string containing the desired user account that the service runs under.

pszPassword needs to either be NULL or point at a null-terminated string containing the password for the pszAccount account.

Edit: You are better off simply getting rid of the variables altogether and pass the desired values directly to CreateService(). Try this:

#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;
    }
} 
answered on Stack Overflow Nov 15, 2013 by Remy Lebeau • edited Nov 15, 2013 by Remy Lebeau

User contributions licensed under CC BY-SA 3.0