PIDL.pas 13 KB

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