Jaki proces trzyma plik

0

Jak sprawdzić, jaki proces "trzyma" dany plik ?

Chodzi o to, że mamy plik: muzyka.mp3 który jest odtwarzany przez Winampa. Jak to sprawdzić ?

Dzięki

0

Nie sądze, aby nikt nie wiedział, na pewno jest sposób na to... jak to sprawdzić ?

0

ściągnij sobie wholockme

0

Ściągnąłem, ale nie ma w nim źródeł.

Parametry wydawać temu programowi widzę, że można, ale czy pozostaje mi tylko taki sposób ?

0

pozostaje Ci jeszcze grzebanie w googlu i/lub na MSDNie

0

Widzę, że nikt nie ma pomysłu...

To może z innej strony. Czy jest w Windows jakiś program (coś jak cmd.exe), któremu wydam rozkaz pobrania odniesienia do procesu pliku ?

Może systemowy Menedżer Procesów ma taką opcję. Oczywiście wszystko odbywać się ma po cichu, bez okien.

0

to moze ja troche podpowiem

najpierw klaska do przechowywania danych

  TApp = class
    fPID: Integer;
    fPArentPID: Integer;
    fPIDName: string;
    fThread: Integer;
    fDLLName: TStringList;
    fDLLPath: TStringList;
    fDescription: string;
  end;

oczywiscie u Ciebie moze wygladac inaczej.
Teraz robimy snapshoot procesow i enumerujemy kazdy w celu pobrania aplikacji
Zapiujemy do stringlisty obiekty (podajemy jako klucz np PID lub co Ci wyodniej)

function TForm2.GetApps(AppName: string): TStringList;
var fHandle: THandle;
  fModHandle: THandle;
  fProcess: TProcessEntry32;
  fModule: TMODULEENTRY32;
  App: TApp;
  i: Integer;
  IsDLL: Boolean;
  IsProcess: Boolean;
  fDesc: string;
  sPath: string;
begin
  IsDLL := False;
  IsProcess := False;


  Result := TStringList.Create();
  Result.Clear();
  fDesc := 'DLL Name: %s'#13#10 +
           'DLL Path: %s'#13#10 +
           'ModuleId: %d'#13#10;

  fHandle := CreateToolHelp32SnapShot(TH32CS_SNAPALL, GetCurrentProcessId);
  fProcess.dwSize := SizeOf(fProcess);

  IsProcess := Process32First(fHandle, fProcess);

  while IsProcess do
  begin
    App := TApp.Create();
    App.fDLLName := TStringList.Create();
    App.fDLLPath := TStringList.Create();
    IsDLL := Module32First(fHandle, fModule);
    while IsDLL do
    begin
      if Edit1.Text <> '' then
        sPath := fModule.szModule
      else
        sPath := ExtractFileExt(fModule.szModule); 
      if SameText(sPath, Edit1.Text + '.dll') then
      begin
        App.fPID := fProcess.th32ProcessID;
        App.fPIDName := fProcess.szExeFile;
        App.fDLLName.Add(fModule.szModule);
        App.fDLLPath.Add(fModule.szExePath);
        App.fDescription := App.fDescription +
          Format(fDesc, [fModule.szModule, fModule.szExePath, fModule.th32ModuleID]);
      end;
      IsDLL := Module32Next(fHandle, fModule)
    end;
    if App.fDLLName.Count > 0 then
      Result.AddObject(IntToStr(App.fPID), App);
    IsProcess := Process32Next(fHandle, fProcess);
  end;
  CloseHandle(fHandle);
  Result.Count;
end;

PS. Pamietaj pozniej o zwolnieniu obiektow ze stringlisty

0

W porządku, Dziękuję.

Jednak to nie rozwiązuje do końca problemu, bo to tylko moduły (biblioteki) załadowane przez aplikację, a nie wszystkie pliki z jakich korzysta w sensie (używa/odtwarza/edytuje/podgląda (zdjęcie)).

Ale już coraz bliżej :)

0

fModule.szExePath

w tych klasach <ort>na pewno </ort>jest cos co Ci pomoze

0

crowa, pomóż.

Coś w tym kodzie jest nie tak.
Wskazuję, na te miejsca, gdzie prawdopodobnie jest źle:

