none
知道了进程的ID号或是句柄如何获取到该进程里的所有线程的句柄??? RRS feed

答案

全部回复

  • BOOL ListProcessThreads( DWORD dwOwnerPID ) 
    { 
     HANDLE hThreadSnap = INVALID_HANDLE_VALUE; 
     THREADENTRY32 te32; 
     
     // Take a snapshot of all running threads 
     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 ); 
     
     // Retrieve information about the first thread,
     // and exit if unsuccessful
     if( !Thread32First( hThreadSnap, &te32 ) ) 
     {
      printError( TEXT("Thread32First") ); // show cause of failure
      CloseHandle( hThreadSnap );     // clean the snapshot object
      return( FALSE );
     }
    
     // Now walk the thread list of the system,
     // and display information about each thread
     // associated with the specified process
     do 
     { 
      if( te32.th32OwnerProcessID == dwOwnerPID )
      {
       printf( "\n\n   THREAD ID   = 0x%08X", te32.th32ThreadID ); 
       printf( "\n   Base priority = %d", te32.tpBasePri ); 
       printf( "\n   Delta priority = %d", te32.tpDeltaPri ); 
      }
     } while( Thread32Next(hThreadSnap, &te32 ) ); 
    
     CloseHandle( hThreadSnap );
     return( TRUE );
    }
    
    

    主要使用到的是Thread32First/Thread32Next(),关于函数的具体用法请参看MSDN文档

    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.
    2011年7月18日 3:11
    版主
  • BOOL ListProcessThreads( DWORD dwOwnerPID ) 
    { 
     HANDLE hThreadSnap = INVALID_HANDLE_VALUE; 
     THREADENTRY32 te32; 
     
     // Take a snapshot of all running threads 
     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 ); 
     
     // Retrieve information about the first thread,
     // and exit if unsuccessful
     if( !Thread32First( hThreadSnap, &te32 ) ) 
     {
     printError( TEXT("Thread32First") ); // show cause of failure
     CloseHandle( hThreadSnap );   // clean the snapshot object
     return( FALSE );
     }
    
     // Now walk the thread list of the system,
     // and display information about each thread
     // associated with the specified process
     do 
     { 
     if( te32.th32OwnerProcessID == dwOwnerPID )
     {
      printf( "\n\n  THREAD ID  = 0x%08X", te32.th32ThreadID ); 
      printf( "\n  Base priority = %d", te32.tpBasePri ); 
      printf( "\n  Delta priority = %d", te32.tpDeltaPri ); 
     }
     } while( Thread32Next(hThreadSnap, &te32 ) ); 
    
     CloseHandle( hThreadSnap );
     return( TRUE );
    }
    
    

    主要使用到的是Thread32First/Thread32Next(),关于函数的具体用法请参看MSDN文档

    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.
    这个只能获取到线程的ID,我知道,但是获取不到句柄,难道句柄和ID可以互相转化????
    2011年7月18日 8:56
  • 试试OpenThread()函数
    可以参考这个:http://www.codeproject.com/KB/threads/w32process.aspx
    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.
    • 已标记为答案 Shaotine 2011年7月18日 13:30
    2011年7月18日 11:16
    版主