Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>With the exception of the <code>schSCManager</code> and <code>szPath</code> variables, <em>all</em> of the other variables you are passing to <code>CreateService()</code> have not been initialized, they contain random values. That is especially important for the <code>psz...</code> variables, because they are pointers, so you are effectively passing random memory addresses to <code>CreateService()</code>. That is why you are getting an <code>ERROR_INVALID_ADDRESS</code> error.</p> <p><strong>You need to initialize your variables!</strong></p> <p><code>pszServiceName</code> needs to point at a null-terminated string containing the desired service name.</p> <p><code>pszDisplayName</code> needs to point at a null-terminated string containing the desired service display name.</p> <p><code>dwStartType</code> needs to contain a valid start type integer value.</p> <p><code>pszDependencies</code> 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.</p> <p><code>pszAccount</code> needs to either be NULL or point at a null-terminated string containing the desired user account that the service runs under.</p> <p><code>pszPassword</code> needs to either be NULL or point at a null-terminated string containing the password for the <code>pszAccount</code> account.</p> <p><strong>Edit:</strong> You are better off simply getting rid of the variables altogether and pass the desired values directly to <code>CreateService()</code>. Try this:</p> <pre><code>#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; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload