Click here to Skip to main content
15,921,212 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating a multi desktop application that create and open many desktops in the windows user session,To do this i am using Win32 functions like CreateDesktop,SwitchDesktop, OpenDesktop..Etc. and I create an icon in the task bar, and the user can select one o f the desktops to switch on, everything work fine but...

I want to make a live little image preview of what is happening in every desktop, like Linux does ... so i think in make a screen capture, and showing the image in the control panel, so the user know the desktop that want to switch ...

What I have tried:

The problem is that i try many many things, send printscreen key in the desktops, GetDC, BitBlt, CreateCompatibleDC, and many others ... but always either have a black image or invalid handle error. Only when I'm in the virtual desktop, the image show something. but in the others desktops, always black screen.

So can anyone help me with a way to capture images from Virtual Desktop when we are in main desktop.
Posted
Updated 8-Jun-21 22:51pm

1 solution

You may need to switch the thread within the application to the desktop before you can actually take any screenshots or capture any graphics. You can do this using the SetThreadDesktop[^] function. Just make sure to call the function from a new thread as any handles associated with the active desktop can cause the function to fail.
 
Share this answer
 
Comments
Member 11584373 9-Jun-21 5:26am    
Can you please share me any sample code. I am creating desktop using below snippet and trying to capture screenshots from another tool(basically a long run tool) which run parallel in virtual desktop when we are in Main desktop

IntPtr hOldDesktop = GetThreadDesktop(GetCurrentThreadId());
IntPtr hNewDesktop = IntPtr.Zero;
hNewDesktop = CreateDesktop(
name,
IntPtr.Zero,
IntPtr.Zero,
0,
(uint)DESKTOP_ACCESS.GENERIC_ALL,
IntPtr.Zero);
SetThreadDesktop(hNewDesktop);
Chris Copeland 9-Jun-21 6:05am    
Presumably you're maintaining a list somewhere of all the desktop handles that you've created, a collection of IntPtr values. If you want to take screen captures from any of those desktops you'll need to create a new thread (which should not open any handles on the main desktop), switch the thread to the desktop using the SetThreadDesktop() method, then perform whatever screen capture functions you are using.

If you don't switch the thread desktop handle then it won't have access to the handles needed to take screen captures. And if you try and switch the thread desktop of the main thread (or UI thread) then it will likely fail, hence the new thread needed.
Member 11584373 14-Jun-21 10:15am    
Hi Chris, I have tried as per your suggestion, but still it is blank image only. Can you please suggest if anything I can think off

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900