8

I am writing some kind of IPC functionality and need to pass certain resources from one process to another. This works well for Pipe handles etc. which can be duplicated via DuplicateHandle. Now I need to pass a HDC from one process to the other. Is this even possible? If yes: how?

Sub-Question: I am assuming passing window handles (HWND) from one process to the other is safe. Is this assumption correct?

Heinrich Ulbricht
  • 10,064
  • 4
  • 54
  • 85

3 Answers3

8

HWNDs can be shared between processes, SendMessage() wouldn't work otherwise. They are however scoped to a specific desktop, a desktop is associated with a session. There is one session for each logged-in user. And session 0 is special, the session in which services run. And there's a secure desktop, the one you see at login or when you press Ctrl+Alt+Del, you cannot mess with the password entry box. But as long as both processes run in the same desktop you won't have any trouble.

HDCs are murky, never tried that. I wouldn't recommend it. You can always create one from a HWND with GetDC().

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • SendMessage is indeed a good example that it has to work between processes (and my processes will be running on the same desktop in the same session). Thanks. So my main concern are still the HDCs. You are right, I could pass the HWNDs, but that means I would have to restructure code I don't want to touch. I just wait a bit more, maybe somebody knows of a definite Yes or No... – Heinrich Ulbricht Mar 23 '10 at 14:51
  • why don't you just try it? If you don't have to restructure code then you'll find out quick enough. – Hans Passant Mar 23 '10 at 15:03
  • 1
    Mh you've got a point here. But even if it worked I'd feel better to build on documented behavior rather than on coincidence which might make it work on my machine but break on others (it also has to work on XP, Vista and 7). But trying definitely won't hurt. Then at least I could learn that it does not work :) – Heinrich Ulbricht Mar 23 '10 at 15:12
  • 1
    I had the same problem. Passed the HWND instead of HDC and created a HDC from HWND. And it works fine. Thanks for the tip @HansPassant – Aamir May 29 '13 at 17:01
7

All GDI handles are stored in a table that is mapped into every process. The entries in the table contain the process id of the owning process, and this is checked on every GDI access to the handle.

So, (ironically), GDI handles - including HDCs - are valid system wide. But can only be used from the process that created them.


This Page documents the process affinity of GDI objects. Of course, as a counter point it is worth noting that some COM functions, and window messages like WM_PRINT do not have any interprocess restrictions and they ARE passed HDC's, so they clearly have to do something behind the scenes to marshal the HDC from one process to the next.

Chris Becke
  • 34,244
  • 12
  • 79
  • 148
  • I think you and nobugz are both right with stressing that my problem can be viewed as sharing GDI resources in general. I found a related post here: http://stackoverflow.com/questions/133948/sharing-gdi-handles-between-processes-in-windows-ce-6-0 There somebody wants to share HFONTs. Maybe this is the same league as HDCs. But if there is this table mapped into every process then I wish there was a simple DuplicateGDIHandleFromThisTableForTheCurrentProcess function... – Heinrich Ulbricht Mar 23 '10 at 16:10
  • 2
    The fact that the table exists in all processes is a artifact of the current implementation of GDI, not a design feature. It can go away. And it lots of cases the GDI objects have pointers to usermode data structures so that, even though the handle is valid systemwide, the actual GDI object can only be successfully accesses in a single process. – Chris Becke Mar 23 '10 at 18:35
  • Ok so what will definitely not work is to use the same handle without further marshalling into the other process. This leaves the question whether there is a publicly available function for doing this... – Heinrich Ulbricht Mar 25 '10 at 08:20
  • +1 for pointing to the MSDN which indeed states very clearly: "Handles to GDI objects are private to a process. That is, only the process that created the GDI object can use the object handle." And it also lists what it understands by the term "GDI object". Thanks again! – Heinrich Ulbricht Mar 29 '10 at 11:05
3

Assuming, that you want to paint onto a HDC belonging to one process from another process (e.g. by using BitBlt) then as pointed out by nobugz and Chris Becke you cannot share that HDC across process boundaries. But, further assuming that the HDC of that one process belongs to a window (and your intention is to finally draw onto that window) then you can pass that window handle to the other process and in this process use GetDc to obtain a HDC. With this HDC you can then paint onto the window of the other process.

iamjoosy
  • 3,299
  • 20
  • 30