msi/tests: Fixed a typo.
[wine.git] / dlls / shell32 / pidl.c
blobe4fe443896bf1f8beee05e5b8f846b5ec09b7cbc
1 /*
2 * pidl Handling
4 * Copyright 1998 Juergen Schmied
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * NOTES
21 * a pidl == NULL means desktop and is legal
25 #include "config.h"
26 #include "wine/port.h"
28 #include <ctype.h>
29 #include <stdarg.h>
30 #include <stdlib.h>
31 #include <string.h>
33 #define COBJMACROS
34 #define NONAMELESSUNION
35 #define NONAMELESSSTRUCT
37 #include "windef.h"
38 #include "winbase.h"
39 #include "winreg.h"
40 #include "objbase.h"
41 #include "shlguid.h"
42 #include "winerror.h"
43 #include "winnls.h"
44 #include "undocshell.h"
45 #include "shell32_main.h"
46 #include "shellapi.h"
47 #include "shlwapi.h"
49 #include "pidl.h"
50 #include "wine/debug.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(pidl);
53 WINE_DECLARE_DEBUG_CHANNEL(shell);
55 /* from comctl32.dll */
56 extern LPVOID WINAPI Alloc(INT);
57 extern BOOL WINAPI Free(LPVOID);
59 /*************************************************************************
60 * ILGetDisplayNameEx [SHELL32.186]
62 * Retrieves the display name of an ItemIDList
64 * PARAMS
65 * psf [I] Shell Folder to start with, if NULL the desktop is used
66 * pidl [I] ItemIDList relativ to the psf to get the display name for
67 * path [O] Filled in with the display name, assumed to be at least MAX_PATH long
68 * type [I] Type of display name to retrieve
69 * 0 = SHGDN_FORPARSING | SHGDN_FORADDRESSBAR uses always the desktop as root
70 * 1 = SHGDN_NORMAL relative to the root folder
71 * 2 = SHGDN_INFOLDER relative to the root folder, only the last name
73 * RETURNS
74 * True if the display name could be retrieved successfully, False otherwise
76 BOOL WINAPI ILGetDisplayNameExA(LPSHELLFOLDER psf, LPCITEMIDLIST pidl, LPSTR path, DWORD type)
78 BOOL ret = FALSE;
79 WCHAR wPath[MAX_PATH];
81 TRACE("%p %p %p %ld\n", psf, pidl, path, type);
83 if (!pidl || !path)
84 return FALSE;
86 ret = ILGetDisplayNameExW(psf, pidl, wPath, type);
87 WideCharToMultiByte(CP_ACP, 0, wPath, -1, path, MAX_PATH, NULL, NULL);
88 TRACE("%p %p %s\n", psf, pidl, debugstr_a(path));
90 return ret;
93 BOOL WINAPI ILGetDisplayNameExW(LPSHELLFOLDER psf, LPCITEMIDLIST pidl, LPWSTR path, DWORD type)
95 LPSHELLFOLDER psfParent, lsf = psf;
96 HRESULT ret = NO_ERROR;
97 LPCITEMIDLIST pidllast;
98 STRRET strret;
99 DWORD flag;
101 TRACE("%p %p %p %ld\n", psf, pidl, path, type);
103 if (!pidl || !path)
104 return FALSE;
106 if (!lsf)
108 ret = SHGetDesktopFolder(&lsf);
109 if (FAILED(ret))
110 return FALSE;
113 if (type >= 0 && type <= 2)
115 switch (type)
117 case ILGDN_FORPARSING:
118 flag = SHGDN_FORPARSING | SHGDN_FORADDRESSBAR;
119 break;
120 case ILGDN_NORMAL:
121 flag = SHGDN_NORMAL;
122 break;
123 case ILGDN_INFOLDER:
124 flag = SHGDN_INFOLDER;
125 break;
126 default:
127 FIXME("Unknown type parameter = %lx\n", type);
128 flag = SHGDN_FORPARSING | SHGDN_FORADDRESSBAR;
129 break;
131 if (!*(const WORD*)pidl || type == ILGDN_FORPARSING)
133 ret = IShellFolder_GetDisplayNameOf(lsf, pidl, flag, &strret);
134 if (SUCCEEDED(ret))
136 if(!StrRetToStrNW(path, MAX_PATH, &strret, pidl))
137 ret = E_FAIL;
140 else
142 ret = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psfParent, &pidllast);
143 if (SUCCEEDED(ret))
145 ret = IShellFolder_GetDisplayNameOf(psfParent, pidllast, flag, &strret);
146 if (SUCCEEDED(ret))
148 if(!StrRetToStrNW(path, MAX_PATH, &strret, pidllast))
149 ret = E_FAIL;
151 IShellFolder_Release(psfParent);
156 TRACE("%p %p %s\n", psf, pidl, debugstr_w(path));
158 if (!psf)
159 IShellFolder_Release(lsf);
160 return SUCCEEDED(ret);
163 BOOL WINAPI ILGetDisplayNameEx(LPSHELLFOLDER psf, LPCITEMIDLIST pidl, LPVOID path, DWORD type)
165 TRACE_(shell)("%p %p %p %ld\n", psf, pidl, path, type);
167 if (SHELL_OsIsUnicode())
168 return ILGetDisplayNameExW(psf, pidl, path, type);
169 return ILGetDisplayNameExA(psf, pidl, path, type);
172 /*************************************************************************
173 * ILGetDisplayName [SHELL32.15]
175 BOOL WINAPI ILGetDisplayName(LPCITEMIDLIST pidl, LPVOID path)
177 TRACE_(shell)("%p %p\n", pidl, path);
179 if (SHELL_OsIsUnicode())
180 return ILGetDisplayNameExW(NULL, pidl, path, ILGDN_FORPARSING);
181 return ILGetDisplayNameExA(NULL, pidl, path, ILGDN_FORPARSING);
184 /*************************************************************************
185 * ILFindLastID [SHELL32.16]
187 * NOTES
188 * observed: pidl=Desktop return=pidl
190 LPITEMIDLIST WINAPI ILFindLastID(LPCITEMIDLIST pidl)
192 LPCITEMIDLIST pidlLast = pidl;
194 TRACE("(pidl=%p)\n",pidl);
196 if (!pidl)
197 return NULL;
199 while (pidl->mkid.cb)
201 pidlLast = pidl;
202 pidl = ILGetNext(pidl);
204 return (LPITEMIDLIST)pidlLast;
207 /*************************************************************************
208 * ILRemoveLastID [SHELL32.17]
210 * NOTES
211 * when pidl=Desktop return=FALSE
213 BOOL WINAPI ILRemoveLastID(LPITEMIDLIST pidl)
215 TRACE_(shell)("pidl=%p\n",pidl);
217 if (!pidl || !pidl->mkid.cb)
218 return 0;
219 ILFindLastID(pidl)->mkid.cb = 0;
220 return 1;
223 /*************************************************************************
224 * ILClone [SHELL32.18]
226 * NOTES
227 * duplicate an idlist
229 LPITEMIDLIST WINAPI ILClone (LPCITEMIDLIST pidl)
231 DWORD len;
232 LPITEMIDLIST newpidl;
234 if (!pidl)
235 return NULL;
237 len = ILGetSize(pidl);
238 newpidl = (LPITEMIDLIST)SHAlloc(len);
239 if (newpidl)
240 memcpy(newpidl,pidl,len);
242 TRACE("pidl=%p newpidl=%p\n",pidl, newpidl);
243 pdump(pidl);
245 return newpidl;
248 /*************************************************************************
249 * ILCloneFirst [SHELL32.19]
251 * NOTES
252 * duplicates the first idlist of a complex pidl
254 LPITEMIDLIST WINAPI ILCloneFirst(LPCITEMIDLIST pidl)
256 DWORD len;
257 LPITEMIDLIST pidlNew = NULL;
259 TRACE("pidl=%p\n", pidl);
260 pdump(pidl);
262 if (pidl)
264 len = pidl->mkid.cb;
265 pidlNew = (LPITEMIDLIST) SHAlloc (len+2);
266 if (pidlNew)
268 memcpy(pidlNew,pidl,len+2); /* 2 -> mind a desktop pidl */
270 if (len)
271 ILGetNext(pidlNew)->mkid.cb = 0x00;
274 TRACE("-- newpidl=%p\n",pidlNew);
276 return pidlNew;
279 /*************************************************************************
280 * ILLoadFromStream (SHELL32.26)
282 * NOTES
283 * the first two bytes are the len, the pidl is following then
285 HRESULT WINAPI ILLoadFromStream (IStream * pStream, LPITEMIDLIST * ppPidl)
287 WORD wLen = 0;
288 DWORD dwBytesRead;
289 HRESULT ret = E_FAIL;
292 TRACE_(shell)("%p %p\n", pStream , ppPidl);
294 if (*ppPidl)
296 SHFree(*ppPidl);
297 *ppPidl = NULL;
300 IStream_AddRef (pStream);
302 if (SUCCEEDED(IStream_Read(pStream, (LPVOID)&wLen, 2, &dwBytesRead)))
304 TRACE("PIDL length is %d\n", wLen);
305 if (wLen != 0)
307 *ppPidl = SHAlloc (wLen);
308 if (SUCCEEDED(IStream_Read(pStream, *ppPidl , wLen, &dwBytesRead)))
310 TRACE("Stream read OK\n");
311 ret = S_OK;
313 else
315 WARN("reading pidl failed\n");
316 SHFree(*ppPidl);
317 *ppPidl = NULL;
320 else
322 *ppPidl = NULL;
323 ret = S_OK;
327 /* we are not yet fully compatible */
328 if (*ppPidl && !pcheck(*ppPidl))
330 WARN("Check failed\n");
331 SHFree(*ppPidl);
332 *ppPidl = NULL;
335 IStream_Release (pStream);
336 TRACE("done\n");
337 return ret;
340 /*************************************************************************
341 * ILSaveToStream (SHELL32.27)
343 * NOTES
344 * the first two bytes are the len, the pidl is following then
346 HRESULT WINAPI ILSaveToStream (IStream * pStream, LPCITEMIDLIST pPidl)
348 LPCITEMIDLIST pidl;
349 WORD wLen = 0;
350 HRESULT ret = E_FAIL;
352 TRACE_(shell)("%p %p\n", pStream, pPidl);
354 IStream_AddRef (pStream);
356 pidl = pPidl;
357 while (pidl->mkid.cb)
359 wLen += sizeof(WORD) + pidl->mkid.cb;
360 pidl = ILGetNext(pidl);
363 if (SUCCEEDED(IStream_Write(pStream, (LPVOID)&wLen, 2, NULL)))
365 if (SUCCEEDED(IStream_Write(pStream, pPidl, wLen, NULL)))
366 ret = S_OK;
368 IStream_Release (pStream);
370 return ret;
373 /*************************************************************************
374 * SHILCreateFromPath [SHELL32.28]
376 * Create an ItemIDList from a path
378 * PARAMS
379 * path [I]
380 * ppidl [O]
381 * attributes [I/O] requested attributes on call and actual attributes when
382 * the function returns
384 * RETURNS
385 * NO_ERROR if successful, or an OLE errer code otherwise
387 * NOTES
388 * Wrapper for IShellFolder_ParseDisplayName().
390 HRESULT WINAPI SHILCreateFromPathA(LPCSTR path, LPITEMIDLIST * ppidl, DWORD * attributes)
392 WCHAR lpszDisplayName[MAX_PATH];
394 TRACE_(shell)("%s %p 0x%08lx\n", path, ppidl, attributes ? *attributes : 0);
396 if (!MultiByteToWideChar(CP_ACP, 0, path, -1, lpszDisplayName, MAX_PATH))
397 lpszDisplayName[MAX_PATH-1] = 0;
399 return SHILCreateFromPathW(lpszDisplayName, ppidl, attributes);
402 HRESULT WINAPI SHILCreateFromPathW(LPCWSTR path, LPITEMIDLIST * ppidl, DWORD * attributes)
404 LPSHELLFOLDER sf;
405 DWORD pchEaten;
406 HRESULT ret = E_FAIL;
408 TRACE_(shell)("%s %p 0x%08lx\n", debugstr_w(path), ppidl, attributes ? *attributes : 0);
410 if (SUCCEEDED (SHGetDesktopFolder(&sf)))
412 ret = IShellFolder_ParseDisplayName(sf, 0, NULL, (LPWSTR)path, &pchEaten, ppidl, attributes);
413 IShellFolder_Release(sf);
415 return ret;
418 HRESULT WINAPI SHILCreateFromPathAW (LPCVOID path, LPITEMIDLIST * ppidl, DWORD * attributes)
420 if ( SHELL_OsIsUnicode())
421 return SHILCreateFromPathW (path, ppidl, attributes);
422 return SHILCreateFromPathA (path, ppidl, attributes);
425 /*************************************************************************
426 * SHCloneSpecialIDList [SHELL32.89]
428 * Create an ItemIDList to one of the special folders.
430 * PARAMS
431 * hwndOwner [in]
432 * nFolder [in] CSIDL_xxxxx
433 * fCreate [in] Create folder if it does not exist
435 * RETURNS
436 * Success: The newly created pidl
437 * Failure: NULL, if inputs are invalid.
439 * NOTES
440 * exported by ordinal.
441 * Caller is responsible for deallocating the returned ItemIDList with the
442 * shells IMalloc interface, aka ILFree.
444 LPITEMIDLIST WINAPI SHCloneSpecialIDList(HWND hwndOwner, DWORD nFolder, BOOL fCreate)
446 LPITEMIDLIST ppidl;
447 TRACE_(shell)("(hwnd=%p,csidl=0x%lx,%s).\n", hwndOwner, nFolder, fCreate ? "T" : "F");
449 if (fCreate)
450 nFolder |= CSIDL_FLAG_CREATE;
452 SHGetSpecialFolderLocation(hwndOwner, nFolder, &ppidl);
453 return ppidl;
456 /*************************************************************************
457 * ILGlobalClone [SHELL32.20]
459 * Clones an ItemIDList using Alloc.
461 * PARAMS
462 * pidl [I] ItemIDList to clone
464 * RETURNS
465 * Newly allocated ItemIDList.
467 * NOTES
468 * exported by ordinal.
470 LPITEMIDLIST WINAPI ILGlobalClone(LPCITEMIDLIST pidl)
472 DWORD len;
473 LPITEMIDLIST newpidl;
475 if (!pidl)
476 return NULL;
478 len = ILGetSize(pidl);
479 newpidl = (LPITEMIDLIST)Alloc(len);
480 if (newpidl)
481 memcpy(newpidl,pidl,len);
483 TRACE("pidl=%p newpidl=%p\n",pidl, newpidl);
484 pdump(pidl);
486 return newpidl;
489 /*************************************************************************
490 * ILIsEqual [SHELL32.21]
493 BOOL WINAPI ILIsEqual(LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
495 char szData1[MAX_PATH];
496 char szData2[MAX_PATH];
498 LPCITEMIDLIST pidltemp1 = pidl1;
499 LPCITEMIDLIST pidltemp2 = pidl2;
501 TRACE("pidl1=%p pidl2=%p\n",pidl1, pidl2);
504 * Explorer reads from registry directly (StreamMRU),
505 * so we can only check here
507 if (!pcheck(pidl1) || !pcheck (pidl2))
508 return FALSE;
510 pdump (pidl1);
511 pdump (pidl2);
513 if (!pidl1 || !pidl2)
514 return FALSE;
516 while (pidltemp1->mkid.cb && pidltemp2->mkid.cb)
518 _ILSimpleGetText(pidltemp1, szData1, MAX_PATH);
519 _ILSimpleGetText(pidltemp2, szData2, MAX_PATH);
521 if (strcasecmp( szData1, szData2 ))
522 return FALSE;
524 pidltemp1 = ILGetNext(pidltemp1);
525 pidltemp2 = ILGetNext(pidltemp2);
528 if (!pidltemp1->mkid.cb && !pidltemp2->mkid.cb)
529 return TRUE;
531 return FALSE;
534 /*************************************************************************
535 * ILIsParent [SHELL32.23]
537 * Verifies that pidlParent is indeed the (immediate) parent of pidlChild.
539 * PARAMS
540 * pidlParent [I]
541 * pidlChild [I]
542 * bImmediate [I] only return true if the parent is the direct parent
543 * of the child
545 * RETURNS
546 * True if the parent ItemIDlist is a complete part of the child ItemIdList,
547 * False otherwise.
549 * NOTES
550 * parent = a/b, child = a/b/c -> true, c is in folder a/b
551 * child = a/b/c/d -> false if bImmediate is true, d is not in folder a/b
552 * child = a/b/c/d -> true if bImmediate is false, d is in a subfolder of a/b
554 BOOL WINAPI ILIsParent(LPCITEMIDLIST pidlParent, LPCITEMIDLIST pidlChild, BOOL bImmediate)
556 char szData1[MAX_PATH];
557 char szData2[MAX_PATH];
558 LPCITEMIDLIST pParent = pidlParent;
559 LPCITEMIDLIST pChild = pidlChild;
561 TRACE("%p %p %x\n", pidlParent, pidlChild, bImmediate);
563 if (!pParent || !pChild)
564 return FALSE;
566 while (pParent->mkid.cb && pChild->mkid.cb)
568 _ILSimpleGetText(pParent, szData1, MAX_PATH);
569 _ILSimpleGetText(pChild, szData2, MAX_PATH);
571 if (strcasecmp( szData1, szData2 ))
572 return FALSE;
574 pParent = ILGetNext(pParent);
575 pChild = ILGetNext(pChild);
578 /* child shorter or has equal length to parent */
579 if (pParent->mkid.cb || !pChild->mkid.cb)
580 return FALSE;
582 /* not immediate descent */
583 if ( ILGetNext(pChild)->mkid.cb && bImmediate)
584 return FALSE;
586 return TRUE;
589 /*************************************************************************
590 * ILFindChild [SHELL32.24]
592 * Compares elements from pidl1 and pidl2.
594 * PARAMS
595 * pidl1 [I]
596 * pidl2 [I]
598 * RETURNS
599 * pidl1 is desktop pidl2
600 * pidl1 shorter pidl2 pointer to first different element of pidl2
601 * if there was at least one equal element
602 * pidl2 shorter pidl1 0
603 * pidl2 equal pidl1 pointer to last 0x00-element of pidl2
605 * NOTES
606 * exported by ordinal.
608 LPITEMIDLIST WINAPI ILFindChild(LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
610 char szData1[MAX_PATH];
611 char szData2[MAX_PATH];
613 LPCITEMIDLIST pidltemp1 = pidl1;
614 LPCITEMIDLIST pidltemp2 = pidl2;
615 LPCITEMIDLIST ret=NULL;
617 TRACE("pidl1=%p pidl2=%p\n",pidl1, pidl2);
619 /* explorer reads from registry directly (StreamMRU),
620 so we can only check here */
621 if ((!pcheck (pidl1)) || (!pcheck (pidl2)))
622 return FALSE;
624 pdump (pidl1);
625 pdump (pidl2);
627 if (_ILIsDesktop(pidl1))
629 ret = pidl2;
631 else
633 while (pidltemp1->mkid.cb && pidltemp2->mkid.cb)
635 _ILSimpleGetText(pidltemp1, szData1, MAX_PATH);
636 _ILSimpleGetText(pidltemp2, szData2, MAX_PATH);
638 if (strcasecmp(szData1,szData2))
639 break;
641 pidltemp1 = ILGetNext(pidltemp1);
642 pidltemp2 = ILGetNext(pidltemp2);
643 ret = pidltemp2;
646 if (pidltemp1->mkid.cb)
647 ret = NULL; /* elements of pidl1 left*/
649 TRACE_(shell)("--- %p\n", ret);
650 return (LPITEMIDLIST)ret; /* pidl 1 is shorter */
653 /*************************************************************************
654 * ILCombine [SHELL32.25]
656 * Concatenates two complex ItemIDLists.
658 * PARAMS
659 * pidl1 [I] first complex ItemIDLists
660 * pidl2 [I] complex ItemIDLists to append
662 * RETURNS
663 * if both pidl's == NULL NULL
664 * if pidl1 == NULL cloned pidl2
665 * if pidl2 == NULL cloned pidl1
666 * otherwise new pidl with pidl2 appended to pidl1
668 * NOTES
669 * exported by ordinal.
670 * Does not destroy the passed in ItemIDLists!
672 LPITEMIDLIST WINAPI ILCombine(LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
674 DWORD len1,len2;
675 LPITEMIDLIST pidlNew;
677 TRACE("pidl=%p pidl=%p\n",pidl1,pidl2);
679 if (!pidl1 && !pidl2) return NULL;
681 pdump (pidl1);
682 pdump (pidl2);
684 if (!pidl1)
686 pidlNew = ILClone(pidl2);
687 return pidlNew;
690 if (!pidl2)
692 pidlNew = ILClone(pidl1);
693 return pidlNew;
696 len1 = ILGetSize(pidl1)-2;
697 len2 = ILGetSize(pidl2);
698 pidlNew = SHAlloc(len1+len2);
700 if (pidlNew)
702 memcpy(pidlNew,pidl1,len1);
703 memcpy(((BYTE *)pidlNew)+len1,pidl2,len2);
706 /* TRACE(pidl,"--new pidl=%p\n",pidlNew);*/
707 return pidlNew;
710 /*************************************************************************
711 * SHGetRealIDL [SHELL32.98]
713 * NOTES
715 HRESULT WINAPI SHGetRealIDL(LPSHELLFOLDER lpsf, LPCITEMIDLIST pidlSimple, LPITEMIDLIST *pidlReal)
717 IDataObject* pDataObj;
718 HRESULT hr;
720 hr = IShellFolder_GetUIObjectOf(lpsf, 0, 1, &pidlSimple,
721 &IID_IDataObject, 0, (LPVOID*)&pDataObj);
722 if (SUCCEEDED(hr))
724 STGMEDIUM medium;
725 FORMATETC fmt;
727 fmt.cfFormat = RegisterClipboardFormatA(CFSTR_SHELLIDLIST);
728 fmt.ptd = NULL;
729 fmt.dwAspect = DVASPECT_CONTENT;
730 fmt.lindex = -1;
731 fmt.tymed = TYMED_HGLOBAL;
733 hr = IDataObject_GetData(pDataObj, &fmt, &medium);
735 IDataObject_Release(pDataObj);
737 if (SUCCEEDED(hr))
739 /*assert(pida->cidl==1);*/
740 LPIDA pida = (LPIDA)GlobalLock(medium.u.hGlobal);
742 LPCITEMIDLIST pidl_folder = (LPCITEMIDLIST) ((LPBYTE)pida+pida->aoffset[0]);
743 LPCITEMIDLIST pidl_child = (LPCITEMIDLIST) ((LPBYTE)pida+pida->aoffset[1]);
745 *pidlReal = ILCombine(pidl_folder, pidl_child);
747 if (!*pidlReal)
748 hr = E_OUTOFMEMORY;
750 GlobalUnlock(medium.u.hGlobal);
751 GlobalFree(medium.u.hGlobal);
755 return hr;
758 /*************************************************************************
759 * SHLogILFromFSIL [SHELL32.95]
761 * NOTES
762 * pild = CSIDL_DESKTOP ret = 0
763 * pild = CSIDL_DRIVES ret = 0
765 LPITEMIDLIST WINAPI SHLogILFromFSIL(LPITEMIDLIST pidl)
767 FIXME("(pidl=%p)\n",pidl);
769 pdump(pidl);
771 return 0;
774 /*************************************************************************
775 * ILGetSize [SHELL32.152]
777 * Gets the byte size of an ItemIDList including zero terminator
779 * PARAMS
780 * pidl [I] ItemIDList
782 * RETURNS
783 * size of pidl in bytes
785 * NOTES
786 * exported by ordinal
788 UINT WINAPI ILGetSize(LPCITEMIDLIST pidl)
790 LPCSHITEMID si = &(pidl->mkid);
791 UINT len=0;
793 if (pidl)
795 while (si->cb)
797 len += si->cb;
798 si = (LPCSHITEMID)(((const BYTE*)si)+si->cb);
800 len += 2;
802 TRACE("pidl=%p size=%u\n",pidl, len);
803 return len;
806 /*************************************************************************
807 * ILGetNext [SHELL32.153]
809 * Gets the next ItemID of an ItemIDList
811 * PARAMS
812 * pidl [I] ItemIDList
814 * RETURNS
815 * null -> null
816 * desktop -> null
817 * simple pidl -> pointer to 0x0000 element
819 * NOTES
820 * exported by ordinal.
822 LPITEMIDLIST WINAPI ILGetNext(LPCITEMIDLIST pidl)
824 WORD len;
826 TRACE("%p\n", pidl);
828 if (pidl)
830 len = pidl->mkid.cb;
831 if (len)
833 pidl = (LPCITEMIDLIST) (((const BYTE*)pidl)+len);
834 TRACE("-- %p\n", pidl);
835 return (LPITEMIDLIST)pidl;
838 return NULL;
841 /*************************************************************************
842 * ILAppend [SHELL32.154]
844 * Adds the single ItemID item to the ItemIDList indicated by pidl.
845 * If bEnd is FALSE, inserts the item in the front of the list,
846 * otherwise it adds the item to the end. (???)
848 * PARAMS
849 * pidl [I] ItemIDList to extend
850 * item [I] ItemID to prepend/append
851 * bEnd [I] Indicates if the item should be appended
853 * NOTES
854 * Destroys the passed in idlist! (???)
856 LPITEMIDLIST WINAPI ILAppend(LPITEMIDLIST pidl, LPCITEMIDLIST item, BOOL bEnd)
858 LPITEMIDLIST idlRet;
860 WARN("(pidl=%p,pidl=%p,%08u)semi-stub\n",pidl,item,bEnd);
862 pdump (pidl);
863 pdump (item);
865 if (_ILIsDesktop(pidl))
867 idlRet = ILClone(item);
868 if (pidl)
869 SHFree (pidl);
870 return idlRet;
873 if (bEnd)
874 idlRet = ILCombine(pidl, item);
875 else
876 idlRet = ILCombine(item, pidl);
878 SHFree(pidl);
879 return idlRet;
882 /*************************************************************************
883 * ILFree [SHELL32.155]
885 * Frees memory (if not NULL) allocated by SHMalloc allocator
887 * PARAMS
888 * pidl [I]
890 * RETURNS
891 * Nothing
893 * NOTES
894 * exported by ordinal
896 void WINAPI ILFree(LPITEMIDLIST pidl)
898 TRACE("(pidl=%p)\n",pidl);
899 if (pidl)
900 SHFree(pidl);
903 /*************************************************************************
904 * ILGlobalFree [SHELL32.156]
906 * Frees memory (if not NULL) allocated by Alloc allocator
908 * PARAMS
909 * pidl [I]
911 * RETURNS
912 * Nothing
914 * NOTES
915 * exported by ordinal.
917 void WINAPI ILGlobalFree( LPITEMIDLIST pidl)
919 TRACE("%p\n", pidl);
921 if (pidl)
922 Free(pidl);
925 /*************************************************************************
926 * ILCreateFromPathA [SHELL32.189]
928 * Creates a complex ItemIDList from a path and returns it.
930 * PARAMS
931 * path [I]
933 * RETURNS
934 * the newly created complex ItemIDList or NULL if failed
936 * NOTES
937 * exported by ordinal.
939 LPITEMIDLIST WINAPI ILCreateFromPathA (LPCSTR path)
941 LPITEMIDLIST pidlnew = NULL;
943 TRACE_(shell)("%s\n", debugstr_a(path));
945 if (SUCCEEDED(SHILCreateFromPathA(path, &pidlnew, NULL)))
946 return pidlnew;
947 return NULL;
950 /*************************************************************************
951 * ILCreateFromPathW [SHELL32.190]
953 * See ILCreateFromPathA.
955 LPITEMIDLIST WINAPI ILCreateFromPathW (LPCWSTR path)
957 LPITEMIDLIST pidlnew = NULL;
959 TRACE_(shell)("%s\n", debugstr_w(path));
961 if (SUCCEEDED(SHILCreateFromPathW(path, &pidlnew, NULL)))
962 return pidlnew;
963 return NULL;
966 /*************************************************************************
967 * ILCreateFromPath [SHELL32.157]
969 LPITEMIDLIST WINAPI ILCreateFromPathAW (LPCVOID path)
971 if ( SHELL_OsIsUnicode())
972 return ILCreateFromPathW (path);
973 return ILCreateFromPathA (path);
976 /*************************************************************************
977 * _ILParsePathW [internal]
979 * Creates an ItemIDList from a path and returns it.
981 * PARAMS
982 * path [I] path to parse and convert into an ItemIDList
983 * lpFindFile [I] pointer to buffer to initialize the FileSystem
984 * Bind Data object with
985 * bBindCtx [I] indicates to create a BindContext and assign a
986 * FileSystem Bind Data object
987 * ppidl [O] the newly create ItemIDList
988 * prgfInOut [I/O] requested attributes on input and actual
989 * attributes on return
991 * RETURNS
992 * NO_ERROR on success or an OLE error code
994 * NOTES
995 * If either lpFindFile is non-NULL or bBindCtx is TRUE, this function
996 * creates a BindContext object and assigns a FileSystem Bind Data object
997 * to it, passing the BindContext to IShellFolder_ParseDisplayName. Each
998 * IShellFolder uses that FileSystem Bind Data object of the BindContext
999 * to pass data about the current path element to the next object. This
1000 * is used to avoid having to verify the current path element on disk, so
1001 * that creating an ItemIDList from a nonexistent path still can work.
1003 static HRESULT WINAPI _ILParsePathW(LPCWSTR path, LPWIN32_FIND_DATAW lpFindFile,
1004 BOOL bBindCtx, LPITEMIDLIST *ppidl, LPDWORD prgfInOut)
1006 LPSHELLFOLDER pSF = NULL;
1007 LPBC pBC = NULL;
1008 HRESULT ret;
1010 TRACE("%s %p %d (%p)->%p (%p)->0x%lx\n", debugstr_w(path), lpFindFile, bBindCtx,
1011 ppidl, ppidl ? *ppidl : NULL,
1012 prgfInOut, prgfInOut ? *prgfInOut : 0);
1014 ret = SHGetDesktopFolder(&pSF);
1015 if (FAILED(ret))
1016 return ret;
1018 if (lpFindFile || bBindCtx)
1019 ret = IFileSystemBindData_Constructor(lpFindFile, &pBC);
1021 if (SUCCEEDED(ret))
1023 ret = IShellFolder_ParseDisplayName(pSF, 0, pBC, (LPOLESTR)path, NULL, ppidl, prgfInOut);
1026 if (pBC)
1028 IBindCtx_Release(pBC);
1029 pBC = NULL;
1032 IShellFolder_Release(pSF);
1034 if (!SUCCEEDED(ret) && ppidl)
1035 *ppidl = NULL;
1037 TRACE("%s %p 0x%lx\n", debugstr_w(path), ppidl ? *ppidl : NULL, prgfInOut ? *prgfInOut : 0);
1039 return ret;
1042 /*************************************************************************
1043 * SHSimpleIDListFromPath [SHELL32.162]
1045 * Creates a simple ItemIDList from a path and returns it. This function
1046 * does not fail on nonexistent paths.
1048 * PARAMS
1049 * path [I] path to parse and convert into an ItemIDList
1051 * RETURNS
1052 * the newly created simple ItemIDList
1054 * NOTES
1055 * Simple in the name does not mean a relative ItemIDList but rather a
1056 * fully qualified list, where only the file name is filled in and the
1057 * directory flag for those ItemID elements this is known about, eg.
1058 * it is not the last element in the ItemIDList or the actual directory
1059 * exists on disk.
1060 * exported by ordinal.
1062 LPITEMIDLIST WINAPI SHSimpleIDListFromPathA(LPCSTR lpszPath)
1064 LPITEMIDLIST pidl = NULL;
1065 LPWSTR wPath = NULL;
1066 int len;
1068 TRACE("%s\n", debugstr_a(lpszPath));
1070 if (lpszPath)
1072 len = MultiByteToWideChar(CP_ACP, 0, lpszPath, -1, NULL, 0);
1073 wPath = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1074 MultiByteToWideChar(CP_ACP, 0, lpszPath, -1, wPath, len);
1077 _ILParsePathW(wPath, NULL, TRUE, &pidl, NULL);
1079 HeapFree(GetProcessHeap(), 0, wPath);
1080 TRACE("%s %p\n", debugstr_a(lpszPath), pidl);
1081 return pidl;
1084 LPITEMIDLIST WINAPI SHSimpleIDListFromPathW(LPCWSTR lpszPath)
1086 LPITEMIDLIST pidl = NULL;
1088 TRACE("%s\n", debugstr_w(lpszPath));
1090 _ILParsePathW(lpszPath, NULL, TRUE, &pidl, NULL);
1091 TRACE("%s %p\n", debugstr_w(lpszPath), pidl);
1092 return pidl;
1095 LPITEMIDLIST WINAPI SHSimpleIDListFromPathAW(LPCVOID lpszPath)
1097 if ( SHELL_OsIsUnicode())
1098 return SHSimpleIDListFromPathW (lpszPath);
1099 return SHSimpleIDListFromPathA (lpszPath);
1102 /*************************************************************************
1103 * SHGetDataFromIDListA [SHELL32.247]
1105 * NOTES
1106 * the pidl can be a simple one. since we can't get the path out of the pidl
1107 * we have to take all data from the pidl
1109 HRESULT WINAPI SHGetDataFromIDListA(LPSHELLFOLDER psf, LPCITEMIDLIST pidl,
1110 int nFormat, LPVOID dest, int len)
1112 LPSTR filename, shortname;
1113 WIN32_FIND_DATAA * pfd;
1115 TRACE_(shell)("sf=%p pidl=%p 0x%04x %p 0x%04x stub\n",psf,pidl,nFormat,dest,len);
1117 pdump(pidl);
1118 if (!psf || !dest)
1119 return E_INVALIDARG;
1121 switch (nFormat)
1123 case SHGDFIL_FINDDATA:
1124 pfd = dest;
1126 if (_ILIsDrive(pidl) || _ILIsSpecialFolder(pidl))
1127 return E_INVALIDARG;
1129 if (len < sizeof(WIN32_FIND_DATAA))
1130 return E_INVALIDARG;
1132 ZeroMemory(pfd, sizeof (WIN32_FIND_DATAA));
1133 _ILGetFileDateTime( pidl, &(pfd->ftLastWriteTime));
1134 pfd->dwFileAttributes = _ILGetFileAttributes(pidl, NULL, 0);
1135 pfd->nFileSizeLow = _ILGetFileSize ( pidl, NULL, 0);
1137 filename = _ILGetTextPointer(pidl);
1138 shortname = _ILGetSTextPointer(pidl);
1140 if (filename)
1141 lstrcpynA(pfd->cFileName, filename, MAX_PATH);
1142 else
1143 pfd->cFileName[0] = '\0';
1145 if (shortname)
1146 lstrcpynA(pfd->cAlternateFileName, shortname, MAX_PATH);
1147 else
1148 pfd->cAlternateFileName[0] = '\0';
1149 return NOERROR;
1151 case SHGDFIL_NETRESOURCE:
1152 case SHGDFIL_DESCRIPTIONID:
1153 FIXME_(shell)("SHGDFIL %i stub\n", nFormat);
1154 break;
1156 default:
1157 ERR_(shell)("Unknown SHGDFIL %i, please report\n", nFormat);
1160 return E_INVALIDARG;
1163 /*************************************************************************
1164 * SHGetDataFromIDListW [SHELL32.248]
1167 HRESULT WINAPI SHGetDataFromIDListW(LPSHELLFOLDER psf, LPCITEMIDLIST pidl,
1168 int nFormat, LPVOID dest, int len)
1170 LPSTR filename, shortname;
1171 WIN32_FIND_DATAW * pfd = dest;
1173 TRACE_(shell)("sf=%p pidl=%p 0x%04x %p 0x%04x stub\n",psf,pidl,nFormat,dest,len);
1175 pdump(pidl);
1177 if (!psf || !dest)
1178 return E_INVALIDARG;
1180 switch (nFormat)
1182 case SHGDFIL_FINDDATA:
1183 pfd = dest;
1185 if (_ILIsDrive(pidl))
1186 return E_INVALIDARG;
1188 if (len < sizeof(WIN32_FIND_DATAW))
1189 return E_INVALIDARG;
1191 ZeroMemory(pfd, sizeof (WIN32_FIND_DATAA));
1192 _ILGetFileDateTime( pidl, &(pfd->ftLastWriteTime));
1193 pfd->dwFileAttributes = _ILGetFileAttributes(pidl, NULL, 0);
1194 pfd->nFileSizeLow = _ILGetFileSize ( pidl, NULL, 0);
1196 filename = _ILGetTextPointer(pidl);
1197 shortname = _ILGetSTextPointer(pidl);
1199 if (!filename)
1200 pfd->cFileName[0] = '\0';
1201 else if (!MultiByteToWideChar(CP_ACP, 0, filename, -1, pfd->cFileName, MAX_PATH))
1202 pfd->cFileName[MAX_PATH-1] = 0;
1204 if (!shortname)
1205 pfd->cAlternateFileName[0] = '\0';
1206 else if (!MultiByteToWideChar(CP_ACP, 0, shortname, -1, pfd->cAlternateFileName, 14))
1207 pfd->cAlternateFileName[13] = 0;
1208 return NOERROR;
1210 case SHGDFIL_NETRESOURCE:
1211 case SHGDFIL_DESCRIPTIONID:
1212 FIXME_(shell)("SHGDFIL %i stub\n", nFormat);
1213 break;
1215 default:
1216 ERR_(shell)("Unknown SHGDFIL %i, please report\n", nFormat);
1219 return E_INVALIDARG;
1222 /*************************************************************************
1223 * SHGetPathFromIDListA [SHELL32.@][NT 4.0: SHELL32.220]
1225 * PARAMETERS
1226 * pidl, [IN] pidl
1227 * pszPath [OUT] path
1229 * RETURNS
1230 * path from a passed PIDL.
1232 * NOTES
1233 * NULL returns FALSE
1234 * desktop pidl gives path to desktop directory back
1235 * special pidls returning FALSE
1237 BOOL WINAPI SHGetPathFromIDListA(LPCITEMIDLIST pidl, LPSTR pszPath)
1239 WCHAR wszPath[MAX_PATH];
1240 BOOL bSuccess;
1242 bSuccess = SHGetPathFromIDListW(pidl, wszPath);
1243 if (bSuccess)
1244 WideCharToMultiByte(CP_ACP, 0, wszPath, -1, pszPath, MAX_PATH, NULL, NULL);
1246 return bSuccess;
1249 /*************************************************************************
1250 * SHGetPathFromIDListW [SHELL32.@]
1252 * See SHGetPathFromIDListA.
1254 BOOL WINAPI SHGetPathFromIDListW(LPCITEMIDLIST pidl, LPWSTR pszPath)
1256 HRESULT hr;
1257 LPCITEMIDLIST pidlLast;
1258 LPSHELLFOLDER psfFolder;
1259 DWORD dwAttributes;
1260 STRRET strret;
1262 TRACE_(shell)("(pidl=%p,%p)\n", pidl, pszPath);
1263 pdump(pidl);
1265 if (!pidl)
1266 return FALSE;
1268 hr = SHBindToParent(pidl, &IID_IShellFolder, (VOID**)&psfFolder, &pidlLast);
1269 if (FAILED(hr)) return FALSE;
1271 dwAttributes = SFGAO_FILESYSTEM;
1272 hr = IShellFolder_GetAttributesOf(psfFolder, 1, &pidlLast, &dwAttributes);
1273 if (FAILED(hr) || !(dwAttributes & SFGAO_FILESYSTEM)) {
1274 IShellFolder_Release(psfFolder);
1275 return FALSE;
1278 hr = IShellFolder_GetDisplayNameOf(psfFolder, pidlLast, SHGDN_FORPARSING, &strret);
1279 IShellFolder_Release(psfFolder);
1280 if (FAILED(hr)) return FALSE;
1282 hr = StrRetToBufW(&strret, pidlLast, pszPath, MAX_PATH);
1284 TRACE_(shell)("-- %s, 0x%08lx\n",debugstr_w(pszPath), hr);
1285 return SUCCEEDED(hr);
1288 /*************************************************************************
1289 * SHBindToParent [shell version 5.0]
1291 HRESULT WINAPI SHBindToParent(LPCITEMIDLIST pidl, REFIID riid, LPVOID *ppv, LPCITEMIDLIST *ppidlLast)
1293 IShellFolder * psfDesktop;
1294 HRESULT hr=E_FAIL;
1296 TRACE_(shell)("pidl=%p\n", pidl);
1297 pdump(pidl);
1299 if (!pidl || !ppv)
1300 return E_INVALIDARG;
1302 *ppv = NULL;
1303 if (ppidlLast)
1304 *ppidlLast = NULL;
1306 hr = SHGetDesktopFolder(&psfDesktop);
1307 if (FAILED(hr))
1308 return hr;
1310 if (_ILIsPidlSimple(pidl))
1312 /* we are on desktop level */
1313 hr = IShellFolder_QueryInterface(psfDesktop, riid, ppv);
1315 else
1317 LPITEMIDLIST pidlParent = ILClone(pidl);
1318 ILRemoveLastID(pidlParent);
1319 hr = IShellFolder_BindToObject(psfDesktop, pidlParent, NULL, riid, ppv);
1320 SHFree (pidlParent);
1323 IShellFolder_Release(psfDesktop);
1325 if (SUCCEEDED(hr) && ppidlLast)
1326 *ppidlLast = ILFindLastID(pidl);
1328 TRACE_(shell)("-- psf=%p pidl=%p ret=0x%08lx\n", *ppv, (ppidlLast)?*ppidlLast:NULL, hr);
1329 return hr;
1332 /**************************************************************************
1334 * internal functions
1336 * ### 1. section creating pidls ###
1338 *************************************************************************
1340 LPITEMIDLIST _ILAlloc(PIDLTYPE type, unsigned int size)
1342 LPITEMIDLIST pidlOut = NULL;
1344 pidlOut = SHAlloc(size + 5);
1345 if(pidlOut)
1347 LPPIDLDATA pData;
1348 LPITEMIDLIST pidlNext;
1350 ZeroMemory(pidlOut, size + 5);
1351 pidlOut->mkid.cb = size + 3;
1353 pData = _ILGetDataPointer(pidlOut);
1354 if (pData)
1355 pData->type = type;
1357 pidlNext = ILGetNext(pidlOut);
1358 if (pidlNext)
1359 pidlNext->mkid.cb = 0x00;
1360 TRACE("-- (pidl=%p, size=%u)\n", pidlOut, size);
1363 return pidlOut;
1366 LPITEMIDLIST _ILCreateDesktop()
1368 LPITEMIDLIST ret;
1370 TRACE("()\n");
1371 ret = SHAlloc(2);
1372 if (ret)
1373 ret->mkid.cb = 0;
1374 return ret;
1377 LPITEMIDLIST _ILCreateMyComputer()
1379 TRACE("()\n");
1380 return _ILCreateGuid(PT_GUID, &CLSID_MyComputer);
1383 LPITEMIDLIST _ILCreateMyDocuments()
1385 TRACE("()\n");
1386 return _ILCreateGuid(PT_GUID, &CLSID_MyDocuments);
1389 LPITEMIDLIST _ILCreateIExplore()
1391 TRACE("()\n");
1392 return _ILCreateGuid(PT_GUID, &CLSID_Internet);
1395 LPITEMIDLIST _ILCreateControlPanel()
1397 LPITEMIDLIST parent = _ILCreateGuid(PT_GUID, &CLSID_MyComputer), ret = NULL;
1399 TRACE("()\n");
1400 if (parent)
1402 LPITEMIDLIST cpl = _ILCreateGuid(PT_SHELLEXT, &CLSID_ControlPanel);
1404 if (cpl)
1406 ret = ILCombine(parent, cpl);
1407 SHFree(cpl);
1409 SHFree(parent);
1411 return ret;
1414 LPITEMIDLIST _ILCreatePrinters()
1416 LPITEMIDLIST parent = _ILCreateGuid(PT_GUID, &CLSID_MyComputer), ret = NULL;
1418 TRACE("()\n");
1419 if (parent)
1421 LPITEMIDLIST printers = _ILCreateGuid(PT_YAGUID, &CLSID_Printers);
1423 if (printers)
1425 ret = ILCombine(parent, printers);
1426 SHFree(printers);
1428 SHFree(parent);
1430 return ret;
1433 LPITEMIDLIST _ILCreateNetwork()
1435 TRACE("()\n");
1436 return _ILCreateGuid(PT_GUID, &CLSID_NetworkPlaces);
1439 LPITEMIDLIST _ILCreateBitBucket()
1441 TRACE("()\n");
1442 return _ILCreateGuid(PT_GUID, &CLSID_RecycleBin);
1445 LPITEMIDLIST _ILCreateGuid(PIDLTYPE type, REFIID guid)
1447 LPITEMIDLIST pidlOut;
1449 if (type == PT_SHELLEXT || type == PT_GUID || type == PT_YAGUID)
1451 pidlOut = _ILAlloc(type, sizeof(GUIDStruct));
1452 if (pidlOut)
1454 LPPIDLDATA pData = _ILGetDataPointer(pidlOut);
1456 memcpy(&(pData->u.guid.guid), guid, sizeof(GUID));
1457 TRACE("-- create GUID-pidl %s\n",
1458 debugstr_guid(&(pData->u.guid.guid)));
1461 else
1463 WARN("%d: invalid type for GUID\n", type);
1464 pidlOut = NULL;
1466 return pidlOut;
1469 LPITEMIDLIST _ILCreateGuidFromStrA(LPCSTR szGUID)
1471 IID iid;
1473 if (!SUCCEEDED(SHCLSIDFromStringA(szGUID, &iid)))
1475 ERR("%s is not a GUID\n", szGUID);
1476 return NULL;
1478 return _ILCreateGuid(PT_GUID, &iid);
1481 LPITEMIDLIST _ILCreateGuidFromStrW(LPCWSTR szGUID)
1483 IID iid;
1485 if (!SUCCEEDED(SHCLSIDFromStringW(szGUID, &iid)))
1487 ERR("%s is not a GUID\n", debugstr_w(szGUID));
1488 return NULL;
1490 return _ILCreateGuid(PT_GUID, &iid);
1493 LPITEMIDLIST _ILCreateFromFindDataW( WIN32_FIND_DATAW *wfd )
1495 /* FIXME: should make unicode PIDLs */
1496 WIN32_FIND_DATAA fda;
1498 memset( &fda, 0, sizeof fda );
1499 fda.dwFileAttributes = wfd->dwFileAttributes;
1500 fda.ftCreationTime = wfd->ftCreationTime;
1501 fda.ftLastAccessTime = wfd->ftLastAccessTime;
1502 fda.ftLastWriteTime = wfd->ftLastWriteTime;
1503 fda.nFileSizeHigh = wfd->nFileSizeHigh;
1504 fda.nFileSizeLow = wfd->nFileSizeLow;
1505 fda.dwReserved0 = wfd->dwReserved0;
1506 fda.dwReserved1 = wfd->dwReserved1;
1507 WideCharToMultiByte( CP_ACP, 0, wfd->cFileName, -1,
1508 fda.cFileName, MAX_PATH, NULL, NULL );
1509 return _ILCreateFromFindDataA( &fda );
1512 LPITEMIDLIST _ILCreateFromFindDataA(WIN32_FIND_DATAA * stffile )
1514 char buff[MAX_PATH + 14 +1]; /* see WIN32_FIND_DATA */
1515 char * pbuff = buff;
1516 size_t len, len1;
1517 LPITEMIDLIST pidl;
1518 PIDLTYPE type;
1520 if (!stffile)
1521 return NULL;
1523 TRACE("(%s, %s)\n",stffile->cAlternateFileName, stffile->cFileName);
1525 /* prepare buffer with both names */
1526 len = strlen (stffile->cFileName) + 1;
1527 memcpy (pbuff, stffile->cFileName, len);
1528 pbuff += len;
1530 len1 = strlen (stffile->cAlternateFileName)+1;
1531 memcpy (pbuff, stffile->cAlternateFileName, len1);
1533 type = (stffile->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? PT_FOLDER : PT_VALUE;
1536 * FileStruct already has one byte for the first name, so use len - 1 in
1537 * size calculation
1539 pidl = _ILAlloc(type, sizeof(FileStruct) + (len - 1) + len1);
1540 if (pidl)
1542 LPPIDLDATA pData;
1543 LPSTR pszDest;
1545 /* set attributes */
1546 pData = _ILGetDataPointer(pidl);
1547 if (pData)
1549 pData->type = type;
1550 FileTimeToDosDateTime( &(stffile->ftLastWriteTime),
1551 &pData->u.file.uFileDate, &pData->u.file.uFileTime);
1552 pData->u.file.dwFileSize = stffile->nFileSizeLow;
1553 pData->u.file.uFileAttribs = (WORD)stffile->dwFileAttributes;
1555 pszDest = _ILGetTextPointer(pidl);
1556 if (pszDest)
1558 memcpy(pszDest, buff, len + len1);
1559 TRACE("-- create Value: %s\n",debugstr_a(pszDest));
1562 return pidl;
1565 HRESULT _ILCreateFromPathA(LPCSTR szPath, LPITEMIDLIST* ppidl)
1567 HANDLE hFile;
1568 WIN32_FIND_DATAA stffile;
1570 if (!ppidl)
1571 return E_INVALIDARG;
1573 hFile = FindFirstFileA(szPath, &stffile);
1574 if (hFile == INVALID_HANDLE_VALUE)
1575 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
1577 FindClose(hFile);
1579 *ppidl = _ILCreateFromFindDataA(&stffile);
1581 return *ppidl ? S_OK : E_OUTOFMEMORY;
1584 HRESULT _ILCreateFromPathW(LPCWSTR szPath, LPITEMIDLIST* ppidl)
1586 HANDLE hFile;
1587 WIN32_FIND_DATAW stffile;
1589 if (!ppidl)
1590 return E_INVALIDARG;
1592 hFile = FindFirstFileW(szPath, &stffile);
1593 if (hFile == INVALID_HANDLE_VALUE)
1594 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
1596 FindClose(hFile);
1598 *ppidl = _ILCreateFromFindDataW(&stffile);
1600 return *ppidl ? S_OK : E_OUTOFMEMORY;
1603 LPITEMIDLIST _ILCreateDrive(LPCWSTR lpszNew)
1605 LPITEMIDLIST pidlOut;
1607 TRACE("(%s)\n",debugstr_w(lpszNew));
1609 pidlOut = _ILAlloc(PT_DRIVE, sizeof(DriveStruct));
1610 if (pidlOut)
1612 LPSTR pszDest;
1614 pszDest = _ILGetTextPointer(pidlOut);
1615 if (pszDest)
1617 strcpy(pszDest, "x:\\");
1618 pszDest[0]=toupperW(lpszNew[0]);
1619 TRACE("-- create Drive: %s\n", debugstr_a(pszDest));
1622 return pidlOut;
1625 /**************************************************************************
1626 * _ILGetDrive()
1628 * Gets the text for the drive eg. 'c:\'
1630 * RETURNS
1631 * strlen (lpszText)
1633 DWORD _ILGetDrive(LPCITEMIDLIST pidl,LPSTR pOut, UINT uSize)
1635 TRACE("(%p,%p,%u)\n",pidl,pOut,uSize);
1637 if(_ILIsMyComputer(pidl))
1638 pidl = ILGetNext(pidl);
1640 if (pidl && _ILIsDrive(pidl))
1641 return _ILSimpleGetText(pidl, pOut, uSize);
1643 return 0;
1646 /**************************************************************************
1648 * ### 2. section testing pidls ###
1650 **************************************************************************
1651 * _ILIsDesktop()
1652 * _ILIsMyComputer()
1653 * _ILIsSpecialFolder()
1654 * _ILIsDrive()
1655 * _ILIsFolder()
1656 * _ILIsValue()
1657 * _ILIsPidlSimple()
1659 BOOL _ILIsDesktop(LPCITEMIDLIST pidl)
1661 TRACE("(%p)\n",pidl);
1663 return pidl && pidl->mkid.cb ? 0 : 1;
1666 BOOL _ILIsMyComputer(LPCITEMIDLIST pidl)
1668 REFIID iid = _ILGetGUIDPointer(pidl);
1670 TRACE("(%p)\n",pidl);
1672 if (iid)
1673 return IsEqualIID(iid, &CLSID_MyComputer);
1674 return FALSE;
1677 BOOL _ILIsSpecialFolder (LPCITEMIDLIST pidl)
1679 LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1681 TRACE("(%p)\n",pidl);
1683 return (pidl && ( (lpPData && (PT_GUID== lpPData->type || PT_SHELLEXT== lpPData->type || PT_YAGUID == lpPData->type)) ||
1684 (pidl && pidl->mkid.cb == 0x00)
1688 BOOL _ILIsDrive(LPCITEMIDLIST pidl)
1690 LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1692 TRACE("(%p)\n",pidl);
1694 return (pidl && lpPData && (PT_DRIVE == lpPData->type ||
1695 PT_DRIVE1 == lpPData->type ||
1696 PT_DRIVE2 == lpPData->type ||
1697 PT_DRIVE3 == lpPData->type));
1700 BOOL _ILIsFolder(LPCITEMIDLIST pidl)
1702 LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1704 TRACE("(%p)\n",pidl);
1706 return (pidl && lpPData && (PT_FOLDER == lpPData->type || PT_FOLDER1 == lpPData->type));
1709 BOOL _ILIsValue(LPCITEMIDLIST pidl)
1711 LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1713 TRACE("(%p)\n",pidl);
1715 return (pidl && lpPData && PT_VALUE == lpPData->type);
1718 BOOL _ILIsCPanelStruct(LPCITEMIDLIST pidl)
1720 LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1722 TRACE("(%p)\n",pidl);
1724 return (pidl && lpPData && (lpPData->type == 0));
1727 /**************************************************************************
1728 * _ILIsPidlSimple
1730 BOOL _ILIsPidlSimple(LPCITEMIDLIST pidl)
1732 BOOL ret = TRUE;
1734 if(! _ILIsDesktop(pidl)) /* pidl=NULL or mkid.cb=0 */
1736 WORD len = pidl->mkid.cb;
1737 LPCITEMIDLIST pidlnext = (LPCITEMIDLIST) (((const BYTE*)pidl) + len );
1739 if (pidlnext->mkid.cb)
1740 ret = FALSE;
1743 TRACE("%s\n", ret ? "Yes" : "No");
1744 return ret;
1747 /**************************************************************************
1749 * ### 3. section getting values from pidls ###
1752 /**************************************************************************
1753 * _ILSimpleGetText
1755 * gets the text for the first item in the pidl (eg. simple pidl)
1757 * returns the length of the string
1759 DWORD _ILSimpleGetText (LPCITEMIDLIST pidl, LPSTR szOut, UINT uOutSize)
1761 DWORD dwReturn=0;
1762 LPSTR szSrc;
1763 GUID const * riid;
1764 char szTemp[MAX_PATH];
1766 TRACE("(%p %p %x)\n",pidl,szOut,uOutSize);
1768 if (!pidl)
1769 return 0;
1771 if (szOut)
1772 *szOut = 0;
1774 if (_ILIsDesktop(pidl))
1776 /* desktop */
1777 if (HCR_GetClassNameA(&CLSID_ShellDesktop, szTemp, MAX_PATH))
1779 if (szOut)
1780 lstrcpynA(szOut, szTemp, uOutSize);
1782 dwReturn = strlen (szTemp);
1785 else if (( szSrc = _ILGetTextPointer(pidl) ))
1787 /* filesystem */
1788 if (szOut)
1789 lstrcpynA(szOut, szSrc, uOutSize);
1791 dwReturn = strlen(szSrc);
1793 else if (( riid = _ILGetGUIDPointer(pidl) ))
1795 /* special folder */
1796 if ( HCR_GetClassNameA(riid, szTemp, MAX_PATH) )
1798 if (szOut)
1799 lstrcpynA(szOut, szTemp, uOutSize);
1801 dwReturn = strlen (szTemp);
1804 else
1806 ERR("-- no text\n");
1809 TRACE("-- (%p=%s 0x%08lx)\n",szOut,debugstr_a(szOut),dwReturn);
1810 return dwReturn;
1813 /**************************************************************************
1814 * _ILSimpleGetTextW
1816 * gets the text for the first item in the pidl (eg. simple pidl)
1818 * returns the length of the string
1820 DWORD _ILSimpleGetTextW (LPCITEMIDLIST pidl, LPWSTR szOut, UINT uOutSize)
1822 DWORD dwReturn;
1823 char szTemp[MAX_PATH];
1824 FileStructW *pFileStructW = _ILGetFileStructW(pidl);
1826 TRACE("(%p %p %x)\n",pidl,szOut,uOutSize);
1828 if (pFileStructW) {
1829 lstrcpynW(szOut, pFileStructW->wszName, uOutSize);
1830 dwReturn = lstrlenW(pFileStructW->wszName);
1831 } else {
1832 dwReturn = _ILSimpleGetText(pidl, szTemp, MAX_PATH);
1834 if (!MultiByteToWideChar(CP_ACP, 0, szTemp, -1, szOut, uOutSize))
1835 *szOut = 0;
1838 TRACE("-- (%p=%s 0x%08lx)\n",szOut,debugstr_w(szOut),dwReturn);
1839 return dwReturn;
1842 /**************************************************************************
1844 * ### 4. getting pointers to parts of pidls ###
1846 **************************************************************************
1847 * _ILGetDataPointer()
1849 LPPIDLDATA _ILGetDataPointer(LPCITEMIDLIST pidl)
1851 if(pidl && pidl->mkid.cb != 0x00)
1852 return (LPPIDLDATA) &(pidl->mkid.abID);
1853 return NULL;
1856 /**************************************************************************
1857 * _ILGetTextPointer()
1858 * gets a pointer to the long filename string stored in the pidl
1860 LPSTR _ILGetTextPointer(LPCITEMIDLIST pidl)
1862 /* TRACE(pidl,"(pidl%p)\n", pidl);*/
1864 LPPIDLDATA pdata = _ILGetDataPointer(pidl);
1866 if (!pdata)
1867 return NULL;
1869 switch (pdata->type)
1871 case PT_GUID:
1872 case PT_SHELLEXT:
1873 case PT_YAGUID:
1874 return NULL;
1876 case PT_DRIVE:
1877 case PT_DRIVE1:
1878 case PT_DRIVE2:
1879 case PT_DRIVE3:
1880 return (LPSTR)&(pdata->u.drive.szDriveName);
1882 case PT_FOLDER:
1883 case PT_FOLDER1:
1884 case PT_VALUE:
1885 case PT_IESPECIAL1:
1886 case PT_IESPECIAL2:
1887 return (LPSTR)&(pdata->u.file.szNames);
1889 case PT_WORKGRP:
1890 case PT_COMP:
1891 case PT_NETWORK:
1892 case PT_NETPROVIDER:
1893 case PT_SHARE:
1894 return (LPSTR)&(pdata->u.network.szNames);
1896 return NULL;
1899 /**************************************************************************
1900 * _ILGetSTextPointer()
1901 * gets a pointer to the short filename string stored in the pidl
1903 LPSTR _ILGetSTextPointer(LPCITEMIDLIST pidl)
1905 /* TRACE(pidl,"(pidl%p)\n", pidl); */
1907 LPPIDLDATA pdata =_ILGetDataPointer(pidl);
1909 if (!pdata)
1910 return NULL;
1912 switch (pdata->type)
1914 case PT_FOLDER:
1915 case PT_VALUE:
1916 case PT_IESPECIAL1:
1917 case PT_IESPECIAL2:
1918 return (LPSTR)(pdata->u.file.szNames + strlen (pdata->u.file.szNames) + 1);
1920 case PT_WORKGRP:
1921 return (LPSTR)(pdata->u.network.szNames + strlen (pdata->u.network.szNames) + 1);
1923 return NULL;
1926 /**************************************************************************
1927 * _ILGetGUIDPointer()
1929 * returns reference to guid stored in some pidls
1931 IID* _ILGetGUIDPointer(LPCITEMIDLIST pidl)
1933 LPPIDLDATA pdata =_ILGetDataPointer(pidl);
1935 TRACE("%p\n", pidl);
1937 if (!pdata)
1938 return NULL;
1940 TRACE("pdata->type 0x%04x\n", pdata->type);
1941 switch (pdata->type)
1943 case PT_SHELLEXT:
1944 case PT_GUID:
1945 case PT_YAGUID:
1946 return &(pdata->u.guid.guid);
1948 default:
1949 TRACE("Unknown pidl type 0x%04x\n", pdata->type);
1950 break;
1952 return NULL;
1955 /******************************************************************************
1956 * _ILGetFileStructW [Internal]
1958 * Get pointer the a SHITEMID's FileStructW field if present
1960 * PARAMS
1961 * pidl [I] The SHITEMID
1963 * RETURNS
1964 * Success: Pointer to pidl's FileStructW field.
1965 * Failure: NULL
1967 FileStructW* _ILGetFileStructW(LPCITEMIDLIST pidl) {
1968 FileStructW *pFileStructW;
1969 WORD cbOffset;
1971 if (!(_ILIsValue(pidl) || _ILIsFolder(pidl)))
1972 return NULL;
1974 cbOffset = *(WORD*)((LPBYTE)pidl + pidl->mkid.cb - sizeof(WORD));
1975 pFileStructW = (FileStructW*)((LPBYTE)pidl + cbOffset);
1977 /* Currently I don't see a fool prove way to figure out if a pidl is for sure of WinXP
1978 * style with a FileStructW member. If we switch all our shellfolder-implementations to
1979 * the new format, this won't be a problem. For now, we do as many sanity checks as possible. */
1980 if (cbOffset & 0x1 || /* FileStructW member is word aligned in the pidl */
1981 /* FileStructW is positioned after FileStruct */
1982 cbOffset < sizeof(pidl->mkid.cb) + sizeof(PIDLTYPE) + sizeof(FileStruct) ||
1983 /* There has to be enough space at cbOffset in the pidl to hold FileStructW and cbOffset */
1984 cbOffset > pidl->mkid.cb - sizeof(cbOffset) - sizeof(FileStructW) ||
1985 pidl->mkid.cb != cbOffset + pFileStructW->cbLen)
1987 WARN("Invalid pidl format (cbOffset = %d)!\n", cbOffset);
1988 return NULL;
1991 return pFileStructW;
1994 /*************************************************************************
1995 * _ILGetFileDateTime
1997 * Given the ItemIdList, get the FileTime
1999 * PARAMS
2000 * pidl [I] The ItemIDList
2001 * pFt [I] the resulted FILETIME of the file
2003 * RETURNS
2004 * True if Successful
2006 * NOTES
2009 BOOL _ILGetFileDateTime(LPCITEMIDLIST pidl, FILETIME *pFt)
2011 LPPIDLDATA pdata = _ILGetDataPointer(pidl);
2013 if (!pdata)
2014 return FALSE;
2016 switch (pdata->type)
2018 case PT_FOLDER:
2019 case PT_VALUE:
2020 DosDateTimeToFileTime(pdata->u.file.uFileDate, pdata->u.file.uFileTime, pFt);
2021 break;
2022 default:
2023 return FALSE;
2025 return TRUE;
2028 BOOL _ILGetFileDate (LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2030 FILETIME ft,lft;
2031 SYSTEMTIME time;
2032 BOOL ret;
2034 if (_ILGetFileDateTime( pidl, &ft ))
2036 FileTimeToLocalFileTime(&ft, &lft);
2037 FileTimeToSystemTime (&lft, &time);
2039 ret = GetDateFormatA(LOCALE_USER_DEFAULT,DATE_SHORTDATE,&time, NULL, pOut, uOutSize);
2040 if (ret)
2042 /* Append space + time without seconds */
2043 pOut[ret-1] = ' ';
2044 GetTimeFormatA(LOCALE_USER_DEFAULT, TIME_NOSECONDS, &time, NULL, &pOut[ret], uOutSize - ret);
2047 else
2049 pOut[0] = '\0';
2050 ret = FALSE;
2052 return ret;
2055 /*************************************************************************
2056 * _ILGetFileSize
2058 * Given the ItemIdList, get the FileSize
2060 * PARAMS
2061 * pidl [I] The ItemIDList
2062 * pOut [I] The buffer to save the result
2063 * uOutsize [I] The size of the buffer
2065 * RETURNS
2066 * The FileSize
2068 * NOTES
2069 * pOut can be null when no string is needed
2072 DWORD _ILGetFileSize (LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2074 LPPIDLDATA pdata = _ILGetDataPointer(pidl);
2075 DWORD dwSize;
2077 if (!pdata)
2078 return 0;
2080 switch (pdata->type)
2082 case PT_VALUE:
2083 dwSize = pdata->u.file.dwFileSize;
2084 if (pOut)
2085 StrFormatByteSizeA(dwSize, pOut, uOutSize);
2086 return dwSize;
2088 if (pOut)
2089 *pOut = 0x00;
2090 return 0;
2093 BOOL _ILGetExtension (LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2095 char szTemp[MAX_PATH];
2096 const char * pPoint;
2097 LPCITEMIDLIST pidlTemp=pidl;
2099 TRACE("pidl=%p\n",pidl);
2101 if (!pidl)
2102 return FALSE;
2104 pidlTemp = ILFindLastID(pidl);
2106 if (!_ILIsValue(pidlTemp))
2107 return FALSE;
2108 if (!_ILSimpleGetText(pidlTemp, szTemp, MAX_PATH))
2109 return FALSE;
2111 pPoint = PathFindExtensionA(szTemp);
2113 if (!*pPoint)
2114 return FALSE;
2116 pPoint++;
2117 lstrcpynA(pOut, pPoint, uOutSize);
2118 TRACE("%s\n",pOut);
2120 return TRUE;
2123 /*************************************************************************
2124 * _ILGetFileType
2126 * Given the ItemIdList, get the file type description
2128 * PARAMS
2129 * pidl [I] The ItemIDList (simple)
2130 * pOut [I] The buffer to save the result
2131 * uOutsize [I] The size of the buffer
2133 * RETURNS
2134 * nothing
2136 * NOTES
2137 * This function copies as much as possible into the buffer.
2139 void _ILGetFileType(LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2141 if(_ILIsValue(pidl))
2143 char sTemp[64];
2145 if(uOutSize > 0)
2146 pOut[0] = 0;
2147 if (_ILGetExtension (pidl, sTemp, 64))
2149 if (!( HCR_MapTypeToValueA(sTemp, sTemp, 64, TRUE)
2150 && HCR_MapTypeToValueA(sTemp, pOut, uOutSize, FALSE )))
2152 lstrcpynA (pOut, sTemp, uOutSize - 6);
2153 strcat (pOut, "-file");
2157 else
2158 lstrcpynA(pOut, "Folder", uOutSize);
2161 /*************************************************************************
2162 * _ILGetFileAttributes
2164 * Given the ItemIdList, get the Attrib string format
2166 * PARAMS
2167 * pidl [I] The ItemIDList
2168 * pOut [I] The buffer to save the result
2169 * uOutsize [I] The size of the Buffer
2171 * RETURNS
2172 * Attributes
2174 * FIXME
2175 * return value 0 in case of error is a valid return value
2178 DWORD _ILGetFileAttributes(LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2180 LPPIDLDATA pData = _ILGetDataPointer(pidl);
2181 WORD wAttrib = 0;
2182 int i;
2184 if (!pData)
2185 return 0;
2187 switch(pData->type)
2189 case PT_FOLDER:
2190 case PT_VALUE:
2191 wAttrib = pData->u.file.uFileAttribs;
2192 break;
2195 if(uOutSize >= 6)
2197 i=0;
2198 if(wAttrib & FILE_ATTRIBUTE_READONLY)
2199 pOut[i++] = 'R';
2200 if(wAttrib & FILE_ATTRIBUTE_HIDDEN)
2201 pOut[i++] = 'H';
2202 if(wAttrib & FILE_ATTRIBUTE_SYSTEM)
2203 pOut[i++] = 'S';
2204 if(wAttrib & FILE_ATTRIBUTE_ARCHIVE)
2205 pOut[i++] = 'A';
2206 if(wAttrib & FILE_ATTRIBUTE_COMPRESSED)
2207 pOut[i++] = 'C';
2208 pOut[i] = 0x00;
2210 return wAttrib;
2213 /*************************************************************************
2214 * ILFreeaPidl
2216 * free a aPidl struct
2218 void _ILFreeaPidl(LPITEMIDLIST * apidl, UINT cidl)
2220 UINT i;
2222 if (apidl)
2224 for (i = 0; i < cidl; i++)
2225 SHFree(apidl[i]);
2226 SHFree(apidl);
2230 /*************************************************************************
2231 * ILCopyaPidl
2233 * copies an aPidl struct
2235 LPITEMIDLIST* _ILCopyaPidl(LPCITEMIDLIST * apidlsrc, UINT cidl)
2237 UINT i;
2238 LPITEMIDLIST *apidldest;
2240 apidldest = SHAlloc(cidl * sizeof(LPITEMIDLIST));
2241 if (!apidlsrc)
2242 return NULL;
2244 for (i = 0; i < cidl; i++)
2245 apidldest[i] = ILClone(apidlsrc[i]);
2247 return apidldest;
2250 /*************************************************************************
2251 * _ILCopyCidaToaPidl
2253 * creates aPidl from CIDA
2255 LPITEMIDLIST* _ILCopyCidaToaPidl(LPITEMIDLIST* pidl, LPIDA cida)
2257 UINT i;
2258 LPITEMIDLIST *dst;
2260 dst = SHAlloc(cida->cidl * sizeof(LPITEMIDLIST));
2261 if (!dst)
2262 return NULL;
2264 if (pidl)
2265 *pidl = ILClone((LPITEMIDLIST)(&((LPBYTE)cida)[cida->aoffset[0]]));
2267 for (i = 0; i < cida->cidl; i++)
2268 dst[i] = ILClone((LPITEMIDLIST)(&((LPBYTE)cida)[cida->aoffset[i + 1]]));
2270 return dst;