views:

108

answers:

3

I have this code:

#define _WIN32_WINNT 0x0500
#include <cstdlib>
#include <iostream>
#include <windows.h>

using namespace std;

int main(int argc, char *argv[])
{
    Sleep(500);
    HDESK hOriginalThread;
    HDESK hOriginalInput;
    hOriginalThread = GetThreadDesktop(GetCurrentThreadId());
    hOriginalInput = OpenInputDesktop(0, FALSE, DESKTOP_SWITCHDESKTOP);


    HDESK hNewDesktop=CreateDesktop("Test",NULL,NULL,0,DELETE|READ_CONTROL|WRITE_DAC|WRITE_OWNER|GENERIC_ALL,NULL);

    cout<<SetThreadDesktop(hNewDesktop);
    Sleep(575);
    SwitchDesktop(hNewDesktop);
    system("cmd");
    Sleep(1000);
    SwitchDesktop(hOriginalInput);
    SetThreadDesktop(hOriginalThread);
    CloseDesktop(hNewDesktop);
    CloseDesktop(hOriginalInput);
    Sleep(1000);
    return 0;
}

When I run this, it creates new desktop, switch to it, but command prompt not appeared. I must manualy terminate process "cmd" and my program then continue. Is there a way to show window of any application on other desktop? And how I can change background of desktop I created? Please help.

+1  A: 

Are you just trying to run "cmd" in non-blocking mode? I believe you can do that in Windows with:

system(1, "cmd");
BobbyShaftoe
+1  A: 

You can pick which desktop to start an application in when the application starts.

STARTUPINFO si = {0};
si.cb = sizeof(STARTUPINFO);
si.lpDesktop = L"winsta0\\Default";

Then pass this struct into CreateProcess or CreateProcessAsUser.


You can also pick which session to start the application in (Enable the session ID column in task manager to see which one you want)

You can create a process in another session by using: SetTokenInformation

on the token that you use in CreateProcessAsUser passing in a TokenSessionId

You can't change the session of an already running process.

Brian R. Bondy
+1  A: 

Don't use *Desktop functions, I promise they don't do what you think they do, they're a holdover from NT4 - you're really trying to create a new Session, and the OS owns creating sessions. Just don't do it.

-guy who works in Windows org

Paul Betts