A preliminary study on virtual desktop technology

Source: Internet
Author: User

"Article title": A preliminary discussion on Virtual desktop technology
"article author": Newjueqi
"Author Email": [Email protected]
"Author QQ No.": 190678908
"Writing language": vc++6.0
"Operating Platform": XP-SP2
"Author statement": Feel that this is a very interesting technology, this is a study notes it! I am only interested, no other purpose, the fault of the place please give a hero to forgive!

This article was published in the study forum http://bbs.pediy.com/showthread.PHP?t=82537
Shell Mania http://www.unpack.cn/viewthread.php?tid=32988


What is a desktop?
Every system running Windows NT has a window workstation object, which is the first layer of the securable object, the source of inheritance for all user securable objects, each window workstation object can have some desktop objects, and each desktop has a chain of windows. The window chain contains various windows that appear on the owning desktop. Window NT uses two desktop window objects, one is used to handle the landing interface, shielding, lock workstations, and so on, one is our landing after the operation of the Windows.
Window NT manages this desktop object through the "explorer.exe" process. That's why we Kill "Explorer.exe" in the Task Manager and our desktop disappears.

What is a virtual desktop?
Virtual Desktop is a desktop based on the original computer to create a new desktop, on the new desktop can be carried out daily operations.

What is the purpose of virtual desktops?
(1) I think the most important use of this technology is to make any UI interface software into a background software (that is, not see any interface, including the launch interface)
(2) When you can work is a desktop, entertainment is a desktop (you can go to download the site to search for the use of such software, the function described above is very interesting)

How to implement virtual desktops
In Windows, to create a new desktop available to Api:createdesktop (), the function is declared as follows
Hdesk Createdesktop (
LPCTSTR Lpszdesktop,//Name of new desktop
LPCTSTR Lpszdevice,//null
Lpdevmode pDevMode,//null
DWORD dwFlags,//Specify how the application is compatible on the desktop
Access_mask dwdesiredaccess,//Specify Permissions for new desktops
Lpsecurity_attributes LPSA//Specifies whether the handle can be inherited
);

The return value is the handle to the newly created desktop.

So how do you run the program on this new desktop after you've created a new desktop? Don't worry, let's start with a review of the function CreateProcess () that creates the process, in the parameters of this function startupinfo has lpdesktop such a property, if this property is null in the current desktop creation thread, if the name of the desktop is specified, The process will start on the specified desktop, so you want to initialize some programs in the new desktop you created, just specify the Lpdesktop parameter as the name of the new desktop.

There is also a simple way to hang the new thread under the newly created desktop, using the API function SetThreadDesktop (), declared as follows:
BOOL SetThreadDesktop (
Hdesk Hdesktop//pointing to the specified desktop handle
);

But use this function to be aware of the point, according to MSDN: the SetThreadDesktop function will fail if the calling thread have any Windows or hooks on its curre NT Desktop (unless the hdesktop parameter is a handle-to-the-current desktop) means that unless the desktop handle you want to specify is the desktop, the call to this function will fail if the current thread has Any window (that is, the UI interface).

How to achieve different desktop switching between?
To switch between different desktops, use the API function Switchdesktop, declared as follows:
BOOL Switchdesktop (
Hdesk hdesktop//Desktop handle
);

Alternatively, you can switch the desktop by clicking the "Toggle" button.

