Get the Windows Start Menu folder location and name (Views: 28)
Problem/Question/Abstract: How to get the Start Menu folder location in Windows regardless of the OS (WinNT Wks, WinNT Srv, Win95, Win98)? Answer: Solve 1: procedure FreePidl(pidl: PItemIDList); var allocator: IMalloc; begin if Succeeded(SHGetMalloc(allocator)) then begin allocator.Free(pidl); {$IFDEF VER90} allocator.Release; {$ENDIF} end; end; procedure TForm1.Button2Click(Sender: TObject); var pidl: PItemIDList; buf: array[0..MAX_PATH] of Char; begin if Succeeded(ShGetSpecialFolderLocation(Handle, CSIDL_STARTMENU, pidl)) then begin if ShGetPathfromIDList(pidl, buf) then ShowMessage(buf); FreePIDL(pidl); end; end; Needs ShlObj in the Uses clause. Solve 2: To obtain the name of the Windows\Start Menu\Program directory on a system, use code like this: function GetSpecialDir(Index: Integer): string; var S: string; IDL: PItemIDList; begin Result := ''; if Succeeded(SHGetSpecialFolderLocation(0, Index, IDL)) then begin SetLength(S, MAX_PATH); if Succeeded(SHGetPathFromIDList(IDL, PChar(S))) then Result := PChar(S); end; end; You call it with code like: ProgDir := GetSpecialDir(CSIDL_PROGRAMS); The CSIDL_ identifiers are specified in ShlObj.pas, so are the SHGetSpecialFolderLocation and SHGetPathFromIDList functions. Solve 3: Here is an example to get the startup folder: { ... } var idRoot: PItemIDList; Buf: array[1..MAX_PATH] of Char; begin StartupFolder := ''; try if SHGetSpecialFolderLocation(Handle, CSIDL_STARTUP, idRoot) = NOERROR then begin FillChar(Buf, SizeOf(Buf), #32); SHGetPathFromIDList(idRoot, PChar(@Buf)); SetString(StartupFolder, PChar(@Buf), Length(Buf)); StartupFolder := Trim(StartupFolder) + '\'; end; except end; end; { ... } |