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 procedure FreePidl(pidl: PItemIDList);
2 var
3 allocator: IMalloc;
4 begin
5 if Succeeded(SHGetMalloc(allocator)) then
6 begin
7 allocator.Free(pidl);
8 {$IFDEF VER90}
9 allocator.Release;
10 {$ENDIF}
11 end;
12 end;
13
14 procedure TForm1.Button2Click(Sender: TObject);
15 var
16 pidl: PItemIDList;
17 buf: array[0..MAX_PATH] of Char;
18 begin
19 if Succeeded(ShGetSpecialFolderLocation(Handle, CSIDL_STARTMENU, pidl)) then
20 begin
21 if ShGetPathfromIDList(pidl, buf) then
22 ShowMessage(buf);
23 FreePIDL(pidl);
24 end;
25 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:
26
27 function GetSpecialDir(Index: Integer): string;
28 var
29 S: string;
30 IDL: PItemIDList;
31 begin
32 Result := '';
33 if Succeeded(SHGetSpecialFolderLocation(0, Index, IDL)) then
34 begin
35 SetLength(S, MAX_PATH);
36 if Succeeded(SHGetPathFromIDList(IDL, PChar(S))) then
37 Result := PChar(S);
38 end;
39 end;
You call it with code like:
40
41 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:
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 { ... }
|