我写的代码,已知一进程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);  //   关掉线程快照

解决方案 »

  1.   

    hThreadSnap=CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwProcessId);  //已知进程ID这句中参数不对.用 TH32CS_SNAPTHREAD
      

  2.   

    我把参数该成 TH32CS_SNAPTHREAD ,但接收到的是所有进程中的所有线程,即SPY++中测得的的系统全部线程,我的已知进程ID “dwProcessId”是这样得到的,代码:
       DWORD dwProcessId=0;
       GetWindowThreadProcessId(m_hWnd,&dwProcessId); 这和错误有关吗?另外,我的这些代码是在DLL中写的。  请指教!!!
      

  3.   

    只能用TH32CS_SNAPTHREAD 参数获得系统所有线程序, 
    线程属于哪个进程可以通过THREADENTRY32 me32里的
    父进程获得.
      

  4.   

    在下谢谢 shadowac(走向黑暗)和 T97102003(池塘里的水手) 啦,不知道怎么才能给分呀?
      

  5.   

    The   following   example   obtains   a   list   of   running   threads   for   the   specified   process.   First,   the   RefreshThreadList   function   takes   a   snapshot   of   the   currently   executing   threads   in   the   system   using   the   CreateToolhelp32Snapshot   function,   then   it   walks   through   the   list   recorded   in   the   snapshot,   using   the   Thread32First   and   Thread32Next   functions.   The   parameter   for   RefreshThreadList   is   the   identifier   of   the   process   whose   threads   will   be   listed.     
        
      #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);     
      }