Discussion:
Providing a CreateFilemapping handle to another process ?
(too old to reply)
R.Wieser
2018-05-27 13:32:27 UTC
Permalink
Hello all,

As WM_COPYDATA seems only to be able to transfer *to* another process (and
not from / both ways) I thought it would be a good idea to see if I could
create a 'shared memory' solution.

Using CreateFileMapping/OpenFileMapping and CreateViewOfFile works alright
(for both "sending" as well as "receiving") as long as I use a "filename".

But when I tried to go the "unnamed memory object" way I got stuck after
transferring the CreateFileMapping handle to another process. No matter
what I try, I cannot get the DuplicateHandle to give me anything else than a
"6 - invalid handle" result.

Even just trying to duplicate the senders process handle returned the above
error. :-\

And yes, before sending it I first converted the "GetCurrentProcess" pseudo
handle (-1) to a real one (using
DuplicateHandle,GetCurrentProcess(),GetCurrentProcess(),GetCurrentProcess(),&hProcessRealHandle,0,1,DUPLICATE_SAME_ACCESS
) :-)

Help ?

Regards
Rudy Wieser
JJ
2018-05-29 02:03:20 UTC
Permalink
Post by R.Wieser
Hello all,
As WM_COPYDATA seems only to be able to transfer *to* another process (and
not from / both ways) I thought it would be a good idea to see if I could
create a 'shared memory' solution.
Using CreateFileMapping/OpenFileMapping and CreateViewOfFile works alright
(for both "sending" as well as "receiving") as long as I use a "filename".
But when I tried to go the "unnamed memory object" way I got stuck after
transferring the CreateFileMapping handle to another process. No matter
what I try, I cannot get the DuplicateHandle to give me anything else than a
"6 - invalid handle" result.
Even just trying to duplicate the senders process handle returned the above
error. :-\
And yes, before sending it I first converted the "GetCurrentProcess" pseudo
handle (-1) to a real one (using
DuplicateHandle,GetCurrentProcess(),GetCurrentProcess(),GetCurrentProcess(),&hProcessRealHandle,0,1,DUPLICATE_SAME_ACCESS
) :-)
Help ?
Regards
Rudy Wieser
Works fine when duplicating the process handle.
R.Wieser
2018-05-29 07:47:18 UTC
Permalink
JJ,
Post by JJ
Works fine when duplicating the process handle.
Which exactly is the whole problem. How do you do that ? With
DuplicateHandle ? If so, how ? I cannot get it to work that way.

Mind you, there are *two* 'DuplicateHandle's involved:

One at the senders side to convert the 'GetCurrentProcess' pseudo handle
into a real one - which than gets, together with the 'CreateFileMapping'
handle, send to the other side.

And the other side which uses both of the above in another 'DuplicateHandle'
to generate a memory-mapping handle thats valid in its own address space.
(this is where the "invalid handle" error is returned).

... At least, that is what I think should be done, but for which I have not
been able to find any example (MSDN or otherwise).

And to be honest, I currently even start to doubt if the conversion from a
pseudo process handle to a real one returns one thats valid in another
process ...


By the way, I rewrote (as a test!) the whole thing to send the process ID,
and have the other side use an 'OpenProcess' with the PROCESS_DUP_HANDLE
flag, and that worked (the resulting process handle together with the
filemapping handle generated a handle that I could use with MapViewOfFile).

In other words, a

DuplicateHandle,hSrcProcess,hSrcMap,hTrgProcess,&hTrgMap,0,0,DUPLICATE_SAME_ACCESS

doesn't work, but a

OpenProcess,PROCESS_DUP_HANDLE,0, lSrcProcessID

does ...

