i am trying to make a service create a process in opened session in windows. i have this code:
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);
if i use the last lin e(that commented) in the install of the service all works fine because it runs when the service is been installed so it happens inside the User session but when i want the service to do it it fails, sessionId is ok, the failure starts at :
if (WTSQueryUserToken(sessionId,&dummy)) {
i know that WTSQueryUserToken is a function that should be run from service, sessionid is 1 (and it is the real number from cmd check) and the dummy is suppose to hold the user token after it but for some reason it fails.... any ideas?