Windows Multi Desktop C++Development

Windows multi desktop development has not been widely applied. The author used it in an automatic setup task that requires opening hundreds of additional windows on a Windows server to facilitate the management of multiple windows for classification management

Key instructions

1. Each desktop interface has a name, and when the system loads, the default desktop name is “Default”
2. To create and use a certain desktop code, it is necessary to operate in a newly opened thread (otherwise it will still take effect on the default desktop)

Create and open desktop

<pre class="prism-highlight prism-language-cpp">// new desktop name
std::string name = "CustomDesktop2";
// First, try opening it.
auto handle = OpenDesktopA(name.c_str(), 0, true, GENERIC_ALL);
if(handle == 0)
{
  // open failed or create desktop
  handle = CreateDesktopA(name.c_str(), 0, 0, 0, GENERIC_ALL, 0);
}

Show Desktop

<pre class="prism-highlight prism-language-cpp"> SwitchDesktop(handle);

Use Desktop

<pre class="prism-highlight prism-language-cpp"> SetThreadDesktop(handle);

Create an appliaction proces on a new desktop

<pre class="prism-highlight prism-language-cpp">// Start a new thread to use a custom desktop
std::thread t([](){
    // Use the custom desktop
    SetThreadDesktop(handle);
    // Desktop name
    std::string name = "CustomDesktop2";

    // Run the program
    std::string filepath = "D:\\1.exe";
    STARTUPINFOA info = { sizeof(STARTUPINFOA) };
    info.dwFlags = STARTF_USESHOWWINDOW;
    info.wShowWindow = SW_SHOW;
    // Set the desktop name
    info.lpDesktop = (LPSTR)name.c_str();
    PROCESS_INFORMATION proc;
    if(!CreateProcessA(
        0, 
        (LPSTR)filepath.c_str(),
        0,
        0,
        FALSE,
        0,
        0,
        NULL,
        &info,
        &proc)
    ){
        return;
    }
    CloseHandle(proc.hProcess);
    CloseHandle(proc.hThread);

});
t.detach();

Title of this article:<Windows Multi Desktop C++Development>Author:minimini
Original link:https://www.xxmjw.com/post/10.html
Unless otherwise specified, all content is original. Please indicate when reprinting.

Related

minimini

minimini