PIDL.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 ShlObj, Windows, ActiveX;
  27. function PIDL_GetNextItem(PIDL: PItemIDList):PItemIDList;
  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. function Pidl_GetFullyQualified(const PiParentFolder: IShellFolder;
  35. pidl: PItemIDList): PItemIDList;
  36. procedure PIDL_GetRelative(var pidlFQ, ppidlRoot, ppidlItem: PItemIDList);
  37. function PIDL_GetFromPath(pszFile: PChar): PItemIDList;
  38. function PIDL_GetFileFolder(pidl: PItemIDList; var piFolder: IShellFolder):boolean;
  39. function PIDL_GetFromParentFolder(pParentFolder: IShellFolder; pszFile: PChar): PItemIDList;
  40. procedure PIDL_Free(PIDL:PItemIDList);
  41. function PIDL_Equal(PIDL1,PIDL2:PItemIDList):boolean;
  42. var ShellMalloc: IMalloc;
  43. CF_FILECONTENTS:UInt; // don't modify value
  44. CF_FILEDESCRIPTOR:UInt; // don't modify value
  45. CF_FILENAME:UInt; // don't modify value
  46. CF_FILENAMEMAP:UInt; // don't modify value
  47. CF_FILENAMEMAPW:UInt; // don't modify value
  48. CF_INDRAGLOOP:UInt; // don't modify value
  49. CF_NETRESOURCES:UInt; // don't modify value
  50. CF_PASTESUCCEEDED:UInt; // don't modify value
  51. CF_PERFORMEDDROPEFFECT:UInt; // don't modify value
  52. CF_PREFERREDDROPEFFECT:UInt; // don't modify value
  53. CF_PRINTERGROUP:UInt; // don't modify value
  54. CF_SHELLIDLIST:UInt; // don't modify value
  55. CF_SHELLIDLISTOFFSET:UInt; // don't modify value
  56. CF_SHELLURL:UInt; // don't modify value
  57. implementation
  58. const NullTerm=2;
  59. function PIDL_GetNextItem(PIDL: PItemIDList):PItemIDList;
  60. // PURPOSE: Returns a pointer to the next item in the ITEMIDLIST.
  61. // PARAMETERS:
  62. // pidl - Pointer to an ITEMIDLIST to walk through
  63. begin
  64. if PIDL<>nil then Result:=PItemIDList(PAnsiChar(PIDL)+PIDL^.mkid.cb)
  65. else Result:=nil
  66. end;
  67. function PIDL_GetSize(pidl: PITEMIDLIST): integer;
  68. // PURPOSE: Returns the total number of bytes in an ITEMIDLIST.
  69. // PARAMETERS:
  70. // pidl - Pointer to the ITEMIDLIST that you want the size of.
  71. begin
  72. Result:=0;
  73. if pidl<>nil then
  74. begin
  75. Inc(Result, SizeOf(pidl^.mkid.cb));
  76. while pidl^.mkid.cb <> 0 do
  77. begin
  78. Inc(Result, pidl^.mkid.cb);
  79. Inc(longint(pidl), pidl^.mkid.cb);
  80. end;
  81. end;
  82. end;
  83. function PIDL_Create(Size: UINT): PItemIDList;
  84. // PURPOSE: Creates a new ITEMIDLIST of the specified size.
  85. // PARAMETERS:
  86. // piMalloc - Pointer to the allocator interface that should allocate memory.
  87. // cbSize - Size of the ITEMIDLIST to create.
  88. // RETURN VALUE:
  89. // Returns a pointer to the new ITEMIDLIST, or NULL if a problem occured.
  90. begin
  91. Result:=ShellMalloc.Alloc(Size);
  92. if Result<>nil then
  93. FillChar(Result^, Size, #0);
  94. end;
  95. function PIDL_Concatenate(pidl1, pidl2: PItemIDList): PItemIDList;
  96. // PURPOSE: Creates a new ITEMIDLIST with pidl2 appended to pidl1.
  97. // PARAMETERS:
  98. // piMalloc - Pointer to the allocator interface that should create the new ITEMIDLIST.
  99. // pidl1- Pointer to an ITEMIDLIST that contains the root.
  100. // pidl2 - Pointer to an ITEMIDLIST that contains what should be appended to the root.
  101. // RETURN VALUE:
  102. // Returns a new ITEMIDLIST if successful, NULL otherwise.
  103. var cb1, cb2: UINT;
  104. begin
  105. if (pidl1<>nil) then cb1:=PIDL_GetSize(pidl1)-NullTerm else cb1:=0;
  106. cb2:=PIDL_GetSize(pidl2);
  107. Result:=PIDL_Create(cb1 + cb2);
  108. if Result<>nil then
  109. begin
  110. if pidl1<>nil then CopyMemory(Result,pidl1,cb1);
  111. CopyMemory(PAnsiChar(Result)+cb1,pidl2,cb2);
  112. end;
  113. end;
  114. function PIDL_Copy(pidlSource: PItemIDList): PItemIDList;
  115. // PURPOSE: Creates a new copy of an ITEMIDLIST.
  116. // PARAMETERS:
  117. // piMalloc - Pointer to the allocator interfaced to be used to allocate the new ITEMIDLIST.
  118. // RETURN VALUE:
  119. // Returns a pointer to the new ITEMIDLIST, or NULL if an error occurs.
  120. var cbSource:UINT;
  121. begin
  122. Result:=nil;
  123. if pidlSource=nil then exit;
  124. cbSource:=PIDL_GetSize(pidlSource);
  125. Result:=PIDL_Create(cbSource);
  126. if Result=nil then exit;
  127. CopyMemory(Result,pidlSource,cbSource);
  128. end;
  129. function PIDL_GetDisplayName(piFolder: IShellFolder; pidl: PItemIDList;
  130. dwFlags: DWORD; pszName: PChar; cchMax: UINT): boolean;
  131. // PURPOSE: Returns the display name for the item pointed to by pidl. The
  132. // function assumes the pidl is relative to piFolder. If piFolder
  133. // is NULL, the function assumes the item is fully qualified.
  134. // PARAMETERS:
  135. // piFolder - Pointer to the IShellFolder for the folder containing the item.
  136. // pidl - Pointer to an ITEMIDLIST relative to piFolder that we want
  137. // the display name for.
  138. // dwFlags - Flags to pass to ISF::GetDisplayNameOf().
  139. // pszName - Pointer to the string where the display name is returned.
  140. // cchMax - Maximum number of characters in pszName.
  141. // RETURN VALUE:
  142. // Returns TRUE if successful, FALSE otherwise.
  143. var Str: TStrRet;
  144. begin
  145. if (piFolder=nil) and (Failed(SHGetDesktopFolder(piFolder))) then
  146. begin
  147. Result:=false;
  148. exit;
  149. end;
  150. Result:=TRUE;
  151. if piFolder.GetDisplayNameOf(pidl, dwFlags, Str) = NOERROR then
  152. begin
  153. case Str.uType of
  154. STRRET_WSTR:
  155. lstrcpyn(pszName, str.pOleStr, cchMax);
  156. STRRET_OFFSET:
  157. MultiByteToWideChar(CP_ACP, 0, PAnsiChar(pidl)+str.uOffset, -1, pszName, cchMax);
  158. STRRET_CSTR:
  159. MultiByteToWideChar(CP_ACP, 0, str.cStr, -1, pszName, cchMax);
  160. else Result := FALSE;
  161. end;
  162. end
  163. else Result:=FALSE;
  164. // piFolder._Release; -> automaticly done by D4
  165. end;
  166. function Pidl_GetFullyQualified(const PiParentFolder: IShellFolder;
  167. pidl: PItemIDList): PItemIDList;
  168. // PURPOSE: Takes a relative PIDL and it's parent IShellFolder, and returns
  169. // a fully qualified ITEMIDLIST.
  170. // PARAMETERS:
  171. // piParentFolder - Pointer to the IShellFolder of the parent folder.
  172. // pidl - ITEMIDLIST relative to piParentFolder
  173. // RETURN VALUE:
  174. // Returns a fully qualified ITEMIDLIST or NULL if there is a problem.
  175. var piDesktopFolder:IShellFolder;
  176. szBuffer: array[1..Max_Path] of char;
  177. szOleChar: array[1..Max_Path] of TOLECHAR;
  178. ulEaten, ulAttribs:ULong;
  179. begin
  180. Result:=nil;
  181. if Failed(SHGetDesktopFolder(piDesktopFolder)) then exit;
  182. if PIDL_GetDisplayName(piParentFolder, pidl, SHGDN_FORPARSING, @szBuffer, sizeof(szBuffer))=false then
  183. exit;
  184. MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, @szBuffer, -1, @szOleChar, sizeof(szOleChar));
  185. ulAttribs := 0;
  186. if Failed(piDesktopFolder.ParseDisplayName(0, nil, @szOleChar, ulEaten,
  187. Result, ulAttribs)) then Result:=nil;
  188. // piDesktopFolder._Release; automaticly done by D4
  189. end;
  190. procedure PIDL_GetRelative(var pidlFQ, ppidlRoot, ppidlItem: PItemIDList);
  191. // PURPOSE: Takes a fully qualified pidl and returns the the relative pidl
  192. // and the root part of that pidl.
  193. // PARAMETERS:
  194. // pidlFQ - Pointer to the fully qualified ITEMIDLIST that needs to be parsed.
  195. // pidlRoot - Points to the pidl that will contain the root after parsing.
  196. // pidlItem - Points to the item relative to pidlRoot after parsing.
  197. var pidlTemp, pidlNext: PItemIDList;
  198. begin
  199. if pidlFQ=nil then
  200. begin
  201. ppidlRoot:=nil;
  202. ppidlItem:=nil;
  203. exit;
  204. end;
  205. ppidlItem:=nil;
  206. ppidlRoot:=PIDL_Copy(pidlFQ);
  207. pidlTemp:=ppidlRoot;
  208. while pidlTemp^.mkid.cb>0 do
  209. begin
  210. pidlNext:=PIDL_GetNextItem(pidlTemp);
  211. if pidlNext^.mkid.cb=0 then
  212. begin
  213. ppidlItem:=PIDL_Copy(pidlTemp);
  214. pidlTemp^.mkid.cb:=0;
  215. pidlTemp^.mkid.abID[0]:=0;
  216. end;
  217. pidlTemp:=pidlNext;
  218. end;
  219. end;
  220. function PIDL_GetFromPath(pszFile: PChar): PItemIDList;
  221. // PURPOSE: This routine takes a full path to a file and converts that
  222. // to a fully qualified ITEMIDLIST.
  223. // PARAMETERS:
  224. // pszFile - Full path to the file.
  225. // RETURN VALUE:
  226. // Returns a fully qualified ITEMIDLIST, or NULL if an error occurs.
  227. var piDesktop: IShellFolder;
  228. ulEaten, ulAttribs:ULong;
  229. begin
  230. Result:=nil;
  231. if Failed(SHGetDesktopFolder(piDesktop)) then exit;
  232. piDesktop._AddRef;
  233. ulAttribs := 0;
  234. if Failed(piDesktop.ParseDisplayName(0, nil, pszFile, ulEaten,
  235. Result, ulAttribs)) then Result:=nil;
  236. // piDesktop._Release; -> automaticly done by D4
  237. end;
  238. function PIDL_GetFileFolder(pidl: PItemIDList; var piFolder: IShellFolder):boolean;
  239. // PURPOSE: This routine takes a fully qualified pidl for a folder and returns
  240. // the IShellFolder pointer for that pidl
  241. // PARAMETERS:
  242. // pidl - Pointer to a fully qualified ITEMIDLIST for the folder
  243. // piParentFolder - Pointer to the IShellFolder of the folder (Return value).
  244. // RETURN VALUE:
  245. // Returns TRUE if successful, FALSE otherwise.
  246. var piDesktopFolder: IShellFolder;
  247. begin
  248. Result:=false;
  249. if Failed(SHGetDesktopFolder(piDesktopFolder)) then exit;
  250. if assigned(PiFolder)=false then
  251. if Failed(SHGetDesktopFolder(PiFolder)) then exit;
  252. if Failed(piDesktopFolder.BindToObject(pidl, nil, IID_IShellFolder,
  253. pointer(PiFolder)))=false then Result:=true;
  254. //piDesktopFolder._Release; -> automaticly done by D4
  255. end;
  256. function PIDL_GetFromParentFolder(pParentFolder: IShellFolder; pszFile: PChar): PItemIDList;
  257. // PURPOSE: This routine takes a Shell folder for the parent and the FileName in the folder
  258. // and converts that to a relative ITEMIDLIST.
  259. // PARAMETERS:
  260. // pParentFolder - Pointer to the IShellFolder for the folder containing the
  261. // fileName.
  262. // pszFile - file name in the folder.
  263. // RETURN VALUE:
  264. // Returns a relative ITEMIDLIST, or NULL if an error occurs.
  265. var chEaten, dwAttributes: ULONG;
  266. NotResult: Boolean;
  267. ErrorMode: Word;
  268. begin
  269. ErrorMode := SetErrorMode(SEM_FAILCRITICALERRORS or SEM_NOOPENFILEERRORBOX);
  270. try
  271. dwAttributes := 0;
  272. NotResult := Failed(pParentFolder.ParseDisplayName(0, nil, pszFile, chEaten, Result,
  273. dwAttributes));
  274. finally
  275. SetErrorMode(ErrorMode);
  276. end;
  277. if NotResult then Result:=nil;
  278. end;
  279. procedure PIDL_Free(PIDL:PItemIDList);
  280. begin
  281. if PIDL<>nil then
  282. ShellMalloc.Free(PIDL);
  283. end;
  284. function PIDL_Equal(PIDL1,PIDL2:PItemIDList):boolean;
  285. var i,size:integer;
  286. p1,p2:pchar;
  287. begin
  288. Result:=false;
  289. if (PIDL1=nil) or (PIDL2=nil) then exit;
  290. size:=PIDL_GetSize(PIDL1);
  291. if size<>PIDL_GetSize(PIDL2) then exit;
  292. i:=0;
  293. p1:=PChar(PIDL1);
  294. p2:=PChar(PIDL2);
  295. while i<size do
  296. if p1[i]<>p2[i] then exit else inc(i);
  297. Result:=true;
  298. end;
  299. initialization
  300. SHGetMalloc(ShellMalloc);
  301. CF_FILECONTENTS:=RegisterClipboardFormat('FileContents');
  302. CF_FILEDESCRIPTOR:=RegisterClipboardFormat('FileGroupDescriptor');
  303. CF_FILENAME:=RegisterClipboardFormat('FileName');
  304. CF_FILENAMEMAP:=RegisterClipboardFormat('FileNameMap');
  305. CF_FILENAMEMAPW:=RegisterClipboardFormat('FileNameMapW');
  306. CF_INDRAGLOOP:=RegisterClipboardFormat('InShellDragLoop');
  307. CF_NETRESOURCES:=RegisterClipboardFormat('Net Resource');
  308. CF_PASTESUCCEEDED:=RegisterClipboardFormat('Paste Succeeded');
  309. CF_PERFORMEDDROPEFFECT:=RegisterClipboardFormat('Performed DropEffect');
  310. CF_PREFERREDDROPEFFECT:=RegisterClipboardFormat('Preferred DropEffect');
  311. CF_PRINTERGROUP:=RegisterClipboardFormat('PrinterFriendlyName');
  312. CF_SHELLIDLIST:=RegisterClipboardFormat('Shell IDList Array');
  313. CF_SHELLIDLISTOFFSET:=RegisterClipboardFormat('Shell Object Offsets');
  314. CF_SHELLURL:=RegisterClipboardFormat('UniformResourceLocator');
  315. finalization
  316. // ShellMalloc._Release; -> automaticly done by D4
  317. end.