When calling a C function inside a DLL from my application, SetThreadDesktop fails. GetLastError() returns Code 170 which translates to:
ERROR_BUSY
170 (0xAA)
The requested resource is in use.
I'm using this example code inside my DLL: https://github.com/bmharper/WindowsDesktopDuplicationSample
Here ist the part which might be wrong (at least in my use case):
// Get desktop
HDESK hDesk = OpenInputDesktop(0, FALSE, GENERIC_ALL);
if (!hDesk)
return "Failed to open desktop";
// Attach desktop to this thread (presumably for cases where this is not the main/UI thread)
bool deskAttached = SetThreadDesktop(hDesk) != 0;
CloseDesktop(hDesk);
hDesk = nullptr;
if (!deskAttached)
return "Failed to attach recording thread to desktop";
I also tried running from another application where no problem occures and the screen capturing runs fine. The difference here is that no application Window is being used at all. It's a console application. If I understand correctly the problem is that I already have a window. But I don't know what the Desktop Duplication API expects here.