首页  编辑  

获取进程全路径名称

Tags: /超级猛料/OS.操作系统/浏览器、系统/   Date Created:

procedure TForm1 . GetCurrentApps ;

{

 ·µ »Ø µ± Ç° ÔË ÐÐ µÄ ½ø ³Ì µÄ ¿É Ö´ ÐÐ ÎÄ ¼þ Ãû ³Æ ÁÐ ±í

}

var

 ContinueLoop : BOOL ;

 SnapshotHandle : THandle ;

 ProcessEntry32 : TProcessEntry32 ;

 hProcess : THandle ;

 Buffer : array [ 0 .. MAX_PATH ] of char ;

 S : string ;

 L : integer ;

begin

 SnapshotHandle := CreateToolhelp32Snapshot ( TH32CS_SNAPPROCESS , 0 );

 ProcessEntry32 . dwSize := Sizeof ( ProcessEntry32 );

 ContinueLoop := Process32First ( SnapshotHandle , ProcessEntry32 );

 ListView1 . Items . BeginUpdate ;

 ListView1 . Clear ;

  while ContinueLoop do

    with ListView1 . Items . Add do

    begin

     hProcess := OpenProcess ( PROCESS_QUERY_INFORMATION or PROCESS_VM_READ , False , ProcessEntry32 . th32ProcessID );

     FillChar ( Buffer , SizeOf ( Buffer ), 0 );

     L := GetModuleFileNameEx ( hProcess , 0 , Buffer , SizeOf ( Buffer ));

     S := StrPas ( Buffer );

     Caption := ChangeFileExt ( ProcessEntry32 . szExeFile , '' );

     SubItems . Add ( IntToStr ( ProcessEntry32 . th32ProcessID ));

     SubItems . Add ( IntToStr ( ProcessEntry32 . cntThreads ));

      if L > 0 then

      begin

       L := Pos ( ':\' , S );

        if L > 2 then System . Delete ( S , 1 , L - 2 );

       SubItems . Add ( S );

       ImageIndex := GetIconIndex ( S , FILE_ATTRIBUTE_NORMAL );

      end

      else

      begin

       SubItems . Add ( ProcessEntry32 . szExeFile );

       ImageIndex := GetIconIndex ( ProcessEntry32 . szExeFile , FILE_ATTRIBUTE_NORMAL );

      end ;

     ContinueLoop := Process32Next ( SnapshotHandle , ProcessEntry32 );

     CloseHandle ( hProcess );

    end ;

 ListView1 . Items . EndUpdate ;

 CloseHandle ( SnapshotHandle );

end ;

function RunningProcessesList: Boolean;

 function BuildListTH: Boolean;

 var

   SnapProcHandle: THandle;

   ProcEntry: TProcessEntry32;

   NextProc: Boolean;

 begin

   SnapProcHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

   if SnapProcHandle <> THandle(-1) then

   begin

     Result := True;

     ProcEntry.dwSize := Sizeof(ProcEntry);

     NextProc := Process32First(SnapProcHandle, ProcEntry);

     while NextProc do

     begin

       List.InitInstance(Pointer(ProcEntry.th32ProcessID));

       List.AddObject(ProcEntry.szExeFile,

Pointer(ProcEntry.th32ProcessID));

       NextProc := Process32Next(SnapProcHandle, ProcEntry);

       Inc(Cont);

     end;

     CloseHandle(SnapProcHandle);

   end else

     Result := False;

 end;

 function BuildListPS: Boolean;

 var

   PIDs: array[0..1024] of DWORD;

   Handle: THandle;

   Needed: DWORD;

   I: Integer;

   ModuleFileName: array[0..MAX_PATH] of Char;

 begin

   Result := EnumProcesses(@PIDs, Sizeof(PIDs), Needed);

   if not Result then Exit;

   for I := 0 to (Needed div Sizeof(DWORD)) - 1 do

     if PIDs[I] <> 0 then

     begin

       Handle := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ,

False, PIDs[I]);

       if Handle <> 0 then

       begin

         if GetModuleFileNameEx(Handle, 0, ModuleFileName,

Sizeof(ModuleFileName)) = 0 then

         begin

           List.AddObject('[System]', Pointer(INVALID_HANDLE_VALUE))

         end

         else

             begin

                  List.AddObject(ModuleFileName, Pointer(PIDs[I]));

             end;

         CloseHandle(Handle);

       end;

     end;

 end;

begin

    if (Win32Platform = VER_PLATFORM_WIN32_NT) and (Win32MajorVersion = 4)

then

       Result := BuildListPS

    else

        Result := BuildListTH;

end;