TLHELP32 enumerating processes, threads, module information

Source: Internet
Author: User

The API provided by TLHELP32 makes it easy to get snapshot information for processes, modules, and threads.

The header file involved:

Tlhelp32.h


Enumerate process information:

HANDLE hSnapShot;

PROCESSENTRY32 pe32 = {sizeof (PROCESSENTRY32)};

HANDLE Hprocesssnap = CreateToolhelp32Snapshot (th32cs_snapprocess, dwpid);

if (Hprocesssnap = = INVALID_HANDLE_VALUE)

{

return-1;

}

if (Process32First (Hprocesssnap, &pe32))

{

Do

{

printf ("Process ID:%d\n", pe32.th32processid);

}

while (Process32Next (Hprocesssnap, &pe32));

}

CloseHandle (HPROCESSSNAP);


Enumerate module information:

MODULEENTRY32 Mod32;

Mod32.dwsize=sizeof (MOD32);

HANDLE Hsnap = CreateToolhelp32Snapshot (Th32cs_snapmodule, dwpid);

if (Hsnap = = INVALID_HANDLE_VALUE)

{

printf ("Can't open this program module! ");

CloseHandle (HSNAP);

Return

}

BOOL bmodule = Module32first (HSNAP,&MOD32);

while (Bmodule)

{

printf ("Module process ID:%d \ n", mod32.th32processid);

Bmodule=module32next (HSNAP,&MOD32);

}

CloseHandle (HSNAP);


Enumerate Threads Thread Information:

THREADENTRY32 te32;

Te32.dwsize=sizeof (TE32);

HANDLE Hsnap = createtoolhelp32snapshot (th32cs_snapthread, 0);

if (Hsnap = = INVALID_HANDLE_VALUE)

{

printf ("Can't open this program module! ");

CloseHandle (HSNAP);

Return

}

BOOL Bthread = Thread32first (HSNAP,&TE32);

while (Bthread)

{

printf ("Owner process ID:%d \ n", te32.th32ownerprocessid);

printf ("Thread ID:%d \ n", Te32.th32threadid);

Bthread = Thread32next (HSNAP,&TE32);

}

CloseHandle (HSNAP);


TLHELP32 enumerating processes, threads, module information

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.