Author: Jonas Bilinkevicius
How to get the Start Menu folder location in Windows regardless of the OS (WinNT
Wks, WinNT Srv, Win95, Win98)?
Answer:
Solve 1:
1
2 procedure FreePidl(pidl: PItemIDList);
3 var
4 allocator: IMalloc;
5 begin
6 if Succeeded(SHGetMalloc(allocator)) then
7 begin
8 allocator.Free(pidl);
9 {$IFDEF VER90}
10 allocator.Release;
11 {$ENDIF}
12 end;
13 end;
14
15 procedure TForm1.Button2Click(Sender: TObject);
16 var
17 pidl: PItemIDList;
18 buf: array[0..MAX_PATH] of Char;
19 begin
20 if Succeeded(ShGetSpecialFolderLocation(Handle, CSIDL_STARTMENU, pidl)) then
21 begin
22 if ShGetPathfromIDList(pidl, buf) then
23 ShowMessage(buf);
24 FreePIDL(pidl);
25 end;
26 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:
27
28 function GetSpecialDir(Index: Integer): string;
29 var
30 S: string;
31 IDL: PItemIDList;
32 begin
33 Result := '';
34 if Succeeded(SHGetSpecialFolderLocation(0, Index, IDL)) then
35 begin
36 SetLength(S, MAX_PATH);
37 if Succeeded(SHGetPathFromIDList(IDL, PChar(S))) then
38 Result := PChar(S);
39 end;
40 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:
41
42 { ... }
43 var
44 idRoot: PItemIDList;
45 Buf: array[1..MAX_PATH] of Char;
46 begin
47 StartupFolder := '';
48 try
49 if SHGetSpecialFolderLocation(Handle, CSIDL_STARTUP, idRoot) = NOERROR then
50 begin
51 FillChar(Buf, SizeOf(Buf), #32);
52 SHGetPathFromIDList(idRoot, PChar(@Buf));
53 SetString(StartupFolder, PChar(@Buf), Length(Buf));
54 StartupFolder := Trim(StartupFolder) + '\';
55 end;
56 except
57 end;
58 end;
59 { ... }
|