<form id="hz9zz"></form>
  • <form id="hz9zz"></form>

      <nobr id="hz9zz"></nobr>

      <form id="hz9zz"></form>

    1. 明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

      獲得當前進程的下文

      [摘要]//進程描述信息typedef struct _tagPROCESSINFO DWORD dwPID; TCHAR strPath[_MAX_PATH]; TCHAR strName[_MAX_FNAME]; PROCESSINFO, *LPPROCESSINFO;//獲取進程信息列表BO...
      //進程描述信息
      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\r\n", 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\r\n", path_buffer );
          }
         }
         CloseHandle( hProcess ) ;
        }
      }

      return TRUE;
      }


      日韩精品一区二区三区高清