windirs.pp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. unit windirs;
  2. {*******************************************************************************
  3. IMPORTANT NOTES:
  4. SHGetFolderPath function is deprecated. Only some CSIDL values are supported.
  5. As of Windows Vista, this function is merely a wrapper for SHGetKnownFolderPath.
  6. The CSIDL value is translated to its associated KNOWNFOLDERID and then SHGetKnownFolderPath
  7. is called. New applications should use the known folder system rather than the older
  8. CSIDL system, which is supported only for backward compatibility.
  9. *******************************************************************************}
  10. {$mode objfpc}
  11. {$H+}
  12. interface
  13. uses
  14. windows;
  15. // CSIDL_* contants are also declared in "ShellApi" and "shfolder" units.
  16. const
  17. CSIDL_PROGRAMS = $0002; { %SYSTEMDRIVE%\Program Files }
  18. CSIDL_PERSONAL = $0005; { %USERPROFILE%\My Documents }
  19. CSIDL_FAVORITES = $0006; { %USERPROFILE%\Favorites }
  20. CSIDL_STARTUP = $0007; { %USERPROFILE%\Start menu\Programs\Startup }
  21. CSIDL_RECENT = $0008; { %USERPROFILE%\Recent }
  22. CSIDL_SENDTO = $0009; { %USERPROFILE%\Sendto }
  23. CSIDL_STARTMENU = $000B; { %USERPROFILE%\Start menu }
  24. CSIDL_MYMUSIC = $000D; { %USERPROFILE%\Documents\My Music }
  25. CSIDL_MYVIDEO = $000E; { %USERPROFILE%\Documents\My Videos }
  26. CSIDL_DESKTOPDIRECTORY = $0010; { %USERPROFILE%\Desktop }
  27. CSIDL_NETHOOD = $0013; { %USERPROFILE%\NetHood }
  28. CSIDL_TEMPLATES = $0015; { %USERPROFILE%\Templates }
  29. CSIDL_COMMON_STARTMENU = $0016; { %PROFILEPATH%\All users\Start menu }
  30. CSIDL_COMMON_PROGRAMS = $0017; { %PROFILEPATH%\All users\Start menu\Programs }
  31. CSIDL_COMMON_STARTUP = $0018; { %PROFILEPATH%\All users\Start menu\Programs\Startup }
  32. CSIDL_COMMON_DESKTOPDIRECTORY = $0019; { %PROFILEPATH%\All users\Desktop }
  33. CSIDL_APPDATA = $001A; { %USERPROFILE%\Application Data (roaming) }
  34. CSIDL_PRINTHOOD = $001B; { %USERPROFILE%\Printhood }
  35. CSIDL_LOCAL_APPDATA = $001C; { %USERPROFILE%\Local Settings\Application Data (non roaming) }
  36. CSIDL_COMMON_FAVORITES = $001F; { %PROFILEPATH%\All users\Favorites }
  37. CSIDL_INTERNET_CACHE = $0020; { %USERPROFILE%\Local Settings\Temporary Internet Files }
  38. CSIDL_COOKIES = $0021; { %USERPROFILE%\Cookies }
  39. CSIDL_HISTORY = $0022; { %USERPROFILE%\Local settings\History }
  40. CSIDL_COMMON_APPDATA = $0023; { %PROFILESPATH%\All Users\Application Data }
  41. CSIDL_WINDOWS = $0024; { %SYSTEMROOT% }
  42. CSIDL_SYSTEM = $0025; { %SYSTEMROOT%\SYSTEM32 (may be system on 95/98/ME) }
  43. CSIDL_PROGRAM_FILES = $0026; { %SYSTEMDRIVE%\Program Files }
  44. CSIDL_MYPICTURES = $0027; { %USERPROFILE%\My Documents\My Pictures }
  45. CSIDL_PROFILE = $0028; { %USERPROFILE% }
  46. CSIDL_PROGRAM_FILES_COMMON = $002B; { %SYSTEMDRIVE%\Program Files\Common }
  47. CSIDL_COMMON_TEMPLATES = $002D; { %PROFILEPATH%\All Users\Templates }
  48. CSIDL_COMMON_DOCUMENTS = $002E; { %PROFILEPATH%\All Users\Documents }
  49. CSIDL_COMMON_ADMINTOOLS = $002F; { %PROFILEPATH%\All Users\Start Menu\Programs\Administrative Tools }
  50. CSIDL_ADMINTOOLS = $0030; { %USERPROFILE%\Start Menu\Programs\Administrative Tools }
  51. CSIDL_COMMON_MUSIC = $0035; { %PROFILEPATH%\All Users\Documents\my music }
  52. CSIDL_COMMON_PICTURES = $0036; { %PROFILEPATH%\All Users\Documents\my pictures }
  53. CSIDL_COMMON_VIDEO = $0037; { %PROFILEPATH%\All Users\Documents\my videos }
  54. CSIDL_CDBURN_AREA = $003B; { %USERPROFILE%\Local Settings\Application Data\Microsoft\CD Burning }
  55. CSIDL_PROFILES = $003E; { %PROFILEPATH% }
  56. CSIDL_FLAG_CREATE = $8000; { (force creation of requested folder if it doesn't exist yet) }
  57. function GetWindowsSpecialDir(ID: Integer; CreateIfNotExists: Boolean = True): String;
  58. function GetWindowsSpecialDirUnicode(ID: Integer; CreateIfNotExists: Boolean = True): UnicodeString;
  59. function GetWindowsSystemDirectory: String;
  60. function GetWindowsSystemDirectoryUnicode: UnicodeString;
  61. implementation
  62. uses
  63. sysutils;
  64. type
  65. // HRESULT SHGetFolderPath(
  66. // _In_ HWND hwndOwner,
  67. // _In_ int nFolder,
  68. // _In_ HANDLE hToken,
  69. // _In_ DWORD dwFlags,
  70. // _Out_ LPTSTR pszPath
  71. // );
  72. TSHGetFolderPathW = function(Ahwnd: HWND; Csidl: Integer; Token: THandle;
  73. Flags: DWORD; Path: PWideChar): HRESULT; stdcall;
  74. const
  75. SSHGetFolderPathW = 'SHGetFolderPathW';
  76. SLibName = 'shell32.dll';
  77. var
  78. _SHGetFolderPathW : TSHGetFolderPathW = nil;
  79. DLLHandle: THandle = 0;
  80. procedure InitDLL;
  81. var
  82. DLLPath: UnicodeString;
  83. begin
  84. if DLLHandle = 0 then
  85. begin
  86. // Load DLL using a full path, in order to prevent spoofing (Mantis #18185)
  87. DLLPath := GetWindowsSystemDirectoryUnicode;
  88. if Length(DLLPath) > 0 then
  89. begin
  90. DLLPath := IncludeTrailingPathDelimiter(DLLPath) + SLibName;
  91. DLLHandle := LoadLibraryW(PWideChar(DLLPath));
  92. if DLLHandle <> 0 then
  93. Pointer(_SHGetFolderPathW) := GetProcAddress(DLLHandle, SSHGetFolderPathW);
  94. end;
  95. end;
  96. if @_SHGetFolderPathW = nil then
  97. raise Exception.Create('Could not locate SHGetFolderPath function');
  98. end;
  99. procedure FinitDLL;
  100. begin
  101. if DLLHandle <> 0 then
  102. begin
  103. FreeLibrary(DLLHandle);
  104. DLLHandle := 0;
  105. end;
  106. end;
  107. function GetWindowsSystemDirectoryUnicode: UnicodeString;
  108. var
  109. Buffer: array [0..MAX_PATH] of WideChar;
  110. CharCount: Integer;
  111. begin
  112. CharCount := GetSystemDirectoryW(@Buffer[0], MAX_PATH);
  113. // CharCount is length in TCHARs not including the terminating null character.
  114. // If result did not fit, CharCount will be bigger than buffer size.
  115. if (CharCount > 0) and (CharCount < MAX_PATH) then
  116. Result := StrPas(Buffer)
  117. else
  118. Result := '';
  119. end;
  120. function GetWindowsSystemDirectory: String;
  121. begin
  122. Result := String(GetWindowsSystemDirectoryUnicode);
  123. end;
  124. function GetWindowsSpecialDirUnicode(ID: Integer; CreateIfNotExists: Boolean = True): UnicodeString;
  125. var
  126. Buffer: array [0..MAX_PATH] of WideChar;
  127. begin
  128. InitDLL;
  129. Result := '';
  130. if CreateIfNotExists then
  131. ID := ID or CSIDL_FLAG_CREATE;
  132. if _SHGetFolderPathW(0, ID, 0, 0, @Buffer[0]) = S_OK then
  133. Result := IncludeTrailingPathDelimiter(StrPas(Buffer));
  134. end;
  135. function GetWindowsSpecialDir(ID: Integer; CreateIfNotExists: Boolean = True): String;
  136. begin
  137. Result := String(GetWindowsSpecialDirUnicode(ID, CreateIfNotExists));
  138. end;
  139. finalization
  140. FinitDLL;
  141. end.