创建环境块崩溃服务

CreateEnvironmentBlock crashes service

本文关键字:服务 崩溃 环境 创建      更新时间:2023-10-16

我正在尝试从Windows服务启动GUI应用程序。但是当我调用 CreateEnvironmentBlock() 函数时,它会在那里挂起一段时间,然后崩溃显示对话框"SampleService.exe停止工作并被关闭。问题导致应用程序停止正常工作。如果有可用的解决方案,Windows 会通知您。以下是我的代码。

DWORD dwSessionId = 0;          // Session ID
HANDLE hToken = NULL;           // Active session token
HANDLE hDupToken = NULL;        // Duplicate session token
WCHAR szErr[1024] = {0};
STARTUPINFO* startupInfo;
PROCESS_INFORMATION processInformation;
PWTS_SESSION_INFO pSessionInfo = 0;
DWORD dwCount = 0;
LPVOID lpEnvironment = NULL;            // Environtment block
OutputDebugString(_T("My Sample Service: startApplication: Entry"));
// Get the list of all terminal sessions 
WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, 0, 1, &pSessionInfo, &dwCount);
int dataSize = sizeof(WTS_SESSION_INFO);
// look over obtained list in search of the active session
for (DWORD i = 0; i < dwCount; ++i)
{
   WTS_SESSION_INFO si = pSessionInfo[i];
   if (WTSActive == si.State)
   { 
      // If the current session is active – store its ID
      dwSessionId = si.SessionId;
      break;
   }
}   
OutputDebugString(_T("My Sample Service: startApplication: freewtsmemory"));
WTSFreeMemory(pSessionInfo);
OutputDebugString(_T("My Sample Service: startApplication: WTSQueryUserToken"));
// Get token of the logged in user by the active session ID 
BOOL bRet = WTSQueryUserToken(dwSessionId, &hToken);
if (!bRet)
{       
  swprintf(szErr, _T("WTSQueryUserToken Error: %d"), GetLastError());
  OutputDebugString(szErr);
  return false;
}
OutputDebugString(_T("My Sample Service: startApplication: duplicatetokenex"));
// Get duplicate token from the active logged in user's token
bRet = DuplicateTokenEx(hToken,     // Active session token
             TOKEN_ASSIGN_PRIMARY | TOKEN_ALL_ACCESS,           // Desired access
                     NULL,                      // Token attributes                                         
                     SecurityImpersonation,    // Impersonation level
                     TokenPrimary,              // Token type
                     &hDupToken);               // New/Duplicate token
if (!bRet)
{
    swprintf(szErr, _T("DuplicateTokenEx Error: %d"), GetLastError());
OutputDebugString(szErr);
    return false;
}
// Get all necessary environment variables of logged in user
// to pass them to the process
OutputDebugString(_T("My Sample Service: startApplication: createenvironmentblock"));
try{
 bRet = CreateEnvironmentBlock(&lpEnvironment, hDupToken, FALSE);
}
catch( const exception &e) 
{
swprintf(szErr, _T("CreateEnvironmentBlock Exception: %s"), e);
OutputDebugString(szErr);
    return false;
}
if(!bRet)
{
    swprintf(szErr, _T("CreateEnvironmentBlock Error: %d"), GetLastError());
OutputDebugString(szErr);
    return false;
}

// Initialize Startup and Process info  
startupInfo->cb = sizeof(STARTUPINFO);
OutputDebugString(_T("My Sample Service: startApplication: createprocess"));
// Start the process on behalf of the current user 
BOOL returnCode = CreateProcessAsUser(hDupToken, 
                            NULL, 
                            L"C:\KM\TEST.exe", 
                            NULL,
                            NULL,
                            FALSE,
                            NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE |      CREATE_UNICODE_ENVIRONMENT,
                            lpEnvironment,
                            NULL,
                            startupInfo,
                            &processInformation);
if( !returnCode)
{
    swprintf(szErr, _T("CreateProcessAsUser Error: %d"), GetLastError());
    OutputDebugString(szErr);
    return false;
}
CloseHandle(hDupToken);
return true;

它在调试视图中显示"我的示例服务:启动应用程序:创建环境块"并停止服务。 请帮助我解决这个问题。 请注意,我使用的是Windows Vista。

问候公里。

您需要先初始化指针,然后才能以定义的方式使用它们。

STARTUPINFO* startupInfo;
...
startupInfo->cb = sizeof(STARTUPINFO);

如果您的变量声明更接近它们的使用位置,则此错误可能更明显。如果您遵循某些规则,即变量只能在函数开始时声明,则可能需要考虑创建更多函数。

而且,值得一提的是,在解决此类问题时,您始终可以将Visual Studio的调试器附加到服务进程,而不是依赖OutputDebugString。只需确保服务进程是Visual Studio构建的最后一件事,并且进程,符号文件和源代码都应保持一致。