Call Microsoft unlisted Zwqueryinformationthread function to get thread ID based on thread handle

Source: Internet
Author: User

This time in the company project in order to support the XP system colleague code used to

GetThreadId

This Microsoft API but the least supported version of this API is

Windows version Windows Vista [Desktop Apps | UWP Apps] Windows Server 2003 [Desktop Apps | UWP Apps]

Finally, we used

Zwqueryinformationthread to solve

After the resolution of the record if you encounter the same problem can be consulted

Reference documents

http://undocumented.ntinternals.net/index.html?page=UserMode%2FUndocumented%20Functions%2FNT%20Objects% 2fthread%2fthread_information_class.html

The specific code is as follows:

1#include"stdafx.h"2#include <Windows.h>3 #defineStatus_success ((NTSTATUS) 0x00000000l)//Ntsubauth4typedefenum_threadinfoclass {5Threadbasicinformation =0,6Threadtimes =1,7ThreadPriority =2,8Threadbasepriority =3,9Threadaffinitymask =4,TenThreadimpersonationtoken =5, OneThreaddescriptortableentry =6, AThreadenablealignmentfaultfixup =7, -Threadeventpair_reusable =8, -Threadquerysetwin32startaddress =9, theThreadzerotlscell =Ten, -Threadperformancecount = One, -Threadamilastthread = A, -Threadidealprocessor = -, +Threadpriorityboost = -, -Threadsettlsarrayaddress = the,//Obsolete +Threadisiopending = -, AThreadhidefromdebugger = -, atThreadbreakontermination = -, -Threadswitchlegacystate = +, -threadisterminated = -, -Threadlastsystemcall = +, -Threadiopriority = A, -Threadcycletime = at, inThreadpagepriority = -, -Threadactualbasepriority = -, toThreadtebinformation = -, +Threadcswitchmon = -,//Obsolete -THREADCSWITCHPMU = -, theThreadwow64context = in, *Threadgroupinformation = -, $Threadumsinformation = to,//UMSPanax NotoginsengThreadcounterprofiling = +, -Threadidealprocessorex = -, theThreadcpuaccountinginformation = the, +Threadsuspendcount = *, AThreadactualgroupaffinity = A, theThreaddynamiccodepolicyinfo = the, +Maxthreadinfoclass = $, - } threadinfoclass; $typedef NTSTATUS (winapi*zwqueryinformationthread) ( $ _in_ HANDLE Threadhandle, - _in_ threadinfoclass Threadinformationclass, - _in_ PVOID threadinformation, the _in_ ULONG threadinformationlength, - _out_opt_ Pulong returnlengthWuyi ); thetypedefstruct_client_id { - DWORD uniqueprocess; Wu DWORD Uniquethread; - } client_id; Abouttypedef CLIENT_ID *pclient_id; $typedefstruct_thread_basic_information { - NTSTATUS exitstatus; - PVOID tebbaseaddress; - client_id ClientId; A kaffinity Affinitymask; + LONG priority; the LONG basepriority; -} thread_basic_information, *pthread_basic_information; $ Zwqueryinformationthread Zwqueryinformationthread; the hmodule init (LPCSTR szfuncnmae) the { thehmodule hmodule = LoadLibrary (_t ("Ntdll.dll")); the     if(hmodule) -Zwqueryinformationthread =(Zwqueryinformationthread):: GetProcAddress (Hmodule, szfuncnmae); in     returnhmodule; the } the intWINAPI ThreadProc (lpvoid ARG) About { the     inti =100000; the      while(--i) the     { +printf"%d\r\n", i); -Sleep ( -); the     }Bayi     returni; the } the intMain () - { -DWORD dwThreadID =NULL; theDWORD Dwoldthreadid =NULL; theHmodule hmodule =nullptr; thehmodule = init ("Zwqueryinformationthread"); theHANDLE hthread =:: CreateThread (NULL, NULL, (lpthread_start_routine) threadproc, NULL, NULL, &dwoldthreadid); -     if(Hmodule &&zwqueryinformationthread) the     { the thread_basic_information TBI; theDWORD Lret =NULL;94ZeroMemory (&AMP;TBI,sizeof(thread_basic_information)); the         if(Zwqueryinformationthread (Hthread, threadbasicinformation, &tbi,sizeof(thread_basic_information), &lret)! =status_success) the         { theprintf"get failed \ r \ n");98         } Aboutprintf"PROCESSid%d ThreadID%d\r\n", TBI. Clientid.uniqueprocess, TBI. Clientid.uniquethread); - FreeLibrary (hmodule);101     }102 103 CloseHandle (hthread);104     return 0; the}

Call Microsoft unlisted Zwqueryinformationthread function to get thread ID based on thread handle

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.