Created
August 1, 2016 19:57
-
-
Save oklas/f788e5503e317d68d7f308cc40edcbca to your computer and use it in GitHub Desktop.
The windows handle pass to and get from master process
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern DWORD g_masterProcessId; | |
HANDLE DuplicateHandleToMaster( HANDLE handle ) | |
{ | |
HANDLE master_handle = NULL; | |
HANDLE master_process = OpenProcess( | |
PROCESS_DUP_HANDLE, FALSE, g_masterProcessId | |
); | |
if( ! master_process ) return NULL; | |
BOOL hr = DuplicateHandle( | |
GetCurrentProcess(), handle, | |
master_process, &master_handle, | |
0, FALSE, DUPLICATE_SAME_ACCESS | |
); | |
if( ! hr ) { | |
CloseHandle( master_process ); | |
return NULL; | |
} | |
return master_handle; | |
} | |
HANDLE DuplicateHandleFromMaster( HANDLE handle ) | |
{ | |
HANDLE local_handle = NULL; | |
HANDLE master_process = OpenProcess( | |
PROCESS_DUP_HANDLE, FALSE, g_masterProcessId | |
); | |
if( ! master_process ) return NULL; | |
BOOL hr = DuplicateHandle( | |
master_process, handle, | |
GetCurrentProcess(), &local_handle, | |
0, FALSE, DUPLICATE_SAME_ACCESS | |
); | |
if( ! hr ) { | |
CloseHandle( master_process ); | |
return NULL; | |
} | |
return local_handle; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment