獲得當前進程的列表(源碼)

來源:互聯網
上載者:User

//進程描述資訊
typedef struct _tagPROCESSINFO
{
 DWORD  dwPID;
 TCHAR  strPath[_MAX_PATH];
 TCHAR  strName[_MAX_FNAME];
 
} PROCESSINFO, *LPPROCESSINFO;

//擷取進程資訊列表
BOOL EnumProcessesInfo( PROCESSINFO* lpPsInfo, ULONG ulSize, ULONG* pulNeeded )
// lpPsInfo [out] : 指向PROCESSINFO結構數組的指標
// nSize [in] : lpPsInfo中的元素個數
// nNeeded [out] : 實際的元素個數
// 傳回值 : TRUE : 成功; FALSE : 失敗
{
 ASSERT( pulNeeded );
 
 LPDWORD        lpdwPIDs ;   //儲存進程ID數組
 DWORD          dwbSize, dwbSize2;
 
 dwbSize2 = 256 * sizeof( DWORD );
 lpdwPIDs = NULL;

 do {

  if( lpdwPIDs ) {

   HeapFree( GetProcessHeap(), 0, lpdwPIDs );
   dwbSize2 *= 2;
  }

  lpdwPIDs = (LPDWORD)HeapAlloc( GetProcessHeap(), 0, dwbSize2 );
  if( lpdwPIDs == NULL ) {
   return FALSE ;
  }

  if( ! ::EnumProcesses( lpdwPIDs, dwbSize2, &dwbSize ) ) {

   HeapFree( GetProcessHeap(), 0, lpdwPIDs ) ;
   return FALSE ;
  }

 }while( dwbSize == dwbSize2 ) ;

 ULONG ulCount  = dwbSize / sizeof( DWORD );
 
 //如果為詢問數量,則返回實際數量
 if ( NULL == lpPsInfo && 0 == ulSize ) {
 
  *pulNeeded = ulCount;
  return TRUE;
 }

 ASSERT( lpPsInfo );
 if ( NULL == lpPsInfo ) {
  return FALSE;
 }

 if ( ulSize <= ulCount ) {
  *pulNeeded = ulSize;
 }
 else {
  *pulNeeded = ulCount;
 }

 //獲得進程資訊
 HANDLE hProcess;
 HMODULE hModule;
 DWORD  dwSize;

  
   char path_buffer[_MAX_PATH];
   char drive[_MAX_DRIVE];
   char dir[_MAX_DIR];
   char fname[_MAX_FNAME];
   char ext[_MAX_EXT];
  
 // Loop through each ProcID.
 for( ULONG ulIndex = 0 ; ulIndex < (*pulNeeded) ; ulIndex++ )
 {
  // Open the process (if we can... security does not
  // permit every process in the system).
//  TRACE("PID To Open:%d ", lpdwPIDs[ulIndex] );

  lpPsInfo[ulIndex].dwPID = lpdwPIDs[ulIndex];
      lpPsInfo[ulIndex].strPath[0] = 0;
      lpPsInfo[ulIndex].strName[0] = 0;
     
      // Because Can't Open 0 And 8 Process,
      // Mark Them At There
      if ( 0 == lpdwPIDs[ulIndex] ) {

         strcpy( lpPsInfo[ulIndex].strName, "System Idle Process" );
         continue;
      }
      else if ( 8 == lpdwPIDs[ulIndex] ) {

         strcpy( lpPsInfo[ulIndex].strName, "System" );
         continue;
      }

      // Open Process And Get Process Infomation
  hProcess = OpenProcess(
       PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
       FALSE, lpPsInfo[ulIndex].dwPID );
  if( hProcess != NULL )
  {
   // Here we call EnumProcessModules to get only the
   // first module in the process this is important,
   // because this will be the .EXE module for which we
   // will retrieve the full path name in a second.
   if( EnumProcessModules( hProcess, &hModule,
      sizeof(hModule), &dwSize ) ) {

    // Get Full pathname:
    if( GetModuleFileNameEx( hProcess, hModule,
                     path_buffer, sizeof(path_buffer) ) ) {
              
               _tsplitpath( path_buffer, drive, dir, fname, ext );              
               strcpy( lpPsInfo[ulIndex].strPath, path_buffer );
               sprintf( lpPsInfo[ulIndex].strName, "%s%s", fname, ext );
//               TRACE( "ModuleFileName:%s ", path_buffer );
    }
   }
   CloseHandle( hProcess ) ;
  }
 } 

參考文章:

(http://www.csdn.net/expert/topic/580/580874.xml?temp=.7492029)

特此致謝!

 

 return TRUE;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.