Search code examples
winapigdi

CreateDC(), CreateCompatibleDC() or similar for a memory DC without a desktop


I'd like to create a Memory Device Context that I can use to invoke GDI functions, paint windows (using WM_PRINTCLIENT) etc. from a program that doesn't have access to the desktop (e.g. from a windows service without desktop permissions or a locked screen).

Normally, I'd use CreateCompatibleDC(), but it requires a source hDC to copy from, so this function is not available if the calling program doesn't have a desktop. Also CreateDC("DISPLAY",...) will fail here for the same reason.

What I need is a software Device Context capable of running any normal DC calls, but tied to a specific BITMAP that I can use to get the results from.

Is there a way to solve this? Perhaps borrowing from a printer driver? (I don't need high performance).

I need this to work on Windows Server 2019, and I can use C#, C++, etc.


Solution

  • CreateCompatibleDC(nullptr) will work in this instance.