Three ways to get a thread handle

Source: Internet
Author: User
Tags inheritance

Three ways to get a thread handle
1. Pass the PID of the process and invoke the API function:

HANDLE openprocess (
          DWORD dwdesiredaccess,  //Access flag
          BOOL bInheritHandle,    //HANDLE inheritance Option
          DWORD dwprocessid       //process identifier
);

HANDLE openthread (
          DWORD dwdesiredaccess,  //Access right
          BOOL binherithandle,    //HANDLE Inheritance option
          DWORD dwthreadid        //thread identifier

2. When the process is created, the hprocess in the struct process_information is the thread handle

bool CreateProcess (LPCTSTR lpapplicationname,//Name of executable module LPTSTR Lpcommand Line,//command line string lpsecurity_attributes lpprocessattributes,//SD lpsecurity_attributes lpthreadattr
  Ibutes,//SD BOOL binherithandles,//Handle inheritance option DWORD dwcreationflags,//Creation Flags  LPVOID lpenvironment,//new environment block LPCTSTR lpcurrentdirectory,//current directory name Lpstartupinfo

lpstartupinfo,//startup information Lpprocess_information lpprocessinformation//process information); 
                                        typedef struct _PROCESS_INFORMATION {HANDLE hprocess; 
                                        HANDLE Hthread; 
                                        DWORD Dwprocessid; 
                                    DWORD dwThreadID; 

} process_information; 

2.1 When creating a process, there are two parameters that relate to the inheritance of the process:
(1) Lpsecurity_attributes lpprocessattributes,//SD
The SD parameter is a property of the process object itself, and the process object is generally called the kernel object, which determines whether the object itself has inherited attributes. When you create it, you can decide whether the created process has inherited properties

Lpsecurity_attributes sa;
Sa.binherithandle = TRUE;

(2) BOOL binherithandles,//Handle inheritance option
This property determines whether the kernel objects created (with inherited property SD) can be inherited, regardless of the properties of the kernel itself.

  bInheritHandles = TRUE;

3. Copy the handle of the process handle to copy the specified kernel object from the specified process

char szbuf[maxbyte] = {0};
Startupinfo si;
Process_information Pi;
memset (&si, 0, sizeof (STARTUPINFO));
memset (&pi, 0, sizeof (process_information));
                            Create a child process BOOL BRet = CreateProcess (Szbuf, NULL, &sa,
                            NULL, TRUE, NULL,

NULL, NULL, &SI, &PI);
Copy the handle of the process from the parent process to the child process HANDLE Pseudohandle = getcurrentprocess ();//pseudo handle HANDLE duphandle; BOOL bRet =duplicatehandle (pseudohandle,//copy of the source src Pi. process,//the handle of the copy subprocess, Pi. Process represents the handle pi of the child process in the parent process. process,//destination, copy to where &duphandle,//copy results 0, FALSE, dupli cate_same_access); 

3.1 Pseudo handle: The set of handles in a process is represented as an index, and the value of the pseudo-handle can be found by debugging is 0XFFFFFFFF (-1), which always represents the handle of the process itself index=-1

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.