I imagine that it has something to do with the DUPLICATE_SAME_ACCESS option
I'm using, but have not been able to find any kind of info telling me
anything else about it - and even less about (the usage of) the
dwDesiredAccess field. And without such info I'm up the creek without a
paddle. :-(

Regards,
Rudy Wieser
JJ
2018-05-29 14:29:12 UTC
Permalink
Post by R.Wieser
JJ,
Post by JJ
Works fine when duplicating the process handle.
Which exactly is the whole problem. How do you do that ? With
DuplicateHandle ? If so, how ? I cannot get it to work that way.
For the source process:

DuplicateHandle(GetCurrentProcess(), GetCurrentProcess(),
GetCurrentProcess(), &hCurrentProcess, PROCESS_DUP_HANDLE, TRUE, 0);

For the target process:

DuplicateHandle(hCurrentProcess, hDestProcess, hDestProcess,
&hDestProcess2, PROCESS_DUP_HANDLE, TRUE, 0);

Or...

OpenProcess(PROCESS_DUP_HANDLE, true, dwDestPID);
Post by R.Wieser
One at the senders side to convert the 'GetCurrentProcess' pseudo handle
into a real one - which than gets, together with the 'CreateFileMapping'
handle, send to the other side.
And the other side which uses both of the above in another 'DuplicateHandle'
to generate a memory-mapping handle thats valid in its own address space.
(this is where the "invalid handle" error is returned).
.... At least, that is what I think should be done, but for which I have not
been able to find any example (MSDN or otherwise).
And to be honest, I currently even start to doubt if the conversion from a
pseudo process handle to a real one returns one thats valid in another
process ...
By the way, I rewrote (as a test!) the whole thing to send the process ID,
and have the other side use an 'OpenProcess' with the PROCESS_DUP_HANDLE
flag, and that worked (the resulting process handle together with the
filemapping handle generated a handle that I could use with MapViewOfFile).
In other words, a
DuplicateHandle,hSrcProcess,hSrcMap,hTrgProcess,&hTrgMap,0,0,DUPLICATE_SAME_ACCESS
doesn't work, but a
OpenProcess,PROCESS_DUP_HANDLE,0, lSrcProcessID
does ...
I imagine that it has something to do with the DUPLICATE_SAME_ACCESS option
I'm using, but have not been able to find any kind of info telling me
anything else about it - and even less about (the usage of) the
dwDesiredAccess field. And without such info I'm up the creek without a
paddle. :-(
Duplicating the file mapping handle works fine too. i.e.

DuplicateHandle(hCurrentProcess, hFileMapping, hDestProcess,
&hFileMappingDestProcess, 0, TRUE, DUPLICATE_SAME_ACCESS);

Please keep in mind that duplicated handles for a remote process will only
be valid for that remote process. i.e. those handle values are valid only on
that remote process' context. So, don't use those handles from the process
that duplicate the handles.

Once the destination process' file mapping handle is created, you'll have to
let the destination process know the value of that handle. In the
destination process, use the value as a handle. i.e. as is. Typecast it if
necessary. Then pass it to MapViewOfFile().

During my test, ironically, I had to use window message to pass the handle
value to the destination process. All those IPC setup... For a mere 4 bytes
of data transfer. Now I'm feeling your frustration. :(
JJ
2018-05-29 15:36:10 UTC
Permalink
Post by JJ
DuplicateHandle(hCurrentProcess, hDestProcess, hDestProcess,
&hDestProcess2, PROCESS_DUP_HANDLE, TRUE, 0);
Sorry. This should have been:

DuplicateHandle(hCurrentProcess, hDestProcess, hCurrentProcess,
&hDestProcess2, PROCESS_DUP_HANDLE, TRUE, 0);
R.Wieser
2018-05-29 18:41:56 UTC
Permalink
JJ,
For the source process: [snip]
Problem: The second DuplicateHandle (I'm using the one in your "oops, it
should have been this" message) still fails. But now with an "5 - access
denied" message. IOW, we've progressed ... but not yet to my target.

And something I do not quite understand: The first DuplicateHandle generates
something that, according to its description, should only be valid in that
(source) process. But for some reason I do not understand it is than valid
enough to be send to and used with a second DuplicateHandle in a fully other
(target) process, and not even as a "this is the process where the
to-be-converted handle comes from". What gives ?
OpenProcess(PROCESS_DUP_HANDLE, true, dwDestPID);
Yep, I got it to work with that one. But I still would want to know how
its done using that DuplicateHandle function (even if its more work - two
DuplicateHandles vs a single OpenProcess)
Duplicating the file mapping handle works fine too. i.e.
DuplicateHandle(hCurrentProcess, hFileMapping, hDestProcess,
&hFileMappingDestProcess, 0, TRUE, DUPLICATE_SAME_ACCESS);
Yes, with the OpenProcess result.
So, don't use those handles from the process that duplicate the handles.
Lolz. See my above "And something I do not quite understand"
Typecast it if necessary.
Don't worry about that: I'm using Assembly, which regards all values as
being equal. :-)
During my test, ironically, I had to use window message to pass
the handle value to the destination process. All those IPC setup...
For a mere 4 bytes of data transfer
??? I'm not sure what you mean there. SendMesssage is pretty-much the
easiest way to get the two handles (source process- and the MapViewOfFile
handle) to the target process.

Its the crap around it (allowing both sides access to some shared memory)
that takes so much work to create. :-(

But when its done I will be able to transfer much more than just 4 bytes. In
both directions even!
Now I'm feeling your frustration. :(
:-)

Regards,
Rudy Wieser

Loading...