| 1 |
/* |
| 2 |
* Creates a window station and starts a process under it. The new process |
| 3 |
* also gets a new console. |
| 4 |
*/ |
| 5 |
|
| 6 |
#include <windows.h> |
| 7 |
#include <string.h> |
| 8 |
#include <stdio.h> |
| 9 |
|
| 10 |
int main() |
| 11 |
{ |
| 12 |
BOOL success; |
| 13 |
|
| 14 |
SECURITY_ATTRIBUTES sa; |
| 15 |
memset(&sa, 0, sizeof(sa)); |
| 16 |
sa.bInheritHandle = TRUE; |
| 17 |
|
| 18 |
HWINSTA originalStation = GetProcessWindowStation(); |
| 19 |
printf("originalStation == 0x%x\n", originalStation); |
| 20 |
HWINSTA station = CreateWindowStation(NULL, |
| 21 |
0, |
| 22 |
WINSTA_ALL_ACCESS, |
| 23 |
&sa); |
| 24 |
printf("station == 0x%x\n", station); |
| 25 |
if (!SetProcessWindowStation(station)) |
| 26 |
printf("SetWindowStation failed!\n"); |
| 27 |
HDESK desktop = CreateDesktop("Default", NULL, NULL, |
| 28 |
/*dwFlags=*/0, GENERIC_ALL, |
| 29 |
&sa); |
| 30 |
printf("desktop = 0x%x\n", desktop); |
| 31 |
|
| 32 |
char stationName[256]; |
| 33 |
stationName[0] = '\0'; |
| 34 |
success = GetUserObjectInformation(station, UOI_NAME, |
| 35 |
stationName, sizeof(stationName), |
| 36 |
NULL); |
| 37 |
printf("stationName = [%s]\n", stationName); |
| 38 |
|
| 39 |
char startupDesktop[256]; |
| 40 |
sprintf(startupDesktop, "%s\\Default", stationName); |
| 41 |
|
| 42 |
STARTUPINFO sui; |
| 43 |
PROCESS_INFORMATION pi; |
| 44 |
memset(&sui, 0, sizeof(sui)); |
| 45 |
memset(&pi, 0, sizeof(pi)); |
| 46 |
sui.cb = sizeof(STARTUPINFO); |
| 47 |
sui.lpDesktop = startupDesktop; |
| 48 |
|
| 49 |
// Start a cmd subprocess, and have it start its own cmd subprocess. |
| 50 |
// Both subprocesses will connect to the same non-interactive window |
| 51 |
// station. |
| 52 |
|
| 53 |
const char program[] = "c:\\windows\\system32\\cmd.exe"; |
| 54 |
char cmdline[256]; |
| 55 |
sprintf(cmdline, "%s /c cmd", program); |
| 56 |
success = CreateProcess(program, |
| 57 |
cmdline, |
| 58 |
NULL, |
| 59 |
NULL, |
| 60 |
/*bInheritHandles=*/FALSE, |
| 61 |
/*dwCreationFlags=*/CREATE_NEW_CONSOLE, |
| 62 |
NULL, NULL, |
| 63 |
&sui, |
| 64 |
&pi); |
| 65 |
|
| 66 |
printf("pid == %d\n", pi.dwProcessId); |
| 67 |
|
| 68 |
// This sleep is necessary. We must give the child enough time to |
| 69 |
// connect to the specified window station. |
| 70 |
Sleep(5000); |
| 71 |
|
| 72 |
SetProcessWindowStation(originalStation); |
| 73 |
CloseWindowStation(station); |
| 74 |
CloseDesktop(desktop); |
| 75 |
Sleep(5000); |
| 76 |
|
| 77 |
return 0; |
| 78 |
} |