我写的代码,已知一进程ID,我想获得该进程中的所以的线程ID,代码如下,但有错误。HANDLE hThreadSnap=NULL;
THREADENTRY32 me32={0};
me32.dwSize=sizeof(THREADENTRY32);
hThreadSnap=CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwProcessId); //已知进程ID
if(Thread32First(hThreadSnap,&me32))
{
do
{
CString str1;
str1.Format("找到线程,ID:%d",me32.th32ThreadID);
AfxMessageBox(str1);
}
while(Thread32Next(hThreadSnap,&me32));
}
else
{
AfxMessageBox("没有找到任何线程");
}
CloseHandle(hThreadSnap); // 关掉线程快照
THREADENTRY32 me32={0};
me32.dwSize=sizeof(THREADENTRY32);
hThreadSnap=CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwProcessId); //已知进程ID
if(Thread32First(hThreadSnap,&me32))
{
do
{
CString str1;
str1.Format("找到线程,ID:%d",me32.th32ThreadID);
AfxMessageBox(str1);
}
while(Thread32Next(hThreadSnap,&me32));
}
else
{
AfxMessageBox("没有找到任何线程");
}
CloseHandle(hThreadSnap); // 关掉线程快照
DWORD dwProcessId=0;
GetWindowThreadProcessId(m_hWnd,&dwProcessId); 这和错误有关吗?另外,我的这些代码是在DLL中写的。 请指教!!!
线程属于哪个进程可以通过THREADENTRY32 me32里的
父进程获得.
#include <windows.h>
#include <tlhelp32.h>
#include <stdio.h>
BOOL RefreshThreadList (DWORD dwOwnerPID)
{
HANDLE hThreadSnap = NULL;
BOOL bRet = FALSE;
THREADENTRY32 te32 = {0};
// Take a snapshot of all threads currently in the system.
hThreadSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (hThreadSnap == INVALID_HANDLE_VALUE)
return (FALSE);
// Fill in the size of the structure before using it.
te32.dwSize = sizeof(THREADENTRY32);
// Walk the thread snapshot to find all threads of the process.
// If the thread belongs to the process, add its information
// to the display list.
if (Thread32First(hThreadSnap, &te32))
{
do
{
if (te32.th32OwnerProcessID == dwOwnerPID)
{
printf( "\nTID\t\t%d\n", te32.th32ThreadID);
printf( "Owner PID\t%d\n", te32.th32OwnerProcessID);
printf( "Delta Priority\t%d\n", te32.tpDeltaPri);
printf( "Base Priority\t%d\n", te32.tpBasePri);
}
}
while (Thread32Next(hThreadSnap, &te32));
bRet = TRUE;
}
else
bRet = FALSE; // could not walk the list of threads
// Do not forget to clean up the snapshot object.
CloseHandle (hThreadSnap);
return (bRet);
}