【发布时间】:2014-11-13 16:04:33
【问题描述】:
我正在尝试让服务在 Windows 中打开的会话中创建一个进程。 我有这个代码:
sessionId =WTSGetActiveConsoleSessionId();
if (WTSQueryUserToken(sessionId,&dummy)) {
if (!DuplicateTokenEx(dummy, TOKEN_ALL_ACCESS, NULL, SecurityDelegation, TokenPrimary, &token)) {
CloseHandle(dummy);
return false;
}
CloseHandle(dummy);
// Create process for user with desktop
myfile = fopen("c:\\temp\\test123.txt", "a");
fprintf(myfile, "before create!!!!\n");
fclose(myfile);
if (!CreateProcessAsUser(token, NULL,NULL, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi)) { // The "new console" is necessary. Otherwise the process can hang our main process
CloseHandle(token);
myfile = fopen("c:\\temp\\test123.txt", "a");
fprintf(myfile, " create failed!\n");
fclose(myfile);
return false;
}
CloseHandle(token);
}
else {
myfile = fopen("c:\\temp\\test123.txt", "a");
fprintf(myfile, "Dummy fail\n");
fprintf(myfile, "last error is %d \n", GetLastError());
fclose(myfile);
}
//int ret = CreateProcess(FILE_EXEC, NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
如果我在安装服务时使用最后一行(注释的)一切正常,因为它在安装服务时运行,所以它发生在用户会话中但是当我希望服务执行它时它会失败, sessionId 没问题,失败开始于:
if (WTSQueryUserToken(sessionId,&dummy)) {
我知道 WTSQueryUserToken 是一个应该从服务运行的函数,sessionid 是 1(它是 cmd 检查的实数),并且假人假设在它之后持有用户令牌,但由于某种原因它失败了。 ...有什么想法吗?
【问题讨论】:
-
错误号是?请注意,您对
GetLastError的调用将返回废话,因为您在函数失败后很久才调用它。