Frage

I have an application talking to an OPC server. When I run it as a Windows Service it cannot open the connection to the OPC server. When I am activating the Option 'Allow service to interact with desktop' it works! But how can I make this as a default for my service application. I tried to use the SERVICE_INTERACTIVE_PROCESS flag for the 'CreateService' API function but it failed with 0x0057 (Invalid Parameter).

enter image description here

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

For pszAccount and pszPassword are NULL to use local system account.

schService = CreateService(
    schSCManager,                   // SCManager database
    pszServiceName,                 // Name of service
    pszDisplayName,                 // Name to display
    SERVICE_QUERY_STATUS,           // Desired access
    SERVICE_WIN32_OWN_PROCESS|SERVICE_INTERACTIVE_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
    );
War es hilfreich?

Lösung

CreateService will report that error if the service type parameter is not set correctly:

If you specify either SERVICE_WIN32_OWN_PROCESS or SERVICE_WIN32_SHARE_PROCESS, and the service is running in the context of the LocalSystem account, you can also specify the following value: SERVICE_INTERACTIVE_PROCESS

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top