在对快照进行进程遍历后,如何获取所有进程的完整命令行?

gra*_*tii 3 c winapi process 32bit-64bit

所以,我的目标是获得所有当前正在运行的进程的完整命令行。为此,我所做的是使用CreateToolhelp32SnapshotAPI拍摄进程的快照,然后进行进程遍历以将PROCESSENTRY32类型进程存储在名为 的数组中process_list

BOOL GetProcessList( FILE *f, PROCESSENTRY32* process_list, int process_count)
{
  HANDLE hProcessSnap;
  HANDLE hProcess;
  PROCESSENTRY32 pe32;
  DWORD dwPriorityClass;


  hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
  if( hProcessSnap == INVALID_HANDLE_VALUE )
  {
    return( FALSE );
  }

  // Set the size of the structure before using it.
  pe32.dwSize = sizeof( PROCESSENTRY32 );

  // Retrieve information about the first process,
  // and exit if unsuccessful
  if( !Process32First( hProcessSnap, &pe32 ) )

  {
    CloseHandle( hProcessSnap );          // clean the snapshot object
    return( FALSE );
  }
  // Now walk the snapshot of processes, and
  // display information about each process in turn
  int i = 0;
  do
  {
    // Retrieve the priority class.
    dwPriorityClass = 0;
    hProcess = OpenProcess( PROCESS_QUERY_INFORMATION|PROCESS_VM_READ, FALSE, pe32.th32ProcessID );

    if( hProcess == NULL ) { }

    else
    {
      dwPriorityClass = GetPriorityClass( hProcess );
      if( !dwPriorityClass )
      CloseHandle( hProcess );
    }

    process_list[i] = pe32;
    i++;

  } while( Process32Next( hProcessSnap, &pe32 ) && i <= process_count);

  CloseHandle( hProcessSnap );
  return( TRUE );
}
Run Code Online (Sandbox Code Playgroud)

现在,是否可以遍历这个进程数组并收集每个进程的完整命令行?我怎样才能做到这一点?

如果这很重要,我会将代码编译为 64 位进程,并且需要主机上运行的所有进程的完整命令行。

Rem*_*eau 5

对于每个进程 ID:

  1. 用于OpenProcess()获取HANDLE进程。

  2. 然后使用QueryFullProcessImageName(), GetProcessImageFileName(), 或GetModuleFileNameEx获取进程的路径和文件名。

  3. 然后用于NtQueryInformationProcess()检索进程PEB结构的地址,其中包含一个ProcessParameters成员,该成员包含进程的命令行参数(您也可以从 中获取图像路径PEB)。用ReadProcessMemory()阅读的内容PEB

有关更多详细信息,请查看以下文章:

使用 NtQueryInformationProcess 获取进程信息