function TForm2.GetApps(AppName: string): TStringList;

Zmienna AppName nigdy nie została wykorzystana w kodzie, więc nie wiem po co jest i gdzie jej użyć.

IsDLL := Module32First(fHandle, fModule);

Tutaj powinno chyba być (fModHandle, fModule), ale i tak nie działa poprawnie, bo nie pokazuje modułów od wskazanego procesu.

Przed:

IsDLL := Module32First(fHandle, fModule);

...powinno jeszcze chyba być:

fModHandle   := CreateToolHelp32SnapShot(TH32CS_SNAPMODULE, fProcess.th32ProcessID);

może ktoś mi poprawić ten kod ?

0

i tak Ci to niewiele da. moduły to są załadowane biblioteki różnorakiego rodzaju. owszem, są to otwarte pliki, ale jedynie mała ich część.

0
unit proc;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, ComCtrls, Grids, ValEdit, MTSUtilsUnit, TLHelp32,
  Menus, PsAPI;

type
  TApp = class
    fPID: Integer;
    fPArentPID: Integer;
    fPIDName: string;
    fThread: Integer;
    fDLLName: TStringList;
    fDLLPath: TStringList;
    fDescription: string;
  end;

  TForm2 = class(TForm)
    StatusBar1: TStatusBar;
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    Panel4: TPanel;
    Splitter2: TSplitter;
    Edit1: TEdit;
    Button1: TButton;
    Label1: TLabel;
    RichEdit1: TRichEdit;
    PopupMenu1: TPopupMenu;
    kill1: TMenuItem;
    StringGrid1: TStringGrid;
    function GetApps(AppName: string): TStringList;
    function GetInfo(PID: Integer): string;
    function Kill(PID: Integer): Boolean;
    procedure kill1Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
      var CanSelect: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;
  ApplicationList: TStringList;
  row: Integer;

implementation

{$R *.dfm}

function TForm2.Kill(PID: Integer): Boolean;
var fHandle: THandle;
begin
  fHandle := OpenProcess(PROCESS_TERMINATE, BOOL(0), PID);
  if TerminateProcess(fHandle, 0) then
    Result := True
  else
    Result := False;

  CloseHandle(fHandle);
end;

procedure TForm2.kill1Click(Sender: TObject);
var i: Integer;
  fApp: TApp;
begin
  if Kill(StrToInt(StringGrid1.Cells[1, row])) then
  begin
    ApplicationList.Delete(row);
    StringGrid1.RowCount := ApplicationList.Count;
    for i := 1 to ApplicationList.Count - 1 do
    begin
      fApp := TApp(ApplicationList.Objects[i]);
      Form2.StringGrid1.Cells[0,i] := fApp.fPIDName;
      Form2.StringGrid1.Cells[1,i] := IntToStr(fApp.fPID);
    end;
    MessageBox(0, 'Terminate successfully', 'Kill', MB_ICONINFORMATION or MB_OK);
  end
  else
    MessageBox(0, 'Could not terminate process', 'Kill', MB_ICONINFORMATION or MB_OK);
end;

procedure TForm2.StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
  var CanSelect: Boolean);
var fApp: TApp;
begin
  row := ARow;
  RichEdit1.Lines.Clear();
  if ApplicationList.Count >= row then
  begin
    fApp := TApp(ApplicationList.Objects[row]);
    RichEdit1.Lines.Add(fApp.fDescription);
  end;
end;

procedure TForm2.Button1Click(Sender: TObject);
var i: Integer;
  fApp: TApp;
  sItem: string;
  CanSelect: Boolean;
begin
  for i := 0 to ApplicationList.Count - 1 do
  begin
    fApp := TApp(ApplicationList.Objects[i]);
    FreeAndNil(fApp.fDLLName);
    FreeAndNil(fApp.fDLLPath);
    FreeAndNil(fApp);
  end;
  FreeAndNil(ApplicationList);

  ApplicationList := GetApps(Edit1.Text);
  StringGrid1.RowCount := ApplicationList.Count;
  for i := 0 to ApplicationList.Count - 1 do
  begin
    fApp := TApp(ApplicationList.Objects[i]);
    StringGrid1.Cells[0,i] := fApp.fPIDName;
    StringGrid1.Cells[1,i] := IntToStr(fApp.fPID);
  end;
  StringGrid1.OnSelectCell(Self, 0, 1, CanSelect);
