Conversion of pseudo-handle and handle

Source: Internet
Author: User

Refer:

1. http://www.cnblogs.com/Kernone/archive/2009/08/18/1549286.html

2. http://blog.sina.com.cn/s/blog_4b226b9201011bu6.html

When using many functions, we need to obtain an object handle. Some functions, such as getcurrentprocess ()/getcurrentthread (), return a pseudo handle, namely, pseudo handle.

The so-called pseudo handle, that is, the handle pointing to the current thread or process, is not a real handle. It does not create a handle, nor increase the reference count, because closehandle () is called for no processing.

It only points to the main process or thread that calls it. This will change because of different callers. For example, caller A uses a pseudo handle, which points to caller A, and caller a passes the handle to caller X, the handle points to caller X.

The pseudo handle of the process is always 0 xffffffff, while the pseudo handle of the thread is always 0 xfffffffe.

By using the powerful duplicatehandle function, you can convert a pseudo handle to a real handle.

Function prototype:

BOOL DuplicateHandle(  HANDLE hSourceProcessHandle,  // handle to source process  HANDLE hSourceHandle,         // handle to duplicate  HANDLE hTargetProcessHandle,  // handle to target process  LPHANDLE lpTargetHandle,      // duplicate handle  DWORD dwDesiredAccess,        // requested access  BOOL bInheritHandle,          // handle inheritance option  DWORD dwOptions               // optional actions);

Parameters:

1> hsourceprocesshandle: Process Handle of handle to be copied;

2> hsourcehandle: handle to be copied;

3> htargetprocesshandle: receives the process handle of the handle after replication;

4> lptargethandle: point to the address of the handle after storage replication. If the value is null, the function copies the handle, but does not return the copied handle.

5> dwdesiredaccess: Specifies the access permission for the new handle;

6> binherithandle: indicates whether the new handle can be inherited;

7> dwoptions: Specifies the optional behavior. The value can be 0 or any combination of the following values:

Duplicate_close_source: Disable the original handle;

Duplicate_same_access: Ignore the access permission specified by dwdesiredaccess for the new handle. The new handle has the same access permission as the original handle;

Return Value:

If the operation succeeds, a non-zero value (true) is returned;

If it fails, 0 (false) is returned. (You can call getlasterror () to obtain detailed error information)

Note:

1. the original handle and the copied handle are similar references. The two point to the same Kernel Object and change either of the original or copied handles, will lead to another change;

2. duplicatehandle () will increase the usage count of a specific object. Therefore, when using the copy object handle, you should call closehandle () to close the handle to reduce the usage count of the object;

3. The handle pointed to by lptargethandle is not necessarily the same as the handle created at createthread;

Example:

HANDLE hPseudoThread=GetCurrentThread();HANDLE hProcess=GetCurrentProcess();HANDLE hRealThread=NULL;DuplicateHandle(hProcess, hPseudoThread, hProcess, &hRealThread, 0, false, 0);

Debugging content:

Hpseudothread 0 xfffffffe void *

Hprocess 0 xffffffff void *

Hrealthread 0x00000ed8 void *

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.