CreateEnvironmentBlock崩溃服务

时间:2013-09-07 23:53:46

标签: c++ windows winapi windows-vista

我正在尝试从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;

它在debugview中显示“我的示例服务:startApplication:createenvironmentblock”并停止了服务。请帮我解决这个问题。请注意我使用的是windows vista。

此致 KM。

1 个答案:

答案 0 :(得分:1)

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

STARTUPINFO* startupInfo;

...

startupInfo->cb = sizeof(STARTUPINFO);

如果您的变量被宣布更接近使用它们的位置,那么这个错误可能更明显。如果你遵循一些规则,变量只能在函数的开头声明,你可能需要考虑制作更多的函数。

而且,对于它的价值,在解决这些问题时,您始终可以将Visual Studio的调试器附加到服务流程,而不是依赖OutputDebugString。只需确保服务进程是Visual Studio构建的最后一件事,进程,符号文件和源代码都应该对齐。