PIDL.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. CF_PREFERREDDROPEFFECT: UINT;
  46. implementation
  47. uses
  48. SysUtils, CompThread, OperationWithTimeout;
  49. const NullTerm=2;
  50. function PIDL_GetNextItem(PIDL: PItemIDList): PItemIDList;
  51. // PURPOSE: Returns a pointer to the next item in the ITEMIDLIST.
  52. // PARAMETERS:
  53. // pidl - Pointer to an ITEMIDLIST to walk through
  54. begin
  55. if PIDL<>nil then Result := PItemIDList(PAnsiChar(PIDL) + PIDL^.mkid.cb)
  56. else Result := nil;
  57. end;
  58. function PIDL_GetSize(PIDL: PITEMIDLIST): Integer;
  59. // PURPOSE: Returns the total number of bytes in an ITEMIDLIST.
  60. // PARAMETERS:
  61. // pidl - Pointer to the ITEMIDLIST that you want the size of.
  62. begin
  63. Result := 0;
  64. if PIDL <> nil then
  65. begin
  66. Inc(Result, SizeOf(PIDL^.mkid.cb));
  67. while PIDL^.mkid.cb <> 0 do
  68. begin
  69. Inc(Result, PIDL^.mkid.cb);
  70. Inc(LongInt(PIDL), PIDL^.mkid.cb);
  71. end;
  72. end;
  73. end;
  74. function PIDL_Create(Size: UINT): PItemIDList;
  75. // PURPOSE: Creates a new ITEMIDLIST of the specified size.
  76. // PARAMETERS:
  77. // piMalloc - Pointer to the allocator interface that should allocate memory.
  78. // cbSize - Size of the ITEMIDLIST to create.
  79. // RETURN VALUE:
  80. // Returns a pointer to the new ITEMIDLIST, or NULL if a problem occured.
  81. begin
  82. Result := ShellMalloc.Alloc(Size);
  83. if Result <> nil then
  84. FillChar(Result^, Size, #0);
  85. end;
  86. function PIDL_Concatenate(PIDL1, PIDL2: PItemIDList): PItemIDList;
  87. // PURPOSE: Creates a new ITEMIDLIST with pidl2 appended to pidl1.
  88. // PARAMETERS:
  89. // piMalloc - Pointer to the allocator interface that should create the new ITEMIDLIST.
  90. // pidl1- Pointer to an ITEMIDLIST that contains the root.
  91. // pidl2 - Pointer to an ITEMIDLIST that contains what should be appended to the root.
  92. // RETURN VALUE:
  93. // Returns a new ITEMIDLIST if successful, NULL otherwise.
  94. var
  95. cb1, cb2: UINT;
  96. begin
  97. if (PIDL1 <> nil) then cb1 := PIDL_GetSize(PIDL1) - NullTerm else cb1 := 0;
  98. cb2 := PIDL_GetSize(PIDL2);
  99. Result := PIDL_Create(cb1 + cb2);
  100. if Result <> nil then
  101. begin
  102. if PIDL1 <> nil then CopyMemory(Result, PIDL1, cb1);
  103. CopyMemory(PAnsiChar(Result) + cb1, PIDL2, cb2);
  104. end;
  105. end;
  106. function PIDL_Copy(PIDLSource: PItemIDList): PItemIDList;
  107. // PURPOSE: Creates a new copy of an ITEMIDLIST.
  108. // PARAMETERS:
  109. // piMalloc - Pointer to the allocator interfaced to be used to allocate the new ITEMIDLIST.
  110. // RETURN VALUE:
  111. // Returns a pointer to the new ITEMIDLIST, or NULL if an error occurs.
  112. var
  113. cbSource: UINT;
  114. begin
  115. Result := nil;
  116. if pidlSource = nil then Exit;
  117. cbSource := PIDL_GetSize(PIDLSource);
  118. Result := PIDL_Create(cbSource);
  119. if Result = nil then Exit;
  120. CopyMemory(Result, PIDLSource, cbSource);
  121. end;
  122. function PIDL_GetDisplayName(piFolder: IShellFolder; PIDL: PItemIDList;
  123. dwFlags: DWORD; pszName: PChar; cchMax: UINT): Boolean;
  124. // PURPOSE: Returns the display name for the item pointed to by pidl. The
  125. // function assumes the pidl is relative to piFolder. If piFolder
  126. // is NULL, the function assumes the item is fully qualified.
  127. // PARAMETERS:
  128. // piFolder - Pointer to the IShellFolder for the folder containing the item.
  129. // pidl - Pointer to an ITEMIDLIST relative to piFolder that we want
  130. // the display name for.
  131. // dwFlags - Flags to pass to ISF::GetDisplayNameOf().
  132. // pszName - Pointer to the string where the display name is returned.
  133. // cchMax - Maximum number of characters in pszName.
  134. // RETURN VALUE:
  135. // Returns TRUE if successful, FALSE otherwise.
  136. var
  137. Str: TStrRet;
  138. begin
  139. if (piFolder = nil) and (Failed(SHGetDesktopFolder(piFolder))) then
  140. begin
  141. Result := False;
  142. Exit;
  143. end;
  144. Result := True;
  145. if piFolder.GetDisplayNameOf(PIDL, dwFlags, Str) = NOERROR then
  146. begin
  147. case Str.uType of
  148. STRRET_WSTR:
  149. lstrcpyn(pszName, str.pOleStr, cchMax);
  150. STRRET_OFFSET:
  151. MultiByteToWideChar(CP_ACP, 0, PAnsiChar(PIDL) + str.uOffset, -1, pszName, cchMax);
  152. STRRET_CSTR:
  153. MultiByteToWideChar(CP_ACP, 0, str.cStr, -1, pszName, cchMax);
  154. else
  155. Result := False;
  156. end;
  157. end
  158. else Result := False;
  159. // piFolder._Release; -> automaticly done by D4
  160. end;
  161. procedure PIDL_GetRelative(var pidlFQ, PPIDLRoot, PPIDLItem: PItemIDList);
  162. // PURPOSE: Takes a fully qualified pidl and returns the the relative pidl
  163. // and the root part of that pidl.
  164. // PARAMETERS:
  165. // pidlFQ - Pointer to the fully qualified ITEMIDLIST that needs to be parsed.
  166. // pidlRoot - Points to the pidl that will contain the root after parsing.
  167. // pidlItem - Points to the item relative to pidlRoot after parsing.
  168. var
  169. PIDLTemp, PIDLNext: PItemIDList;
  170. begin
  171. if PIDLFQ = nil then
  172. begin
  173. PPIDLRoot := nil;
  174. PPIDLItem := nil;
  175. Exit;
  176. end;
  177. PPIDLItem := nil;
  178. PPIDLRoot := PIDL_Copy(PIDLFQ);
  179. PIDLTemp := PPIDLRoot;
  180. while PIDLTemp^.mkid.cb>0 do
  181. begin
  182. PIDLNext := PIDL_GetNextItem(PIDLTemp);
  183. if PIDLNext^.mkid.cb = 0 then
  184. begin
  185. PPIDLItem := PIDL_Copy(PIDLTemp);
  186. PIDLTemp^.mkid.cb := 0;
  187. PIDLTemp^.mkid.abID[0] := 0;
  188. end;
  189. PIDLTemp := PIDLNext;
  190. end;
  191. end;
  192. function PIDL_GetFromPath(pszFile: PChar): PItemIDList;
  193. // PURPOSE: This routine takes a full path to a file and converts that
  194. // to a fully qualified ITEMIDLIST.
  195. // PARAMETERS:
  196. // pszFile - Full path to the file.
  197. // RETURN VALUE:
  198. // Returns a fully qualified ITEMIDLIST, or NULL if an error occurs.
  199. var
  200. piDesktop: IShellFolder;
  201. ulEaten, ulAttribs: ULong;
  202. begin
  203. Result := nil;
  204. if Failed(SHGetDesktopFolder(piDesktop)) then Exit;
  205. piDesktop._AddRef;
  206. ulAttribs := 0;
  207. if Failed(piDesktop.ParseDisplayName(0, nil, pszFile, ulEaten, Result, ulAttribs)) then Result := nil;
  208. // piDesktop._Release; -> automaticly done by D4
  209. end;
  210. function PIDL_GetFileFolder(PIDL: PItemIDList; var piFolder: IShellFolder): Boolean;
  211. // PURPOSE: This routine takes a fully qualified pidl for a folder and returns
  212. // the IShellFolder pointer for that pidl
  213. // PARAMETERS:
  214. // pidl - Pointer to a fully qualified ITEMIDLIST for the folder
  215. // piParentFolder - Pointer to the IShellFolder of the folder (Return value).
  216. // RETURN VALUE:
  217. // Returns TRUE if successful, FALSE otherwise.
  218. var
  219. piDesktopFolder: IShellFolder;
  220. begin
  221. Result:=false;
  222. if Failed(SHGetDesktopFolder(piDesktopFolder)) then Exit;
  223. if (not Assigned(PiFolder)) and Failed(SHGetDesktopFolder(PiFolder)) then Exit;
  224. if not Failed(piDesktopFolder.BindToObject(PIDL, nil, IID_IShellFolder, Pointer(PiFolder))) then Result := True;
  225. //piDesktopFolder._Release; -> automaticly done by D4
  226. end;
  227. function PIDL_GetFromParentFolder(pParentFolder: IShellFolder; pszFile: PChar): PItemIDList;
  228. // PURPOSE: This routine takes a Shell folder for the parent and the FileName in the folder
  229. // and converts that to a relative ITEMIDLIST.
  230. // PARAMETERS:
  231. // pParentFolder - Pointer to the IShellFolder for the folder containing the
  232. // fileName.
  233. // pszFile - file name in the folder.
  234. // RETURN VALUE:
  235. // Returns a relative ITEMIDLIST, or NULL if an error occurs.
  236. var
  237. Eaten: ULONG;
  238. ShAttr: ULONG;
  239. begin
  240. ShellFolderParseDisplayNameWithTimeout(pParentFolder, 0, nil, pszFile, Eaten, Result, ShAttr, 2 * MSecsPerSec);
  241. end;
  242. procedure PIDL_Free(PIDL: PItemIDList);
  243. begin
  244. if PIDL <> nil then
  245. ShellMalloc.Free(PIDL);
  246. end;
  247. function PIDL_Equal(PIDL1,PIDL2: PItemIDList): Boolean;
  248. var
  249. I, Size: Integer;
  250. P1, P2: PChar;
  251. begin
  252. Result := False;
  253. if (PIDL1 = nil) or (PIDL2 = nil) then Exit;
  254. Size := PIDL_GetSize(PIDL1);
  255. if Size <> PIDL_GetSize(PIDL2) then Exit;
  256. I := 0;
  257. P1 := PChar(PIDL1);
  258. P2 := PChar(PIDL2);
  259. while I < Size do
  260. begin
  261. if P1[I] <> P2[I] then Exit
  262. else Inc(I);
  263. end;
  264. Result := True;
  265. end;
  266. initialization
  267. SHGetMalloc(ShellMalloc);
  268. CF_FILENAMEMAP := RegisterClipboardFormat('FileNameMap');
  269. CF_FILENAMEMAPW := RegisterClipboardFormat('FileNameMapW');
  270. CF_SHELLIDLIST := RegisterClipboardFormat('Shell IDList Array');
  271. CF_PREFERREDDROPEFFECT := RegisterClipboardFormat('Preferred DropEffect');
  272. finalization
  273. // ShellMalloc._Release; -> automaticly done by D4
  274. end.