end;

procedure TForm2.FormCreate(Sender: TObject);
begin
  StringGrid1.Cells[0,0] := 'Name';
  StringGrid1.Cells[1,0] := 'PID';
end;

function TForm2.GetInfo(PID: Integer): string;
var fHandle: THandle;
  fModule: TModuleEntry32;
  sInfo: string;
begin
  Result := '';
  sInfo := 'DLL Name: %s'#13#10 +
           'DLL Path: %s'#13#10 +
           'ModuleId: %d'#13#10;

  fHandle := CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, PID);

  if fHandle <> INVALID_HANDLE_VALUE then
  if Module32First(fHandle, fModule) then
  repeat
    if SameText(ExtractFileExt(fModule.szModule), '.dll') then
    begin
      sInfo := Format(sInfo, [fModule.szModule, fModule.szExePath, fModule.th32ModuleID]);
      Result := Result + sInfo;
    end;
  until not Module32Next(fHandle, fModule);
end;

function TForm2.GetApps(AppName: string): TStringList;
var fHandle: THandle;
  fModHandle: THandle;
  fProcess: TProcessEntry32;
  fModule: TMODULEENTRY32;
  App: TApp;
  i: Integer;
  IsDLL: Boolean;
  IsProcess: Boolean;
  fDesc: string;
  sPath: string;
begin
  IsDLL := False;
  IsProcess := False;


  Result := TStringList.Create();
  Result.Clear();
  fDesc := 'DLL Name: %s'#13#10 +
           'DLL Path: %s'#13#10 +
           'ModuleId: %d'#13#10;

  fHandle := CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS, 0);
  fProcess.dwSize := SizeOf(fProcess);

  IsProcess := Process32First(fHandle, fProcess);

  while IsProcess do
  begin
    App := TApp.Create();
    App.fDLLName := TStringList.Create();
    App.fDLLPath := TStringList.Create();
    fModHandle := CreateToolHelp32SnapShot(TH32CS_SNAPMODULE, fProcess.th32ProcessID);
    IsDLL := Module32First(fModHandle, fModule);
    while IsDLL do
    begin
      if Edit1.Text <> '' then
        sPath := fModule.szModule
      else
        sPath := ExtractFileExt(fModule.szModule);
      if SameText(sPath, Edit1.Text + '.dll') then
      begin
        App.fPID := fProcess.th32ProcessID;
        App.fPIDName := fProcess.szExeFile;
        App.fDLLName.Add(fModule.szModule);
        App.fDLLPath.Add(fModule.szExePath);
        App.fDescription := App.fDescription +
          Format(fDesc, [fModule.szModule, fModule.szExePath, fModule.th32ModuleID]);
      end;
      IsDLL := Module32Next(fModHandle, fModule)
    end;
    if App.fDLLName.Count > 0 then
      Result.AddObject(IntToStr(App.fPID), App);
    IsProcess := Process32Next(fHandle, fProcess);
  end;
  CloseHandle(fHandle);
  Result.Count;
end;

end.

Caly moduł-na pewno dziala i pobiera dll (Tobie nie sa potrzebene dll tylko wszystko jak leci, wiec wywal odfiltrowanie po dll) [diabel]

0

Przepisałem tak jak w kodzie, utworzyłem niezbędne komponenty, takie same nazwy, aby wszystko grało...

i nie pobiera nic.

Nie wiem, jak ten kod może działać.
Poraz kolejny piszę, że w procedurze:

function TForm2.GetApps(AppName: string): TStringList;

Zmienna AppName, nie jest nigdy wykorzystana, a więc całość albo nie działa, albo nie wyświetla wyniku, bo nie ma skąd pobrać...

Jak masz gotowe źródło, z formą i skompilowane, to chętne zobacze, bo to co tu próbuje skompilować to niestety nie działa.
Wrzuć na rapida źródło z formatką.

1 użytkowników online, w tym zalogowanych: 0, gości: 1