Recent

Author Topic: Extract path from active explorer window  (Read 1079 times)

LemonParty

  • Hero Member
  • *****
  • Posts: 563
Extract path from active explorer window
« on: November 04, 2025, 02:33:12 pm »
Hello.

Does anybody have a code to extract path from active explorer window?
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

Thaddy

  • Hero Member
  • *****
  • Posts: 19488
  • Glad to be alive.
Re: Extract path from active explorer window
« Reply #1 on: November 04, 2025, 03:09:20 pm »
Outside or inside the program?
From the TOpenDialog (which encapsulates the explorer) it is simply reading the property Filename....
For the explorer itself (windows assumed) you need the COM library for explorer, which you can generate from the explorer itself.
« Last Edit: November 04, 2025, 03:14:00 pm by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

LemonParty

  • Hero Member
  • *****
  • Posts: 563
Re: Extract path from active explorer window
« Reply #2 on: November 04, 2025, 03:25:44 pm »
I guess it is a second variant. I have an explorer window (active) and I want to extract the location on where this window is opened. And you right it needs COM library.
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

Thaddy

  • Hero Member
  • *****
  • Posts: 19488
  • Glad to be alive.
Re: Extract path from active explorer window
« Reply #3 on: November 04, 2025, 04:13:00 pm »
You can import that from explorer.exe.
Any "programmer" that knows only one programming language is not a programmer

LemonParty

  • Hero Member
  • *****
  • Posts: 563
Re: Extract path from active explorer window
« Reply #4 on: November 05, 2025, 03:16:13 pm »
Here is a code in C#:
Code: C  [Select][+][-]
  1.     IntPtr MyHwnd = FindWindow(null, "Directory");
  2.     var t = Type.GetTypeFromProgID("Shell.Application");
  3.     dynamic o = Activator.CreateInstance(t);
  4.     try
  5.     {
  6.         var ws = o.Windows();
  7.         for (int i = 0; i < ws.Count; i++)
  8.         {
  9.             var ie = ws.Item(i);
  10.             if (ie == null || ie.hwnd != (long)MyHwnd) continue;
  11.             var path = System.IO.Path.GetFileName((string)ie.FullName);
  12.             if (path.ToLower() == "explorer.exe")
  13.             {
  14.                 var explorepath = ie.document.focuseditem.path;
  15.             }
  16.         }
  17.     }
  18.     finally
  19.     {
  20.         Marshal.FinalReleaseComObject(o);
  21.     }
Can someone help to translate it to Pascal?
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

paweld

  • Hero Member
  • *****
  • Posts: 1685
Re: Extract path from active explorer window
« Reply #5 on: November 06, 2025, 05:56:59 am »
Code: Pascal  [Select][+][-]
  1. uses
  2.   Variants, Windows, ActiveX, ComObj, ShlObj;
  3.  
  4. procedure TForm1.Button1Click(Sender: TObject);
  5. var
  6.   ShellApp: Variant;
  7.   WS: Variant;
  8.   IE: Variant;
  9.   Path, ExplorePath: WideString;
  10.   i: Integer;
  11. begin
  12.   ShellApp := CreateOleObject('Shell.Application');
  13.   try
  14.     WS := ShellApp.Windows;
  15.     for i := 0 to WS.Count - 1 do
  16.     begin
  17.       IE := WS.Item(i);
  18.       if VarIsNull(IE) then
  19.         continue;
  20.       Path := ExtractFileName(WideString(IE.FullName));
  21.       if LowerCase(Path) = 'explorer.exe' then
  22.       begin
  23.         ExplorePath := IE.Document.Folder.Self.Path; //current directory or selected object (file/dir): IE.Document.FocusedItem.Path;
  24.         Memo1.Lines.Add(ExplorePath);
  25.       end;
  26.     end;
  27.   finally
  28.     ShellApp := Unassigned;
  29.   end;
  30. end;          
Best regards / Pozdrawiam
paweld

LemonParty

  • Hero Member
  • *****
  • Posts: 563
Re: Extract path from active explorer window
« Reply #6 on: November 06, 2025, 09:41:26 am »
Thank you very much.

Here is checked function, that extract path from active window:
Code: Pascal  [Select][+][-]
  1. function GetActivePath: String;
  2. {$IfDef Windows}
  3. var
  4.   H: THandle;
  5.   ShellApp: Variant;
  6.   WS: Variant;
  7.   IE: Variant;
  8.   Path, ExplorePath: WideString;
  9.   i: Integer;
  10. begin
  11.   Result:= '';
  12.   H:= GetForegroundWindow;
  13.   ShellApp := CreateOleObject('Shell.Application');
  14.   try
  15.     WS := ShellApp.Windows;
  16.     for i := 0 to WS.Count - 1 do begin
  17.       IE := WS.Item(i);
  18.       if VarIsNull(IE) then
  19.         continue;
  20.       Path := ExtractFileName(WideString(IE.FullName));
  21.       if (LowerCase(Path) = 'explorer.exe') and (H = IE.hwnd) then begin
  22.         ExplorePath := IE.Document.Folder.Self.Path; //current directory or selected object (file/dir): IE.Document.FocusedItem.Path;
  23.         Result:= UTF8Encode( ExplorePath );
  24.         Break;
  25.       end;
  26.     end;
  27.   finally
  28.     ShellApp := Unassigned;
  29.   end;
  30. end;
  31. {$Else}
  32. begin
  33.  
  34. end;
  35. {$EndIf}
« Last Edit: November 06, 2025, 09:44:15 am by LemonParty »
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

 

TinyPortal © 2005-2018