views:

209

answers:

3

Hi,

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?

Thanks for your help!

+2  A: 

Tough question. HWNDs and HDCs are GDI handles, not kernel handles, I doubt DuplicateHandle() works on them. HWNDs can be shared between processes, SendMessage() wouldn't work otherwise. They are however scoped, at a minimum on sessions, possibly desktops. For example, on Vista and Win7 session 0 where the services run cannot see the window handles of the desktop session. Or the logon session, you can't mess with the password entry box. But as long as both processes run in the desktop you won't have any trouble. WM_COPYDATA is another IPC mechanism.

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

Hans Passant
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
why don't you just try it? If you don't have to restructure code then you'll find out quick enough.
Hans Passant
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
+2  A: 

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
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-0There 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
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
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
+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
A: 

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