How to get FileName and Directory of Windows Control Panels?

351 Views Asked by At

In Delphi, create a VCL Forms Application. Use the 64-bit Windows platform if you are on a 64-bit Windows.

Use the following code:

unit Unit1;

interface

uses
  CodeSiteLogging,
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    procedure GetControlPanelItems;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses
  Winapi.ShlObj, Winapi.ShellAPI, System.Win.ComObj;

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  GetControlPanelItems;
end;

procedure TForm1.GetControlPanelItems;
var
  psfDeskTop: IShellFolder;
  psfControl: IShellFolder;
  pidControl: PITEMIDLIST;
  pidChild: PITEMIDLIST;
  pidAbsolute: PItemIdList;
  pEnumList: IEnumIDList;
  celtFetched: ULONG;
  FileInfo: SHFILEINFOW;
  ShExeInfo: SHELLEXECUTEINFO;
begin
  OleCheck(SHGetDesktopFolder(psfDeskTop));
  OleCheck(SHGetSpecialFolderLocation(0, CSIDL_CONTROLS, pidControl));
  OleCheck(psfDeskTop.BindToObject(pidControl, nil, IID_IShellFolder, psfControl));
  OleCheck(psfControl.EnumObjects(0, SHCONTF_NONFOLDERS or SHCONTF_INCLUDEHIDDEN or SHCONTF_FOLDERS, pEnumList));

  while pEnumList.Next(1, pidChild, celtFetched) = 0 do
  begin
    pidAbsolute := ILCombine(pidControl, pidChild);
    SHGetFileInfo(LPCTSTR(pidAbsolute), 0, FileInfo, SizeOf(FileInfo), SHGFI_PIDL or SHGFI_DISPLAYNAME or SHGFI_TYPENAME);

    CodeSite.Send('TForm1.GetControlPanelItems: szDisplayName', FileInfo.szDisplayName);

    // Exe-Info:
    ZeroMemory(@ShExeInfo, SizeOf(ShExeInfo));
    ShExeInfo.cbSize := SizeOf(ShExeInfo);
    ShExeInfo.lpVerb := 'Open';
    // control panel item's PIDL:
    ShExeInfo.lpIDList := pidAbsolute;
    ShExeInfo.nShow := SW_SHOWNORMAL;
    ShExeInfo.fMask := SEE_MASK_IDLIST;
    //ShExeInfo.lpFile := ???
    //ShExeInfo.lpDirectory := ???

    CodeSite.Send('TForm1.GetControlPanelItems: ShExeInfo.lpFile', ShExeInfo.lpFile);
    CodeSite.Send('TForm1.GetControlPanelItems: ShExeInfo.lpDirectory', ShExeInfo.lpDirectory);
  end;
end;

end.

This gets me the Display names of the control panels.

But how can I get the file-paths? (ShExeInfo.lpDirectory, ShExeInfo.lpFile)

1

There are 1 best solutions below

1
Victoria On

As others mentioned here, it might be worthless to try getting file name of a certain applet binary as there can be more than one applet implemented in a single binary. For your overall task, dropped in your comment, creating shell shortcut links, simply use absolute ITEMIDLIST that you know in your loop, and set it to the created IShellLink object by the SetIDList method.