Getprocessidofthread alternative implementations of WinXP and previous operating systems

Source: Internet
Author: User

or learn the vld2.x version to see:

The Getprocessidofthread API is not available in Windows XP and previous operating systems, and an alternative implementation is given here:

Header file:

#if _WIN32_WINNT < 0x0600//Windows XP or earlier, no Getprocessidofthread ()
DWORD _getprocessidofthread (HANDLE thread);
#define Getprocessidofthread _getprocessidofthread
#endif

Source file:

Get through the Ntqueryinformationthread function:

DWORD _getprocessidofthread (HANDLE thread)
{
typedef struct _CLIENT_ID {
HANDLE uniqueprocess;
HANDLE Uniquethread;
} client_id, *pclient_id;

typedef LONG NTSTATUS;
typedef LONG Kpriority;

typedef struct _THREAD_BASIC_INFORMATION {
NTSTATUS Exitstatus;
PVOID tebbaseaddress;
client_id ClientId;
Kaffinity Affinitymask;
Kpriority priority;
Kpriority basepriority;
} thread_basic_information, *pthread_basic_information;

Const static Threadinfoclass threadbasicinformation = (threadinfoclass) 0;

typedef NTSTATUS (WINAPI *pntqueryinformationthread) (HANDLE thread,
Threadinfoclass Infoclass, PVOID buffer, ULONG buffersize,
Pulong used);

static Pntqueryinformationthread ntqueryinformationthread = NULL;

Thread_basic_information TBI;
NTSTATUS status;
if (Ntqueryinformationthread = = NULL) {
hmodule ntdll = Getmodulehandlew (L "Ntdll.dll");
if (ntdll)
{
Ntqueryinformationthread = (pntqueryinformationthread) GetProcAddress (Ntdll, "Ntqueryinformationthread");
}

if (Ntqueryinformationthread = = NULL) {
return 0;
}
}

Status =Ntqueryinformationthread(Thread, threadbasicinformation, &AMP;TBI, sizeof (TBI), NULL);
if (Status < 0) {
Shall we go through all the trouble for setting last error?
return 0;
}

Return(DWORD) TBI. Clientid.uniqueprocess;
}

Getprocessidofthread alternative implementations of WinXP and previous operating systems

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.