0

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

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

问候,KM。

4

1 回答 1

1

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

STARTUPINFO* startupInfo;

...

startupInfo->cb = sizeof(STARTUPINFO);

如果您的变量声明得更接近它们的使用位置,那么这个错误可能会更明显。如果您遵循只能在函数开头声明变量的规则,您可能需要考虑创建更多函数。

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

于 2013-09-08T00:38:15.103 回答