First of all, if you are a novice, I would recommend you to avoid sharing files between processes and duplication of any handles: 1) these related techniques are not very easy for understanding; 2) they are unsafe; 3) this is not a recommended practice; a recommended practices should maintain main feature of the processes — strong isolation between them, and breaking through this isolation is a questionable practice. It can be used probably in some really rare and sophisticated technology, if any.
Now, I don't see what could be the use of duplication of the handle you have obtained, because — how would you use it? The problem is the third parameter of your call to
CreateFile
,
dwShareMode == 0
. How can you use the handle's duplicate if you are not sharing the file? You need to use
FILE_SHARE_READ
, or
FILE_SHARE_WRITE
or, say,
FILE_SHARE_READ | FILE_SHARE_WRITE
. Read, everything is explained:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858%28v=vs.85%29.aspx[
^].
And them you can share the handler, either in this or a foreign process. You need to pass the original handle and a handle to a different process or obtain a handle of a foreign process, use it to duplicate a handle and pass a handle duplicate back to a foreign process. Everything is explained:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724251%28v=vs.85%29.aspx[
^].
Now, how to communicate between processes is your problem, a separate one. This is called
Inter-Process Communication (IPC). You just need to learn about it; this is a separate big topic. You can use sockets, named pipes, shared memory and so on.
Before going into all of that, please re-read my first paragraph above and think is you really need it. Also, when asking questions, it's the best to start with explaining your purpose and your ultimate goals.
—SA