DuplicateHandle of Windows API

Source: Internet
Author: User

One way to share kernel object handles between processes:DuplicateHandle
simply put, the function obtains a table entry in a process handle table and copies it to the handle table of another process.     

  BOOL WINAPI DuplicateHandle (__in HANDLE hsou Rceprocesshandle , __in HANDLE hsourcehandle , __in HANDL E htargetprocesshandle , __out lphandle lptarge Thandle , __in DWORD dwdesiredaccess , __in BOOL binherithandle , __in DWORD dwoptions ); 

Hsourceprocesshandle: the source process kernel handle (that is, the process handle that is responsible for passing the kernel object handle)   
Hsourcehandle: Kernel object handle to pass

Lptargethandle: Receive address of kernel object handle (first declare a handle
Dwdesiredaccess:targethandle handle uses what access mask (this mask is an entry in the handle table)
Span style= "font-size:18px" > bInheritHandle: Whether you have an inherited
dwoptions: When set duplicate_same_access , the source is the same as all flags of the kernel object, at this time wdesiredaccess can be marked as 0 when set duplicate_close_source , when the transfer is complete, close the kernel object handle in the source

  Note : Do not attempt to use CloseHandle () to close targethandle in the source process, because this TargetHandle handle value is not part of the source process's handle table, and if the error is closed, it will produce unpredictable results!
 

I do not know why I use the copy handle function, I use interprocess communication to pass the handle to the target process is not OK?
This is a big mistake, and we are ostensibly copying the handle value, actually copying all the items in the source process handle table to the handle table of the target process, and making the counter of the kernel object + 1, if it is simply a handle value, it will not be incremented in the handle table of the target process.

The following example shows Create a thread in ApiDuplicateHandleSource.exe, copy the thread handle to ApiDuplicateHandle.exe, and end the thread in the process ApiDuplicateHandle.exe.

//ApiDuplicateHandleSource.cpp
#include <iostream>#include<Windows.h>#include<process.h>#include<tchar.h>#include<TlHelp32.h>using namespacestd;unsigned __stdcall Thread (void*Lppragma); HANDLE Getprocesshandle (LPCTSTR szName);intMain (void) {HANDLE hthread=NULL; Hthread= (HANDLE) _beginthreadex (NULL,0, Thread, NULL,0, NULL); cout<<"My thread handle is"<< Hthread <<Endl; HANDLE Htarget=NULL; BOOL BSUCC=FALSE; //are you trying to say that the hthread here are related to calling DuplicateHandle? BSUCC = DuplicateHandle (GetCurrentProcess (),//the source process handle for the current processHthread,//the thread resource handle (kernel object) that exists in the current processGetprocesshandle (_t ("ApiDuplicateHandle.exe")),//Handle to the target process&htarget,//the destination handle to get (out) 0,//How to accessFALSE,//The resulting handle can not be obtained by inheriting the child process of its processduplicate_same_access);//Access Options if(BSUCC) {cout<<"The handle is copied successfully with the handle value:"<< Htarget <<Endl; } cin.Get(); return 0;} unsigned __stdcall thread (void*Lppragma) { while(1) {Sleep ( +); cout<<"Please terminal me!"<<Endl; } return 0;} HANDLE Getprocesshandle (LPCTSTR szName) {HANDLE hsanpshot=NULL; Hsanpshot= CreateToolhelp32Snapshot (th32cs_snapprocess,0); if(Invalid_handle_value = =hsanpshot) { returnNULL; } PROCESSENTRY32 PE; BOOL bOk=FALSE; Pe.dwsize=sizeof(PE); BOk= Process32First (Hsanpshot, &PE); if(!bOk)returnNULL; Do { if( !_tcscmp (Pe.szexefile, SzName)) { returnopenprocess (process_all_access, FALSE, Pe.th32processid); } bOk= Process32Next (Hsanpshot, &PE); } while(BOK); returnNULL;}
//ApiDuplicateHandle.cpp
#include <iostream>#include<Windows.h>#include<stdlib.h>#include<process.h>using namespacestd;intMain (void) {HANDLE hrecv=NULL; cout<<"Enter the handle you copied:"<<Endl; CIN>>Hrecv; TerminateThread (HRECV,0); Cin.Get(); return 0;}

Reference :

Http://www.cnblogs.com/staring-hxs/p/3576927.html

Http://blog.sina.com.cn/s/blog_4cfc933d0100rsrv.html

DuplicateHandle of Windows API

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.