Skip to content

Instantly share code, notes, and snippets.

@oklas
Created August 1, 2016 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oklas/f788e5503e317d68d7f308cc40edcbca to your computer and use it in GitHub Desktop.
Save oklas/f788e5503e317d68d7f308cc40edcbca to your computer and use it in GitHub Desktop.
The windows handle pass to and get from master process
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