tags:

views:

68

answers:

2

Is it possible to pass a raw NT handle (eg, to an event object) via a DCOM call - on the local machine, of course. If so, how would one go about doing so?

A: 

IntPtr (which I think is spelled INT_PTR in COM).

Windows programmer
If I'm passing to a remote process, just passing the raw integer value won't help - the handle needs to be duplicated onto the other side. That would be like casting a BSTR to an INT_PTR and hoping for the best - not going to work in DCOM :)
bdonlan
You said it's a HANDLE. Although the value of a HANDLE fits into an INT_PTR, it's not really a pointer value, just like it isn't an integer value. It is not like copying the pointer value of a BSTR or any actual pointer value.
Windows programmer
A 'NT HANDLE' is a more specific kind of handle :)
bdonlan
Yes, and the kernel knows the value of each handle. It is NOT like casting a BSTR whose pointer value is a virtual address.
Windows programmer
MSDN says HANDLE_PTR. MSDN says a 32-bit process can communicate with a 64-bit process but a 32-bit HANDLE_PTR is 32 bits on the wire. How does a 32-bit process access a 64-bit HANDLE_PTR?
Windows programmer
-1 @Windows programmer: Yes, the kernel does know the value of each handle, but they are, in general, process specific. There are few system-wide handles, and the type OP specifies is not one of them. So he could pass the handle over, but it'd be useless in the other processes' context. A to call `DuplicateHandle()` or similar on it in the target process is a step in the right direction.
mrduclaw
+1  A: 

See this SO answer : http://stackoverflow.com/questions/819710/duplicatehandle-use-in-first-or-second-process/819735#819735 and the comment and thus I suggest that DuplicateHandle is what you need.

Preet Sangha
But how does the server know what process is the client?
Larry Osterman
I suspect you'll have to pass that information out of band
Preet Sangha
I have 2 downvotes so far for passing information out of band.
Windows programmer
Actually at least one of the downvotes are because your solution doesn't actually work - all it does is to pass the handle value to the client. And it might not work when the COM server is 64bits and the client is 32bits since the stubs won't match (INT_PTR is 64bits wide in 64bit code and 32bits wide in 32bit code).
Larry Osterman
Good point. What does happen if a 64-bit kernel sets a HANDLE value that doesn't fit in 32 bits and then the client turns out to be 32 bits?
Windows programmer
Hmm. DuplicateHandle seems kind of like an ugly hack that I'd like to avoid, but I guess it would work in my case, as the objects are running in the same security context. As for 64-bit kernels and HANDLES, AIUI the 32-bit emulation layer does some undocumented magic to ensure all handles are allocated below the 32-bit limit.
bdonlan
If a 32-bit process needs to get a 32-bit duplicate of a 64-bit HANDLE, will it work?
Windows programmer
@Windows programmer, the 32-bit process cannot invoke DuplicateHandle, but the 64-bit program can invoke it to actively pass it to the 32-bit process, I think. Anyway, these kinds of questions are good for... another question, not comments on this answer :)
bdonlan
Well we've got Larry Osterman helping us here :-)
Windows programmer