ucrtbase: Add support for r-value demangling in unDName.
[wine.git] / dlls / shell32 / pidl.c
blobf22c62756657bbdf7b5f0868e0e72b6a18143018
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 <ctype.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
30 #define COBJMACROS
31 #define NONAMELESSUNION
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winreg.h"
36 #include "objbase.h"
37 #include "shlguid.h"
38 #include "winerror.h"
39 #include "winnls.h"
40 #include "shell32_main.h"
41 #include "shlwapi.h"
43 #include "pidl.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(pidl);
47 WINE_DECLARE_DEBUG_CHANNEL(shell);
49 /* from comctl32.dll */
50 extern LPVOID WINAPI Alloc(INT);
51 extern BOOL WINAPI Free(LPVOID);
53 static LPSTR _ILGetSTextPointer(LPCITEMIDLIST pidl);
54 static LPWSTR _ILGetTextPointerW(LPCITEMIDLIST pidl);
56 /*************************************************************************
57 * ILGetDisplayNameExA
59 * Retrieves the display name of an ItemIDList
61 * PARAMS
62 * psf [I] Shell Folder to start with, if NULL the desktop is used
63 * pidl [I] ItemIDList relative to the psf to get the display name for
64 * path [O] Filled in with the display name, assumed to be at least MAX_PATH long
65 * type [I] Type of display name to retrieve
66 * 0 = SHGDN_FORPARSING | SHGDN_FORADDRESSBAR uses always the desktop as root
67 * 1 = SHGDN_NORMAL relative to the root folder
68 * 2 = SHGDN_INFOLDER relative to the root folder, only the last name
70 * RETURNS
71 * True if the display name could be retrieved successfully, False otherwise
73 static BOOL ILGetDisplayNameExA(LPSHELLFOLDER psf, LPCITEMIDLIST pidl, LPSTR path, DWORD type)
75 BOOL ret = FALSE;
76 WCHAR wPath[MAX_PATH];
78 TRACE("%p %p %p %d\n", psf, pidl, path, type);
80 if (!pidl || !path)
81 return FALSE;
83 ret = ILGetDisplayNameExW(psf, pidl, wPath, type);
84 WideCharToMultiByte(CP_ACP, 0, wPath, -1, path, MAX_PATH, NULL, NULL);
85 TRACE("%p %p %s\n", psf, pidl, debugstr_a(path));
87 return ret;
90 BOOL ILGetDisplayNameExW(LPSHELLFOLDER psf, LPCITEMIDLIST pidl, LPWSTR path, DWORD type)
92 LPSHELLFOLDER psfParent, lsf = psf;
93 HRESULT ret = NO_ERROR;
94 LPCITEMIDLIST pidllast;
95 STRRET strret;
96 DWORD flag;
98 TRACE("%p %p %p %x\n", psf, pidl, path, type);
100 if (!pidl || !path)
101 return FALSE;
103 if (!lsf)
105 ret = SHGetDesktopFolder(&lsf);
106 if (FAILED(ret))
107 return FALSE;
110 switch (type)
112 case ILGDN_FORPARSING:
113 flag = SHGDN_FORPARSING | SHGDN_FORADDRESSBAR;
114 break;
115 case ILGDN_NORMAL:
116 flag = SHGDN_NORMAL;
117 break;
118 case ILGDN_INFOLDER:
119 flag = SHGDN_INFOLDER;
120 break;
121 default:
122 FIXME("Unknown type parameter = %x\n", type);
123 flag = SHGDN_FORPARSING | SHGDN_FORADDRESSBAR;
124 break;
127 if (!*(const WORD*)pidl || type == ILGDN_FORPARSING)
129 ret = IShellFolder_GetDisplayNameOf(lsf, pidl, flag, &strret);
130 if (SUCCEEDED(ret))
132 if(!StrRetToStrNW(path, MAX_PATH, &strret, pidl))
133 ret = E_FAIL;
136 else
138 ret = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psfParent, &pidllast);
139 if (SUCCEEDED(ret))
141 ret = IShellFolder_GetDisplayNameOf(psfParent, pidllast, flag, &strret);
142 if (SUCCEEDED(ret))
144 if(!StrRetToStrNW(path, MAX_PATH, &strret, pidllast))
145 ret = E_FAIL;
147 IShellFolder_Release(psfParent);
151 TRACE("%p %p %s\n", psf, pidl, debugstr_w(path));
153 if (!psf)
154 IShellFolder_Release(lsf);
155 return SUCCEEDED(ret);
158 /*************************************************************************
159 * ILGetDisplayNameEx [SHELL32.186]
161 BOOL WINAPI ILGetDisplayNameEx(LPSHELLFOLDER psf, LPCITEMIDLIST pidl, LPVOID path, DWORD type)
163 TRACE_(shell)("%p %p %p %d\n", psf, pidl, path, type);
165 if (SHELL_OsIsUnicode())
166 return ILGetDisplayNameExW(psf, pidl, path, type);
167 return ILGetDisplayNameExA(psf, pidl, path, type);
170 /*************************************************************************
171 * ILGetDisplayName [SHELL32.15]
173 BOOL WINAPI ILGetDisplayName(LPCITEMIDLIST pidl, LPVOID path)
175 TRACE_(shell)("%p %p\n", pidl, path);
177 if (SHELL_OsIsUnicode())
178 return ILGetDisplayNameExW(NULL, pidl, path, ILGDN_FORPARSING);
179 return ILGetDisplayNameExA(NULL, pidl, path, ILGDN_FORPARSING);
182 /*************************************************************************
183 * ILFindLastID [SHELL32.16]
185 * NOTES
186 * observed: pidl=Desktop return=pidl
188 LPITEMIDLIST WINAPI ILFindLastID(LPCITEMIDLIST pidl)
190 LPCITEMIDLIST pidlLast = pidl;
192 TRACE("(pidl=%p)\n",pidl);
194 if (!pidl)
195 return NULL;
197 while (pidl->mkid.cb)
199 pidlLast = pidl;
200 pidl = ILGetNext(pidl);
202 return (LPITEMIDLIST)pidlLast;
205 /*************************************************************************
206 * ILRemoveLastID [SHELL32.17]
208 * NOTES
209 * when pidl=Desktop return=FALSE
211 BOOL WINAPI ILRemoveLastID(LPITEMIDLIST pidl)
213 TRACE_(shell)("pidl=%p\n",pidl);
215 if (_ILIsEmpty(pidl))
216 return FALSE;
217 ILFindLastID(pidl)->mkid.cb = 0;
218 return TRUE;
221 /*************************************************************************
222 * ILClone [SHELL32.18]
224 * NOTES
225 * duplicate an idlist
227 LPITEMIDLIST WINAPI ILClone (LPCITEMIDLIST pidl)
229 DWORD len;
230 LPITEMIDLIST newpidl;
232 if (!pidl)
233 return NULL;
235 len = ILGetSize(pidl);
236 newpidl = SHAlloc(len);
237 if (newpidl)
238 memcpy(newpidl,pidl,len);
240 TRACE("pidl=%p newpidl=%p\n",pidl, newpidl);
241 pdump(pidl);
243 return newpidl;
246 /*************************************************************************
247 * ILCloneFirst [SHELL32.19]
249 * NOTES
250 * duplicates the first idlist of a complex pidl
252 LPITEMIDLIST WINAPI ILCloneFirst(LPCITEMIDLIST pidl)
254 DWORD len;
255 LPITEMIDLIST pidlNew = NULL;
257 TRACE("pidl=%p\n", pidl);
258 pdump(pidl);
260 if (pidl)
262 len = pidl->mkid.cb;
263 pidlNew = SHAlloc(len+2);
264 if (pidlNew)
266 memcpy(pidlNew,pidl,len+2); /* 2 -> mind a desktop pidl */
268 if (len)
269 ILGetNext(pidlNew)->mkid.cb = 0x00;
272 TRACE("-- newpidl=%p\n",pidlNew);
274 return pidlNew;
277 /*************************************************************************
278 * ILLoadFromStream (SHELL32.26)
280 * NOTES
281 * the first two bytes are the len, the pidl is following then
283 HRESULT WINAPI ILLoadFromStream (IStream * pStream, LPITEMIDLIST * ppPidl)
285 WORD wLen = 0;
286 DWORD dwBytesRead;
287 HRESULT ret = E_FAIL;
290 TRACE_(shell)("%p %p\n", pStream , ppPidl);
292 SHFree(*ppPidl);
293 *ppPidl = NULL;
295 IStream_AddRef (pStream);
297 if (SUCCEEDED(IStream_Read(pStream, &wLen, 2, &dwBytesRead)))
299 TRACE("PIDL length is %d\n", wLen);
300 if (wLen != 0)
302 *ppPidl = SHAlloc (wLen);
303 if (SUCCEEDED(IStream_Read(pStream, *ppPidl , wLen, &dwBytesRead)))
305 TRACE("Stream read OK\n");
306 ret = S_OK;
308 else
310 WARN("reading pidl failed\n");
311 SHFree(*ppPidl);
312 *ppPidl = NULL;
315 else
317 *ppPidl = NULL;
318 ret = S_OK;
322 /* we are not yet fully compatible */
323 if (*ppPidl && !pcheck(*ppPidl))
325 WARN("Check failed\n");
326 SHFree(*ppPidl);
327 *ppPidl = NULL;
330 IStream_Release (pStream);
331 TRACE("done\n");
332 return ret;
335 /*************************************************************************
336 * ILSaveToStream (SHELL32.27)
338 * NOTES
339 * the first two bytes are the len, the pidl is following then
341 HRESULT WINAPI ILSaveToStream (IStream * pStream, LPCITEMIDLIST pPidl)
343 WORD wLen = 0;
344 HRESULT ret = E_FAIL;
346 TRACE_(shell)("%p %p\n", pStream, pPidl);
348 IStream_AddRef (pStream);
350 wLen = ILGetSize(pPidl);
352 if (SUCCEEDED(IStream_Write(pStream, &wLen, 2, NULL)))
354 if (SUCCEEDED(IStream_Write(pStream, pPidl, wLen, NULL)))
355 ret = S_OK;
357 IStream_Release (pStream);
359 return ret;
362 /*************************************************************************
363 * SHILCreateFromPath [SHELL32.28]
365 * Create an ItemIDList from a path
367 * PARAMS
368 * path [I]
369 * ppidl [O]
370 * attributes [I/O] requested attributes on call and actual attributes when
371 * the function returns
373 * RETURNS
374 * NO_ERROR if successful, or an OLE errer code otherwise
376 * NOTES
377 * Wrapper for IShellFolder_ParseDisplayName().
379 static HRESULT SHILCreateFromPathA(LPCSTR path, LPITEMIDLIST * ppidl, DWORD * attributes)
381 WCHAR lpszDisplayName[MAX_PATH];
383 TRACE_(shell)("%s %p 0x%08x\n", path, ppidl, attributes ? *attributes : 0);
385 if (!MultiByteToWideChar(CP_ACP, 0, path, -1, lpszDisplayName, MAX_PATH))
386 lpszDisplayName[MAX_PATH-1] = 0;
388 return SHILCreateFromPathW(lpszDisplayName, ppidl, attributes);
391 HRESULT SHILCreateFromPathW(LPCWSTR path, LPITEMIDLIST * ppidl, DWORD * attributes)
393 LPSHELLFOLDER sf;
394 DWORD pchEaten;
395 HRESULT ret = E_FAIL;
397 TRACE_(shell)("%s %p 0x%08x\n", debugstr_w(path), ppidl, attributes ? *attributes : 0);
399 if (SUCCEEDED (SHGetDesktopFolder(&sf)))
401 ret = IShellFolder_ParseDisplayName(sf, 0, NULL, (LPWSTR)path, &pchEaten, ppidl, attributes);
402 IShellFolder_Release(sf);
404 return ret;
407 HRESULT WINAPI SHILCreateFromPathAW (LPCVOID path, LPITEMIDLIST * ppidl, DWORD * attributes)
409 if ( SHELL_OsIsUnicode())
410 return SHILCreateFromPathW (path, ppidl, attributes);
411 return SHILCreateFromPathA (path, ppidl, attributes);
414 /*************************************************************************
415 * SHCloneSpecialIDList [SHELL32.89]
417 * Create an ItemIDList to one of the special folders.
419 * PARAMS
420 * hwndOwner [in]
421 * nFolder [in] CSIDL_xxxxx
422 * fCreate [in] Create folder if it does not exist
424 * RETURNS
425 * Success: The newly created pidl
426 * Failure: NULL, if inputs are invalid.
428 * NOTES
429 * exported by ordinal.
430 * Caller is responsible for deallocating the returned ItemIDList with the
431 * shells IMalloc interface, aka ILFree.
433 LPITEMIDLIST WINAPI SHCloneSpecialIDList(HWND hwndOwner, DWORD nFolder, BOOL fCreate)
435 LPITEMIDLIST ppidl;
436 TRACE_(shell)("(hwnd=%p,csidl=0x%x,%s).\n", hwndOwner, nFolder, fCreate ? "T" : "F");
438 if (fCreate)
439 nFolder |= CSIDL_FLAG_CREATE;
441 SHGetSpecialFolderLocation(hwndOwner, nFolder, &ppidl);
442 return ppidl;
445 /*************************************************************************
446 * ILGlobalClone [SHELL32.20]
448 * Clones an ItemIDList using Alloc.
450 * PARAMS
451 * pidl [I] ItemIDList to clone
453 * RETURNS
454 * Newly allocated ItemIDList.
456 * NOTES
457 * exported by ordinal.
459 LPITEMIDLIST WINAPI ILGlobalClone(LPCITEMIDLIST pidl)
461 DWORD len;
462 LPITEMIDLIST newpidl;
464 if (!pidl)
465 return NULL;
467 len = ILGetSize(pidl);
468 newpidl = Alloc(len);
469 if (newpidl)
470 memcpy(newpidl,pidl,len);
472 TRACE("pidl=%p newpidl=%p\n",pidl, newpidl);
473 pdump(pidl);
475 return newpidl;
478 /*************************************************************************
479 * ILIsEqual [SHELL32.21]
482 BOOL WINAPI ILIsEqual(LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
484 char szData1[MAX_PATH];
485 char szData2[MAX_PATH];
487 LPCITEMIDLIST pidltemp1 = pidl1;
488 LPCITEMIDLIST pidltemp2 = pidl2;
490 TRACE("pidl1=%p pidl2=%p\n",pidl1, pidl2);
493 * Explorer reads from registry directly (StreamMRU),
494 * so we can only check here
496 if (!pcheck(pidl1) || !pcheck (pidl2))
497 return FALSE;
499 pdump (pidl1);
500 pdump (pidl2);
502 if (!pidl1 || !pidl2)
503 return FALSE;
505 while (pidltemp1->mkid.cb && pidltemp2->mkid.cb)
507 _ILSimpleGetText(pidltemp1, szData1, MAX_PATH);
508 _ILSimpleGetText(pidltemp2, szData2, MAX_PATH);
510 if (lstrcmpiA( szData1, szData2 ))
511 return FALSE;
513 pidltemp1 = ILGetNext(pidltemp1);
514 pidltemp2 = ILGetNext(pidltemp2);
517 if (!pidltemp1->mkid.cb && !pidltemp2->mkid.cb)
518 return TRUE;
520 return FALSE;
523 /*************************************************************************
524 * ILIsParent [SHELL32.23]
526 * Verifies that pidlParent is indeed the (immediate) parent of pidlChild.
528 * PARAMS
529 * pidlParent [I]
530 * pidlChild [I]
531 * bImmediate [I] only return true if the parent is the direct parent
532 * of the child
534 * RETURNS
535 * True if the parent ItemIDlist is a complete part of the child ItemIdList,
536 * False otherwise.
538 * NOTES
539 * parent = a/b, child = a/b/c -> true, c is in folder a/b
540 * child = a/b/c/d -> false if bImmediate is true, d is not in folder a/b
541 * child = a/b/c/d -> true if bImmediate is false, d is in a subfolder of a/b
542 * child = a/b -> false if bImmediate is true
543 * child = a/b -> true if bImmediate is false
545 BOOL WINAPI ILIsParent(LPCITEMIDLIST pidlParent, LPCITEMIDLIST pidlChild, BOOL bImmediate)
547 char szData1[MAX_PATH];
548 char szData2[MAX_PATH];
549 LPCITEMIDLIST pParent = pidlParent;
550 LPCITEMIDLIST pChild = pidlChild;
552 TRACE("%p %p %x\n", pidlParent, pidlChild, bImmediate);
554 if (!pParent || !pChild)
555 return FALSE;
557 while (pParent->mkid.cb && pChild->mkid.cb)
559 _ILSimpleGetText(pParent, szData1, MAX_PATH);
560 _ILSimpleGetText(pChild, szData2, MAX_PATH);
562 if (lstrcmpiA( szData1, szData2 ))
563 return FALSE;
565 pParent = ILGetNext(pParent);
566 pChild = ILGetNext(pChild);
569 /* child has shorter name than parent */
570 if (pParent->mkid.cb)
571 return FALSE;
573 /* not immediate descent */
574 if ((!pChild->mkid.cb || ILGetNext(pChild)->mkid.cb) && bImmediate)
575 return FALSE;
577 return TRUE;
580 /*************************************************************************
581 * ILFindChild [SHELL32.24]
583 * Compares elements from pidl1 and pidl2.
585 * PARAMS
586 * pidl1 [I]
587 * pidl2 [I]
589 * RETURNS
590 * pidl1 is desktop pidl2
591 * pidl1 shorter pidl2 pointer to first different element of pidl2
592 * if there was at least one equal element
593 * pidl2 shorter pidl1 0
594 * pidl2 equal pidl1 pointer to last 0x00-element of pidl2
596 * NOTES
597 * exported by ordinal.
599 LPITEMIDLIST WINAPI ILFindChild(LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
601 char szData1[MAX_PATH];
602 char szData2[MAX_PATH];
604 LPCITEMIDLIST pidltemp1 = pidl1;
605 LPCITEMIDLIST pidltemp2 = pidl2;
606 LPCITEMIDLIST ret=NULL;
608 TRACE("pidl1=%p pidl2=%p\n",pidl1, pidl2);
610 /* explorer reads from registry directly (StreamMRU),
611 so we can only check here */
612 if ((!pcheck (pidl1)) || (!pcheck (pidl2)))
613 return FALSE;
615 pdump (pidl1);
616 pdump (pidl2);
618 if (_ILIsDesktop(pidl1))
620 ret = pidl2;
622 else
624 while (pidltemp1->mkid.cb && pidltemp2->mkid.cb)
626 _ILSimpleGetText(pidltemp1, szData1, MAX_PATH);
627 _ILSimpleGetText(pidltemp2, szData2, MAX_PATH);
629 if (lstrcmpiA(szData1,szData2))
630 break;
632 pidltemp1 = ILGetNext(pidltemp1);
633 pidltemp2 = ILGetNext(pidltemp2);
634 ret = pidltemp2;
637 if (pidltemp1->mkid.cb)
638 ret = NULL; /* elements of pidl1 left*/
640 TRACE_(shell)("--- %p\n", ret);
641 return (LPITEMIDLIST)ret; /* pidl 1 is shorter */
644 /*************************************************************************
645 * ILCombine [SHELL32.25]
647 * Concatenates two complex ItemIDLists.
649 * PARAMS
650 * pidl1 [I] first complex ItemIDLists
651 * pidl2 [I] complex ItemIDLists to append
653 * RETURNS
654 * if both pidl's == NULL NULL
655 * if pidl1 == NULL cloned pidl2
656 * if pidl2 == NULL cloned pidl1
657 * otherwise new pidl with pidl2 appended to pidl1
659 * NOTES
660 * exported by ordinal.
661 * Does not destroy the passed in ItemIDLists!
663 LPITEMIDLIST WINAPI ILCombine(LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
665 DWORD len1,len2;
666 LPITEMIDLIST pidlNew;
668 TRACE("pidl=%p pidl=%p\n",pidl1,pidl2);
670 if (!pidl1 && !pidl2) return NULL;
672 pdump (pidl1);
673 pdump (pidl2);
675 if (!pidl1)
677 pidlNew = ILClone(pidl2);
678 return pidlNew;
681 if (!pidl2)
683 pidlNew = ILClone(pidl1);
684 return pidlNew;
687 len1 = ILGetSize(pidl1)-2;
688 len2 = ILGetSize(pidl2);
689 pidlNew = SHAlloc(len1+len2);
691 if (pidlNew)
693 memcpy(pidlNew,pidl1,len1);
694 memcpy(((BYTE *)pidlNew)+len1,pidl2,len2);
697 /* TRACE(pidl,"--new pidl=%p\n",pidlNew);*/
698 return pidlNew;
701 /*************************************************************************
702 * SHGetRealIDL [SHELL32.98]
704 * NOTES
706 HRESULT WINAPI SHGetRealIDL(LPSHELLFOLDER lpsf, LPCITEMIDLIST pidlSimple, LPITEMIDLIST *pidlReal)
708 IDataObject* pDataObj;
709 HRESULT hr;
711 hr = IShellFolder_GetUIObjectOf(lpsf, 0, 1, &pidlSimple,
712 &IID_IDataObject, 0, (LPVOID*)&pDataObj);
713 if (SUCCEEDED(hr))
715 STGMEDIUM medium;
716 FORMATETC fmt;
718 fmt.cfFormat = RegisterClipboardFormatW(CFSTR_SHELLIDLISTW);
719 fmt.ptd = NULL;
720 fmt.dwAspect = DVASPECT_CONTENT;
721 fmt.lindex = -1;
722 fmt.tymed = TYMED_HGLOBAL;
724 hr = IDataObject_GetData(pDataObj, &fmt, &medium);
726 IDataObject_Release(pDataObj);
728 if (SUCCEEDED(hr))
730 /*assert(pida->cidl==1);*/
731 LPIDA pida = GlobalLock(medium.u.hGlobal);
733 LPCITEMIDLIST pidl_folder = (LPCITEMIDLIST) ((LPBYTE)pida+pida->aoffset[0]);
734 LPCITEMIDLIST pidl_child = (LPCITEMIDLIST) ((LPBYTE)pida+pida->aoffset[1]);
736 *pidlReal = ILCombine(pidl_folder, pidl_child);
738 if (!*pidlReal)
739 hr = E_OUTOFMEMORY;
741 GlobalUnlock(medium.u.hGlobal);
742 GlobalFree(medium.u.hGlobal);
746 return hr;
749 /*************************************************************************
750 * SHLogILFromFSIL [SHELL32.95]
752 * NOTES
753 * pild = CSIDL_DESKTOP ret = 0
754 * pild = CSIDL_DRIVES ret = 0
756 LPITEMIDLIST WINAPI SHLogILFromFSIL(LPITEMIDLIST pidl)
758 FIXME("(pidl=%p)\n",pidl);
760 pdump(pidl);
762 return 0;
765 /*************************************************************************
766 * ILGetSize [SHELL32.152]
768 * Gets the byte size of an ItemIDList including zero terminator
770 * PARAMS
771 * pidl [I] ItemIDList
773 * RETURNS
774 * size of pidl in bytes
776 * NOTES
777 * exported by ordinal
779 UINT WINAPI ILGetSize(LPCITEMIDLIST pidl)
781 LPCSHITEMID si = &(pidl->mkid);
782 UINT len=0;
784 if (pidl)
786 while (si->cb)
788 len += si->cb;
789 si = (LPCSHITEMID)(((const BYTE*)si)+si->cb);
791 len += 2;
793 TRACE("pidl=%p size=%u\n",pidl, len);
794 return len;
797 /*************************************************************************
798 * ILGetNext [SHELL32.153]
800 * Gets the next ItemID of an ItemIDList
802 * PARAMS
803 * pidl [I] ItemIDList
805 * RETURNS
806 * null -> null
807 * desktop -> null
808 * simple pidl -> pointer to 0x0000 element
810 * NOTES
811 * exported by ordinal.
813 LPITEMIDLIST WINAPI ILGetNext(LPCITEMIDLIST pidl)
815 WORD len;
817 TRACE("%p\n", pidl);
819 if (pidl)
821 len = pidl->mkid.cb;
822 if (len)
824 pidl = (LPCITEMIDLIST) (((const BYTE*)pidl)+len);
825 TRACE("-- %p\n", pidl);
826 return (LPITEMIDLIST)pidl;
829 return NULL;
832 /*************************************************************************
833 * ILAppendID [SHELL32.154]
835 * Adds the single ItemID item to the ItemIDList indicated by pidl.
836 * If bEnd is FALSE, inserts the item in the front of the list,
837 * otherwise it adds the item to the end. (???)
839 * PARAMS
840 * pidl [I] ItemIDList to extend
841 * item [I] ItemID to prepend/append
842 * bEnd [I] Indicates if the item should be appended
844 * NOTES
845 * Destroys the passed in idlist! (???)
847 LPITEMIDLIST WINAPI ILAppendID(LPITEMIDLIST pidl, LPCSHITEMID item, BOOL bEnd)
849 LPITEMIDLIST idlRet;
850 LPCITEMIDLIST itemid = (LPCITEMIDLIST)item;
852 WARN("(pidl=%p,pidl=%p,%08u)semi-stub\n",pidl,item,bEnd);
854 pdump (pidl);
855 pdump (itemid);
857 if (_ILIsDesktop(pidl))
859 idlRet = ILClone(itemid);
860 SHFree (pidl);
861 return idlRet;
864 if (bEnd)
865 idlRet = ILCombine(pidl, itemid);
866 else
867 idlRet = ILCombine(itemid, pidl);
869 SHFree(pidl);
870 return idlRet;
873 /*************************************************************************
874 * ILFree [SHELL32.155]
876 * Frees memory (if not NULL) allocated by SHMalloc allocator
878 * PARAMS
879 * pidl [I]
881 * RETURNS
882 * Nothing
884 * NOTES
885 * exported by ordinal
887 void WINAPI ILFree(LPITEMIDLIST pidl)
889 TRACE("(pidl=%p)\n",pidl);
890 SHFree(pidl);
893 /*************************************************************************
894 * ILGlobalFree [SHELL32.156]
896 * Frees memory (if not NULL) allocated by Alloc allocator
898 * PARAMS
899 * pidl [I]
901 * RETURNS
902 * Nothing
904 * NOTES
905 * exported by ordinal.
907 void WINAPI ILGlobalFree( LPITEMIDLIST pidl)
909 TRACE("%p\n", pidl);
911 Free(pidl);
914 /*************************************************************************
915 * ILCreateFromPathA [SHELL32.189]
917 * Creates a complex ItemIDList from a path and returns it.
919 * PARAMS
920 * path [I]
922 * RETURNS
923 * the newly created complex ItemIDList or NULL if failed
925 * NOTES
926 * exported by ordinal.
928 LPITEMIDLIST WINAPI ILCreateFromPathA (LPCSTR path)
930 LPITEMIDLIST pidlnew = NULL;
932 TRACE_(shell)("%s\n", debugstr_a(path));
934 if (SUCCEEDED(SHILCreateFromPathA(path, &pidlnew, NULL)))
935 return pidlnew;
936 return NULL;
939 /*************************************************************************
940 * ILCreateFromPathW [SHELL32.190]
942 * See ILCreateFromPathA.
944 LPITEMIDLIST WINAPI ILCreateFromPathW (LPCWSTR path)
946 LPITEMIDLIST pidlnew = NULL;
948 TRACE_(shell)("%s\n", debugstr_w(path));
950 if (SUCCEEDED(SHILCreateFromPathW(path, &pidlnew, NULL)))
951 return pidlnew;
952 return NULL;
955 /*************************************************************************
956 * ILCreateFromPath [SHELL32.157]
958 LPITEMIDLIST WINAPI ILCreateFromPathAW (LPCVOID path)
960 if ( SHELL_OsIsUnicode())
961 return ILCreateFromPathW (path);
962 return ILCreateFromPathA (path);
965 /*************************************************************************
966 * _ILParsePathW [internal]
968 * Creates an ItemIDList from a path and returns it.
970 * PARAMS
971 * path [I] path to parse and convert into an ItemIDList
972 * lpFindFile [I] pointer to buffer to initialize the FileSystem
973 * Bind Data object with
974 * bBindCtx [I] indicates to create a BindContext and assign a
975 * FileSystem Bind Data object
976 * ppidl [O] the newly create ItemIDList
977 * prgfInOut [I/O] requested attributes on input and actual
978 * attributes on return
980 * RETURNS
981 * NO_ERROR on success or an OLE error code
983 * NOTES
984 * If either lpFindFile is non-NULL or bBindCtx is TRUE, this function
985 * creates a BindContext object and assigns a FileSystem Bind Data object
986 * to it, passing the BindContext to IShellFolder_ParseDisplayName. Each
987 * IShellFolder uses that FileSystem Bind Data object of the BindContext
988 * to pass data about the current path element to the next object. This
989 * is used to avoid having to verify the current path element on disk, so
990 * that creating an ItemIDList from a nonexistent path still can work.
992 static HRESULT _ILParsePathW(LPCWSTR path, LPWIN32_FIND_DATAW lpFindFile,
993 BOOL bBindCtx, LPITEMIDLIST *ppidl, LPDWORD prgfInOut)
995 LPSHELLFOLDER pSF = NULL;
996 LPBC pBC = NULL;
997 HRESULT ret;
999 TRACE("%s %p %d (%p)->%p (%p)->0x%x\n", debugstr_w(path), lpFindFile, bBindCtx,
1000 ppidl, ppidl ? *ppidl : NULL,
1001 prgfInOut, prgfInOut ? *prgfInOut : 0);
1003 ret = SHGetDesktopFolder(&pSF);
1004 if (FAILED(ret))
1005 return ret;
1007 if (lpFindFile || bBindCtx)
1008 ret = IFileSystemBindData_Constructor(lpFindFile, &pBC);
1010 if (SUCCEEDED(ret))
1012 ret = IShellFolder_ParseDisplayName(pSF, 0, pBC, (LPOLESTR)path, NULL, ppidl, prgfInOut);
1015 if (pBC)
1017 IBindCtx_Release(pBC);
1018 pBC = NULL;
1021 IShellFolder_Release(pSF);
1023 if (FAILED(ret) && ppidl)
1024 *ppidl = NULL;
1026 TRACE("%s %p 0x%x\n", debugstr_w(path), ppidl ? *ppidl : NULL, prgfInOut ? *prgfInOut : 0);
1028 return ret;
1031 /*************************************************************************
1032 * SHSimpleIDListFromPath [SHELL32.162]
1034 * Creates a simple ItemIDList from a path and returns it. This function
1035 * does not fail on nonexistent paths.
1037 * PARAMS
1038 * path [I] path to parse and convert into an ItemIDList
1040 * RETURNS
1041 * the newly created simple ItemIDList
1043 * NOTES
1044 * Simple in the name does not mean a relative ItemIDList but rather a
1045 * fully qualified list, where only the file name is filled in and the
1046 * directory flag for those ItemID elements this is known about, eg.
1047 * it is not the last element in the ItemIDList or the actual directory
1048 * exists on disk.
1049 * exported by ordinal.
1051 LPITEMIDLIST SHSimpleIDListFromPathA(LPCSTR lpszPath)
1053 LPITEMIDLIST pidl = NULL;
1054 LPWSTR wPath = NULL;
1055 int len;
1057 TRACE("%s\n", debugstr_a(lpszPath));
1059 if (lpszPath)
1061 len = MultiByteToWideChar(CP_ACP, 0, lpszPath, -1, NULL, 0);
1062 wPath = heap_alloc(len * sizeof(WCHAR));
1063 MultiByteToWideChar(CP_ACP, 0, lpszPath, -1, wPath, len);
1066 _ILParsePathW(wPath, NULL, TRUE, &pidl, NULL);
1068 heap_free(wPath);
1069 TRACE("%s %p\n", debugstr_a(lpszPath), pidl);
1070 return pidl;
1073 LPITEMIDLIST SHSimpleIDListFromPathW(LPCWSTR lpszPath)
1075 LPITEMIDLIST pidl = NULL;
1077 TRACE("%s\n", debugstr_w(lpszPath));
1079 _ILParsePathW(lpszPath, NULL, TRUE, &pidl, NULL);
1080 TRACE("%s %p\n", debugstr_w(lpszPath), pidl);
1081 return pidl;
1084 LPITEMIDLIST WINAPI SHSimpleIDListFromPathAW(LPCVOID lpszPath)
1086 if ( SHELL_OsIsUnicode())
1087 return SHSimpleIDListFromPathW (lpszPath);
1088 return SHSimpleIDListFromPathA (lpszPath);
1091 /*************************************************************************
1092 * SHGetDataFromIDListA [SHELL32.247]
1094 * NOTES
1095 * the pidl can be a simple one. since we can't get the path out of the pidl
1096 * we have to take all data from the pidl
1098 HRESULT WINAPI SHGetDataFromIDListA(LPSHELLFOLDER psf, LPCITEMIDLIST pidl,
1099 int nFormat, LPVOID dest, int len)
1101 LPSTR filename, shortname;
1102 WIN32_FIND_DATAA * pfd;
1104 TRACE_(shell)("%p, %p, %d, %p, %d.\n", psf, pidl, nFormat, dest, len);
1106 pdump(pidl);
1107 if (!psf || !dest)
1108 return E_INVALIDARG;
1110 switch (nFormat)
1112 case SHGDFIL_FINDDATA:
1113 pfd = dest;
1115 if (_ILIsDrive(pidl) || _ILIsSpecialFolder(pidl))
1116 return E_INVALIDARG;
1118 if (len < sizeof(WIN32_FIND_DATAA))
1119 return E_INVALIDARG;
1121 ZeroMemory(pfd, sizeof (WIN32_FIND_DATAA));
1122 _ILGetFileDateTime( pidl, &(pfd->ftLastWriteTime));
1123 pfd->dwFileAttributes = _ILGetFileAttributes(pidl, NULL, 0);
1124 pfd->nFileSizeLow = _ILGetFileSize ( pidl, NULL, 0);
1126 filename = _ILGetTextPointer(pidl);
1127 shortname = _ILGetSTextPointer(pidl);
1129 if (filename)
1130 lstrcpynA(pfd->cFileName, filename, sizeof(pfd->cFileName));
1131 else
1132 pfd->cFileName[0] = '\0';
1134 if (shortname)
1135 lstrcpynA(pfd->cAlternateFileName, shortname, sizeof(pfd->cAlternateFileName));
1136 else
1137 pfd->cAlternateFileName[0] = '\0';
1138 return S_OK;
1140 case SHGDFIL_NETRESOURCE:
1141 case SHGDFIL_DESCRIPTIONID:
1142 FIXME_(shell)("SHGDFIL %i stub\n", nFormat);
1143 break;
1145 default:
1146 ERR_(shell)("Unknown SHGDFIL %i, please report\n", nFormat);
1149 return E_INVALIDARG;
1152 /*************************************************************************
1153 * SHGetDataFromIDListW [SHELL32.248]
1156 HRESULT WINAPI SHGetDataFromIDListW(LPSHELLFOLDER psf, LPCITEMIDLIST pidl,
1157 int nFormat, LPVOID dest, int len)
1159 LPSTR filename, shortname;
1160 WIN32_FIND_DATAW * pfd = dest;
1162 TRACE_(shell)("%p, %p, %d, %p, %d.\n", psf, pidl, nFormat, dest, len);
1164 pdump(pidl);
1166 if (!psf || !dest)
1167 return E_INVALIDARG;
1169 switch (nFormat)
1171 case SHGDFIL_FINDDATA:
1172 pfd = dest;
1174 if (_ILIsDrive(pidl))
1175 return E_INVALIDARG;
1177 if (len < sizeof(WIN32_FIND_DATAW))
1178 return E_INVALIDARG;
1180 ZeroMemory(pfd, sizeof (WIN32_FIND_DATAW));
1181 _ILGetFileDateTime( pidl, &(pfd->ftLastWriteTime));
1182 pfd->dwFileAttributes = _ILGetFileAttributes(pidl, NULL, 0);
1183 pfd->nFileSizeLow = _ILGetFileSize ( pidl, NULL, 0);
1185 filename = _ILGetTextPointer(pidl);
1186 shortname = _ILGetSTextPointer(pidl);
1188 if (!filename)
1189 pfd->cFileName[0] = '\0';
1190 else if (!MultiByteToWideChar(CP_ACP, 0, filename, -1, pfd->cFileName, MAX_PATH))
1191 pfd->cFileName[MAX_PATH-1] = 0;
1193 if (!shortname)
1194 pfd->cAlternateFileName[0] = '\0';
1195 else if (!MultiByteToWideChar(CP_ACP, 0, shortname, -1, pfd->cAlternateFileName, 14))
1196 pfd->cAlternateFileName[13] = 0;
1197 return S_OK;
1199 case SHGDFIL_NETRESOURCE:
1200 case SHGDFIL_DESCRIPTIONID:
1201 FIXME_(shell)("SHGDFIL %i stub\n", nFormat);
1202 break;
1204 default:
1205 ERR_(shell)("Unknown SHGDFIL %i, please report\n", nFormat);
1208 return E_INVALIDARG;
1211 /*************************************************************************
1212 * SHGetPathFromIDListA [SHELL32.@][NT 4.0: SHELL32.220]
1214 * PARAMETERS
1215 * pidl, [IN] pidl
1216 * pszPath [OUT] path
1218 * RETURNS
1219 * path from a passed PIDL.
1221 * NOTES
1222 * NULL returns FALSE
1223 * desktop pidl gives path to desktop directory back
1224 * special pidls returning FALSE
1226 BOOL WINAPI SHGetPathFromIDListA(LPCITEMIDLIST pidl, LPSTR pszPath)
1228 WCHAR wszPath[MAX_PATH];
1229 BOOL bSuccess;
1231 bSuccess = SHGetPathFromIDListW(pidl, wszPath);
1232 WideCharToMultiByte(CP_ACP, 0, wszPath, -1, pszPath, MAX_PATH, NULL, NULL);
1234 return bSuccess;
1237 /*************************************************************************
1238 * SHGetPathFromIDListW [SHELL32.@]
1240 * See SHGetPathFromIDListA.
1242 BOOL WINAPI SHGetPathFromIDListW(LPCITEMIDLIST pidl, LPWSTR pszPath)
1244 return SHGetPathFromIDListEx(pidl, pszPath, MAX_PATH, 0);
1247 /*************************************************************************
1248 * SHGetPathFromIDListEx [SHELL32.@]
1250 BOOL WINAPI SHGetPathFromIDListEx(LPCITEMIDLIST pidl, WCHAR *path, DWORD path_size, GPFIDL_FLAGS flags)
1252 HRESULT hr;
1253 LPCITEMIDLIST pidlLast;
1254 LPSHELLFOLDER psfFolder;
1255 DWORD dwAttributes;
1256 STRRET strret;
1258 TRACE_(shell)("(pidl=%p,%p,%u,%x)\n", pidl, path, path_size, flags);
1259 pdump(pidl);
1261 if (flags != GPFIDL_DEFAULT)
1262 FIXME("Unsupported flags %x\n", flags);
1264 *path = '\0';
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, path, path_size);
1284 TRACE_(shell)("-- %s, 0x%08x\n",debugstr_w(path), 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%08x\n", *ppv, (ppidlLast)?*ppidlLast:NULL, hr);
1329 return hr;
1332 HRESULT WINAPI SHBindToObject(IShellFolder *psf, LPCITEMIDLIST pidl, IBindCtx *pbc, REFIID riid, void **ppv)
1334 IShellFolder *psfDesktop = NULL;
1335 HRESULT hr;
1337 TRACE_(shell)("%p,%p,%p,%s,%p\n", psf, pidl, pbc, debugstr_guid(riid), ppv);
1338 pdump(pidl);
1340 if (!ppv)
1341 return E_INVALIDARG;
1343 *ppv = NULL;
1345 if (!psf)
1347 hr = SHGetDesktopFolder(&psfDesktop);
1348 if (FAILED(hr))
1349 return hr;
1350 psf = psfDesktop;
1353 if (_ILIsPidlSimple(pidl))
1354 /* we are on desktop level */
1355 hr = IShellFolder_QueryInterface(psf, riid, ppv);
1356 else
1357 hr = IShellFolder_BindToObject(psf, pidl, pbc, riid, ppv);
1359 if (psfDesktop)
1360 IShellFolder_Release(psfDesktop);
1362 TRACE_(shell)("-- ppv=%p ret=0x%08x\n", *ppv, hr);
1363 return hr;
1367 /*************************************************************************
1368 * SHParseDisplayName [SHELL32.@]
1370 HRESULT WINAPI SHParseDisplayName(LPCWSTR name, IBindCtx *bindctx, LPITEMIDLIST *pidlist,
1371 SFGAOF attr_in, SFGAOF *attr_out)
1373 IShellFolder *desktop;
1374 HRESULT hr;
1376 TRACE("%s %p %p %d %p\n", debugstr_w(name), bindctx, pidlist, attr_in, attr_out);
1378 *pidlist = NULL;
1380 if (!name) return E_INVALIDARG;
1382 hr = SHGetDesktopFolder(&desktop);
1383 if (hr != S_OK) return hr;
1385 hr = IShellFolder_ParseDisplayName(desktop, NULL, bindctx, (LPWSTR)name, NULL, pidlist, &attr_in);
1386 if (attr_out) *attr_out = attr_in;
1388 IShellFolder_Release(desktop);
1390 return hr;
1393 /*************************************************************************
1394 * SHGetNameFromIDList [SHELL32.@]
1396 HRESULT WINAPI SHGetNameFromIDList(PCIDLIST_ABSOLUTE pidl, SIGDN sigdnName, PWSTR *ppszName)
1398 IShellFolder *psfparent;
1399 LPCITEMIDLIST child_pidl;
1400 STRRET disp_name;
1401 HRESULT ret;
1403 TRACE("%p 0x%08x %p\n", pidl, sigdnName, ppszName);
1405 *ppszName = NULL;
1406 ret = SHBindToParent(pidl, &IID_IShellFolder, (void**)&psfparent, &child_pidl);
1407 if(SUCCEEDED(ret))
1409 switch(sigdnName)
1411 /* sigdnName & 0xffff */
1412 case SIGDN_NORMALDISPLAY: /* SHGDN_NORMAL */
1413 case SIGDN_PARENTRELATIVEPARSING: /* SHGDN_INFOLDER | SHGDN_FORPARSING */
1414 case SIGDN_PARENTRELATIVEEDITING: /* SHGDN_INFOLDER | SHGDN_FOREDITING */
1415 case SIGDN_DESKTOPABSOLUTEPARSING: /* SHGDN_FORPARSING */
1416 case SIGDN_DESKTOPABSOLUTEEDITING: /* SHGDN_FOREDITING | SHGDN_FORADDRESSBAR*/
1417 case SIGDN_PARENTRELATIVEFORADDRESSBAR: /* SIGDN_INFOLDER | SHGDN_FORADDRESSBAR */
1418 case SIGDN_PARENTRELATIVE: /* SIGDN_INFOLDER */
1420 disp_name.uType = STRRET_WSTR;
1421 ret = IShellFolder_GetDisplayNameOf(psfparent, child_pidl,
1422 sigdnName & 0xffff,
1423 &disp_name);
1424 if(SUCCEEDED(ret))
1425 ret = StrRetToStrW(&disp_name, pidl, ppszName);
1427 break;
1429 case SIGDN_FILESYSPATH:
1430 *ppszName = CoTaskMemAlloc(sizeof(WCHAR)*MAX_PATH);
1431 if(SHGetPathFromIDListW(pidl, *ppszName))
1433 TRACE("Got string %s\n", debugstr_w(*ppszName));
1434 ret = S_OK;
1436 else
1438 CoTaskMemFree(*ppszName);
1439 ret = E_INVALIDARG;
1441 break;
1443 case SIGDN_URL:
1444 default:
1445 FIXME("Unsupported SIGDN %x\n", sigdnName);
1446 ret = E_FAIL;
1449 IShellFolder_Release(psfparent);
1451 return ret;
1454 /*************************************************************************
1455 * SHGetIDListFromObject [SHELL32.@]
1457 HRESULT WINAPI SHGetIDListFromObject(IUnknown *punk, PIDLIST_ABSOLUTE *ppidl)
1459 IPersistIDList *ppersidl;
1460 IPersistFolder2 *ppf2;
1461 IDataObject *pdo;
1462 IFolderView *pfv;
1463 HRESULT ret;
1465 if(!punk)
1466 return E_NOINTERFACE;
1468 *ppidl = NULL;
1470 /* Try IPersistIDList */
1471 ret = IUnknown_QueryInterface(punk, &IID_IPersistIDList, (void**)&ppersidl);
1472 if(SUCCEEDED(ret))
1474 TRACE("IPersistIDList (%p)\n", ppersidl);
1475 ret = IPersistIDList_GetIDList(ppersidl, ppidl);
1476 IPersistIDList_Release(ppersidl);
1477 if(SUCCEEDED(ret))
1478 return ret;
1481 /* Try IPersistFolder2 */
1482 ret = IUnknown_QueryInterface(punk, &IID_IPersistFolder2, (void**)&ppf2);
1483 if(SUCCEEDED(ret))
1485 TRACE("IPersistFolder2 (%p)\n", ppf2);
1486 ret = IPersistFolder2_GetCurFolder(ppf2, ppidl);
1487 IPersistFolder2_Release(ppf2);
1488 if(SUCCEEDED(ret))
1489 return ret;
1492 /* Try IDataObject */
1493 ret = IUnknown_QueryInterface(punk, &IID_IDataObject, (void**)&pdo);
1494 if(SUCCEEDED(ret))
1496 IShellItem *psi;
1497 TRACE("IDataObject (%p)\n", pdo);
1498 ret = SHGetItemFromDataObject(pdo, DOGIF_ONLY_IF_ONE,
1499 &IID_IShellItem, (void**)&psi);
1500 if(SUCCEEDED(ret))
1502 ret = SHGetIDListFromObject((IUnknown*)psi, ppidl);
1503 IShellItem_Release(psi);
1505 IDataObject_Release(pdo);
1507 if(SUCCEEDED(ret))
1508 return ret;
1511 /* Try IFolderView */
1512 ret = IUnknown_QueryInterface(punk, &IID_IFolderView, (void**)&pfv);
1513 if(SUCCEEDED(ret))
1515 IShellFolder *psf;
1516 TRACE("IFolderView (%p)\n", pfv);
1517 ret = IFolderView_GetFolder(pfv, &IID_IShellFolder, (void**)&psf);
1518 if(SUCCEEDED(ret))
1520 /* We might be able to get IPersistFolder2 from a shellfolder. */
1521 ret = SHGetIDListFromObject((IUnknown*)psf, ppidl);
1522 IShellFolder_Release(psf);
1524 IFolderView_Release(pfv);
1525 return ret;
1528 return ret;
1531 /**************************************************************************
1533 * internal functions
1535 * ### 1. section creating pidls ###
1537 *************************************************************************
1540 /* Basic PIDL constructor. Allocates size + 5 bytes, where:
1541 * - two bytes are SHITEMID.cb
1542 * - one byte is PIDLDATA.type
1543 * - two bytes are the NULL PIDL terminator
1544 * Sets type of the returned PIDL to type.
1546 static LPITEMIDLIST _ILAlloc(PIDLTYPE type, unsigned int size)
1548 LPITEMIDLIST pidlOut = NULL;
1550 pidlOut = SHAlloc(size + 5);
1551 if(pidlOut)
1553 LPPIDLDATA pData;
1554 LPITEMIDLIST pidlNext;
1556 ZeroMemory(pidlOut, size + 5);
1557 pidlOut->mkid.cb = size + 3;
1559 pData = _ILGetDataPointer(pidlOut);
1560 if (pData)
1561 pData->type = type;
1563 pidlNext = ILGetNext(pidlOut);
1564 if (pidlNext)
1565 pidlNext->mkid.cb = 0x00;
1566 TRACE("-- (pidl=%p, size=%u)\n", pidlOut, size);
1569 return pidlOut;
1572 LPITEMIDLIST _ILCreateDesktop(void)
1574 LPITEMIDLIST ret;
1576 TRACE("()\n");
1577 ret = SHAlloc(2);
1578 if (ret)
1579 ret->mkid.cb = 0;
1580 return ret;
1583 LPITEMIDLIST _ILCreateMyComputer(void)
1585 TRACE("()\n");
1586 return _ILCreateGuid(PT_GUID, &CLSID_MyComputer);
1589 LPITEMIDLIST _ILCreateMyDocuments(void)
1591 TRACE("()\n");
1592 return _ILCreateGuid(PT_GUID, &CLSID_MyDocuments);
1595 LPITEMIDLIST _ILCreateIExplore(void)
1597 TRACE("()\n");
1598 return _ILCreateGuid(PT_GUID, &CLSID_Internet);
1601 LPITEMIDLIST _ILCreateControlPanel(void)
1603 LPITEMIDLIST parent = _ILCreateGuid(PT_GUID, &CLSID_MyComputer), ret = NULL;
1605 TRACE("()\n");
1606 if (parent)
1608 LPITEMIDLIST cpl = _ILCreateGuid(PT_SHELLEXT, &CLSID_ControlPanel);
1610 if (cpl)
1612 ret = ILCombine(parent, cpl);
1613 SHFree(cpl);
1615 SHFree(parent);
1617 return ret;
1620 LPITEMIDLIST _ILCreatePrinters(void)
1622 LPITEMIDLIST parent = _ILCreateGuid(PT_GUID, &CLSID_MyComputer), ret = NULL;
1624 TRACE("()\n");
1625 if (parent)
1627 LPITEMIDLIST printers = _ILCreateGuid(PT_YAGUID, &CLSID_Printers);
1629 if (printers)
1631 ret = ILCombine(parent, printers);
1632 SHFree(printers);
1634 SHFree(parent);
1636 return ret;
1639 LPITEMIDLIST _ILCreateNetwork(void)
1641 TRACE("()\n");
1642 return _ILCreateGuid(PT_GUID, &CLSID_NetworkPlaces);
1645 LPITEMIDLIST _ILCreateBitBucket(void)
1647 TRACE("()\n");
1648 return _ILCreateGuid(PT_GUID, &CLSID_RecycleBin);
1651 LPITEMIDLIST _ILCreateNetHood(void)
1653 TRACE("()\n");
1654 return _ILCreateGuid(PT_GUID, &CLSID_NetworkPlaces);
1657 LPITEMIDLIST _ILCreateGuid(PIDLTYPE type, REFIID guid)
1659 LPITEMIDLIST pidlOut;
1661 if (type == PT_SHELLEXT || type == PT_GUID || type == PT_YAGUID)
1663 pidlOut = _ILAlloc(type, sizeof(GUIDStruct));
1664 if (pidlOut)
1666 LPPIDLDATA pData = _ILGetDataPointer(pidlOut);
1668 pData->u.guid.guid = *guid;
1669 TRACE("-- create GUID-pidl %s\n",
1670 debugstr_guid(&(pData->u.guid.guid)));
1673 else
1675 WARN("%d: invalid type for GUID\n", type);
1676 pidlOut = NULL;
1678 return pidlOut;
1681 LPITEMIDLIST _ILCreateGuidFromStrA(LPCSTR szGUID)
1683 IID iid;
1685 if (FAILED(SHCLSIDFromStringA(szGUID, &iid)))
1687 ERR("%s is not a GUID\n", szGUID);
1688 return NULL;
1690 return _ILCreateGuid(PT_GUID, &iid);
1693 LPITEMIDLIST _ILCreateGuidFromStrW(LPCWSTR szGUID)
1695 IID iid;
1697 if (FAILED(SHCLSIDFromStringW(szGUID, &iid)))
1699 ERR("%s is not a GUID\n", debugstr_w(szGUID));
1700 return NULL;
1702 return _ILCreateGuid(PT_GUID, &iid);
1705 LPITEMIDLIST _ILCreateFromFindDataW( const WIN32_FIND_DATAW *wfd )
1707 DWORD wlen, alen;
1708 LPITEMIDLIST pidl;
1709 PIDLTYPE type;
1711 if (!wfd)
1712 return NULL;
1714 TRACE("(%s, %s)\n",debugstr_w(wfd->cAlternateFileName), debugstr_w(wfd->cFileName));
1716 type = (wfd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? PT_FOLDER : PT_VALUE;
1718 alen = WideCharToMultiByte( CP_ACP, 0, wfd->cFileName, -1, NULL, 0, NULL, NULL );
1719 wlen = lstrlenW(wfd->cFileName) + 1;
1720 pidl = _ILAlloc(type, FIELD_OFFSET(FileStruct, szNames[alen + (alen & 1)]) +
1721 FIELD_OFFSET(FileStructW, wszName[wlen]) + sizeof(WORD));
1722 if (pidl)
1724 LPPIDLDATA pData = _ILGetDataPointer(pidl);
1725 FileStruct *fs = &pData->u.file;
1726 FileStructW *fsw;
1727 WORD *pOffsetW;
1729 FileTimeToDosDateTime( &wfd->ftLastWriteTime, &fs->uFileDate, &fs->uFileTime);
1730 fs->dwFileSize = wfd->nFileSizeLow;
1731 fs->uFileAttribs = wfd->dwFileAttributes;
1732 WideCharToMultiByte( CP_ACP, 0, wfd->cFileName, -1, fs->szNames, alen, NULL, NULL );
1734 fsw = (FileStructW*)(pData->u.file.szNames + alen + (alen & 0x1));
1735 fsw->cbLen = FIELD_OFFSET(FileStructW, wszName[wlen]) + sizeof(WORD);
1736 FileTimeToDosDateTime( &wfd->ftCreationTime, &fsw->uCreationDate, &fsw->uCreationTime);
1737 FileTimeToDosDateTime( &wfd->ftLastAccessTime, &fsw->uLastAccessDate, &fsw->uLastAccessTime);
1738 memcpy(fsw->wszName, wfd->cFileName, wlen * sizeof(WCHAR));
1740 pOffsetW = (WORD*)((LPBYTE)pidl + pidl->mkid.cb - sizeof(WORD));
1741 *pOffsetW = (LPBYTE)fsw - (LPBYTE)pidl;
1742 TRACE("-- Set Value: %s\n",debugstr_w(fsw->wszName));
1744 return pidl;
1748 HRESULT _ILCreateFromPathW(LPCWSTR szPath, LPITEMIDLIST* ppidl)
1750 HANDLE hFile;
1751 WIN32_FIND_DATAW stffile;
1753 if (!ppidl)
1754 return E_INVALIDARG;
1756 hFile = FindFirstFileW(szPath, &stffile);
1757 if (hFile == INVALID_HANDLE_VALUE)
1758 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
1760 FindClose(hFile);
1762 *ppidl = _ILCreateFromFindDataW(&stffile);
1764 return *ppidl ? S_OK : E_OUTOFMEMORY;
1767 LPITEMIDLIST _ILCreateDrive(LPCWSTR lpszNew)
1769 LPITEMIDLIST pidlOut;
1771 TRACE("(%s)\n",debugstr_w(lpszNew));
1773 pidlOut = _ILAlloc(PT_DRIVE, sizeof(DriveStruct));
1774 if (pidlOut)
1776 LPSTR pszDest;
1778 pszDest = _ILGetTextPointer(pidlOut);
1779 if (pszDest)
1781 strcpy(pszDest, "x:\\");
1782 pszDest[0]=towupper(lpszNew[0]);
1783 TRACE("-- create Drive: %s\n", debugstr_a(pszDest));
1786 return pidlOut;
1789 LPITEMIDLIST _ILCreateEntireNetwork(void)
1791 LPITEMIDLIST pidlOut;
1793 TRACE("\n");
1795 pidlOut = _ILAlloc(PT_NETWORK, FIELD_OFFSET(PIDLDATA, u.network.szNames[sizeof("Entire Network")]));
1796 if (pidlOut)
1798 LPPIDLDATA pData = _ILGetDataPointer(pidlOut);
1800 pData->u.network.dummy = 0;
1801 strcpy(pData->u.network.szNames, "Entire Network");
1803 return pidlOut;
1806 /**************************************************************************
1807 * _ILGetDrive()
1809 * Gets the text for the drive eg. 'c:\'
1811 * RETURNS
1812 * strlen (lpszText)
1814 DWORD _ILGetDrive(LPCITEMIDLIST pidl,LPSTR pOut, UINT uSize)
1816 TRACE("(%p,%p,%u)\n",pidl,pOut,uSize);
1818 if(_ILIsMyComputer(pidl))
1819 pidl = ILGetNext(pidl);
1821 if (pidl && _ILIsDrive(pidl))
1822 return _ILSimpleGetText(pidl, pOut, uSize);
1824 return 0;
1827 /**************************************************************************
1829 * ### 2. section testing pidls ###
1831 **************************************************************************
1832 * _ILIsUnicode()
1833 * _ILIsDesktop()
1834 * _ILIsMyComputer()
1835 * _ILIsSpecialFolder()
1836 * _ILIsDrive()
1837 * _ILIsFolder()
1838 * _ILIsValue()
1839 * _ILIsPidlSimple()
1841 BOOL _ILIsUnicode(LPCITEMIDLIST pidl)
1843 LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1845 TRACE("(%p)\n",pidl);
1847 return (pidl && lpPData && PT_VALUEW == lpPData->type);
1850 BOOL _ILIsDesktop(LPCITEMIDLIST pidl)
1852 TRACE("(%p)\n",pidl);
1854 return !pidl || !pidl->mkid.cb;
1857 BOOL _ILIsMyComputer(LPCITEMIDLIST pidl)
1859 REFIID iid = _ILGetGUIDPointer(pidl);
1861 TRACE("(%p)\n",pidl);
1863 if (iid)
1864 return IsEqualIID(iid, &CLSID_MyComputer);
1865 return FALSE;
1868 BOOL _ILIsSpecialFolder (LPCITEMIDLIST pidl)
1870 LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1872 TRACE("(%p)\n",pidl);
1874 return (pidl && ( (lpPData && (PT_GUID== lpPData->type || PT_SHELLEXT== lpPData->type || PT_YAGUID == lpPData->type)) ||
1875 (pidl && pidl->mkid.cb == 0x00)
1879 BOOL _ILIsDrive(LPCITEMIDLIST pidl)
1881 LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1883 TRACE("(%p)\n",pidl);
1885 return (pidl && lpPData && (PT_DRIVE == lpPData->type ||
1886 PT_DRIVE1 == lpPData->type ||
1887 PT_DRIVE2 == lpPData->type ||
1888 PT_DRIVE3 == lpPData->type));
1891 BOOL _ILIsFolder(LPCITEMIDLIST pidl)
1893 LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1895 TRACE("(%p)\n",pidl);
1897 return (pidl && lpPData && (PT_FOLDER == lpPData->type || PT_FOLDER1 == lpPData->type));
1900 BOOL _ILIsValue(LPCITEMIDLIST pidl)
1902 LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1904 TRACE("(%p)\n",pidl);
1906 return (pidl && lpPData && PT_VALUE == lpPData->type);
1909 BOOL _ILIsCPanelStruct(LPCITEMIDLIST pidl)
1911 LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1913 TRACE("(%p)\n",pidl);
1915 return (pidl && lpPData && (lpPData->type == 0));
1918 /**************************************************************************
1919 * _ILIsPidlSimple
1921 BOOL _ILIsPidlSimple(LPCITEMIDLIST pidl)
1923 BOOL ret = TRUE;
1925 if(! _ILIsDesktop(pidl)) /* pidl=NULL or mkid.cb=0 */
1927 WORD len = pidl->mkid.cb;
1928 LPCITEMIDLIST pidlnext = (LPCITEMIDLIST) (((const BYTE*)pidl) + len );
1930 if (pidlnext->mkid.cb)
1931 ret = FALSE;
1934 TRACE("%s\n", ret ? "Yes" : "No");
1935 return ret;
1938 /**************************************************************************
1940 * ### 3. section getting values from pidls ###
1943 /**************************************************************************
1944 * _ILSimpleGetText
1946 * gets the text for the first item in the pidl (eg. simple pidl)
1948 * returns the length of the string
1950 DWORD _ILSimpleGetText (LPCITEMIDLIST pidl, LPSTR szOut, UINT uOutSize)
1952 DWORD dwReturn=0;
1953 LPSTR szSrc;
1954 LPWSTR szSrcW;
1955 GUID const * riid;
1956 char szTemp[MAX_PATH];
1958 TRACE("(%p %p %x)\n",pidl,szOut,uOutSize);
1960 if (!pidl)
1961 return 0;
1963 if (szOut)
1964 *szOut = 0;
1966 if (_ILIsDesktop(pidl))
1968 /* desktop */
1969 if (HCR_GetClassNameA(&CLSID_ShellDesktop, szTemp, MAX_PATH))
1971 if (szOut)
1972 lstrcpynA(szOut, szTemp, uOutSize);
1974 dwReturn = strlen (szTemp);
1977 else if (( szSrc = _ILGetTextPointer(pidl) ))
1979 /* filesystem */
1980 if (szOut)
1981 lstrcpynA(szOut, szSrc, uOutSize);
1983 dwReturn = strlen(szSrc);
1985 else if (( szSrcW = _ILGetTextPointerW(pidl) ))
1987 /* unicode filesystem */
1988 WideCharToMultiByte(CP_ACP,0,szSrcW, -1, szTemp, MAX_PATH, NULL, NULL);
1990 if (szOut)
1991 lstrcpynA(szOut, szTemp, uOutSize);
1993 dwReturn = strlen (szTemp);
1995 else if (( riid = _ILGetGUIDPointer(pidl) ))
1997 /* special folder */
1998 if ( HCR_GetClassNameA(riid, szTemp, MAX_PATH) )
2000 if (szOut)
2001 lstrcpynA(szOut, szTemp, uOutSize);
2003 dwReturn = strlen (szTemp);
2006 else
2008 ERR("-- no text\n");
2011 TRACE("-- (%p=%s 0x%08x)\n",szOut,debugstr_a(szOut),dwReturn);
2012 return dwReturn;
2015 /**************************************************************************
2016 * _ILSimpleGetTextW
2018 * gets the text for the first item in the pidl (eg. simple pidl)
2020 * returns the length of the string
2022 DWORD _ILSimpleGetTextW (LPCITEMIDLIST pidl, LPWSTR szOut, UINT uOutSize)
2024 DWORD dwReturn;
2025 FileStructW *pFileStructW = _ILGetFileStructW(pidl);
2027 TRACE("(%p %p %x)\n",pidl,szOut,uOutSize);
2029 if (pFileStructW) {
2030 lstrcpynW(szOut, pFileStructW->wszName, uOutSize);
2031 dwReturn = lstrlenW(pFileStructW->wszName);
2032 } else {
2033 GUID const * riid;
2034 WCHAR szTemp[MAX_PATH];
2035 LPSTR szSrc;
2036 LPWSTR szSrcW;
2037 dwReturn=0;
2039 if (!pidl)
2040 return 0;
2042 if (szOut)
2043 *szOut = 0;
2045 if (_ILIsDesktop(pidl))
2047 /* desktop */
2048 if (HCR_GetClassNameW(&CLSID_ShellDesktop, szTemp, MAX_PATH))
2050 if (szOut)
2051 lstrcpynW(szOut, szTemp, uOutSize);
2053 dwReturn = lstrlenW (szTemp);
2056 else if (( szSrcW = _ILGetTextPointerW(pidl) ))
2058 /* unicode filesystem */
2059 if (szOut)
2060 lstrcpynW(szOut, szSrcW, uOutSize);
2062 dwReturn = lstrlenW(szSrcW);
2064 else if (( szSrc = _ILGetTextPointer(pidl) ))
2066 /* filesystem */
2067 MultiByteToWideChar(CP_ACP, 0, szSrc, -1, szTemp, MAX_PATH);
2069 if (szOut)
2070 lstrcpynW(szOut, szTemp, uOutSize);
2072 dwReturn = lstrlenW (szTemp);
2074 else if (( riid = _ILGetGUIDPointer(pidl) ))
2076 /* special folder */
2077 if ( HCR_GetClassNameW(riid, szTemp, MAX_PATH) )
2079 if (szOut)
2080 lstrcpynW(szOut, szTemp, uOutSize);
2082 dwReturn = lstrlenW (szTemp);
2085 else
2087 ERR("-- no text\n");
2091 TRACE("-- (%p=%s 0x%08x)\n",szOut,debugstr_w(szOut),dwReturn);
2092 return dwReturn;
2095 /**************************************************************************
2097 * ### 4. getting pointers to parts of pidls ###
2099 **************************************************************************
2100 * _ILGetDataPointer()
2102 LPPIDLDATA _ILGetDataPointer(LPCITEMIDLIST pidl)
2104 if(!_ILIsEmpty(pidl))
2105 return (LPPIDLDATA)pidl->mkid.abID;
2106 return NULL;
2109 /**************************************************************************
2110 * _ILGetTextPointerW()
2111 * gets a pointer to the unicode long filename string stored in the pidl
2113 static LPWSTR _ILGetTextPointerW(LPCITEMIDLIST pidl)
2115 /* TRACE(pidl,"(pidl%p)\n", pidl);*/
2117 LPPIDLDATA pdata = _ILGetDataPointer(pidl);
2119 if (!pdata)
2120 return NULL;
2122 switch (pdata->type)
2124 case PT_GUID:
2125 case PT_SHELLEXT:
2126 case PT_YAGUID:
2127 return NULL;
2129 case PT_DRIVE:
2130 case PT_DRIVE1:
2131 case PT_DRIVE2:
2132 case PT_DRIVE3:
2133 /*return (LPSTR)&(pdata->u.drive.szDriveName);*/
2134 return NULL;
2136 case PT_FOLDER:
2137 case PT_FOLDER1:
2138 case PT_VALUE:
2139 case PT_IESPECIAL1:
2140 case PT_IESPECIAL2:
2141 /*return (LPSTR)&(pdata->u.file.szNames);*/
2142 return NULL;
2144 case PT_WORKGRP:
2145 case PT_COMP:
2146 case PT_NETWORK:
2147 case PT_NETPROVIDER:
2148 case PT_SHARE:
2149 /*return (LPSTR)&(pdata->u.network.szNames);*/
2150 return NULL;
2152 case PT_VALUEW:
2153 return (LPWSTR)pdata->u.file.szNames;
2155 return NULL;
2159 /**************************************************************************
2160 * _ILGetTextPointer()
2161 * gets a pointer to the long filename string stored in the pidl
2163 LPSTR _ILGetTextPointer(LPCITEMIDLIST pidl)
2165 /* TRACE(pidl,"(pidl%p)\n", pidl);*/
2167 LPPIDLDATA pdata = _ILGetDataPointer(pidl);
2169 if (!pdata)
2170 return NULL;
2172 switch (pdata->type)
2174 case PT_GUID:
2175 case PT_SHELLEXT:
2176 case PT_YAGUID:
2177 return NULL;
2179 case PT_DRIVE:
2180 case PT_DRIVE1:
2181 case PT_DRIVE2:
2182 case PT_DRIVE3:
2183 return pdata->u.drive.szDriveName;
2185 case PT_FOLDER:
2186 case PT_FOLDER1:
2187 case PT_VALUE:
2188 case PT_IESPECIAL1:
2189 case PT_IESPECIAL2:
2190 return pdata->u.file.szNames;
2192 case PT_WORKGRP:
2193 case PT_COMP:
2194 case PT_NETWORK:
2195 case PT_NETPROVIDER:
2196 case PT_SHARE:
2197 return pdata->u.network.szNames;
2199 return NULL;
2202 /**************************************************************************
2203 * _ILGetSTextPointer()
2204 * gets a pointer to the short filename string stored in the pidl
2206 static LPSTR _ILGetSTextPointer(LPCITEMIDLIST pidl)
2208 /* TRACE(pidl,"(pidl%p)\n", pidl); */
2210 LPPIDLDATA pdata =_ILGetDataPointer(pidl);
2212 if (!pdata)
2213 return NULL;
2215 switch (pdata->type)
2217 case PT_FOLDER:
2218 case PT_VALUE:
2219 case PT_IESPECIAL1:
2220 case PT_IESPECIAL2:
2221 return pdata->u.file.szNames + strlen (pdata->u.file.szNames) + 1;
2223 case PT_WORKGRP:
2224 return pdata->u.network.szNames + strlen (pdata->u.network.szNames) + 1;
2226 return NULL;
2229 /**************************************************************************
2230 * _ILGetGUIDPointer()
2232 * returns reference to guid stored in some pidls
2234 IID* _ILGetGUIDPointer(LPCITEMIDLIST pidl)
2236 LPPIDLDATA pdata =_ILGetDataPointer(pidl);
2238 TRACE("%p\n", pidl);
2240 if (!pdata)
2241 return NULL;
2243 TRACE("pdata->type 0x%04x\n", pdata->type);
2244 switch (pdata->type)
2246 case PT_SHELLEXT:
2247 case PT_GUID:
2248 case PT_YAGUID:
2249 return &(pdata->u.guid.guid);
2251 default:
2252 TRACE("Unknown pidl type 0x%04x\n", pdata->type);
2253 break;
2255 return NULL;
2258 /******************************************************************************
2259 * _ILGetFileStructW [Internal]
2261 * Get pointer the a SHITEMID's FileStructW field if present
2263 * PARAMS
2264 * pidl [I] The SHITEMID
2266 * RETURNS
2267 * Success: Pointer to pidl's FileStructW field.
2268 * Failure: NULL
2270 FileStructW* _ILGetFileStructW(LPCITEMIDLIST pidl) {
2271 FileStructW *pFileStructW;
2272 WORD cbOffset;
2274 if (!(_ILIsValue(pidl) || _ILIsFolder(pidl)))
2275 return NULL;
2277 cbOffset = *(const WORD *)((const BYTE *)pidl + pidl->mkid.cb - sizeof(WORD));
2278 pFileStructW = (FileStructW*)((LPBYTE)pidl + cbOffset);
2280 /* Currently I don't see a fool prove way to figure out if a pidl is for sure of WinXP
2281 * style with a FileStructW member. If we switch all our shellfolder-implementations to
2282 * the new format, this won't be a problem. For now, we do as many sanity checks as possible. */
2283 if ((cbOffset & 0x1) || /* FileStructW member is word aligned in the pidl */
2284 /* FileStructW is positioned after FileStruct */
2285 cbOffset < sizeof(pidl->mkid.cb) + sizeof(PIDLTYPE) + sizeof(FileStruct) ||
2286 /* There has to be enough space at cbOffset in the pidl to hold FileStructW and cbOffset */
2287 cbOffset > pidl->mkid.cb - sizeof(cbOffset) - sizeof(FileStructW) ||
2288 pidl->mkid.cb != cbOffset + pFileStructW->cbLen)
2290 WARN("Invalid pidl format (cbOffset = %d)!\n", cbOffset);
2291 return NULL;
2294 return pFileStructW;
2297 /*************************************************************************
2298 * _ILGetFileDateTime
2300 * Given the ItemIdList, get the FileTime
2302 * PARAMS
2303 * pidl [I] The ItemIDList
2304 * pFt [I] the resulted FILETIME of the file
2306 * RETURNS
2307 * True if Successful
2309 * NOTES
2312 BOOL _ILGetFileDateTime(LPCITEMIDLIST pidl, FILETIME *pFt)
2314 LPPIDLDATA pdata = _ILGetDataPointer(pidl);
2316 if (!pdata)
2317 return FALSE;
2319 switch (pdata->type)
2321 case PT_FOLDER:
2322 case PT_VALUE:
2323 DosDateTimeToFileTime(pdata->u.file.uFileDate, pdata->u.file.uFileTime, pFt);
2324 break;
2325 default:
2326 return FALSE;
2328 return TRUE;
2331 BOOL _ILGetFileDate (LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2333 FILETIME ft,lft;
2334 SYSTEMTIME time;
2335 BOOL ret;
2337 if (_ILGetFileDateTime( pidl, &ft ))
2339 FileTimeToLocalFileTime(&ft, &lft);
2340 FileTimeToSystemTime (&lft, &time);
2342 ret = GetDateFormatA(LOCALE_USER_DEFAULT,DATE_SHORTDATE,&time, NULL, pOut, uOutSize);
2343 if (ret)
2345 /* Append space + time without seconds */
2346 pOut[ret-1] = ' ';
2347 GetTimeFormatA(LOCALE_USER_DEFAULT, TIME_NOSECONDS, &time, NULL, &pOut[ret], uOutSize - ret);
2350 else
2352 pOut[0] = '\0';
2353 ret = FALSE;
2355 return ret;
2358 /*************************************************************************
2359 * _ILGetFileSize
2361 * Given the ItemIdList, get the FileSize
2363 * PARAMS
2364 * pidl [I] The ItemIDList
2365 * pOut [I] The buffer to save the result
2366 * uOutsize [I] The size of the buffer
2368 * RETURNS
2369 * The FileSize
2371 * NOTES
2372 * pOut can be null when no string is needed
2375 DWORD _ILGetFileSize (LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2377 LPPIDLDATA pdata = _ILGetDataPointer(pidl);
2378 DWORD dwSize;
2380 if (!pdata)
2381 return 0;
2383 switch (pdata->type)
2385 case PT_VALUE:
2386 dwSize = pdata->u.file.dwFileSize;
2387 if (pOut)
2388 StrFormatKBSizeA(dwSize, pOut, uOutSize);
2389 return dwSize;
2391 if (pOut)
2392 *pOut = 0x00;
2393 return 0;
2396 BOOL _ILGetExtension (LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2398 char szTemp[MAX_PATH];
2399 const char * pPoint;
2400 LPCITEMIDLIST pidlTemp=pidl;
2402 TRACE("pidl=%p\n",pidl);
2404 if (!pidl)
2405 return FALSE;
2407 pidlTemp = ILFindLastID(pidl);
2409 if (!_ILIsValue(pidlTemp))
2410 return FALSE;
2411 if (!_ILSimpleGetText(pidlTemp, szTemp, MAX_PATH))
2412 return FALSE;
2414 pPoint = PathFindExtensionA(szTemp);
2416 if (!*pPoint)
2417 return FALSE;
2419 pPoint++;
2420 lstrcpynA(pOut, pPoint, uOutSize);
2421 TRACE("%s\n",pOut);
2423 return TRUE;
2426 /*************************************************************************
2427 * _ILGetFileType
2429 * Given the ItemIdList, get the file type description
2431 * PARAMS
2432 * pidl [I] The ItemIDList (simple)
2433 * pOut [I] The buffer to save the result
2434 * uOutsize [I] The size of the buffer
2436 * RETURNS
2437 * nothing
2439 * NOTES
2440 * This function copies as much as possible into the buffer.
2442 void _ILGetFileType(LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2444 if(_ILIsValue(pidl))
2446 char sTemp[64];
2448 /* "name" or "name." or any unhandled => "File" */
2449 lstrcpynA (pOut, "File", uOutSize);
2451 /* If there's space for more, try to get a better description. */
2452 if (uOutSize > 6 && _ILGetExtension (pidl, sTemp, 64))
2454 if (!( HCR_MapTypeToValueA(sTemp, sTemp, 64, TRUE)
2455 && HCR_MapTypeToValueA(sTemp, pOut, uOutSize, FALSE ))
2456 && sTemp[0])
2458 lstrcpynA (pOut, sTemp, uOutSize - 6);
2459 strcat (pOut, " file");
2463 else
2464 lstrcpynA(pOut, "Folder", uOutSize);
2467 /*************************************************************************
2468 * _ILGetFileAttributes
2470 * Given the ItemIdList, get the Attrib string format
2472 * PARAMS
2473 * pidl [I] The ItemIDList
2474 * pOut [I] The buffer to save the result
2475 * uOutsize [I] The size of the Buffer
2477 * RETURNS
2478 * Attributes
2480 * FIXME
2481 * return value 0 in case of error is a valid return value
2484 DWORD _ILGetFileAttributes(LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2486 LPPIDLDATA pData = _ILGetDataPointer(pidl);
2487 WORD wAttrib = 0;
2488 int i;
2490 if (!pData)
2491 return 0;
2493 switch(pData->type)
2495 case PT_FOLDER:
2496 case PT_VALUE:
2497 wAttrib = pData->u.file.uFileAttribs;
2498 break;
2501 if(uOutSize >= 6)
2503 i=0;
2504 if(wAttrib & FILE_ATTRIBUTE_READONLY)
2505 pOut[i++] = 'R';
2506 if(wAttrib & FILE_ATTRIBUTE_HIDDEN)
2507 pOut[i++] = 'H';
2508 if(wAttrib & FILE_ATTRIBUTE_SYSTEM)
2509 pOut[i++] = 'S';
2510 if(wAttrib & FILE_ATTRIBUTE_ARCHIVE)
2511 pOut[i++] = 'A';
2512 if(wAttrib & FILE_ATTRIBUTE_COMPRESSED)
2513 pOut[i++] = 'C';
2514 pOut[i] = 0x00;
2516 return wAttrib;
2519 /*************************************************************************
2520 * ILFreeaPidl
2522 * frees an aPidl struct
2524 void _ILFreeaPidl(LPITEMIDLIST * apidl, UINT cidl)
2526 UINT i;
2528 if (apidl)
2530 for (i = 0; i < cidl; i++)
2531 SHFree(apidl[i]);
2532 SHFree(apidl);
2536 /*************************************************************************
2537 * ILCopyaPidl
2539 * copies an aPidl struct
2541 LPITEMIDLIST* _ILCopyaPidl(const LPCITEMIDLIST * apidlsrc, UINT cidl)
2543 UINT i;
2544 LPITEMIDLIST *apidldest;
2546 if (!apidlsrc)
2547 return NULL;
2549 apidldest = SHAlloc(cidl * sizeof(LPITEMIDLIST));
2551 for (i = 0; i < cidl; i++)
2552 apidldest[i] = ILClone(apidlsrc[i]);
2554 return apidldest;
2557 /*************************************************************************
2558 * _ILCopyCidaToaPidl
2560 * creates aPidl from CIDA
2562 LPITEMIDLIST* _ILCopyCidaToaPidl(LPITEMIDLIST* pidl, const CIDA * cida)
2564 UINT i;
2565 LPITEMIDLIST *dst;
2567 dst = SHAlloc(cida->cidl * sizeof(LPITEMIDLIST));
2568 if (!dst)
2569 return NULL;
2571 if (pidl)
2572 *pidl = ILClone((LPCITEMIDLIST)(&((const BYTE*)cida)[cida->aoffset[0]]));
2574 for (i = 0; i < cida->cidl; i++)
2575 dst[i] = ILClone((LPCITEMIDLIST)(&((const BYTE*)cida)[cida->aoffset[i + 1]]));
2577 return dst;