允许服务与桌面交互 - Windows服务与OPC服务器通信

时间:2013-10-11 10:13:16

标签: c++ windows windows-services opc

我有一个与OPC服务器通信的应用程序。 当我将其作为Windows服务运行时,它无法打开与OPC服务器的连接。 当我激活Option'允许服务与桌面交互时#39;有用! 但是如何将其作为我的服务应用程序的默认设置。 我尝试使用SERVICE_INTERACTIVE_PROCESS标志来创建' CreateService' API函数但失败了0x0057(无效参数)。

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

对于pszAccount和pszPassword为NULL,以使用本地系统帐户。

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
    );

1 个答案:

答案 0 :(得分:0)

如果未正确设置服务类型参数,

CreateService将报告该错误:

  

如果指定SERVICE_WIN32_OWN_PROCESS或   SERVICE_WIN32_SHARE_PROCESS,服务正在上下文中运行   在LocalSystem帐户中,您还可以指定以下值:   SERVICE_INTERACTIVE_PROCESS

相关问题