首页
社区
课程
招聘
Ring3如何枚举已打开进程的线程?
发表于: 2013-1-31 11:05 4564

Ring3如何枚举已打开进程的线程?

2013-1-31 11:05
4564
在Ring3,
我的RING3程序用OpenProcess打开了一个RING3进程,
然后我想枚举出该进程的所有线程。

请问,如何在RING3中枚举这个进程里的所有线程?
求指教!

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 0
支持
分享
最新回复 (4)
雪    币: 4817
活跃值: (23)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, ProcessId);
Thread32First(hSnap, ThreadEntry);
Thread32Next(hSnap, ThreadEntry);

注意微软的一个说明

Includes all threads in the system in the snapshot. To enumerate the threads, see Thread32First.

To identify the threads that belong to a specific process, compare its process identifier to the th32OwnerProcessID member of the THREADENTRY32 structure when enumerating the threads.

这句话的大致意思就是Thread32First和Thread32Next枚举的是整个系统的线程,如果只想获取指定进程的线程,匹配ProcessId和ThreadEntry.th32OwnerProcessID 的值就是了。

微软也提供了一个代码供参考

#include <windows.h>
#include <tlhelp32.h>
#include <stdio.h>

//  Forward declarations:
BOOL ListProcessThreads( DWORD dwOwnerPID );
void printError( TCHAR* msg );

void main( )
{
  ListProcessThreads(GetCurrentProcessId() );
}

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( "Thread32First" ); // Show cause of failure
    CloseHandle( hThreadSnap );    // Must clean up 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 ) );

//  Don't forget to clean up the snapshot object.
  CloseHandle( hThreadSnap );
  return( TRUE );
}

void printError( TCHAR* msg )
{
  DWORD eNum;
  TCHAR sysMsg[256];
  TCHAR* p;

  eNum = GetLastError( );
  FormatMessage(
         FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
         NULL, eNum,
         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default lang.
         sysMsg, 256, NULL );

  // Trim the end of the line and terminate it with a null
  p = sysMsg;
  while( ( *p > 31 ) || ( *p == 9 ) )
    ++p;
  do { *p-- = 0; } while( ( p >= sysMsg ) &&
                          ( ( *p == '.' ) || ( *p < 33 ) ) );

  // Display the message
  printf( "\n  WARNING: %s failed with error %d (%s)",
    msg, eNum, sysMsg );
}

不过上面枚举不用打开进程,用进程ID就行。
2013-1-31 11:22
0
雪    币: 79
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
楼上正解,还可以枚举dll
2013-1-31 11:38
0
雪    币: 102
活跃值: (14)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
非常感谢!!!!!!!!!!!!!!!
2013-1-31 12:07
0
雪    币: 77
活跃值: (48)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
5
mark
2014-1-16 17:00
0
游客
登录 | 注册 方可回帖
返回
//