However, a new problem has been derived, it is necessary to know the handle of each desktop, get the handle of the desktop can be getthreaddesktop through the API function, the function declaration is as follows:
Hdesk Getthreaddesktop (
DWORD dwThreadID//thread ID);

The return value is the desktop on which the specified thread resides.
And what we have to recognize is that the thread that created the new desktop is started on the old desktop, so you can easily get a handle to the current desktop with the following statement:
Getthreaddesktop (GetCurrentThreadID ());
The return value is the old desktop handle.

How do I close a newly created desktop?
This problem does not need us to worry about, Microsoft has been thinking for us ^-^, with the Closedesktop function can easily achieve this function, the function is declared as follows:
BOOL Closedesktop (
Hdesk Hdesktop//Specifies the handle of the desktop to be closed
);

Here's an example of a piece of code that runs the calculator on a newly created desktop (calc.exe) to implement the background of the calculator.

#include <windows.h>

HINSTANCE HInst; The current instance handle
HWND hwnd; Window handle
Hdesk Hvirtualdesk; Newly created virtual desktop handle
Process_information Pi; Calculator process Information

Message loops
LRESULT CALLBACK WinProc (
HWND hwnd,//Handle to Window
UINT umsg,//message identifier
WPARAM WPARAM,//First message parameter
LPARAM LPARAM//second message parameter
)
{
Switch (umsg)
{
Case WM_CLOSE:
TerminateProcess (pi.hprocess, 1);
Closedesktop (Hvirtualdesk);
DestroyWindow (HWND);
PostQuitMessage (0);
Break

Case Wm_destroy:

Closedesktop (Hvirtualdesk);
PostQuitMessage (0);
Break

Case Wm_hotkey:

if (0x0001 = = WParam)//For exiting the desktop hotkey Alt+q
{
SendMessage (hwnd,wm_close,0,0);
}
Break

Default
return DefWindowProc (Hwnd,umsg,wparam,lparam);
}
return 0;
}

Create a virtual desktop
void Cratevirtualdesk ()
{
Store the newly created virtual desktop handle in the Hvirtualdesk
Hvirtualdesk=createdesktop ("Newdesk",
Null
Null
Df_allowotheraccounthook,
Generic_all,
NULL);

}


Running an instance of a calculator on a virtual desktop
void Runcalc ()
{
Startupinfo si;

ZeroMemory (&si, sizeof (SI));
SI.CB = sizeof (SI);
Si.lpdesktop = "Newdesk";

ZeroMemory (&pi, sizeof (PI));

if (! CreateProcess (NULL,
"Calc",
Null
Null
FALSE,
0,
Null
Null
&si,
&AMP;PI))
{
MessageBox (NULL, "Run calculator failed", "Error", 0);
ExitProcess (1);
}

}

int WINAPI WinMain (
HINSTANCE hinstance,//handle to current instance
HINSTANCE hprevinstance,//handle to previous instance
LPSTR lpcmdline,//command line
int nCmdShow//show State
)
{
Wndclass wndcls;
MSG msg;

Hinst=hinstance;
ZeroMemory (&wndcls,sizeof (WNDCLS));
wndcls.cbclsextra=0;
wndcls.cbwndextra=0;
Wndcls.hbrbackground= (Hbrush) getstockobject (Black_brush);
Wndcls.hinstance=hinstance;
Wndcls.lpfnwndproc=winproc;
Wndcls.lpszclassname= "Hello";
Wndcls.lpszmenuname=null;
Wndcls.style=cs_hredraw | Cs_vredraw;
RegisterClass (&AMP;WNDCLS);

Hwnd=createwindow ("Hello", "Hello", Ws_overlappedwindow,
300,300,100,100,null,null,hinstance,null);

Register the required hotkey, alt+q to exit the virtual desktop created
if (! RegisterHotKey (Hwnd,0x0001,mod_alt, ' Q '))
{
Handling the exit process
return TRUE;
}

Create a virtual desktop
Cratevirtualdesk ();

Running an instance of a calculator on a virtual desktop
Runcalc ();

ShowWindow (Hwnd,sw_shownormal);
UpdateWindow (HWND);

while (GetMessage (&msg,null,0,0))
{
TranslateMessage (&AMP;MSG);
DispatchMessage (&AMP;MSG);
}

return msg.wparam;
}

After the program runs as


Red Box VirtualDesktop.exe is the code to create the program, Calc.exe is the process of the calculator, you can look at the taskbar, there is no calculator disappear, proved to be running on another desktop, from another point of view, is running in the background (as long as not switch to another desktop).

http://blog.csdn.net/newjueqi/article/details/4144544

A preliminary study on virtual desktop technology

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.