PIDL.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. unit PIDL;
  2. {
  3. Description
  4. ===========
  5. Some methods to pidls. Purpose of methods are described in the code.
  6. Disclaimer
  7. ==========
  8. The author disclaims all warranties, expressed or implied, including,
  9. without limitation, the warranties of merchantability and of fitness
  10. for any purpose. The author assumes no liability for damages, direct or
  11. consequential, which may result from the use of this unit.
  12. Restrictions on Using the Unit
  13. ==============================
  14. This unit is copyright 1998 by Dieter Steinwedel. ALL RIGHTS
  15. ARE RESERVED BY DIETER STEINWEDEL. You are allowed to use it freely
  16. subject to the following restrictions:
  17. • You are not allowed delete or alter the author's name and
  18. copyright in any manner
  19. • You are not allowed to publish a copy, modified version or
  20. compilation neither for payment in any kind nor freely
  21. • You are allowed to create a link to the download in the WWW
  22. • These restrictions and terms apply to you as long as until
  23. I alter them. Changes can found on my homepage
  24. }
  25. interface
  26. uses
  27. ShlObj, Windows, ActiveX;
  28. function PIDL_GetSize(PIDL: PITEMIDLIST): Integer;
  29. function PIDL_Create(Size: UINT): PItemIDList;
  30. function PIDL_Concatenate(PIDL1, PIDL2: PItemIDList): PItemIDList;
  31. function PIDL_Copy(PIDLSource: PItemIDList): PItemIDList;
  32. function PIDL_GetDisplayName(piFolder: IShellFolder; PIDL: PItemIDList;
  33. dwFlags: DWORD; pszName: PChar; cchMax: UINT): Boolean;
  34. procedure PIDL_GetRelative(var PIDLFQ, PPIDLRoot, PPIDLItem: PItemIDList);
  35. function PIDL_GetFromPath(pszFile: PChar): PItemIDList;
  36. function PIDL_GetFileFolder(PIDL: PItemIDList; var piFolder: IShellFolder): Boolean;
  37. function PIDL_GetFromParentFolder(pParentFolder: IShellFolder; pszFile: PChar): PItemIDList;
  38. procedure PIDL_Free(PIDL: PItemIDList);
  39. function PIDL_Equal(PIDL1, PIDL2: PItemIDList): Boolean;
  40. var
  41. ShellMalloc: IMalloc;
  42. CF_FILENAMEMAP: UINT;
  43. CF_FILENAMEMAPW: UINT;
  44. CF_SHELLIDLIST: UINT;
  45. implementation
  46. uses
  47. SysUtils, CompThread, OperationWithTimeout;
  48. const NullTerm=2;
  49. function PIDL_GetNextItem(PIDL: PItemIDList): PItemIDList;
  50. // PURPOSE: Returns a pointer to the next item in the ITEMIDLIST.
  51. // PARAMETERS:
  52. // pidl - Pointer to an ITEMIDLIST to walk through
  53. begin
  54. if PIDL<>nil then Result := PItemIDList(PAnsiChar(PIDL) + PIDL^.mkid.cb)
  55. else Result := nil;
  56. end;
  57. function PIDL_GetSize(PIDL: PITEMIDLIST): Integer;
  58. // PURPOSE: Returns the total number of bytes in an ITEMIDLIST.
  59. // PARAMETERS:
  60. // pidl - Pointer to the ITEMIDLIST that you want the size of.
  61. begin
  62. Result := 0;
  63. if PIDL <> nil then
  64. begin
  65. Inc(Result, SizeOf(PIDL^.mkid.cb));
  66. while PIDL^.mkid.cb <> 0 do
  67. begin
  68. Inc(Result, PIDL^.mkid.cb);
  69. Inc(LongInt(PIDL), PIDL^.mkid.cb);
  70. end;
  71. end;
  72. end;
  73. function PIDL_Create(Size: UINT): PItemIDList;
  74. // PURPOSE: Creates a new ITEMIDLIST of the specified size.
  75. // PARAMETERS:
  76. // piMalloc - Pointer to the allocator interface that should allocate memory.
  77. // cbSize - Size of the ITEMIDLIST to create.
  78. // RETURN VALUE:
  79. // Returns a pointer to the new ITEMIDLIST, or NULL if a problem occured.
  80. begin
  81. Result := ShellMalloc.Alloc(Size);
  82. if Result <> nil then
  83. FillChar(Result^, Size, #0);
  84. end;
  85. function PIDL_Concatenate(PIDL1, PIDL2: PItemIDList): PItemIDList;
  86. // PURPOSE: Creates a new ITEMIDLIST with pidl2 appended to pidl1.
  87. // PARAMETERS:
  88. // piMalloc - Pointer to the allocator interface that should create the new ITEMIDLIST.
  89. // pidl1- Pointer to an ITEMIDLIST that contains the root.
  90. // pidl2 - Pointer to an ITEMIDLIST that contains what should be appended to the root.
  91. // RETURN VALUE:
  92. // Returns a new ITEMIDLIST if successful, NULL otherwise.
  93. var
  94. cb1, cb2: UINT;
  95. begin
  96. if (PIDL1 <> nil) then cb1 := PIDL_GetSize(PIDL1) - NullTerm else cb1 := 0;
  97. cb2 := PIDL_GetSize(PIDL2);
  98. Result := PIDL_Create(cb1 + cb2);
  99. if Result <> nil then
  100. begin
  101. if PIDL1 <> nil then CopyMemory(Result, PIDL1, cb1);
  102. CopyMemory(PAnsiChar(Result) + cb1, PIDL2, cb2);
  103. end;
  104. end;
  105. function PIDL_Copy(PIDLSource: PItemIDList): PItemIDList;
  106. // PURPOSE: Creates a new copy of an ITEMIDLIST.
  107. // PARAMETERS:
  108. // piMalloc - Pointer to the allocator interfaced to be used to allocate the new ITEMIDLIST.
  109. // RETURN VALUE:
  110. // Returns a pointer to the new ITEMIDLIST, or NULL if an error occurs.
  111. var
  112. cbSource: UINT;
  113. begin
  114. Result := nil;
  115. if pidlSource = nil then Exit;
  116. cbSource := PIDL_GetSize(PIDLSource);
  117. Result := PIDL_Create(cbSource);
  118. if Result = nil then Exit;
  119. CopyMemory(Result, PIDLSource, cbSource);
  120. end;
  121. function PIDL_GetDisplayName(piFolder: IShellFolder; PIDL: PItemIDList;
  122. dwFlags: DWORD; pszName: PChar; cchMax: UINT): Boolean;
  123. // PURPOSE: Returns the display name for the item pointed to by pidl. The
  124. // function assumes the pidl is relative to piFolder. If piFolder
  125. // is NULL, the function assumes the item is fully qualified.
  126. // PARAMETERS:
  127. // piFolder - Pointer to the IShellFolder for the folder containing the item.
  128. // pidl - Pointer to an ITEMIDLIST relative to piFolder that we want
  129. // the display name for.
  130. // dwFlags - Flags to pass to ISF::GetDisplayNameOf().
  131. // pszName - Pointer to the string where the display name is returned.
  132. // cchMax - Maximum number of characters in pszName.
  133. // RETURN VALUE:
  134. // Returns TRUE if successful, FALSE otherwise.
  135. var
  136. Str: TStrRet;
  137. begin
  138. if (piFolder = nil) and (Failed(SHGetDesktopFolder(piFolder))) then
  139. begin
  140. Result := False;
  141. Exit;
  142. end;
  143. Result := True;
  144. if piFolder.GetDisplayNameOf(PIDL, dwFlags, Str) = NOERROR then
  145. begin
  146. case Str.uType of
  147. STRRET_WSTR:
  148. lstrcpyn(pszName, str.pOleStr, cchMax);
  149. STRRET_OFFSET:
  150. MultiByteToWideChar(CP_ACP, 0, PAnsiChar(PIDL) + str.uOffset, -1, pszName, cchMax);
  151. STRRET_CSTR:
  152. MultiByteToWideChar(CP_ACP, 0, str.cStr, -1, pszName, cchMax);
  153. else
  154. Result := False;
  155. end;
  156. end
  157. else Result := False;
  158. // piFolder._Release; -> automaticly done by D4
  159. end;
  160. procedure PIDL_GetRelative(var pidlFQ, PPIDLRoot, PPIDLItem: PItemIDList);
  161. // PURPOSE: Takes a fully qualified pidl and returns the the relative pidl
  162. // and the root part of that pidl.
  163. // PARAMETERS:
  164. // pidlFQ - Pointer to the fully qualified ITEMIDLIST that needs to be parsed.
  165. // pidlRoot - Points to the pidl that will contain the root after parsing.
  166. // pidlItem - Points to the item relative to pidlRoot after parsing.
  167. var
  168. PIDLTemp, PIDLNext: PItemIDList;
  169. begin
  170. if PIDLFQ = nil then
  171. begin
  172. PPIDLRoot := nil;
  173. PPIDLItem := nil;
  174. Exit;
  175. end;
  176. PPIDLItem := nil;
  177. PPIDLRoot := PIDL_Copy(PIDLFQ);
  178. PIDLTemp := PPIDLRoot;
  179. while PIDLTemp^.mkid.cb>0 do
  180. begin
  181. PIDLNext := PIDL_GetNextItem(PIDLTemp);
  182. if PIDLNext^.mkid.cb = 0 then
  183. begin
  184. PPIDLItem := PIDL_Copy(PIDLTemp);
  185. PIDLTemp^.mkid.cb := 0;
  186. PIDLTemp^.mkid.abID[0] := 0;
  187. end;
  188. PIDLTemp := PIDLNext;
  189. end;
  190. end;
  191. function PIDL_GetFromPath(pszFile: PChar): PItemIDList;
  192. // PURPOSE: This routine takes a full path to a file and converts that
  193. // to a fully qualified ITEMIDLIST.
  194. // PARAMETERS:
  195. // pszFile - Full path to the file.
  196. // RETURN VALUE:
  197. // Returns a fully qualified ITEMIDLIST, or NULL if an error occurs.
  198. var
  199. piDesktop: IShellFolder;
  200. ulEaten, ulAttribs: ULong;
  201. begin
  202. Result := nil;
  203. if Failed(SHGetDesktopFolder(piDesktop)) then Exit;
  204. piDesktop._AddRef;
  205. ulAttribs := 0;
  206. if Failed(piDesktop.ParseDisplayName(0, nil, pszFile, ulEaten, Result, ulAttribs)) then Result := nil;
  207. // piDesktop._Release; -> automaticly done by D4
  208. end;
  209. function PIDL_GetFileFolder(PIDL: PItemIDList; var piFolder: IShellFolder): Boolean;
  210. // PURPOSE: This routine takes a fully qualified pidl for a folder and returns
  211. // the IShellFolder pointer for that pidl
  212. // PARAMETERS:
  213. // pidl - Pointer to a fully qualified ITEMIDLIST for the folder
  214. // piParentFolder - Pointer to the IShellFolder of the folder (Return value).
  215. // RETURN VALUE:
  216. // Returns TRUE if successful, FALSE otherwise.
  217. var
  218. piDesktopFolder: IShellFolder;
  219. begin
  220. Result:=false;
  221. if Failed(SHGetDesktopFolder(piDesktopFolder)) then Exit;
  222. if (not Assigned(PiFolder)) and Failed(SHGetDesktopFolder(PiFolder)) then Exit;
  223. if not Failed(piDesktopFolder.BindToObject(PIDL, nil, IID_IShellFolder, Pointer(PiFolder))) then Result := True;
  224. //piDesktopFolder._Release; -> automaticly done by D4
  225. end;
  226. function PIDL_GetFromParentFolder(pParentFolder: IShellFolder; pszFile: PChar): PItemIDList;
  227. // PURPOSE: This routine takes a Shell folder for the parent and the FileName in the folder
  228. // and converts that to a relative ITEMIDLIST.
  229. // PARAMETERS:
  230. // pParentFolder - Pointer to the IShellFolder for the folder containing the
  231. // fileName.
  232. // pszFile - file name in the folder.
  233. // RETURN VALUE:
  234. // Returns a relative ITEMIDLIST, or NULL if an error occurs.
  235. var
  236. Eaten: ULONG;
  237. ShAttr: ULONG;
  238. begin
  239. ShellFolderParseDisplayNameWithTimeout(pParentFolder, 0, nil, pszFile, Eaten, Result, ShAttr, 2 * MSecsPerSec);
  240. end;
  241. procedure PIDL_Free(PIDL: PItemIDList);
  242. begin
  243. if PIDL <> nil then
  244. ShellMalloc.Free(PIDL);
  245. end;
  246. function PIDL_Equal(PIDL1,PIDL2: PItemIDList): Boolean;
  247. var
  248. I, Size: Integer;
  249. P1, P2: PChar;
  250. begin
  251. Result := False;
  252. if (PIDL1 = nil) or (PIDL2 = nil) then Exit;
  253. Size := PIDL_GetSize(PIDL1);
  254. if Size <> PIDL_GetSize(PIDL2) then Exit;
  255. I := 0;
  256. P1 := PChar(PIDL1);
  257. P2 := PChar(PIDL2);
  258. while I < Size do
  259. begin
  260. if P1[I] <> P2[I] then Exit
  261. else Inc(I);
  262. end;
  263. Result := True;
  264. end;
  265. initialization
  266. SHGetMalloc(ShellMalloc);
  267. CF_FILENAMEMAP := RegisterClipboardFormat('FileNameMap');
  268. CF_FILENAMEMAPW := RegisterClipboardFormat('FileNameMapW');
  269. CF_SHELLIDLIST := RegisterClipboardFormat('Shell IDList Array');
  270. finalization
  271. // ShellMalloc._Release; -> automaticly done by D4
  272. end.