DuplicateHandle(), use in first or second process?

前端 未结 4 663
挽巷
挽巷 2021-01-13 04:01

The Windows API DuplicateHandle() http://msdn.microsoft.com/en-us/library/ms724251(VS.85).aspx Requires the Object handle to be duplicated and a handle to both the original

相关标签:
4条回答
  • 2021-01-13 04:07
    1. Somehow, the second process needs to get its process ID to the first process. This can be obtained through GetCurrentProcessId()
    2. The first process now needs to use this ID to get a HANDLE to the second process: hProcess = OpenProcess(PROCESS_DUP_HANDLE, FALSE, dwProcessId);
    3. Now you can duplicate the handle in the first process, using the REAL process handle of the second process and the pseudo-handle of the first process: DuplicateHandle(GetCurrentProcess(), hEvent, hProcess, &hDupEvent, 0, FALSE, DUPLICATE_SAME_ACCESS);
    4. Remember to release the handle you created with OpenProcess eventually (I don't know what difference it would make, but you're supposed to...). Also, release BOTH handles to the event: the one given to the second process and the initial handle.
    0 讨论(0)
  • 2021-01-13 04:19

    Use a named pipe or mailslots for IPC, this should work reliably for your purpose. If you need to wait, use named wait handles.

    Otherwise, I'd choose to do DuplicateHandle in the second process in order to set the handle ownership correctly.

    0 讨论(0)
  • 2021-01-13 04:24

    Process Handle is different from process id. OpenProcess takes process id. Use something like...

    HANDLE hProcess = OpenProcess(PROCESS_DUP_HANDLE, FALSE, GetCurrentProcessId());

    0 讨论(0)
  • 2021-01-13 04:30

    If I understood correctly, you want to synchronize two unrelated processes through the same event. If so, you can use named events.

    Create one using CreateEvent API function, provide it a name, then from the second process use OpenEvent API function, specifying the name of the event.

    You have similar functions for other synchronization objects, such as mutexes (OpenMutex) or semaphores (OpenSemaphore).

    0 讨论(0)
提交回复
热议问题