2 * Creates a window station and starts a process under it. The new process
3 * also gets a new console.
14 SECURITY_ATTRIBUTES sa;
15 memset(&sa, 0, sizeof(sa));
16 sa.bInheritHandle = TRUE;
18 HWINSTA originalStation = GetProcessWindowStation();
19 printf("originalStation == 0x%x\n", originalStation);
20 HWINSTA station = CreateWindowStation(NULL,
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,
30 printf("desktop = 0x%x\n", desktop);
32 char stationName[256];
33 stationName[0] = '\0';
34 success = GetUserObjectInformation(station, UOI_NAME,
35 stationName, sizeof(stationName),
37 printf("stationName = [%s]\n", stationName);
39 char startupDesktop[256];
40 sprintf(startupDesktop, "%s\\Default", stationName);
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;
49 // Start a cmd subprocess, and have it start its own cmd subprocess.
50 // Both subprocesses will connect to the same non-interactive window
53 const char program[] = "c:\\windows\\system32\\cmd.exe";
55 sprintf(cmdline, "%s /c cmd", program);
56 success = CreateProcess(program,
60 /*bInheritHandles=*/FALSE,
61 /*dwCreationFlags=*/CREATE_NEW_CONSOLE,
66 printf("pid == %d\n", pi.dwProcessId);
68 // This sleep is necessary. We must give the child enough time to
69 // connect to the specified window station.
72 SetProcessWindowStation(originalStation);
73 CloseWindowStation(station);
74 CloseDesktop(desktop);