shell32: Declare some functions static.
[wine/hacks.git] / dlls / shell32 / pidl.c
blob97cf05125b27e32d551dd175f0b7f65222971e1a
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 %d\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 %d\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 = %x\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 %d\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 SHFree(*ppPidl);
295 *ppPidl = NULL;
297 IStream_AddRef (pStream);
299 if (SUCCEEDED(IStream_Read(pStream, (LPVOID)&wLen, 2, &dwBytesRead)))
301 TRACE("PIDL length is %d\n", wLen);
302 if (wLen != 0)
304 *ppPidl = SHAlloc (wLen);
305 if (SUCCEEDED(IStream_Read(pStream, *ppPidl , wLen, &dwBytesRead)))
307 TRACE("Stream read OK\n");
308 ret = S_OK;
310 else
312 WARN("reading pidl failed\n");
313 SHFree(*ppPidl);
314 *ppPidl = NULL;
317 else
319 *ppPidl = NULL;
320 ret = S_OK;
324 /* we are not yet fully compatible */
325 if (*ppPidl && !pcheck(*ppPidl))
327 WARN("Check failed\n");
328 SHFree(*ppPidl);
329 *ppPidl = NULL;
332 IStream_Release (pStream);
333 TRACE("done\n");
334 return ret;
337 /*************************************************************************
338 * ILSaveToStream (SHELL32.27)
340 * NOTES
341 * the first two bytes are the len, the pidl is following then
343 HRESULT WINAPI ILSaveToStream (IStream * pStream, LPCITEMIDLIST pPidl)
345 LPCITEMIDLIST pidl;
346 WORD wLen = 0;
347 HRESULT ret = E_FAIL;
349 TRACE_(shell)("%p %p\n", pStream, pPidl);
351 IStream_AddRef (pStream);
353 pidl = pPidl;
354 while (pidl->mkid.cb)
356 wLen += sizeof(WORD) + pidl->mkid.cb;
357 pidl = ILGetNext(pidl);
360 if (SUCCEEDED(IStream_Write(pStream, (LPVOID)&wLen, 2, NULL)))
362 if (SUCCEEDED(IStream_Write(pStream, pPidl, wLen, NULL)))
363 ret = S_OK;
365 IStream_Release (pStream);
367 return ret;
370 /*************************************************************************
371 * SHILCreateFromPath [SHELL32.28]
373 * Create an ItemIDList from a path
375 * PARAMS
376 * path [I]
377 * ppidl [O]
378 * attributes [I/O] requested attributes on call and actual attributes when
379 * the function returns
381 * RETURNS
382 * NO_ERROR if successful, or an OLE errer code otherwise
384 * NOTES
385 * Wrapper for IShellFolder_ParseDisplayName().
387 HRESULT WINAPI SHILCreateFromPathA(LPCSTR path, LPITEMIDLIST * ppidl, DWORD * attributes)
389 WCHAR lpszDisplayName[MAX_PATH];
391 TRACE_(shell)("%s %p 0x%08x\n", path, ppidl, attributes ? *attributes : 0);
393 if (!MultiByteToWideChar(CP_ACP, 0, path, -1, lpszDisplayName, MAX_PATH))
394 lpszDisplayName[MAX_PATH-1] = 0;
396 return SHILCreateFromPathW(lpszDisplayName, ppidl, attributes);
399 HRESULT WINAPI SHILCreateFromPathW(LPCWSTR path, LPITEMIDLIST * ppidl, DWORD * attributes)
401 LPSHELLFOLDER sf;
402 DWORD pchEaten;
403 HRESULT ret = E_FAIL;
405 TRACE_(shell)("%s %p 0x%08x\n", debugstr_w(path), ppidl, attributes ? *attributes : 0);
407 if (SUCCEEDED (SHGetDesktopFolder(&sf)))
409 ret = IShellFolder_ParseDisplayName(sf, 0, NULL, (LPWSTR)path, &pchEaten, ppidl, attributes);
410 IShellFolder_Release(sf);
412 return ret;
415 HRESULT WINAPI SHILCreateFromPathAW (LPCVOID path, LPITEMIDLIST * ppidl, DWORD * attributes)
417 if ( SHELL_OsIsUnicode())
418 return SHILCreateFromPathW (path, ppidl, attributes);
419 return SHILCreateFromPathA (path, ppidl, attributes);
422 /*************************************************************************
423 * SHCloneSpecialIDList [SHELL32.89]
425 * Create an ItemIDList to one of the special folders.
427 * PARAMS
428 * hwndOwner [in]
429 * nFolder [in] CSIDL_xxxxx
430 * fCreate [in] Create folder if it does not exist
432 * RETURNS
433 * Success: The newly created pidl
434 * Failure: NULL, if inputs are invalid.
436 * NOTES
437 * exported by ordinal.
438 * Caller is responsible for deallocating the returned ItemIDList with the
439 * shells IMalloc interface, aka ILFree.
441 LPITEMIDLIST WINAPI SHCloneSpecialIDList(HWND hwndOwner, DWORD nFolder, BOOL fCreate)
443 LPITEMIDLIST ppidl;
444 TRACE_(shell)("(hwnd=%p,csidl=0x%x,%s).\n", hwndOwner, nFolder, fCreate ? "T" : "F");
446 if (fCreate)
447 nFolder |= CSIDL_FLAG_CREATE;
449 SHGetSpecialFolderLocation(hwndOwner, nFolder, &ppidl);
450 return ppidl;
453 /*************************************************************************
454 * ILGlobalClone [SHELL32.20]
456 * Clones an ItemIDList using Alloc.
458 * PARAMS
459 * pidl [I] ItemIDList to clone
461 * RETURNS
462 * Newly allocated ItemIDList.
464 * NOTES
465 * exported by ordinal.
467 LPITEMIDLIST WINAPI ILGlobalClone(LPCITEMIDLIST pidl)
469 DWORD len;
470 LPITEMIDLIST newpidl;
472 if (!pidl)
473 return NULL;
475 len = ILGetSize(pidl);
476 newpidl = (LPITEMIDLIST)Alloc(len);
477 if (newpidl)
478 memcpy(newpidl,pidl,len);
480 TRACE("pidl=%p newpidl=%p\n",pidl, newpidl);
481 pdump(pidl);
483 return newpidl;
486 /*************************************************************************
487 * ILIsEqual [SHELL32.21]
490 BOOL WINAPI ILIsEqual(LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
492 char szData1[MAX_PATH];
493 char szData2[MAX_PATH];
495 LPCITEMIDLIST pidltemp1 = pidl1;
496 LPCITEMIDLIST pidltemp2 = pidl2;
498 TRACE("pidl1=%p pidl2=%p\n",pidl1, pidl2);
501 * Explorer reads from registry directly (StreamMRU),
502 * so we can only check here
504 if (!pcheck(pidl1) || !pcheck (pidl2))
505 return FALSE;
507 pdump (pidl1);
508 pdump (pidl2);
510 if (!pidl1 || !pidl2)
511 return FALSE;
513 while (pidltemp1->mkid.cb && pidltemp2->mkid.cb)
515 _ILSimpleGetText(pidltemp1, szData1, MAX_PATH);
516 _ILSimpleGetText(pidltemp2, szData2, MAX_PATH);
518 if (strcasecmp( szData1, szData2 ))
519 return FALSE;
521 pidltemp1 = ILGetNext(pidltemp1);
522 pidltemp2 = ILGetNext(pidltemp2);
525 if (!pidltemp1->mkid.cb && !pidltemp2->mkid.cb)
526 return TRUE;
528 return FALSE;
531 /*************************************************************************
532 * ILIsParent [SHELL32.23]
534 * Verifies that pidlParent is indeed the (immediate) parent of pidlChild.
536 * PARAMS
537 * pidlParent [I]
538 * pidlChild [I]
539 * bImmediate [I] only return true if the parent is the direct parent
540 * of the child
542 * RETURNS
543 * True if the parent ItemIDlist is a complete part of the child ItemIdList,
544 * False otherwise.
546 * NOTES
547 * parent = a/b, child = a/b/c -> true, c is in folder a/b
548 * child = a/b/c/d -> false if bImmediate is true, d is not in folder a/b
549 * child = a/b/c/d -> true if bImmediate is false, d is in a subfolder of a/b
551 BOOL WINAPI ILIsParent(LPCITEMIDLIST pidlParent, LPCITEMIDLIST pidlChild, BOOL bImmediate)
553 char szData1[MAX_PATH];
554 char szData2[MAX_PATH];
555 LPCITEMIDLIST pParent = pidlParent;
556 LPCITEMIDLIST pChild = pidlChild;
558 TRACE("%p %p %x\n", pidlParent, pidlChild, bImmediate);
560 if (!pParent || !pChild)
561 return FALSE;
563 while (pParent->mkid.cb && pChild->mkid.cb)
565 _ILSimpleGetText(pParent, szData1, MAX_PATH);
566 _ILSimpleGetText(pChild, szData2, MAX_PATH);
568 if (strcasecmp( szData1, szData2 ))
569 return FALSE;
571 pParent = ILGetNext(pParent);
572 pChild = ILGetNext(pChild);
575 /* child shorter or has equal length to parent */
576 if (pParent->mkid.cb || !pChild->mkid.cb)
577 return FALSE;
579 /* not immediate descent */
580 if ( ILGetNext(pChild)->mkid.cb && bImmediate)
581 return FALSE;
583 return TRUE;
586 /*************************************************************************
587 * ILFindChild [SHELL32.24]
589 * Compares elements from pidl1 and pidl2.
591 * PARAMS
592 * pidl1 [I]
593 * pidl2 [I]
595 * RETURNS
596 * pidl1 is desktop pidl2
597 * pidl1 shorter pidl2 pointer to first different element of pidl2
598 * if there was at least one equal element
599 * pidl2 shorter pidl1 0
600 * pidl2 equal pidl1 pointer to last 0x00-element of pidl2
602 * NOTES
603 * exported by ordinal.
605 LPITEMIDLIST WINAPI ILFindChild(LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
607 char szData1[MAX_PATH];
608 char szData2[MAX_PATH];
610 LPCITEMIDLIST pidltemp1 = pidl1;
611 LPCITEMIDLIST pidltemp2 = pidl2;
612 LPCITEMIDLIST ret=NULL;
614 TRACE("pidl1=%p pidl2=%p\n",pidl1, pidl2);
616 /* explorer reads from registry directly (StreamMRU),
617 so we can only check here */
618 if ((!pcheck (pidl1)) || (!pcheck (pidl2)))
619 return FALSE;
621 pdump (pidl1);
622 pdump (pidl2);
624 if (_ILIsDesktop(pidl1))
626 ret = pidl2;
628 else
630 while (pidltemp1->mkid.cb && pidltemp2->mkid.cb)
632 _ILSimpleGetText(pidltemp1, szData1, MAX_PATH);
633 _ILSimpleGetText(pidltemp2, szData2, MAX_PATH);
635 if (strcasecmp(szData1,szData2))
636 break;
638 pidltemp1 = ILGetNext(pidltemp1);
639 pidltemp2 = ILGetNext(pidltemp2);
640 ret = pidltemp2;
643 if (pidltemp1->mkid.cb)
644 ret = NULL; /* elements of pidl1 left*/
646 TRACE_(shell)("--- %p\n", ret);
647 return (LPITEMIDLIST)ret; /* pidl 1 is shorter */
650 /*************************************************************************
651 * ILCombine [SHELL32.25]
653 * Concatenates two complex ItemIDLists.
655 * PARAMS
656 * pidl1 [I] first complex ItemIDLists
657 * pidl2 [I] complex ItemIDLists to append
659 * RETURNS
660 * if both pidl's == NULL NULL
661 * if pidl1 == NULL cloned pidl2
662 * if pidl2 == NULL cloned pidl1
663 * otherwise new pidl with pidl2 appended to pidl1
665 * NOTES
666 * exported by ordinal.
667 * Does not destroy the passed in ItemIDLists!
669 LPITEMIDLIST WINAPI ILCombine(LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
671 DWORD len1,len2;
672 LPITEMIDLIST pidlNew;
674 TRACE("pidl=%p pidl=%p\n",pidl1,pidl2);
676 if (!pidl1 && !pidl2) return NULL;
678 pdump (pidl1);
679 pdump (pidl2);
681 if (!pidl1)
683 pidlNew = ILClone(pidl2);
684 return pidlNew;
687 if (!pidl2)
689 pidlNew = ILClone(pidl1);
690 return pidlNew;
693 len1 = ILGetSize(pidl1)-2;
694 len2 = ILGetSize(pidl2);
695 pidlNew = SHAlloc(len1+len2);
697 if (pidlNew)
699 memcpy(pidlNew,pidl1,len1);
700 memcpy(((BYTE *)pidlNew)+len1,pidl2,len2);
703 /* TRACE(pidl,"--new pidl=%p\n",pidlNew);*/
704 return pidlNew;
707 /*************************************************************************
708 * SHGetRealIDL [SHELL32.98]
710 * NOTES
712 HRESULT WINAPI SHGetRealIDL(LPSHELLFOLDER lpsf, LPCITEMIDLIST pidlSimple, LPITEMIDLIST *pidlReal)
714 IDataObject* pDataObj;
715 HRESULT hr;
717 hr = IShellFolder_GetUIObjectOf(lpsf, 0, 1, &pidlSimple,
718 &IID_IDataObject, 0, (LPVOID*)&pDataObj);
719 if (SUCCEEDED(hr))
721 STGMEDIUM medium;
722 FORMATETC fmt;
724 fmt.cfFormat = RegisterClipboardFormatA(CFSTR_SHELLIDLIST);
725 fmt.ptd = NULL;
726 fmt.dwAspect = DVASPECT_CONTENT;
727 fmt.lindex = -1;
728 fmt.tymed = TYMED_HGLOBAL;
730 hr = IDataObject_GetData(pDataObj, &fmt, &medium);
732 IDataObject_Release(pDataObj);
734 if (SUCCEEDED(hr))
736 /*assert(pida->cidl==1);*/
737 LPIDA pida = (LPIDA)GlobalLock(medium.u.hGlobal);
739 LPCITEMIDLIST pidl_folder = (LPCITEMIDLIST) ((LPBYTE)pida+pida->aoffset[0]);
740 LPCITEMIDLIST pidl_child = (LPCITEMIDLIST) ((LPBYTE)pida+pida->aoffset[1]);
742 *pidlReal = ILCombine(pidl_folder, pidl_child);
744 if (!*pidlReal)
745 hr = E_OUTOFMEMORY;
747 GlobalUnlock(medium.u.hGlobal);
748 GlobalFree(medium.u.hGlobal);
752 return hr;
755 /*************************************************************************
756 * SHLogILFromFSIL [SHELL32.95]
758 * NOTES
759 * pild = CSIDL_DESKTOP ret = 0
760 * pild = CSIDL_DRIVES ret = 0
762 LPITEMIDLIST WINAPI SHLogILFromFSIL(LPITEMIDLIST pidl)
764 FIXME("(pidl=%p)\n",pidl);
766 pdump(pidl);
768 return 0;
771 /*************************************************************************
772 * ILGetSize [SHELL32.152]
774 * Gets the byte size of an ItemIDList including zero terminator
776 * PARAMS
777 * pidl [I] ItemIDList
779 * RETURNS
780 * size of pidl in bytes
782 * NOTES
783 * exported by ordinal
785 UINT WINAPI ILGetSize(LPCITEMIDLIST pidl)
787 LPCSHITEMID si = &(pidl->mkid);
788 UINT len=0;
790 if (pidl)
792 while (si->cb)
794 len += si->cb;
795 si = (LPCSHITEMID)(((const BYTE*)si)+si->cb);
797 len += 2;
799 TRACE("pidl=%p size=%u\n",pidl, len);
800 return len;
803 /*************************************************************************
804 * ILGetNext [SHELL32.153]
806 * Gets the next ItemID of an ItemIDList
808 * PARAMS
809 * pidl [I] ItemIDList
811 * RETURNS
812 * null -> null
813 * desktop -> null
814 * simple pidl -> pointer to 0x0000 element
816 * NOTES
817 * exported by ordinal.
819 LPITEMIDLIST WINAPI ILGetNext(LPCITEMIDLIST pidl)
821 WORD len;
823 TRACE("%p\n", pidl);
825 if (pidl)
827 len = pidl->mkid.cb;
828 if (len)
830 pidl = (LPCITEMIDLIST) (((const BYTE*)pidl)+len);
831 TRACE("-- %p\n", pidl);
832 return (LPITEMIDLIST)pidl;
835 return NULL;
838 /*************************************************************************
839 * ILAppend [SHELL32.154]
841 * Adds the single ItemID item to the ItemIDList indicated by pidl.
842 * If bEnd is FALSE, inserts the item in the front of the list,
843 * otherwise it adds the item to the end. (???)
845 * PARAMS
846 * pidl [I] ItemIDList to extend
847 * item [I] ItemID to prepend/append
848 * bEnd [I] Indicates if the item should be appended
850 * NOTES
851 * Destroys the passed in idlist! (???)
853 LPITEMIDLIST WINAPI ILAppend(LPITEMIDLIST pidl, LPCITEMIDLIST item, BOOL bEnd)
855 LPITEMIDLIST idlRet;
857 WARN("(pidl=%p,pidl=%p,%08u)semi-stub\n",pidl,item,bEnd);
859 pdump (pidl);
860 pdump (item);
862 if (_ILIsDesktop(pidl))
864 idlRet = ILClone(item);
865 SHFree (pidl);
866 return idlRet;
869 if (bEnd)
870 idlRet = ILCombine(pidl, item);
871 else
872 idlRet = ILCombine(item, pidl);
874 SHFree(pidl);
875 return idlRet;
878 /*************************************************************************
879 * ILFree [SHELL32.155]
881 * Frees memory (if not NULL) allocated by SHMalloc allocator
883 * PARAMS
884 * pidl [I]
886 * RETURNS
887 * Nothing
889 * NOTES
890 * exported by ordinal
892 void WINAPI ILFree(LPITEMIDLIST pidl)
894 TRACE("(pidl=%p)\n",pidl);
895 SHFree(pidl);
898 /*************************************************************************
899 * ILGlobalFree [SHELL32.156]
901 * Frees memory (if not NULL) allocated by Alloc allocator
903 * PARAMS
904 * pidl [I]
906 * RETURNS
907 * Nothing
909 * NOTES
910 * exported by ordinal.
912 void WINAPI ILGlobalFree( LPITEMIDLIST pidl)
914 TRACE("%p\n", pidl);
916 if (pidl)
917 Free(pidl);
920 /*************************************************************************
921 * ILCreateFromPathA [SHELL32.189]
923 * Creates a complex ItemIDList from a path and returns it.
925 * PARAMS
926 * path [I]
928 * RETURNS
929 * the newly created complex ItemIDList or NULL if failed
931 * NOTES
932 * exported by ordinal.
934 LPITEMIDLIST WINAPI ILCreateFromPathA (LPCSTR path)
936 LPITEMIDLIST pidlnew = NULL;
938 TRACE_(shell)("%s\n", debugstr_a(path));
940 if (SUCCEEDED(SHILCreateFromPathA(path, &pidlnew, NULL)))
941 return pidlnew;
942 return NULL;
945 /*************************************************************************
946 * ILCreateFromPathW [SHELL32.190]
948 * See ILCreateFromPathA.
950 LPITEMIDLIST WINAPI ILCreateFromPathW (LPCWSTR path)
952 LPITEMIDLIST pidlnew = NULL;
954 TRACE_(shell)("%s\n", debugstr_w(path));
956 if (SUCCEEDED(SHILCreateFromPathW(path, &pidlnew, NULL)))
957 return pidlnew;
958 return NULL;
961 /*************************************************************************
962 * ILCreateFromPath [SHELL32.157]
964 LPITEMIDLIST WINAPI ILCreateFromPathAW (LPCVOID path)
966 if ( SHELL_OsIsUnicode())
967 return ILCreateFromPathW (path);
968 return ILCreateFromPathA (path);
971 /*************************************************************************
972 * _ILParsePathW [internal]
974 * Creates an ItemIDList from a path and returns it.
976 * PARAMS
977 * path [I] path to parse and convert into an ItemIDList
978 * lpFindFile [I] pointer to buffer to initialize the FileSystem
979 * Bind Data object with
980 * bBindCtx [I] indicates to create a BindContext and assign a
981 * FileSystem Bind Data object
982 * ppidl [O] the newly create ItemIDList
983 * prgfInOut [I/O] requested attributes on input and actual
984 * attributes on return
986 * RETURNS
987 * NO_ERROR on success or an OLE error code
989 * NOTES
990 * If either lpFindFile is non-NULL or bBindCtx is TRUE, this function
991 * creates a BindContext object and assigns a FileSystem Bind Data object
992 * to it, passing the BindContext to IShellFolder_ParseDisplayName. Each
993 * IShellFolder uses that FileSystem Bind Data object of the BindContext
994 * to pass data about the current path element to the next object. This
995 * is used to avoid having to verify the current path element on disk, so
996 * that creating an ItemIDList from a nonexistent path still can work.
998 static HRESULT WINAPI _ILParsePathW(LPCWSTR path, LPWIN32_FIND_DATAW lpFindFile,
999 BOOL bBindCtx, LPITEMIDLIST *ppidl, LPDWORD prgfInOut)
1001 LPSHELLFOLDER pSF = NULL;
1002 LPBC pBC = NULL;
1003 HRESULT ret;
1005 TRACE("%s %p %d (%p)->%p (%p)->0x%x\n", debugstr_w(path), lpFindFile, bBindCtx,
1006 ppidl, ppidl ? *ppidl : NULL,
1007 prgfInOut, prgfInOut ? *prgfInOut : 0);
1009 ret = SHGetDesktopFolder(&pSF);
1010 if (FAILED(ret))
1011 return ret;
1013 if (lpFindFile || bBindCtx)
1014 ret = IFileSystemBindData_Constructor(lpFindFile, &pBC);
1016 if (SUCCEEDED(ret))
1018 ret = IShellFolder_ParseDisplayName(pSF, 0, pBC, (LPOLESTR)path, NULL, ppidl, prgfInOut);
1021 if (pBC)
1023 IBindCtx_Release(pBC);
1024 pBC = NULL;
1027 IShellFolder_Release(pSF);
1029 if (!SUCCEEDED(ret) && ppidl)
1030 *ppidl = NULL;
1032 TRACE("%s %p 0x%x\n", debugstr_w(path), ppidl ? *ppidl : NULL, prgfInOut ? *prgfInOut : 0);
1034 return ret;
1037 /*************************************************************************
1038 * SHSimpleIDListFromPath [SHELL32.162]
1040 * Creates a simple ItemIDList from a path and returns it. This function
1041 * does not fail on nonexistent paths.
1043 * PARAMS
1044 * path [I] path to parse and convert into an ItemIDList
1046 * RETURNS
1047 * the newly created simple ItemIDList
1049 * NOTES
1050 * Simple in the name does not mean a relative ItemIDList but rather a
1051 * fully qualified list, where only the file name is filled in and the
1052 * directory flag for those ItemID elements this is known about, eg.
1053 * it is not the last element in the ItemIDList or the actual directory
1054 * exists on disk.
1055 * exported by ordinal.
1057 LPITEMIDLIST WINAPI SHSimpleIDListFromPathA(LPCSTR lpszPath)
1059 LPITEMIDLIST pidl = NULL;
1060 LPWSTR wPath = NULL;
1061 int len;
1063 TRACE("%s\n", debugstr_a(lpszPath));
1065 if (lpszPath)
1067 len = MultiByteToWideChar(CP_ACP, 0, lpszPath, -1, NULL, 0);
1068 wPath = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1069 MultiByteToWideChar(CP_ACP, 0, lpszPath, -1, wPath, len);
1072 _ILParsePathW(wPath, NULL, TRUE, &pidl, NULL);
1074 HeapFree(GetProcessHeap(), 0, wPath);
1075 TRACE("%s %p\n", debugstr_a(lpszPath), pidl);
1076 return pidl;
1079 LPITEMIDLIST WINAPI SHSimpleIDListFromPathW(LPCWSTR lpszPath)
1081 LPITEMIDLIST pidl = NULL;
1083 TRACE("%s\n", debugstr_w(lpszPath));
1085 _ILParsePathW(lpszPath, NULL, TRUE, &pidl, NULL);
1086 TRACE("%s %p\n", debugstr_w(lpszPath), pidl);
1087 return pidl;
1090 LPITEMIDLIST WINAPI SHSimpleIDListFromPathAW(LPCVOID lpszPath)
1092 if ( SHELL_OsIsUnicode())
1093 return SHSimpleIDListFromPathW (lpszPath);
1094 return SHSimpleIDListFromPathA (lpszPath);
1097 /*************************************************************************
1098 * SHGetDataFromIDListA [SHELL32.247]
1100 * NOTES
1101 * the pidl can be a simple one. since we can't get the path out of the pidl
1102 * we have to take all data from the pidl
1104 HRESULT WINAPI SHGetDataFromIDListA(LPSHELLFOLDER psf, LPCITEMIDLIST pidl,
1105 int nFormat, LPVOID dest, int len)
1107 LPSTR filename, shortname;
1108 WIN32_FIND_DATAA * pfd;
1110 TRACE_(shell)("sf=%p pidl=%p 0x%04x %p 0x%04x stub\n",psf,pidl,nFormat,dest,len);
1112 pdump(pidl);
1113 if (!psf || !dest)
1114 return E_INVALIDARG;
1116 switch (nFormat)
1118 case SHGDFIL_FINDDATA:
1119 pfd = dest;
1121 if (_ILIsDrive(pidl) || _ILIsSpecialFolder(pidl))
1122 return E_INVALIDARG;
1124 if (len < sizeof(WIN32_FIND_DATAA))
1125 return E_INVALIDARG;
1127 ZeroMemory(pfd, sizeof (WIN32_FIND_DATAA));
1128 _ILGetFileDateTime( pidl, &(pfd->ftLastWriteTime));
1129 pfd->dwFileAttributes = _ILGetFileAttributes(pidl, NULL, 0);
1130 pfd->nFileSizeLow = _ILGetFileSize ( pidl, NULL, 0);
1132 filename = _ILGetTextPointer(pidl);
1133 shortname = _ILGetSTextPointer(pidl);
1135 if (filename)
1136 lstrcpynA(pfd->cFileName, filename, MAX_PATH);
1137 else
1138 pfd->cFileName[0] = '\0';
1140 if (shortname)
1141 lstrcpynA(pfd->cAlternateFileName, shortname, MAX_PATH);
1142 else
1143 pfd->cAlternateFileName[0] = '\0';
1144 return NOERROR;
1146 case SHGDFIL_NETRESOURCE:
1147 case SHGDFIL_DESCRIPTIONID:
1148 FIXME_(shell)("SHGDFIL %i stub\n", nFormat);
1149 break;
1151 default:
1152 ERR_(shell)("Unknown SHGDFIL %i, please report\n", nFormat);
1155 return E_INVALIDARG;
1158 /*************************************************************************
1159 * SHGetDataFromIDListW [SHELL32.248]
1162 HRESULT WINAPI SHGetDataFromIDListW(LPSHELLFOLDER psf, LPCITEMIDLIST pidl,
1163 int nFormat, LPVOID dest, int len)
1165 LPSTR filename, shortname;
1166 WIN32_FIND_DATAW * pfd = dest;
1168 TRACE_(shell)("sf=%p pidl=%p 0x%04x %p 0x%04x stub\n",psf,pidl,nFormat,dest,len);
1170 pdump(pidl);
1172 if (!psf || !dest)
1173 return E_INVALIDARG;
1175 switch (nFormat)
1177 case SHGDFIL_FINDDATA:
1178 pfd = dest;
1180 if (_ILIsDrive(pidl))
1181 return E_INVALIDARG;
1183 if (len < sizeof(WIN32_FIND_DATAW))
1184 return E_INVALIDARG;
1186 ZeroMemory(pfd, sizeof (WIN32_FIND_DATAA));
1187 _ILGetFileDateTime( pidl, &(pfd->ftLastWriteTime));
1188 pfd->dwFileAttributes = _ILGetFileAttributes(pidl, NULL, 0);
1189 pfd->nFileSizeLow = _ILGetFileSize ( pidl, NULL, 0);
1191 filename = _ILGetTextPointer(pidl);
1192 shortname = _ILGetSTextPointer(pidl);
1194 if (!filename)
1195 pfd->cFileName[0] = '\0';
1196 else if (!MultiByteToWideChar(CP_ACP, 0, filename, -1, pfd->cFileName, MAX_PATH))
1197 pfd->cFileName[MAX_PATH-1] = 0;
1199 if (!shortname)
1200 pfd->cAlternateFileName[0] = '\0';
1201 else if (!MultiByteToWideChar(CP_ACP, 0, shortname, -1, pfd->cAlternateFileName, 14))
1202 pfd->cAlternateFileName[13] = 0;
1203 return NOERROR;
1205 case SHGDFIL_NETRESOURCE:
1206 case SHGDFIL_DESCRIPTIONID:
1207 FIXME_(shell)("SHGDFIL %i stub\n", nFormat);
1208 break;
1210 default:
1211 ERR_(shell)("Unknown SHGDFIL %i, please report\n", nFormat);
1214 return E_INVALIDARG;
1217 /*************************************************************************
1218 * SHGetPathFromIDListA [SHELL32.@][NT 4.0: SHELL32.220]
1220 * PARAMETERS
1221 * pidl, [IN] pidl
1222 * pszPath [OUT] path
1224 * RETURNS
1225 * path from a passed PIDL.
1227 * NOTES
1228 * NULL returns FALSE
1229 * desktop pidl gives path to desktop directory back
1230 * special pidls returning FALSE
1232 BOOL WINAPI SHGetPathFromIDListA(LPCITEMIDLIST pidl, LPSTR pszPath)
1234 WCHAR wszPath[MAX_PATH];
1235 BOOL bSuccess;
1237 bSuccess = SHGetPathFromIDListW(pidl, wszPath);
1238 WideCharToMultiByte(CP_ACP, 0, wszPath, -1, pszPath, MAX_PATH, NULL, NULL);
1240 return bSuccess;
1243 /*************************************************************************
1244 * SHGetPathFromIDListW [SHELL32.@]
1246 * See SHGetPathFromIDListA.
1248 BOOL WINAPI SHGetPathFromIDListW(LPCITEMIDLIST pidl, LPWSTR pszPath)
1250 HRESULT hr;
1251 LPCITEMIDLIST pidlLast;
1252 LPSHELLFOLDER psfFolder;
1253 DWORD dwAttributes;
1254 STRRET strret;
1256 TRACE_(shell)("(pidl=%p,%p)\n", pidl, pszPath);
1257 pdump(pidl);
1259 *pszPath = '\0';
1260 if (!pidl)
1261 return FALSE;
1263 hr = SHBindToParent(pidl, &IID_IShellFolder, (VOID**)&psfFolder, &pidlLast);
1264 if (FAILED(hr)) return FALSE;
1266 dwAttributes = SFGAO_FILESYSTEM;
1267 hr = IShellFolder_GetAttributesOf(psfFolder, 1, &pidlLast, &dwAttributes);
1268 if (FAILED(hr) || !(dwAttributes & SFGAO_FILESYSTEM)) {
1269 IShellFolder_Release(psfFolder);
1270 return FALSE;
1273 hr = IShellFolder_GetDisplayNameOf(psfFolder, pidlLast, SHGDN_FORPARSING, &strret);
1274 IShellFolder_Release(psfFolder);
1275 if (FAILED(hr)) return FALSE;
1277 hr = StrRetToBufW(&strret, pidlLast, pszPath, MAX_PATH);
1279 TRACE_(shell)("-- %s, 0x%08x\n",debugstr_w(pszPath), hr);
1280 return SUCCEEDED(hr);
1283 /*************************************************************************
1284 * SHBindToParent [shell version 5.0]
1286 HRESULT WINAPI SHBindToParent(LPCITEMIDLIST pidl, REFIID riid, LPVOID *ppv, LPCITEMIDLIST *ppidlLast)
1288 IShellFolder * psfDesktop;
1289 HRESULT hr=E_FAIL;
1291 TRACE_(shell)("pidl=%p\n", pidl);
1292 pdump(pidl);
1294 if (!pidl || !ppv)
1295 return E_INVALIDARG;
1297 *ppv = NULL;
1298 if (ppidlLast)
1299 *ppidlLast = NULL;
1301 hr = SHGetDesktopFolder(&psfDesktop);
1302 if (FAILED(hr))
1303 return hr;
1305 if (_ILIsPidlSimple(pidl))
1307 /* we are on desktop level */
1308 hr = IShellFolder_QueryInterface(psfDesktop, riid, ppv);
1310 else
1312 LPITEMIDLIST pidlParent = ILClone(pidl);
1313 ILRemoveLastID(pidlParent);
1314 hr = IShellFolder_BindToObject(psfDesktop, pidlParent, NULL, riid, ppv);
1315 SHFree (pidlParent);
1318 IShellFolder_Release(psfDesktop);
1320 if (SUCCEEDED(hr) && ppidlLast)
1321 *ppidlLast = ILFindLastID(pidl);
1323 TRACE_(shell)("-- psf=%p pidl=%p ret=0x%08x\n", *ppv, (ppidlLast)?*ppidlLast:NULL, hr);
1324 return hr;
1327 /**************************************************************************
1329 * internal functions
1331 * ### 1. section creating pidls ###
1333 *************************************************************************
1335 LPITEMIDLIST _ILAlloc(PIDLTYPE type, unsigned int size)
1337 LPITEMIDLIST pidlOut = NULL;
1339 pidlOut = SHAlloc(size + 5);
1340 if(pidlOut)
1342 LPPIDLDATA pData;
1343 LPITEMIDLIST pidlNext;
1345 ZeroMemory(pidlOut, size + 5);
1346 pidlOut->mkid.cb = size + 3;
1348 pData = _ILGetDataPointer(pidlOut);
1349 if (pData)
1350 pData->type = type;
1352 pidlNext = ILGetNext(pidlOut);
1353 if (pidlNext)
1354 pidlNext->mkid.cb = 0x00;
1355 TRACE("-- (pidl=%p, size=%u)\n", pidlOut, size);
1358 return pidlOut;
1361 LPITEMIDLIST _ILCreateDesktop()
1363 LPITEMIDLIST ret;
1365 TRACE("()\n");
1366 ret = SHAlloc(2);
1367 if (ret)
1368 ret->mkid.cb = 0;
1369 return ret;
1372 LPITEMIDLIST _ILCreateMyComputer()
1374 TRACE("()\n");
1375 return _ILCreateGuid(PT_GUID, &CLSID_MyComputer);
1378 LPITEMIDLIST _ILCreateMyDocuments()
1380 TRACE("()\n");
1381 return _ILCreateGuid(PT_GUID, &CLSID_MyDocuments);
1384 LPITEMIDLIST _ILCreateIExplore()
1386 TRACE("()\n");
1387 return _ILCreateGuid(PT_GUID, &CLSID_Internet);
1390 LPITEMIDLIST _ILCreateControlPanel()
1392 LPITEMIDLIST parent = _ILCreateGuid(PT_GUID, &CLSID_MyComputer), ret = NULL;
1394 TRACE("()\n");
1395 if (parent)
1397 LPITEMIDLIST cpl = _ILCreateGuid(PT_SHELLEXT, &CLSID_ControlPanel);
1399 if (cpl)
1401 ret = ILCombine(parent, cpl);
1402 SHFree(cpl);
1404 SHFree(parent);
1406 return ret;
1409 LPITEMIDLIST _ILCreatePrinters()
1411 LPITEMIDLIST parent = _ILCreateGuid(PT_GUID, &CLSID_MyComputer), ret = NULL;
1413 TRACE("()\n");
1414 if (parent)
1416 LPITEMIDLIST printers = _ILCreateGuid(PT_YAGUID, &CLSID_Printers);
1418 if (printers)
1420 ret = ILCombine(parent, printers);
1421 SHFree(printers);
1423 SHFree(parent);
1425 return ret;
1428 LPITEMIDLIST _ILCreateNetwork()
1430 TRACE("()\n");
1431 return _ILCreateGuid(PT_GUID, &CLSID_NetworkPlaces);
1434 LPITEMIDLIST _ILCreateBitBucket()
1436 TRACE("()\n");
1437 return _ILCreateGuid(PT_GUID, &CLSID_RecycleBin);
1440 LPITEMIDLIST _ILCreateGuid(PIDLTYPE type, REFIID guid)
1442 LPITEMIDLIST pidlOut;
1444 if (type == PT_SHELLEXT || type == PT_GUID || type == PT_YAGUID)
1446 pidlOut = _ILAlloc(type, sizeof(GUIDStruct));
1447 if (pidlOut)
1449 LPPIDLDATA pData = _ILGetDataPointer(pidlOut);
1451 memcpy(&(pData->u.guid.guid), guid, sizeof(GUID));
1452 TRACE("-- create GUID-pidl %s\n",
1453 debugstr_guid(&(pData->u.guid.guid)));
1456 else
1458 WARN("%d: invalid type for GUID\n", type);
1459 pidlOut = NULL;
1461 return pidlOut;
1464 LPITEMIDLIST _ILCreateGuidFromStrA(LPCSTR szGUID)
1466 IID iid;
1468 if (!SUCCEEDED(SHCLSIDFromStringA(szGUID, &iid)))
1470 ERR("%s is not a GUID\n", szGUID);
1471 return NULL;
1473 return _ILCreateGuid(PT_GUID, &iid);
1476 LPITEMIDLIST _ILCreateGuidFromStrW(LPCWSTR szGUID)
1478 IID iid;
1480 if (!SUCCEEDED(SHCLSIDFromStringW(szGUID, &iid)))
1482 ERR("%s is not a GUID\n", debugstr_w(szGUID));
1483 return NULL;
1485 return _ILCreateGuid(PT_GUID, &iid);
1488 LPITEMIDLIST _ILCreateFromFindDataW( WIN32_FIND_DATAW *wfd )
1490 /* FIXME: should make unicode PIDLs */
1491 WIN32_FIND_DATAA fda;
1493 memset( &fda, 0, sizeof fda );
1494 fda.dwFileAttributes = wfd->dwFileAttributes;
1495 fda.ftCreationTime = wfd->ftCreationTime;
1496 fda.ftLastAccessTime = wfd->ftLastAccessTime;
1497 fda.ftLastWriteTime = wfd->ftLastWriteTime;
1498 fda.nFileSizeHigh = wfd->nFileSizeHigh;
1499 fda.nFileSizeLow = wfd->nFileSizeLow;
1500 fda.dwReserved0 = wfd->dwReserved0;
1501 fda.dwReserved1 = wfd->dwReserved1;
1502 WideCharToMultiByte( CP_ACP, 0, wfd->cFileName, -1,
1503 fda.cFileName, MAX_PATH, NULL, NULL );
1504 return _ILCreateFromFindDataA( &fda );
1507 LPITEMIDLIST _ILCreateFromFindDataA(WIN32_FIND_DATAA * stffile )
1509 char buff[MAX_PATH + 14 +1]; /* see WIN32_FIND_DATA */
1510 char * pbuff = buff;
1511 size_t len, len1;
1512 LPITEMIDLIST pidl;
1513 PIDLTYPE type;
1515 if (!stffile)
1516 return NULL;
1518 TRACE("(%s, %s)\n",stffile->cAlternateFileName, stffile->cFileName);
1520 /* prepare buffer with both names */
1521 len = strlen (stffile->cFileName) + 1;
1522 memcpy (pbuff, stffile->cFileName, len);
1523 pbuff += len;
1525 len1 = strlen (stffile->cAlternateFileName)+1;
1526 memcpy (pbuff, stffile->cAlternateFileName, len1);
1528 type = (stffile->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? PT_FOLDER : PT_VALUE;
1531 * FileStruct already has one byte for the first name, so use len - 1 in
1532 * size calculation
1534 pidl = _ILAlloc(type, sizeof(FileStruct) + (len - 1) + len1);
1535 if (pidl)
1537 LPPIDLDATA pData;
1538 LPSTR pszDest;
1540 /* set attributes */
1541 pData = _ILGetDataPointer(pidl);
1542 if (pData)
1544 pData->type = type;
1545 FileTimeToDosDateTime( &(stffile->ftLastWriteTime),
1546 &pData->u.file.uFileDate, &pData->u.file.uFileTime);
1547 pData->u.file.dwFileSize = stffile->nFileSizeLow;
1548 pData->u.file.uFileAttribs = (WORD)stffile->dwFileAttributes;
1550 pszDest = _ILGetTextPointer(pidl);
1551 if (pszDest)
1553 memcpy(pszDest, buff, len + len1);
1554 TRACE("-- create Value: %s\n",debugstr_a(pszDest));
1557 return pidl;
1560 HRESULT _ILCreateFromPathA(LPCSTR szPath, LPITEMIDLIST* ppidl)
1562 HANDLE hFile;
1563 WIN32_FIND_DATAA stffile;
1565 if (!ppidl)
1566 return E_INVALIDARG;
1568 hFile = FindFirstFileA(szPath, &stffile);
1569 if (hFile == INVALID_HANDLE_VALUE)
1570 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
1572 FindClose(hFile);
1574 *ppidl = _ILCreateFromFindDataA(&stffile);
1576 return *ppidl ? S_OK : E_OUTOFMEMORY;
1579 HRESULT _ILCreateFromPathW(LPCWSTR szPath, LPITEMIDLIST* ppidl)
1581 HANDLE hFile;
1582 WIN32_FIND_DATAW stffile;
1584 if (!ppidl)
1585 return E_INVALIDARG;
1587 hFile = FindFirstFileW(szPath, &stffile);
1588 if (hFile == INVALID_HANDLE_VALUE)
1589 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
1591 FindClose(hFile);
1593 *ppidl = _ILCreateFromFindDataW(&stffile);
1595 return *ppidl ? S_OK : E_OUTOFMEMORY;
1598 LPITEMIDLIST _ILCreateDrive(LPCWSTR lpszNew)
1600 LPITEMIDLIST pidlOut;
1602 TRACE("(%s)\n",debugstr_w(lpszNew));
1604 pidlOut = _ILAlloc(PT_DRIVE, sizeof(DriveStruct));
1605 if (pidlOut)
1607 LPSTR pszDest;
1609 pszDest = _ILGetTextPointer(pidlOut);
1610 if (pszDest)
1612 strcpy(pszDest, "x:\\");
1613 pszDest[0]=toupperW(lpszNew[0]);
1614 TRACE("-- create Drive: %s\n", debugstr_a(pszDest));
1617 return pidlOut;
1620 /**************************************************************************
1621 * _ILGetDrive()
1623 * Gets the text for the drive eg. 'c:\'
1625 * RETURNS
1626 * strlen (lpszText)
1628 DWORD _ILGetDrive(LPCITEMIDLIST pidl,LPSTR pOut, UINT uSize)
1630 TRACE("(%p,%p,%u)\n",pidl,pOut,uSize);
1632 if(_ILIsMyComputer(pidl))
1633 pidl = ILGetNext(pidl);
1635 if (pidl && _ILIsDrive(pidl))
1636 return _ILSimpleGetText(pidl, pOut, uSize);
1638 return 0;
1641 /**************************************************************************
1643 * ### 2. section testing pidls ###
1645 **************************************************************************
1646 * _ILIsDesktop()
1647 * _ILIsMyComputer()
1648 * _ILIsSpecialFolder()
1649 * _ILIsDrive()
1650 * _ILIsFolder()
1651 * _ILIsValue()
1652 * _ILIsPidlSimple()
1654 BOOL _ILIsDesktop(LPCITEMIDLIST pidl)
1656 TRACE("(%p)\n",pidl);
1658 return pidl && pidl->mkid.cb ? 0 : 1;
1661 BOOL _ILIsMyComputer(LPCITEMIDLIST pidl)
1663 REFIID iid = _ILGetGUIDPointer(pidl);
1665 TRACE("(%p)\n",pidl);
1667 if (iid)
1668 return IsEqualIID(iid, &CLSID_MyComputer);
1669 return FALSE;
1672 BOOL _ILIsSpecialFolder (LPCITEMIDLIST pidl)
1674 LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1676 TRACE("(%p)\n",pidl);
1678 return (pidl && ( (lpPData && (PT_GUID== lpPData->type || PT_SHELLEXT== lpPData->type || PT_YAGUID == lpPData->type)) ||
1679 (pidl && pidl->mkid.cb == 0x00)
1683 BOOL _ILIsDrive(LPCITEMIDLIST pidl)
1685 LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1687 TRACE("(%p)\n",pidl);
1689 return (pidl && lpPData && (PT_DRIVE == lpPData->type ||
1690 PT_DRIVE1 == lpPData->type ||
1691 PT_DRIVE2 == lpPData->type ||
1692 PT_DRIVE3 == lpPData->type));
1695 BOOL _ILIsFolder(LPCITEMIDLIST pidl)
1697 LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1699 TRACE("(%p)\n",pidl);
1701 return (pidl && lpPData && (PT_FOLDER == lpPData->type || PT_FOLDER1 == lpPData->type));
1704 BOOL _ILIsValue(LPCITEMIDLIST pidl)
1706 LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1708 TRACE("(%p)\n",pidl);
1710 return (pidl && lpPData && PT_VALUE == lpPData->type);
1713 BOOL _ILIsCPanelStruct(LPCITEMIDLIST pidl)
1715 LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1717 TRACE("(%p)\n",pidl);
1719 return (pidl && lpPData && (lpPData->type == 0));
1722 /**************************************************************************
1723 * _ILIsPidlSimple
1725 BOOL _ILIsPidlSimple(LPCITEMIDLIST pidl)
1727 BOOL ret = TRUE;
1729 if(! _ILIsDesktop(pidl)) /* pidl=NULL or mkid.cb=0 */
1731 WORD len = pidl->mkid.cb;
1732 LPCITEMIDLIST pidlnext = (LPCITEMIDLIST) (((const BYTE*)pidl) + len );
1734 if (pidlnext->mkid.cb)
1735 ret = FALSE;
1738 TRACE("%s\n", ret ? "Yes" : "No");
1739 return ret;
1742 /**************************************************************************
1744 * ### 3. section getting values from pidls ###
1747 /**************************************************************************
1748 * _ILSimpleGetText
1750 * gets the text for the first item in the pidl (eg. simple pidl)
1752 * returns the length of the string
1754 DWORD _ILSimpleGetText (LPCITEMIDLIST pidl, LPSTR szOut, UINT uOutSize)
1756 DWORD dwReturn=0;
1757 LPSTR szSrc;
1758 GUID const * riid;
1759 char szTemp[MAX_PATH];
1761 TRACE("(%p %p %x)\n",pidl,szOut,uOutSize);
1763 if (!pidl)
1764 return 0;
1766 if (szOut)
1767 *szOut = 0;
1769 if (_ILIsDesktop(pidl))
1771 /* desktop */
1772 if (HCR_GetClassNameA(&CLSID_ShellDesktop, szTemp, MAX_PATH))
1774 if (szOut)
1775 lstrcpynA(szOut, szTemp, uOutSize);
1777 dwReturn = strlen (szTemp);
1780 else if (( szSrc = _ILGetTextPointer(pidl) ))
1782 /* filesystem */
1783 if (szOut)
1784 lstrcpynA(szOut, szSrc, uOutSize);
1786 dwReturn = strlen(szSrc);
1788 else if (( riid = _ILGetGUIDPointer(pidl) ))
1790 /* special folder */
1791 if ( HCR_GetClassNameA(riid, szTemp, MAX_PATH) )
1793 if (szOut)
1794 lstrcpynA(szOut, szTemp, uOutSize);
1796 dwReturn = strlen (szTemp);
1799 else
1801 ERR("-- no text\n");
1804 TRACE("-- (%p=%s 0x%08x)\n",szOut,debugstr_a(szOut),dwReturn);
1805 return dwReturn;
1808 /**************************************************************************
1809 * _ILSimpleGetTextW
1811 * gets the text for the first item in the pidl (eg. simple pidl)
1813 * returns the length of the string
1815 DWORD _ILSimpleGetTextW (LPCITEMIDLIST pidl, LPWSTR szOut, UINT uOutSize)
1817 DWORD dwReturn;
1818 char szTemp[MAX_PATH];
1819 FileStructW *pFileStructW = _ILGetFileStructW(pidl);
1821 TRACE("(%p %p %x)\n",pidl,szOut,uOutSize);
1823 if (pFileStructW) {
1824 lstrcpynW(szOut, pFileStructW->wszName, uOutSize);
1825 dwReturn = lstrlenW(pFileStructW->wszName);
1826 } else {
1827 dwReturn = _ILSimpleGetText(pidl, szTemp, MAX_PATH);
1829 if (!MultiByteToWideChar(CP_ACP, 0, szTemp, -1, szOut, uOutSize))
1830 *szOut = 0;
1833 TRACE("-- (%p=%s 0x%08x)\n",szOut,debugstr_w(szOut),dwReturn);
1834 return dwReturn;
1837 /**************************************************************************
1839 * ### 4. getting pointers to parts of pidls ###
1841 **************************************************************************
1842 * _ILGetDataPointer()
1844 LPPIDLDATA _ILGetDataPointer(LPCITEMIDLIST pidl)
1846 if(pidl && pidl->mkid.cb != 0x00)
1847 return (LPPIDLDATA) &(pidl->mkid.abID);
1848 return NULL;
1851 /**************************************************************************
1852 * _ILGetTextPointer()
1853 * gets a pointer to the long filename string stored in the pidl
1855 LPSTR _ILGetTextPointer(LPCITEMIDLIST pidl)
1857 /* TRACE(pidl,"(pidl%p)\n", pidl);*/
1859 LPPIDLDATA pdata = _ILGetDataPointer(pidl);
1861 if (!pdata)
1862 return NULL;
1864 switch (pdata->type)
1866 case PT_GUID:
1867 case PT_SHELLEXT:
1868 case PT_YAGUID:
1869 return NULL;
1871 case PT_DRIVE:
1872 case PT_DRIVE1:
1873 case PT_DRIVE2:
1874 case PT_DRIVE3:
1875 return (LPSTR)&(pdata->u.drive.szDriveName);
1877 case PT_FOLDER:
1878 case PT_FOLDER1:
1879 case PT_VALUE:
1880 case PT_IESPECIAL1:
1881 case PT_IESPECIAL2:
1882 return (LPSTR)&(pdata->u.file.szNames);
1884 case PT_WORKGRP:
1885 case PT_COMP:
1886 case PT_NETWORK:
1887 case PT_NETPROVIDER:
1888 case PT_SHARE:
1889 return (LPSTR)&(pdata->u.network.szNames);
1891 return NULL;
1894 /**************************************************************************
1895 * _ILGetSTextPointer()
1896 * gets a pointer to the short filename string stored in the pidl
1898 LPSTR _ILGetSTextPointer(LPCITEMIDLIST pidl)
1900 /* TRACE(pidl,"(pidl%p)\n", pidl); */
1902 LPPIDLDATA pdata =_ILGetDataPointer(pidl);
1904 if (!pdata)
1905 return NULL;
1907 switch (pdata->type)
1909 case PT_FOLDER:
1910 case PT_VALUE:
1911 case PT_IESPECIAL1:
1912 case PT_IESPECIAL2:
1913 return (LPSTR)(pdata->u.file.szNames + strlen (pdata->u.file.szNames) + 1);
1915 case PT_WORKGRP:
1916 return (LPSTR)(pdata->u.network.szNames + strlen (pdata->u.network.szNames) + 1);
1918 return NULL;
1921 /**************************************************************************
1922 * _ILGetGUIDPointer()
1924 * returns reference to guid stored in some pidls
1926 IID* _ILGetGUIDPointer(LPCITEMIDLIST pidl)
1928 LPPIDLDATA pdata =_ILGetDataPointer(pidl);
1930 TRACE("%p\n", pidl);
1932 if (!pdata)
1933 return NULL;
1935 TRACE("pdata->type 0x%04x\n", pdata->type);
1936 switch (pdata->type)
1938 case PT_SHELLEXT:
1939 case PT_GUID:
1940 case PT_YAGUID:
1941 return &(pdata->u.guid.guid);
1943 default:
1944 TRACE("Unknown pidl type 0x%04x\n", pdata->type);
1945 break;
1947 return NULL;
1950 /******************************************************************************
1951 * _ILGetFileStructW [Internal]
1953 * Get pointer the a SHITEMID's FileStructW field if present
1955 * PARAMS
1956 * pidl [I] The SHITEMID
1958 * RETURNS
1959 * Success: Pointer to pidl's FileStructW field.
1960 * Failure: NULL
1962 FileStructW* _ILGetFileStructW(LPCITEMIDLIST pidl) {
1963 FileStructW *pFileStructW;
1964 WORD cbOffset;
1966 if (!(_ILIsValue(pidl) || _ILIsFolder(pidl)))
1967 return NULL;
1969 cbOffset = *(const WORD *)((const BYTE *)pidl + pidl->mkid.cb - sizeof(WORD));
1970 pFileStructW = (FileStructW*)((LPBYTE)pidl + cbOffset);
1972 /* Currently I don't see a fool prove way to figure out if a pidl is for sure of WinXP
1973 * style with a FileStructW member. If we switch all our shellfolder-implementations to
1974 * the new format, this won't be a problem. For now, we do as many sanity checks as possible. */
1975 if (cbOffset & 0x1 || /* FileStructW member is word aligned in the pidl */
1976 /* FileStructW is positioned after FileStruct */
1977 cbOffset < sizeof(pidl->mkid.cb) + sizeof(PIDLTYPE) + sizeof(FileStruct) ||
1978 /* There has to be enough space at cbOffset in the pidl to hold FileStructW and cbOffset */
1979 cbOffset > pidl->mkid.cb - sizeof(cbOffset) - sizeof(FileStructW) ||
1980 pidl->mkid.cb != cbOffset + pFileStructW->cbLen)
1982 WARN("Invalid pidl format (cbOffset = %d)!\n", cbOffset);
1983 return NULL;
1986 return pFileStructW;
1989 /*************************************************************************
1990 * _ILGetFileDateTime
1992 * Given the ItemIdList, get the FileTime
1994 * PARAMS
1995 * pidl [I] The ItemIDList
1996 * pFt [I] the resulted FILETIME of the file
1998 * RETURNS
1999 * True if Successful
2001 * NOTES
2004 BOOL _ILGetFileDateTime(LPCITEMIDLIST pidl, FILETIME *pFt)
2006 LPPIDLDATA pdata = _ILGetDataPointer(pidl);
2008 if (!pdata)
2009 return FALSE;
2011 switch (pdata->type)
2013 case PT_FOLDER:
2014 case PT_VALUE:
2015 DosDateTimeToFileTime(pdata->u.file.uFileDate, pdata->u.file.uFileTime, pFt);
2016 break;
2017 default:
2018 return FALSE;
2020 return TRUE;
2023 BOOL _ILGetFileDate (LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2025 FILETIME ft,lft;
2026 SYSTEMTIME time;
2027 BOOL ret;
2029 if (_ILGetFileDateTime( pidl, &ft ))
2031 FileTimeToLocalFileTime(&ft, &lft);
2032 FileTimeToSystemTime (&lft, &time);
2034 ret = GetDateFormatA(LOCALE_USER_DEFAULT,DATE_SHORTDATE,&time, NULL, pOut, uOutSize);
2035 if (ret)
2037 /* Append space + time without seconds */
2038 pOut[ret-1] = ' ';
2039 GetTimeFormatA(LOCALE_USER_DEFAULT, TIME_NOSECONDS, &time, NULL, &pOut[ret], uOutSize - ret);
2042 else
2044 pOut[0] = '\0';
2045 ret = FALSE;
2047 return ret;
2050 /*************************************************************************
2051 * _ILGetFileSize
2053 * Given the ItemIdList, get the FileSize
2055 * PARAMS
2056 * pidl [I] The ItemIDList
2057 * pOut [I] The buffer to save the result
2058 * uOutsize [I] The size of the buffer
2060 * RETURNS
2061 * The FileSize
2063 * NOTES
2064 * pOut can be null when no string is needed
2067 DWORD _ILGetFileSize (LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2069 LPPIDLDATA pdata = _ILGetDataPointer(pidl);
2070 DWORD dwSize;
2072 if (!pdata)
2073 return 0;
2075 switch (pdata->type)
2077 case PT_VALUE:
2078 dwSize = pdata->u.file.dwFileSize;
2079 if (pOut)
2080 StrFormatKBSizeA(dwSize, pOut, uOutSize);
2081 return dwSize;
2083 if (pOut)
2084 *pOut = 0x00;
2085 return 0;
2088 BOOL _ILGetExtension (LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2090 char szTemp[MAX_PATH];
2091 const char * pPoint;
2092 LPCITEMIDLIST pidlTemp=pidl;
2094 TRACE("pidl=%p\n",pidl);
2096 if (!pidl)
2097 return FALSE;
2099 pidlTemp = ILFindLastID(pidl);
2101 if (!_ILIsValue(pidlTemp))
2102 return FALSE;
2103 if (!_ILSimpleGetText(pidlTemp, szTemp, MAX_PATH))
2104 return FALSE;
2106 pPoint = PathFindExtensionA(szTemp);
2108 if (!*pPoint)
2109 return FALSE;
2111 pPoint++;
2112 lstrcpynA(pOut, pPoint, uOutSize);
2113 TRACE("%s\n",pOut);
2115 return TRUE;
2118 /*************************************************************************
2119 * _ILGetFileType
2121 * Given the ItemIdList, get the file type description
2123 * PARAMS
2124 * pidl [I] The ItemIDList (simple)
2125 * pOut [I] The buffer to save the result
2126 * uOutsize [I] The size of the buffer
2128 * RETURNS
2129 * nothing
2131 * NOTES
2132 * This function copies as much as possible into the buffer.
2134 void _ILGetFileType(LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2136 if(_ILIsValue(pidl))
2138 char sTemp[64];
2140 if(uOutSize > 0)
2141 pOut[0] = 0;
2142 if (_ILGetExtension (pidl, sTemp, 64))
2144 if (!( HCR_MapTypeToValueA(sTemp, sTemp, 64, TRUE)
2145 && HCR_MapTypeToValueA(sTemp, pOut, uOutSize, FALSE )))
2147 lstrcpynA (pOut, sTemp, uOutSize - 6);
2148 strcat (pOut, "-file");
2152 else
2153 lstrcpynA(pOut, "Folder", uOutSize);
2156 /*************************************************************************
2157 * _ILGetFileAttributes
2159 * Given the ItemIdList, get the Attrib string format
2161 * PARAMS
2162 * pidl [I] The ItemIDList
2163 * pOut [I] The buffer to save the result
2164 * uOutsize [I] The size of the Buffer
2166 * RETURNS
2167 * Attributes
2169 * FIXME
2170 * return value 0 in case of error is a valid return value
2173 DWORD _ILGetFileAttributes(LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2175 LPPIDLDATA pData = _ILGetDataPointer(pidl);
2176 WORD wAttrib = 0;
2177 int i;
2179 if (!pData)
2180 return 0;
2182 switch(pData->type)
2184 case PT_FOLDER:
2185 case PT_VALUE:
2186 wAttrib = pData->u.file.uFileAttribs;
2187 break;
2190 if(uOutSize >= 6)
2192 i=0;
2193 if(wAttrib & FILE_ATTRIBUTE_READONLY)
2194 pOut[i++] = 'R';
2195 if(wAttrib & FILE_ATTRIBUTE_HIDDEN)
2196 pOut[i++] = 'H';
2197 if(wAttrib & FILE_ATTRIBUTE_SYSTEM)
2198 pOut[i++] = 'S';
2199 if(wAttrib & FILE_ATTRIBUTE_ARCHIVE)
2200 pOut[i++] = 'A';
2201 if(wAttrib & FILE_ATTRIBUTE_COMPRESSED)
2202 pOut[i++] = 'C';
2203 pOut[i] = 0x00;
2205 return wAttrib;
2208 /*************************************************************************
2209 * ILFreeaPidl
2211 * free a aPidl struct
2213 void _ILFreeaPidl(LPITEMIDLIST * apidl, UINT cidl)
2215 UINT i;
2217 if (apidl)
2219 for (i = 0; i < cidl; i++)
2220 SHFree(apidl[i]);
2221 SHFree(apidl);
2225 /*************************************************************************
2226 * ILCopyaPidl
2228 * copies an aPidl struct
2230 LPITEMIDLIST* _ILCopyaPidl(LPCITEMIDLIST * apidlsrc, UINT cidl)
2232 UINT i;
2233 LPITEMIDLIST *apidldest;
2235 apidldest = SHAlloc(cidl * sizeof(LPITEMIDLIST));
2236 if (!apidlsrc)
2237 return NULL;
2239 for (i = 0; i < cidl; i++)
2240 apidldest[i] = ILClone(apidlsrc[i]);
2242 return apidldest;
2245 /*************************************************************************
2246 * _ILCopyCidaToaPidl
2248 * creates aPidl from CIDA
2250 LPITEMIDLIST* _ILCopyCidaToaPidl(LPITEMIDLIST* pidl, LPIDA cida)
2252 UINT i;
2253 LPITEMIDLIST *dst;
2255 dst = SHAlloc(cida->cidl * sizeof(LPITEMIDLIST));
2256 if (!dst)
2257 return NULL;
2259 if (pidl)
2260 *pidl = ILClone((LPITEMIDLIST)(&((LPBYTE)cida)[cida->aoffset[0]]));
2262 for (i = 0; i < cida->cidl; i++)
2263 dst[i] = ILClone((LPITEMIDLIST)(&((LPBYTE)cida)[cida->aoffset[i + 1]]));
2265 return dst;