Do not allow creation of not aligned EMF records by GDI code.
[wine.git] / dlls / shell32 / pidl.c
blob3b366c0c96ee5a0a890743fa958e65dfc57f83b4
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 NONAMELESSUNION
34 #define NONAMELESSSTRUCT
35 #include "windef.h"
36 #include "winbase.h"
37 #include "winreg.h"
38 #include "objbase.h"
39 #include "shlguid.h"
40 #include "winerror.h"
41 #include "winnls.h"
42 #include "undocshell.h"
43 #include "shell32_main.h"
44 #include "shellapi.h"
45 #include "shlwapi.h"
47 #include "pidl.h"
48 #include "debughlp.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(pidl);
52 WINE_DECLARE_DEBUG_CHANNEL(shell);
54 /* from comctl32.dll */
55 extern LPVOID WINAPI Alloc(INT);
56 extern BOOL WINAPI Free(LPVOID);
58 /*************************************************************************
59 * ILGetDisplayNameEx [SHELL32.186]
61 * Retrieves the display name of an ItemIDList
63 * PARAMS
64 * psf [I] Shell Folder to start with, if NULL the desktop is used
65 * pidl [I] ItemIDList relativ to the psf to get the display name for
66 * path [O] Filled in with the display name, assumed to be at least MAX_PATH long
67 * type [I] Type of display name to retrieve
68 * 0 = SHGDN_FORPARSING | SHGDN_FORADDRESSBAR uses always the desktop as root
69 * 1 = SHGDN_NORMAL relative to the root folder
70 * 2 = SHGDN_INFOLDER relative to the root folder, only the last name
72 * RETURNS
73 * True if the display name could be retrieved successfully, False otherwise
75 BOOL WINAPI ILGetDisplayNameExA(LPSHELLFOLDER psf, LPCITEMIDLIST pidl, LPSTR path, DWORD type)
77 BOOL ret = FALSE;
78 WCHAR wPath[MAX_PATH];
80 TRACE("%p %p %p %ld\n", psf, pidl, path, type);
82 if (!pidl || !path)
83 return FALSE;
85 ret = ILGetDisplayNameExW(psf, pidl, wPath, type);
86 WideCharToMultiByte(CP_ACP, 0, wPath, -1, path, MAX_PATH, NULL, NULL);
87 TRACE("%p %p %s\n", psf, pidl, debugstr_a(path));
89 return ret;
92 BOOL WINAPI ILGetDisplayNameExW(LPSHELLFOLDER psf, LPCITEMIDLIST pidl, LPWSTR path, DWORD type)
94 LPSHELLFOLDER psfParent, lsf = psf;
95 HRESULT ret = NO_ERROR;
96 LPCITEMIDLIST pidllast;
97 STRRET strret;
98 DWORD flag;
100 TRACE("%p %p %p %ld\n", psf, pidl, path, type);
102 if (!pidl || !path)
103 return FALSE;
105 if (!lsf)
107 ret = SHGetDesktopFolder(&lsf);
108 if (FAILED(ret))
109 return FALSE;
112 if (type >= 0 && type <= 2)
114 switch (type)
116 case ILGDN_FORPARSING:
117 flag = SHGDN_FORPARSING | SHGDN_FORADDRESSBAR;
118 break;
119 case ILGDN_NORMAL:
120 flag = SHGDN_NORMAL;
121 break;
122 case ILGDN_INFOLDER:
123 flag = SHGDN_INFOLDER;
124 break;
125 default:
126 FIXME("Unknown type parameter = %lx\n", type);
127 flag = SHGDN_FORPARSING | SHGDN_FORADDRESSBAR;
128 break;
130 if (!*(LPWORD)pidl || type == ILGDN_FORPARSING)
132 ret = IShellFolder_GetDisplayNameOf(lsf, pidl, flag, &strret);
133 if (SUCCEEDED(ret))
135 ret = StrRetToStrNW(path, MAX_PATH, &strret, pidl);
138 else
140 ret = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psfParent, &pidllast);
141 if (SUCCEEDED(ret))
143 ret = IShellFolder_GetDisplayNameOf(psfParent, pidllast, flag, &strret);
144 if (SUCCEEDED(ret))
146 ret = StrRetToStrNW(path, MAX_PATH, &strret, pidllast);
148 IShellFolder_Release(psfParent);
153 TRACE("%p %p %s\n", psf, pidl, debugstr_w(path));
155 if (!psf)
156 IShellFolder_Release(lsf);
157 return SUCCEEDED(ret);
160 BOOL WINAPI ILGetDisplayNameEx(LPSHELLFOLDER psf, LPCITEMIDLIST pidl, LPVOID path, DWORD type)
162 TRACE_(shell)("%p %p %p %ld\n", psf, pidl, path, type);
163 if (SHELL_OsIsUnicode())
164 return ILGetDisplayNameExW(psf, pidl, path, type);
165 return ILGetDisplayNameExA(psf, pidl, path, type);
168 /*************************************************************************
169 * ILGetDisplayName [SHELL32.15]
171 BOOL WINAPI ILGetDisplayName(LPCITEMIDLIST pidl, LPVOID path)
173 TRACE_(shell)("%p %p\n", pidl, path);
174 if (SHELL_OsIsUnicode())
175 return ILGetDisplayNameExW(NULL, pidl, path, ILGDN_FORPARSING);
176 return ILGetDisplayNameExA(NULL, pidl, path, ILGDN_FORPARSING);
179 /*************************************************************************
180 * ILFindLastID [SHELL32.16]
182 * NOTES
183 * observed: pidl=Desktop return=pidl
185 LPITEMIDLIST WINAPI ILFindLastID(LPCITEMIDLIST pidl)
187 LPCITEMIDLIST pidlLast = pidl;
189 TRACE("(pidl=%p)\n",pidl);
191 if (!pidl)
192 return NULL;
194 while (pidl->mkid.cb)
196 pidlLast = pidl;
197 pidl = ILGetNext(pidl);
199 return (LPITEMIDLIST)pidlLast;
201 /*************************************************************************
202 * ILRemoveLastID [SHELL32.17]
204 * NOTES
205 * when pidl=Desktop return=FALSE
207 BOOL WINAPI ILRemoveLastID(LPCITEMIDLIST pidl)
209 TRACE_(shell)("pidl=%p\n",pidl);
211 if (!pidl || !pidl->mkid.cb)
212 return 0;
213 ILFindLastID(pidl)->mkid.cb = 0;
214 return 1;
217 /*************************************************************************
218 * ILClone [SHELL32.18]
220 * NOTES
221 * duplicate an idlist
223 LPITEMIDLIST WINAPI ILClone (LPCITEMIDLIST pidl)
224 { DWORD len;
225 LPITEMIDLIST newpidl;
227 if (!pidl)
228 return NULL;
230 len = ILGetSize(pidl);
231 newpidl = (LPITEMIDLIST)SHAlloc(len);
232 if (newpidl)
233 memcpy(newpidl,pidl,len);
235 TRACE("pidl=%p newpidl=%p\n",pidl, newpidl);
236 pdump(pidl);
238 return newpidl;
240 /*************************************************************************
241 * ILCloneFirst [SHELL32.19]
243 * NOTES
244 * duplicates the first idlist of a complex pidl
246 LPITEMIDLIST WINAPI ILCloneFirst(LPCITEMIDLIST pidl)
247 { DWORD len;
248 LPITEMIDLIST pidlNew = NULL;
250 TRACE("pidl=%p \n",pidl);
251 pdump(pidl);
253 if (pidl)
255 len = pidl->mkid.cb;
256 pidlNew = (LPITEMIDLIST) SHAlloc (len+2);
257 if (pidlNew)
259 memcpy(pidlNew,pidl,len+2); /* 2 -> mind a desktop pidl */
261 if (len)
262 ILGetNext(pidlNew)->mkid.cb = 0x00;
265 TRACE("-- newpidl=%p\n",pidlNew);
267 return pidlNew;
270 /*************************************************************************
271 * ILLoadFromStream (SHELL32.26)
273 * NOTES
274 * the first two bytes are the len, the pidl is following then
276 HRESULT WINAPI ILLoadFromStream (IStream * pStream, LPITEMIDLIST * ppPidl)
277 { WORD wLen = 0;
278 DWORD dwBytesRead;
279 HRESULT ret = E_FAIL;
282 TRACE_(shell)("%p %p\n", pStream , ppPidl);
284 if (*ppPidl)
285 { SHFree(*ppPidl);
286 *ppPidl = NULL;
289 IStream_AddRef (pStream);
291 if (SUCCEEDED(IStream_Read(pStream, (LPVOID)&wLen, 2, &dwBytesRead)))
293 TRACE("PIDL length is %d\n", wLen);
294 if (wLen != 0) {
295 *ppPidl = SHAlloc (wLen);
296 if (SUCCEEDED(IStream_Read(pStream, *ppPidl , wLen, &dwBytesRead))) {
297 TRACE("Stream read OK\n");
298 ret = S_OK;
299 } else {
300 WARN("reading pidl failed\n");
301 SHFree(*ppPidl);
302 *ppPidl = NULL;
304 } else {
305 *ppPidl = NULL;
306 ret = S_OK;
310 /* we are not yet fully compatible */
311 if (*ppPidl && !pcheck(*ppPidl))
313 WARN("Check failed\n");
314 SHFree(*ppPidl);
315 *ppPidl = NULL;
319 IStream_Release (pStream);
320 TRACE("done\n");
321 return ret;
324 /*************************************************************************
325 * ILSaveToStream (SHELL32.27)
327 * NOTES
328 * the first two bytes are the len, the pidl is following then
330 HRESULT WINAPI ILSaveToStream (IStream * pStream, LPCITEMIDLIST pPidl)
332 LPCITEMIDLIST pidl;
333 WORD wLen = 0;
334 HRESULT ret = E_FAIL;
336 TRACE_(shell)("%p %p\n", pStream, pPidl);
338 IStream_AddRef (pStream);
340 pidl = pPidl;
341 while (pidl->mkid.cb)
343 wLen += sizeof(WORD) + pidl->mkid.cb;
344 pidl = ILGetNext(pidl);
347 if (SUCCEEDED(IStream_Write(pStream, (LPVOID)&wLen, 2, NULL)))
349 if (SUCCEEDED(IStream_Write(pStream, pPidl, wLen, NULL)))
350 { ret = S_OK;
353 IStream_Release (pStream);
355 return ret;
358 /*************************************************************************
359 * SHILCreateFromPath [SHELL32.28]
361 * Create an ItemIDList from a path
363 * PARAMS
364 * path [I]
365 * ppidl [O]
366 * attributes [I/O] requested attributes on call and actual attributes when
367 * the function returns
369 * RETURNS
370 * NO_ERROR if successful, or an OLE errer code otherwise
372 * NOTES
373 * Wrapper for IShellFolder_ParseDisplayName().
375 HRESULT WINAPI SHILCreateFromPathA(LPCSTR path, LPITEMIDLIST * ppidl, DWORD * attributes)
377 LPSHELLFOLDER sf;
378 WCHAR lpszDisplayName[MAX_PATH];
379 DWORD pchEaten;
380 HRESULT ret = E_FAIL;
382 TRACE_(shell)("%s %p 0x%08lx\n", path, ppidl, attributes ? *attributes : 0);
384 if (!MultiByteToWideChar(CP_ACP, 0, path, -1, lpszDisplayName, MAX_PATH))
385 lpszDisplayName[MAX_PATH-1] = 0;
387 if (SUCCEEDED (SHGetDesktopFolder(&sf)))
389 ret = IShellFolder_ParseDisplayName(sf, 0, NULL, lpszDisplayName, &pchEaten, ppidl, attributes);
390 IShellFolder_Release(sf);
392 return ret;
395 HRESULT WINAPI SHILCreateFromPathW(LPCWSTR path, LPITEMIDLIST * ppidl, DWORD * attributes)
397 LPSHELLFOLDER sf;
398 DWORD pchEaten;
399 HRESULT ret = E_FAIL;
401 TRACE_(shell)("%s %p 0x%08lx\n", debugstr_w(path), ppidl, attributes ? *attributes : 0);
403 if (SUCCEEDED (SHGetDesktopFolder(&sf)))
405 ret = IShellFolder_ParseDisplayName(sf, 0, NULL, (LPWSTR)path, &pchEaten, ppidl, attributes);
406 IShellFolder_Release(sf);
408 return ret;
411 HRESULT WINAPI SHILCreateFromPathAW (LPCVOID path, LPITEMIDLIST * ppidl, DWORD * attributes)
413 if ( SHELL_OsIsUnicode())
414 return SHILCreateFromPathW (path, ppidl, attributes);
415 return SHILCreateFromPathA (path, ppidl, attributes);
418 /*************************************************************************
419 * SHCloneSpecialIDList [SHELL32.89]
421 * Create an ItemIDList to one of the special folders.
423 * PARAMS
424 * hwndOwner [in]
425 * nFolder [in] CSIDL_xxxxx
426 * fCreate [in] Create folder if it does not exist
428 * RETURNS
429 * Success: The newly created pidl
430 * Failure: NULL, if inputs are invalid.
432 * NOTES
433 * exported by ordinal.
434 * Caller is responsible for deallocating the returned ItemIDList with the
435 * shells IMalloc interface, aka ILFree.
437 LPITEMIDLIST WINAPI SHCloneSpecialIDList(HWND hwndOwner, DWORD nFolder, BOOL fCreate)
438 { LPITEMIDLIST ppidl;
439 TRACE_(shell)("(hwnd=%p,csidl=0x%lx,%s).\n", hwndOwner, nFolder, fCreate ? "T" : "F");
441 if (fCreate)
442 nFolder |= CSIDL_FLAG_CREATE;
444 SHGetSpecialFolderLocation(hwndOwner, nFolder, &ppidl);
445 return ppidl;
448 /*************************************************************************
449 * ILGlobalClone [SHELL32.20]
451 * Clones an ItemIDList using Alloc.
453 * PARAMS
454 * pidl [I] ItemIDList to clone
456 * RETURNS
457 * Newly allocated ItemIDList.
459 * NOTES
460 * exported by ordinal.
462 LPITEMIDLIST WINAPI ILGlobalClone(LPCITEMIDLIST pidl)
463 { DWORD len;
464 LPITEMIDLIST newpidl;
466 if (!pidl)
467 return NULL;
469 len = ILGetSize(pidl);
470 newpidl = (LPITEMIDLIST)Alloc(len);
471 if (newpidl)
472 memcpy(newpidl,pidl,len);
474 TRACE("pidl=%p newpidl=%p\n",pidl, newpidl);
475 pdump(pidl);
477 return newpidl;
480 /*************************************************************************
481 * ILIsEqual [SHELL32.21]
484 BOOL WINAPI ILIsEqual(LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
486 char szData1[MAX_PATH];
487 char szData2[MAX_PATH];
489 LPCITEMIDLIST pidltemp1 = pidl1;
490 LPCITEMIDLIST pidltemp2 = pidl2;
492 TRACE("pidl1=%p pidl2=%p\n",pidl1, pidl2);
494 /* explorer reads from registry directly (StreamMRU),
495 so we can only check here */
496 if ((!pcheck (pidl1)) || (!pcheck (pidl2))) return FALSE;
498 pdump (pidl1);
499 pdump (pidl2);
501 if ( (!pidl1) || (!pidl2) ) return FALSE;
503 while (pidltemp1->mkid.cb && pidltemp2->mkid.cb)
505 _ILSimpleGetText(pidltemp1, szData1, MAX_PATH);
506 _ILSimpleGetText(pidltemp2, szData2, MAX_PATH);
508 if (strcasecmp ( szData1, szData2 )!=0 )
509 return FALSE;
511 pidltemp1 = ILGetNext(pidltemp1);
512 pidltemp2 = ILGetNext(pidltemp2);
515 if (!pidltemp1->mkid.cb && !pidltemp2->mkid.cb)
517 return TRUE;
520 return FALSE;
522 /*************************************************************************
523 * ILIsParent [SHELL32.23]
525 * Verifies that pidlParent is indeed the (immediate) parent of pidlChild.
527 * PARAMS
528 * pidlParent [I]
529 * pidlChild [I]
530 * bImmediate [I] only return true if the parent is the direct parent
531 * of the child
533 * RETURNS
534 * True if the parent ItemIDlist is a complete part of the child ItemIdList,
535 * False otherwise.
537 * NOTES
538 * parent = a/b, child = a/b/c -> true, c is in folder a/b
539 * child = a/b/c/d -> false if bImmediate is true, d is not in folder a/b
540 * child = a/b/c/d -> true if bImmediate is false, d is in a subfolder of a/b
542 BOOL WINAPI ILIsParent(LPCITEMIDLIST pidlParent, LPCITEMIDLIST pidlChild, BOOL bImmediate)
544 char szData1[MAX_PATH];
545 char szData2[MAX_PATH];
547 LPCITEMIDLIST pParent = pidlParent;
548 LPCITEMIDLIST pChild = pidlChild;
550 TRACE("%p %p %x\n", pidlParent, pidlChild, bImmediate);
552 if (!pParent || !pChild) return FALSE;
554 while (pParent->mkid.cb && pChild->mkid.cb)
556 _ILSimpleGetText(pParent, szData1, MAX_PATH);
557 _ILSimpleGetText(pChild, szData2, MAX_PATH);
559 if (strcasecmp ( szData1, szData2 )!=0 )
560 return FALSE;
562 pParent = ILGetNext(pParent);
563 pChild = ILGetNext(pChild);
566 if ( pParent->mkid.cb || ! pChild->mkid.cb) /* child shorter or has equal length to parent */
567 return FALSE;
569 if ( ILGetNext(pChild)->mkid.cb && bImmediate) /* not immediate descent */
570 return FALSE;
572 return TRUE;
575 /*************************************************************************
576 * ILFindChild [SHELL32.24]
578 * Compares elements from pidl1 and pidl2.
580 * PARAMS
581 * pidl1 [I]
582 * pidl2 [I]
584 * RETURNS
585 * pidl1 is desktop pidl2
586 * pidl1 shorter pidl2 pointer to first different element of pidl2
587 * if there was at least one equal element
588 * pidl2 shorter pidl1 0
589 * pidl2 equal pidl1 pointer to last 0x00-element of pidl2
591 * NOTES
592 * exported by ordinal.
594 LPITEMIDLIST WINAPI ILFindChild(LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
596 char szData1[MAX_PATH];
597 char szData2[MAX_PATH];
599 LPCITEMIDLIST pidltemp1 = pidl1;
600 LPCITEMIDLIST pidltemp2 = pidl2;
601 LPCITEMIDLIST ret=NULL;
603 TRACE("pidl1=%p pidl2=%p\n",pidl1, pidl2);
605 /* explorer reads from registry directly (StreamMRU),
606 so we can only check here */
607 if ((!pcheck (pidl1)) || (!pcheck (pidl2)))
608 return FALSE;
610 pdump (pidl1);
611 pdump (pidl2);
613 if ( _ILIsDesktop(pidl1) )
615 ret = pidl2;
617 else
619 while (pidltemp1->mkid.cb && pidltemp2->mkid.cb)
621 _ILSimpleGetText(pidltemp1, szData1, MAX_PATH);
622 _ILSimpleGetText(pidltemp2, szData2, MAX_PATH);
624 if (strcasecmp(szData1,szData2))
625 break;
627 pidltemp1 = ILGetNext(pidltemp1);
628 pidltemp2 = ILGetNext(pidltemp2);
629 ret = pidltemp2;
632 if (pidltemp1->mkid.cb)
634 ret = NULL; /* elements of pidl1 left*/
637 TRACE_(shell)("--- %p\n", ret);
638 return (LPITEMIDLIST)ret; /* pidl 1 is shorter */
641 /*************************************************************************
642 * ILCombine [SHELL32.25]
644 * Concatenates two complex ItemIDLists.
646 * PARAMS
647 * pidl1 [I] first complex ItemIDLists
648 * pidl2 [I] complex ItemIDLists to append
650 * RETURNS
651 * if both pidl's == NULL NULL
652 * if pidl1 == NULL cloned pidl2
653 * if pidl2 == NULL cloned pidl1
654 * otherwise new pidl with pidl2 appended to pidl1
656 * NOTES
657 * exported by ordinal.
658 * Does not destroy the passed in ItemIDLists!
660 LPITEMIDLIST WINAPI ILCombine(LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
662 DWORD len1,len2;
663 LPITEMIDLIST pidlNew;
665 TRACE("pidl=%p pidl=%p\n",pidl1,pidl2);
667 if(!pidl1 && !pidl2) return NULL;
669 pdump (pidl1);
670 pdump (pidl2);
672 if(!pidl1)
674 pidlNew = ILClone(pidl2);
675 return pidlNew;
678 if(!pidl2)
680 pidlNew = ILClone(pidl1);
681 return pidlNew;
684 len1 = ILGetSize(pidl1)-2;
685 len2 = ILGetSize(pidl2);
686 pidlNew = SHAlloc(len1+len2);
688 if (pidlNew)
690 memcpy(pidlNew,pidl1,len1);
691 memcpy(((BYTE *)pidlNew)+len1,pidl2,len2);
694 /* TRACE(pidl,"--new pidl=%p\n",pidlNew);*/
695 return pidlNew;
698 /*************************************************************************
699 * SHGetRealIDL [SHELL32.98]
701 * NOTES
703 HRESULT WINAPI SHGetRealIDL(LPSHELLFOLDER lpsf, LPCITEMIDLIST pidlSimple, LPITEMIDLIST *pidlReal)
705 IDataObject* pDataObj;
706 HRESULT hr = IShellFolder_GetUIObjectOf(lpsf, 0, 1, &pidlSimple, &IID_IDataObject, 0, (LPVOID*)&pDataObj);
708 if (SUCCEEDED(hr)) {
709 STGMEDIUM medium;
710 FORMATETC fmt;
712 fmt.cfFormat = RegisterClipboardFormatA(CFSTR_SHELLIDLIST);
713 fmt.ptd = NULL;
714 fmt.dwAspect = DVASPECT_CONTENT;
715 fmt.lindex = -1;
716 fmt.tymed = TYMED_HGLOBAL;
718 hr = IDataObject_GetData(pDataObj, &fmt, &medium);
720 IDataObject_Release(pDataObj);
722 if (SUCCEEDED(hr)) {
723 /*assert(pida->cidl==1);*/
724 LPIDA pida = (LPIDA)GlobalLock(medium.u.hGlobal);
726 LPCITEMIDLIST pidl_folder = (LPCITEMIDLIST) ((LPBYTE)pida+pida->aoffset[0]);
727 LPCITEMIDLIST pidl_child = (LPCITEMIDLIST) ((LPBYTE)pida+pida->aoffset[1]);
729 *pidlReal = ILCombine(pidl_folder, pidl_child);
731 if (!*pidlReal)
732 hr = E_OUTOFMEMORY;
734 GlobalUnlock(medium.u.hGlobal);
735 GlobalFree(medium.u.hGlobal);
739 return hr;
742 /*************************************************************************
743 * SHLogILFromFSIL [SHELL32.95]
745 * NOTES
746 * pild = CSIDL_DESKTOP ret = 0
747 * pild = CSIDL_DRIVES ret = 0
749 LPITEMIDLIST WINAPI SHLogILFromFSIL(LPITEMIDLIST pidl)
751 FIXME("(pidl=%p)\n",pidl);
753 pdump(pidl);
755 return 0;
758 /*************************************************************************
759 * ILGetSize [SHELL32.152]
761 * Gets the byte size of an ItemIDList including zero terminator
763 * PARAMS
764 * pidl [I] ItemIDList
766 * RETURNS
767 * size of pidl in bytes
769 * NOTES
770 * exported by ordinal
772 UINT WINAPI ILGetSize(LPCITEMIDLIST pidl)
774 LPCSHITEMID si = &(pidl->mkid);
775 UINT len=0;
777 if (pidl)
778 { while (si->cb)
779 { len += si->cb;
780 si = (LPSHITEMID)(((LPBYTE)si)+si->cb);
782 len += 2;
784 TRACE("pidl=%p size=%u\n",pidl, len);
785 return len;
788 /*************************************************************************
789 * ILGetNext [SHELL32.153]
791 * Gets the next ItemID of an ItemIDList
793 * PARAMS
794 * pidl [I] ItemIDList
796 * RETURNS
797 * null -> null
798 * desktop -> null
799 * simple pidl -> pointer to 0x0000 element
801 * NOTES
802 * exported by ordinal.
804 LPITEMIDLIST WINAPI ILGetNext(LPCITEMIDLIST pidl)
806 WORD len;
808 TRACE("%p\n", pidl);
810 if(pidl)
812 len = pidl->mkid.cb;
813 if (len)
815 pidl = (LPITEMIDLIST) (((LPBYTE)pidl)+len);
816 TRACE("-- %p\n", pidl);
817 return (LPITEMIDLIST)pidl;
820 return NULL;
823 /*************************************************************************
824 * ILAppend [SHELL32.154]
826 * Adds the single ItemID item to the ItemIDList indicated by pidl.
827 * If bEnd is FALSE, inserts the item in the front of the list,
828 * otherwise it adds the item to the end. (???)
830 * PARAMS
831 * pidl [I] ItemIDList to extend
832 * item [I] ItemID to prepend/append
833 * bEnd [I] Indicates if the item should be appended
835 * NOTES
836 * Destroys the passed in idlist! (???)
838 LPITEMIDLIST WINAPI ILAppend(LPITEMIDLIST pidl, LPCITEMIDLIST item, BOOL bEnd)
840 LPITEMIDLIST idlRet;
842 WARN("(pidl=%p,pidl=%p,%08u)semi-stub\n",pidl,item,bEnd);
844 pdump (pidl);
845 pdump (item);
847 if (_ILIsDesktop(pidl))
849 idlRet = ILClone(item);
850 if (pidl)
851 SHFree (pidl);
852 return idlRet;
855 if (bEnd)
857 idlRet = ILCombine(pidl, item);
859 else
861 idlRet = ILCombine(item, pidl);
864 SHFree(pidl);
865 return idlRet;
868 /*************************************************************************
869 * ILFree [SHELL32.155]
871 * Frees memory (if not NULL) allocated by SHMalloc allocator
873 * PARAMS
874 * pidl [I]
876 * NOTES
877 * exported by ordinal
879 void WINAPI ILFree(LPITEMIDLIST pidl)
881 TRACE("(pidl=%p)\n",pidl);
882 if(pidl) SHFree(pidl);
885 /*************************************************************************
886 * ILGlobalFree [SHELL32.156]
888 * Frees memory (if not NULL) allocated by Alloc allocator
890 * PARAMS
891 * pidl [I]
893 * NOTES
894 * exported by ordinal.
896 void WINAPI ILGlobalFree( LPITEMIDLIST pidl)
898 TRACE("%p\n", pidl);
900 if(!pidl) return;
901 Free(pidl);
904 /*************************************************************************
905 * ILCreateFromPathA [SHELL32.189]
907 * Creates a complex ItemIDList from a path and returns it.
909 * PARAMS
910 * path [I]
912 * RETURNS
913 * the newly created complex ItemIDList or NULL if failed
915 * NOTES
916 * exported by ordinal.
919 LPITEMIDLIST WINAPI ILCreateFromPathA (LPCSTR path)
921 LPITEMIDLIST pidlnew = NULL;
923 TRACE_(shell)("%s\n", debugstr_a(path));
925 if (SUCCEEDED(SHILCreateFromPathA(path, &pidlnew, NULL)))
926 return pidlnew;
927 return NULL;
930 /*************************************************************************
931 * ILCreateFromPathW [SHELL32.190]
933 LPITEMIDLIST WINAPI ILCreateFromPathW (LPCWSTR path)
935 LPITEMIDLIST pidlnew = NULL;
937 TRACE_(shell)("%s\n", debugstr_w(path));
939 if (SUCCEEDED(SHILCreateFromPathW(path, &pidlnew, NULL)))
940 return pidlnew;
941 return NULL;
944 /*************************************************************************
945 * ILCreateFromPath [SHELL32.157]
947 LPITEMIDLIST WINAPI ILCreateFromPathAW (LPCVOID path)
949 if ( SHELL_OsIsUnicode())
950 return ILCreateFromPathW (path);
951 return ILCreateFromPathA (path);
954 /*************************************************************************
955 * _ILParsePathW [internal]
957 * Creates an ItemIDList from a path and returns it.
959 * PARAMS
960 * path [I] path to parse and convert into an ItemIDList
961 * lpFindFile [I] pointer to buffer to initialize the FileSystem
962 * Bind Data object with
963 * bBindCtx [I] indicates to create a BindContext and assign a
964 * FileSystem Bind Data object
965 * ppidl [O] the newly create ItemIDList
966 * prgfInOut [I/O] requested attributes on input and actual
967 * attributes on return
969 * RETURNS
970 * NO_ERROR on success or an OLE error code
972 * NOTES
973 * If either lpFindFile is non-NULL or bBindCtx is TRUE, this function
974 * creates a BindContext object and assigns a FileSystem Bind Data object
975 * to it, passing the BindContext to IShellFolder_ParseDisplayName. Each
976 * IShellFolder uses that FileSystem Bind Data object of the BindContext
977 * to pass data about the current path element to the next object. This
978 * is used to avoid having to verify the current path element on disk, so
979 * that creating an ItemIDList from a non-existent path still can work.
981 static HRESULT WINAPI _ILParsePathW(LPCWSTR path, LPWIN32_FIND_DATAW lpFindFile,
982 BOOL bBindCtx, LPITEMIDLIST *ppidl, LPDWORD prgfInOut)
984 LPSHELLFOLDER pSF = NULL;
985 LPBC pBC = NULL;
986 HRESULT ret;
988 TRACE("%s %p %d (%p)->%p (%p)->0x%lx\n", debugstr_w(path), lpFindFile, bBindCtx,
989 ppidl, ppidl ? *ppidl : NULL,
990 prgfInOut, prgfInOut ? *prgfInOut : 0);
992 ret = SHGetDesktopFolder(&pSF);
993 if (FAILED(ret))
995 return ret;
998 if (lpFindFile || bBindCtx)
999 ret = IFileSystemBindData_Constructor(lpFindFile, &pBC);
1001 if (SUCCEEDED(ret))
1003 ret = IShellFolder_ParseDisplayName(pSF, 0, pBC, (LPOLESTR)path, NULL, ppidl, prgfInOut);
1006 if (pBC)
1008 IBindCtx_Release(pBC);
1009 pBC = NULL;
1012 IShellFolder_Release(pSF);
1014 if (!SUCCEEDED(ret) && ppidl)
1015 *ppidl = NULL;
1017 TRACE("%s %p 0x%lx\n", debugstr_w(path), ppidl ? *ppidl : NULL, prgfInOut ? *prgfInOut : 0);
1019 return ret;
1022 /*************************************************************************
1023 * SHSimpleIDListFromPath [SHELL32.162]
1025 * Creates a simple ItemIDList from a path and returns it. This function
1026 * does not fail on non-existent paths.
1028 * PARAMS
1029 * path [I] path to parse and convert into an ItemIDList
1031 * RETURNS
1032 * the newly created simple ItemIDList
1034 * NOTES
1035 * Simple in the name does not mean a relative ItemIDList but rather a
1036 * fully qualified list, where only the file name is filled in and the
1037 * directory flag for those ItemID elements this is known about, eg.
1038 * it is not the last element in the ItemIDList or the actual directory
1039 * exists on disk.
1040 * exported by ordinal.
1042 LPITEMIDLIST WINAPI SHSimpleIDListFromPathA(LPCSTR lpszPath)
1044 LPITEMIDLIST pidl = NULL;
1045 LPWSTR wPath = NULL;
1046 int len;
1048 TRACE("%s\n", debugstr_a(lpszPath));
1050 if (lpszPath)
1052 len = MultiByteToWideChar(CP_ACP, 0, lpszPath, -1, NULL, 0);
1053 wPath = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1054 MultiByteToWideChar(CP_ACP, 0, lpszPath, -1, wPath, len);
1057 _ILParsePathW(wPath, NULL, TRUE, &pidl, NULL);
1059 if (wPath) HeapFree(GetProcessHeap(), 0, wPath);
1060 TRACE("%s %p\n", debugstr_a(lpszPath), pidl);
1061 return pidl;
1064 LPITEMIDLIST WINAPI SHSimpleIDListFromPathW(LPCWSTR lpszPath)
1066 LPITEMIDLIST pidl = NULL;
1068 TRACE("%s\n", debugstr_w(lpszPath));
1070 _ILParsePathW(lpszPath, NULL, TRUE, &pidl, NULL);
1071 TRACE("%s %p\n", debugstr_w(lpszPath), pidl);
1072 return pidl;
1075 LPITEMIDLIST WINAPI SHSimpleIDListFromPathAW(LPCVOID lpszPath)
1077 if ( SHELL_OsIsUnicode())
1078 return SHSimpleIDListFromPathW (lpszPath);
1079 return SHSimpleIDListFromPathA (lpszPath);
1082 /*************************************************************************
1083 * SHGetDataFromIDListA [SHELL32.247]
1085 * NOTES
1086 * the pidl can be a simple one. since we can't get the path out of the pidl
1087 * we have to take all data from the pidl
1089 HRESULT WINAPI SHGetDataFromIDListA(LPSHELLFOLDER psf, LPCITEMIDLIST pidl, int nFormat, LPVOID dest, int len)
1091 TRACE_(shell)("sf=%p pidl=%p 0x%04x %p 0x%04x stub\n",psf,pidl,nFormat,dest,len);
1093 pdump(pidl);
1094 if (!psf || !dest ) return E_INVALIDARG;
1096 switch (nFormat)
1098 case SHGDFIL_FINDDATA:
1100 LPSTR filename, shortname;
1101 WIN32_FIND_DATAA * pfd = dest;
1103 if (_ILIsDrive(pidl))
1104 return E_INVALIDARG;
1106 if (len < (int)sizeof(WIN32_FIND_DATAA)) return E_INVALIDARG;
1108 ZeroMemory(pfd, sizeof (WIN32_FIND_DATAA));
1109 _ILGetFileDateTime( pidl, &(pfd->ftLastWriteTime));
1110 pfd->dwFileAttributes = _ILGetFileAttributes(pidl, NULL, 0);
1111 pfd->nFileSizeLow = _ILGetFileSize ( pidl, NULL, 0);
1113 filename = _ILGetTextPointer(pidl);
1114 shortname = _ILGetSTextPointer(pidl);
1116 if (filename)
1117 lstrcpynA(pfd->cFileName, filename, MAX_PATH);
1118 else
1119 pfd->cFileName[0] = '\0';
1121 if (shortname)
1122 lstrcpynA(pfd->cAlternateFileName, shortname, MAX_PATH);
1123 else
1124 pfd->cAlternateFileName[0] = '\0';
1126 return NOERROR;
1128 case SHGDFIL_NETRESOURCE:
1129 case SHGDFIL_DESCRIPTIONID:
1130 FIXME_(shell)("SHGDFIL %i stub\n", nFormat);
1131 break;
1133 default:
1134 ERR_(shell)("Unknown SHGDFIL %i, please report\n", nFormat);
1137 return E_INVALIDARG;
1140 /*************************************************************************
1141 * SHGetDataFromIDListW [SHELL32.248]
1144 HRESULT WINAPI SHGetDataFromIDListW(LPSHELLFOLDER psf, LPCITEMIDLIST pidl, int nFormat, LPVOID dest, int len)
1146 TRACE_(shell)("sf=%p pidl=%p 0x%04x %p 0x%04x stub\n",psf,pidl,nFormat,dest,len);
1148 pdump(pidl);
1150 if (! psf || !dest ) return E_INVALIDARG;
1152 switch (nFormat)
1154 case SHGDFIL_FINDDATA:
1156 LPSTR filename, shortname;
1157 WIN32_FIND_DATAW * pfd = dest;
1159 if (_ILIsDrive(pidl))
1160 return E_INVALIDARG;
1162 if (len < (int)sizeof(WIN32_FIND_DATAW)) return E_INVALIDARG;
1164 ZeroMemory(pfd, sizeof (WIN32_FIND_DATAA));
1165 _ILGetFileDateTime( pidl, &(pfd->ftLastWriteTime));
1166 pfd->dwFileAttributes = _ILGetFileAttributes(pidl, NULL, 0);
1167 pfd->nFileSizeLow = _ILGetFileSize ( pidl, NULL, 0);
1169 filename = _ILGetTextPointer(pidl);
1170 shortname = _ILGetSTextPointer(pidl);
1172 if (!filename)
1173 pfd->cFileName[0] = '\0';
1174 else if (!MultiByteToWideChar(CP_ACP, 0, filename, -1, pfd->cFileName, MAX_PATH))
1175 pfd->cFileName[MAX_PATH-1] = 0;
1177 if (!shortname)
1178 pfd->cAlternateFileName[0] = '\0';
1179 else if (!MultiByteToWideChar(CP_ACP, 0, shortname, -1, pfd->cAlternateFileName, 14))
1180 pfd->cAlternateFileName[13] = 0;
1182 return NOERROR;
1183 case SHGDFIL_NETRESOURCE:
1184 case SHGDFIL_DESCRIPTIONID:
1185 FIXME_(shell)("SHGDFIL %i stub\n", nFormat);
1186 break;
1188 default:
1189 ERR_(shell)("Unknown SHGDFIL %i, please report\n", nFormat);
1192 return E_INVALIDARG;
1195 /*************************************************************************
1196 * SHELL_GetPathFromIDListA
1198 HRESULT SHELL_GetPathFromIDListA(LPCITEMIDLIST pidl, LPSTR pszPath, UINT uOutSize)
1200 HRESULT hr = S_OK;
1202 pszPath[0]=0;
1204 /* One case is a PIDL rooted at desktop level */
1205 if (_ILIsValue(pidl) || _ILIsFolder(pidl)) {
1206 hr = SHGetSpecialFolderPathA(0, pszPath, CSIDL_DESKTOP, FALSE);
1208 if (SUCCEEDED(hr))
1209 PathAddBackslashA(pszPath);
1211 /* The only other valid case is a item ID list beginning at "My Computer" */
1212 else if (_ILIsMyComputer(pidl))
1213 pidl = ILGetNext(pidl);
1215 if (SUCCEEDED(hr)) {
1216 LPSTR txt;
1218 while(pidl && pidl->mkid.cb) {
1219 if (_ILIsSpecialFolder(pidl))
1220 {hr = E_INVALIDARG; break;}
1222 txt = _ILGetTextPointer(pidl);
1223 if (!txt)
1224 {hr = E_INVALIDARG; break;}
1226 if (lstrlenA(txt) > pidl->mkid.cb)
1227 ERR("pidl %p is borked\n",pidl);
1229 /* make sure there's enough space for the next segment */
1230 if ( (lstrlenA(txt) + lstrlenA(pszPath)) > uOutSize)
1231 {hr = E_INVALIDARG; break;}
1232 lstrcatA( pszPath, txt );
1234 pidl = ILGetNext(pidl);
1235 if (!pidl)
1236 {hr = E_INVALIDARG; break;}
1238 if (!pidl->mkid.cb) {
1239 /* We are at the end and successfully converted the complete PIDL. */
1240 break;
1243 if( (lstrlenA(pszPath) + 1) > uOutSize)
1244 {hr = E_INVALIDARG; break;}
1245 if (!PathAddBackslashA(pszPath))
1246 {hr = E_INVALIDARG; break;}
1248 } else
1249 hr = E_INVALIDARG;
1251 TRACE_(shell)("-- %s, 0x%08lx\n", pszPath, hr);
1252 return hr;
1255 /*************************************************************************
1256 * SHGetPathFromIDListA [SHELL32.@][NT 4.0: SHELL32.220]
1258 * PARAMETERS
1259 * pidl, [IN] pidl
1260 * pszPath [OUT] path
1262 * RETURNS
1263 * path from a passed PIDL.
1265 * NOTES
1266 * NULL returns FALSE
1267 * desktop pidl gives path to desktop directory back
1268 * special pidls returning FALSE
1270 BOOL WINAPI SHGetPathFromIDListA(LPCITEMIDLIST pidl, LPSTR pszPath)
1272 HRESULT hr;
1274 TRACE_(shell)("(pidl=%p,%p)\n",pidl,pszPath);
1275 pdump(pidl);
1277 if (!pidl)
1278 return FALSE;
1280 hr = SHELL_GetPathFromIDListA(pidl, pszPath, MAX_PATH);
1282 return SUCCEEDED(hr);
1285 /*************************************************************************
1286 * SHELL_GetPathFromIDListW
1288 HRESULT SHELL_GetPathFromIDListW(LPCITEMIDLIST pidl, LPWSTR pszPath, UINT uOutSize)
1290 HRESULT hr = S_OK;
1291 UINT len;
1293 pszPath[0]=0;
1295 /* One case is a PIDL rooted at desktop level */
1296 if (_ILIsValue(pidl) || _ILIsFolder(pidl)) {
1297 hr = SHGetSpecialFolderPathW(0, pszPath, CSIDL_DESKTOP, FALSE);
1299 if (SUCCEEDED(hr))
1300 PathAddBackslashW(pszPath);
1302 /* The only other valid case is a item ID list beginning at "My Computer" */
1303 else if (_ILIsMyComputer(pidl))
1304 pidl = ILGetNext(pidl);
1306 if (SUCCEEDED(hr)) {
1307 LPSTR txt;
1309 while(pidl && pidl->mkid.cb) {
1310 if (_ILIsSpecialFolder(pidl))
1311 {hr = E_INVALIDARG; break;}
1313 txt = _ILGetTextPointer(pidl);
1314 if (!txt)
1315 {hr = E_INVALIDARG; break;}
1317 if (lstrlenA(txt) > pidl->mkid.cb)
1318 ERR("pidl %p is borked\n",pidl);
1319 len = MultiByteToWideChar(CP_ACP, 0, txt, -1, NULL, 0);
1320 if ( (lstrlenW(pszPath) + len) > uOutSize )
1321 {hr = E_INVALIDARG; break;}
1323 MultiByteToWideChar(CP_ACP, 0, txt, -1,
1324 &pszPath[lstrlenW(pszPath)], len);
1326 pidl = ILGetNext(pidl);
1327 if (!pidl)
1328 {hr = E_INVALIDARG; break;}
1330 if (!pidl->mkid.cb) {
1331 /* We are at the end and successfully converted the complete PIDL. */
1332 break;
1335 if ( (lstrlenW(pszPath) + 1) > uOutSize )
1336 {hr = E_INVALIDARG; break;}
1337 if (!PathAddBackslashW(pszPath))
1338 {hr = E_INVALIDARG; break;}
1340 } else
1341 hr = E_INVALIDARG;
1343 TRACE_(shell)("-- %s, 0x%08lx\n", debugstr_w(pszPath), hr);
1344 return hr;
1347 /*************************************************************************
1348 * SHGetPathFromIDListW [SHELL32.@]
1350 BOOL WINAPI SHGetPathFromIDListW(LPCITEMIDLIST pidl, LPWSTR pszPath)
1352 HRESULT hr;
1354 TRACE_(shell)("(pidl=%p,%p)\n", pidl, debugstr_w(pszPath));
1355 pdump(pidl);
1357 if (!pidl)
1358 return FALSE;
1360 hr = SHELL_GetPathFromIDListW(pidl, pszPath, MAX_PATH);
1362 TRACE_(shell)("-- %s, 0x%08lx\n",debugstr_w(pszPath), hr);
1363 return SUCCEEDED(hr);
1366 /*************************************************************************
1367 * SHGetPathFromIDList [SHELL32.@][NT 4.0: SHELL32.219]
1369 BOOL WINAPI SHGetPathFromIDListAW(LPCITEMIDLIST pidl,LPVOID pszPath)
1371 TRACE_(shell)("(pidl=%p,%p)\n",pidl,pszPath);
1373 if (SHELL_OsIsUnicode())
1374 return SHGetPathFromIDListW(pidl,pszPath);
1375 return SHGetPathFromIDListA(pidl,pszPath);
1378 /*************************************************************************
1379 * SHBindToParent [shell version 5.0]
1381 HRESULT WINAPI SHBindToParent(LPCITEMIDLIST pidl, REFIID riid, LPVOID *ppv, LPCITEMIDLIST *ppidlLast)
1383 IShellFolder * psf;
1384 LPITEMIDLIST pidlChild, pidlParent;
1385 HRESULT hr=E_FAIL;
1387 TRACE_(shell)("pidl=%p\n", pidl);
1388 pdump(pidl);
1390 *ppv = NULL;
1391 if (ppidlLast) *ppidlLast = NULL;
1393 if (_ILIsPidlSimple(pidl))
1395 IShellFolder* desktop;
1397 /* we are on desktop level */
1398 hr = SHGetDesktopFolder(&desktop);
1400 if (SUCCEEDED(hr))
1402 hr = IShellFolder_QueryInterface(desktop, riid, ppv);
1404 if (SUCCEEDED(hr) && ppidlLast)
1405 *ppidlLast = ILClone(pidl);
1407 IShellFolder_Release(desktop);
1410 else
1412 pidlChild = ILClone(ILFindLastID(pidl));
1413 pidlParent = ILClone(pidl);
1414 ILRemoveLastID(pidlParent);
1416 hr = SHGetDesktopFolder(&psf);
1418 if (SUCCEEDED(hr))
1419 hr = IShellFolder_BindToObject(psf, pidlParent, NULL, riid, ppv);
1421 if (SUCCEEDED(hr) && ppidlLast)
1422 *ppidlLast = pidlChild;
1423 else
1424 ILFree (pidlChild);
1426 SHFree (pidlParent);
1427 if (psf) IShellFolder_Release(psf);
1431 TRACE_(shell)("-- psf=%p pidl=%p ret=0x%08lx\n", *ppv, (ppidlLast)?*ppidlLast:NULL, hr);
1432 return hr;
1435 /**************************************************************************
1437 * internal functions
1439 * ### 1. section creating pidls ###
1441 *************************************************************************
1443 LPITEMIDLIST _ILAlloc(PIDLTYPE type, size_t size)
1445 LPITEMIDLIST pidlOut = NULL;
1447 if((pidlOut = SHAlloc(size + 5)))
1449 LPPIDLDATA pData;
1450 LPITEMIDLIST pidlNext;
1452 ZeroMemory(pidlOut, size + 5);
1453 pidlOut->mkid.cb = size + 3;
1455 if ((pData = _ILGetDataPointer(pidlOut)))
1456 pData->type = type;
1458 if ((pidlNext = ILGetNext(pidlOut)))
1459 pidlNext->mkid.cb = 0x00;
1460 TRACE("-- (pidl=%p, size=%u)\n", pidlOut, size);
1463 return pidlOut;
1466 LPITEMIDLIST _ILCreateDesktop()
1468 LPITEMIDLIST ret;
1470 TRACE("()\n");
1471 ret = SHAlloc(2);
1472 if (ret)
1473 ret->mkid.cb = 0;
1474 return ret;
1477 LPITEMIDLIST _ILCreateMyComputer()
1478 { TRACE("()\n");
1479 return _ILCreateGuid(PT_GUID, &CLSID_MyComputer);
1482 LPITEMIDLIST _ILCreateIExplore()
1483 { TRACE("()\n");
1484 return _ILCreateGuid(PT_GUID, &CLSID_Internet);
1487 LPITEMIDLIST _ILCreateControlPanel()
1489 LPITEMIDLIST parent = _ILCreateGuid(PT_GUID, &CLSID_MyComputer), ret = NULL;
1491 TRACE("()\n");
1492 if (parent)
1494 LPITEMIDLIST cpl = _ILCreateGuid(PT_GUID, &CLSID_ControlPanel);
1496 if (cpl)
1498 ret = ILCombine(parent, cpl);
1499 SHFree(cpl);
1501 SHFree(parent);
1503 return ret;
1506 LPITEMIDLIST _ILCreatePrinters()
1508 LPITEMIDLIST parent = _ILCreateGuid(PT_GUID, &CLSID_MyComputer), ret = NULL;
1510 TRACE("()\n");
1511 if (parent)
1513 LPITEMIDLIST printers = _ILCreateGuid(PT_GUID, &CLSID_Printers);
1515 if (printers)
1517 ret = ILCombine(parent, printers);
1518 SHFree(printers);
1520 SHFree(parent);
1522 return ret;
1525 LPITEMIDLIST _ILCreateNetwork()
1526 { TRACE("()\n");
1527 return _ILCreateGuid(PT_GUID, &CLSID_NetworkPlaces);
1530 LPITEMIDLIST _ILCreateBitBucket()
1531 { TRACE("()\n");
1532 return _ILCreateGuid(PT_GUID, &CLSID_RecycleBin);
1535 LPITEMIDLIST _ILCreateGuid(PIDLTYPE type, REFIID guid)
1537 LPITEMIDLIST pidlOut;
1539 if (type == PT_SHELLEXT || type == PT_GUID)
1541 pidlOut = _ILAlloc(type, sizeof(GUIDStruct));
1542 if (pidlOut)
1544 LPPIDLDATA pData = _ILGetDataPointer(pidlOut);
1546 memcpy(&(pData->u.guid.guid), guid, sizeof(GUID));
1547 TRACE("-- create GUID-pidl %s\n",
1548 debugstr_guid(&(pData->u.guid.guid)));
1551 else
1553 WARN("%d: invalid type for GUID\n", type);
1554 pidlOut = NULL;
1556 return pidlOut;
1559 LPITEMIDLIST _ILCreateGuidFromStrA(LPCSTR szGUID)
1561 IID iid;
1563 if (!SUCCEEDED(SHCLSIDFromStringA(szGUID, &iid)))
1565 ERR("%s is not a GUID\n", szGUID);
1566 return NULL;
1568 return _ILCreateGuid(PT_GUID, &iid);
1571 LPITEMIDLIST _ILCreateFromFindDataA(WIN32_FIND_DATAA * stffile )
1573 char buff[MAX_PATH + 14 +1]; /* see WIN32_FIND_DATA */
1574 char * pbuff = buff;
1575 size_t len, len1;
1576 LPITEMIDLIST pidl;
1577 PIDLTYPE type;
1579 if (!stffile)
1580 return NULL;
1582 TRACE("(%s, %s)\n",stffile->cAlternateFileName, stffile->cFileName);
1584 /* prepare buffer with both names */
1585 len = strlen (stffile->cFileName) + 1;
1586 memcpy (pbuff, stffile->cFileName, len);
1587 pbuff += len;
1589 len1 = strlen (stffile->cAlternateFileName)+1;
1590 memcpy (pbuff, stffile->cAlternateFileName, len1);
1592 type = (stffile->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? PT_FOLDER :
1593 PT_VALUE;
1594 /* FileStruct already has one byte for the first name, so use len - 1 in
1595 * size calculation
1597 if ((pidl = _ILAlloc(type, sizeof(FileStruct) + (len - 1) + len1)))
1599 LPPIDLDATA pData;
1600 LPSTR pszDest;
1602 /* set attributes */
1603 if ((pData = _ILGetDataPointer(pidl)))
1605 pData->type = type;
1606 FileTimeToDosDateTime(&(stffile->ftLastWriteTime),&pData->u.file.uFileDate,&pData->u.file.uFileTime);
1607 pData->u.file.dwFileSize = stffile->nFileSizeLow;
1608 pData->u.file.uFileAttribs = (WORD)stffile->dwFileAttributes;
1610 if ((pszDest = _ILGetTextPointer(pidl)))
1612 memcpy(pszDest, buff, len + len1);
1613 TRACE("-- create Value: %s\n",debugstr_a(pszDest));
1616 return pidl;
1619 HRESULT _ILCreateFromPathA(LPCSTR szPath, LPITEMIDLIST* ppidl)
1621 HANDLE hFile;
1622 WIN32_FIND_DATAA stffile;
1624 if (!ppidl)
1625 return E_INVALIDARG;
1627 hFile = FindFirstFileA(szPath, &stffile);
1629 if (hFile == INVALID_HANDLE_VALUE)
1630 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
1632 FindClose(hFile);
1634 *ppidl = _ILCreateFromFindDataA(&stffile);
1636 return *ppidl ? S_OK : E_OUTOFMEMORY;
1639 LPITEMIDLIST _ILCreateDrive(LPCWSTR lpszNew)
1641 WCHAR sTemp[4];
1642 LPITEMIDLIST pidlOut;
1644 sTemp[0]=toupperW(lpszNew[0]);
1645 sTemp[1]=':';
1646 sTemp[2]='\\';
1647 sTemp[3]=0x00;
1648 TRACE("(%s)\n",debugstr_w(sTemp));
1650 if ((pidlOut = _ILAlloc(PT_DRIVE, sizeof(DriveStruct))))
1652 LPSTR pszDest;
1654 if ((pszDest = _ILGetTextPointer(pidlOut)))
1656 WideCharToMultiByte(CP_ACP, 0, sTemp, sizeof(sTemp)/sizeof(WCHAR), pszDest, sizeof(sTemp)/sizeof(WCHAR), NULL, NULL);
1657 TRACE("-- create Drive: %s\n", debugstr_a(pszDest));
1660 return pidlOut;
1663 /**************************************************************************
1664 * _ILGetDrive()
1666 * Gets the text for the drive eg. 'c:\'
1668 * RETURNS
1669 * strlen (lpszText)
1671 DWORD _ILGetDrive(LPCITEMIDLIST pidl,LPSTR pOut, UINT uSize)
1672 { TRACE("(%p,%p,%u)\n",pidl,pOut,uSize);
1674 if(_ILIsMyComputer(pidl))
1675 pidl = ILGetNext(pidl);
1677 if (pidl && _ILIsDrive(pidl))
1678 return _ILSimpleGetText(pidl, pOut, uSize);
1680 return 0;
1683 /**************************************************************************
1685 * ### 2. section testing pidls ###
1687 **************************************************************************
1688 * _ILIsDesktop()
1689 * _ILIsMyComputer()
1690 * _ILIsSpecialFolder()
1691 * _ILIsDrive()
1692 * _ILIsFolder()
1693 * _ILIsValue()
1694 * _ILIsPidlSimple()
1696 BOOL _ILIsDesktop(LPCITEMIDLIST pidl)
1697 { TRACE("(%p)\n",pidl);
1698 return pidl && pidl->mkid.cb ? 0 : 1;
1701 BOOL _ILIsMyComputer(LPCITEMIDLIST pidl)
1703 REFIID iid = _ILGetGUIDPointer(pidl);
1705 TRACE("(%p)\n",pidl);
1707 if (iid)
1708 return IsEqualIID(iid, &CLSID_MyComputer);
1709 return FALSE;
1712 BOOL _ILIsSpecialFolder (LPCITEMIDLIST pidl)
1714 LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1715 TRACE("(%p)\n",pidl);
1716 return (pidl && ( (lpPData && (PT_GUID== lpPData->type || PT_SHELLEXT== lpPData->type)) ||
1717 (pidl && pidl->mkid.cb == 0x00)
1721 BOOL _ILIsDrive(LPCITEMIDLIST pidl)
1722 { LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1723 TRACE("(%p)\n",pidl);
1724 return (pidl && lpPData && (PT_DRIVE == lpPData->type ||
1725 PT_DRIVE1 == lpPData->type ||
1726 PT_DRIVE2 == lpPData->type ||
1727 PT_DRIVE3 == lpPData->type));
1730 BOOL _ILIsFolder(LPCITEMIDLIST pidl)
1731 { LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1732 TRACE("(%p)\n",pidl);
1733 return (pidl && lpPData && (PT_FOLDER == lpPData->type || PT_FOLDER1 == lpPData->type));
1736 BOOL _ILIsValue(LPCITEMIDLIST pidl)
1737 { LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1738 TRACE("(%p)\n",pidl);
1739 return (pidl && lpPData && PT_VALUE == lpPData->type);
1742 BOOL _ILIsCPanelStruct(LPCITEMIDLIST pidl)
1743 { LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1744 TRACE("(%p)\n",pidl);
1745 return (pidl && lpPData && (lpPData->type == 0));
1748 /**************************************************************************
1749 * _ILIsPidlSimple
1751 BOOL _ILIsPidlSimple ( LPCITEMIDLIST pidl)
1753 BOOL ret = TRUE;
1755 if(! _ILIsDesktop(pidl)) /* pidl=NULL or mkid.cb=0 */
1757 WORD len = pidl->mkid.cb;
1758 LPCITEMIDLIST pidlnext = (LPCITEMIDLIST) (((LPBYTE)pidl) + len );
1759 if (pidlnext->mkid.cb)
1760 ret = FALSE;
1763 TRACE("%s\n", ret ? "Yes" : "No");
1764 return ret;
1767 /**************************************************************************
1769 * ### 3. section getting values from pidls ###
1772 /**************************************************************************
1773 * _ILSimpleGetText
1775 * gets the text for the first item in the pidl (eg. simple pidl)
1777 * returns the length of the string
1779 DWORD _ILSimpleGetText (LPCITEMIDLIST pidl, LPSTR szOut, UINT uOutSize)
1781 DWORD dwReturn=0;
1782 LPSTR szSrc;
1783 GUID const * riid;
1784 char szTemp[MAX_PATH];
1786 TRACE("(%p %p %x)\n",pidl,szOut,uOutSize);
1788 if (!pidl) return 0;
1790 if (szOut)
1791 *szOut = 0;
1793 if (_ILIsDesktop(pidl))
1795 /* desktop */
1796 if (HCR_GetClassNameA(&CLSID_ShellDesktop, szTemp, MAX_PATH))
1798 if (szOut)
1799 lstrcpynA(szOut, szTemp, uOutSize);
1801 dwReturn = strlen (szTemp);
1804 else if (( szSrc = _ILGetTextPointer(pidl) ))
1806 /* filesystem */
1807 if (szOut)
1808 lstrcpynA(szOut, szSrc, uOutSize);
1810 dwReturn = strlen(szSrc);
1812 else if (( riid = _ILGetGUIDPointer(pidl) ))
1814 /* special folder */
1815 if ( HCR_GetClassNameA(riid, szTemp, MAX_PATH) )
1817 if (szOut)
1818 lstrcpynA(szOut, szTemp, uOutSize);
1820 dwReturn = strlen (szTemp);
1823 else
1825 ERR("-- no text\n");
1828 TRACE("-- (%p=%s 0x%08lx)\n",szOut,debugstr_a(szOut),dwReturn);
1829 return dwReturn;
1832 /**************************************************************************
1833 * _ILSimpleGetTextW
1835 * gets the text for the first item in the pidl (eg. simple pidl)
1837 * returns the length of the string
1839 DWORD _ILSimpleGetTextW (LPCITEMIDLIST pidl, LPWSTR szOut, UINT uOutSize)
1841 DWORD dwReturn;
1842 char szTemp[MAX_PATH];
1844 TRACE("(%p %p %x)\n",pidl,szOut,uOutSize);
1846 dwReturn = _ILSimpleGetText(pidl, szTemp, uOutSize);
1848 if (!MultiByteToWideChar(CP_ACP, 0, szTemp, -1, szOut, MAX_PATH))
1849 *szOut = 0;
1851 TRACE("-- (%p=%s 0x%08lx)\n",szOut,debugstr_w(szOut),dwReturn);
1852 return dwReturn;
1855 /**************************************************************************
1857 * ### 4. getting pointers to parts of pidls ###
1859 **************************************************************************
1860 * _ILGetDataPointer()
1862 LPPIDLDATA _ILGetDataPointer(LPCITEMIDLIST pidl)
1864 if(pidl && pidl->mkid.cb != 0x00)
1865 return (LPPIDLDATA) &(pidl->mkid.abID);
1866 return NULL;
1869 /**************************************************************************
1870 * _ILGetTextPointer()
1871 * gets a pointer to the long filename string stored in the pidl
1873 LPSTR _ILGetTextPointer(LPCITEMIDLIST pidl)
1874 {/* TRACE(pidl,"(pidl%p)\n", pidl);*/
1876 LPPIDLDATA pdata = _ILGetDataPointer(pidl);
1878 if (pdata)
1880 switch (pdata->type)
1882 case PT_GUID:
1883 case PT_SHELLEXT:
1884 return NULL;
1886 case PT_DRIVE:
1887 case PT_DRIVE1:
1888 case PT_DRIVE2:
1889 case PT_DRIVE3:
1890 return (LPSTR)&(pdata->u.drive.szDriveName);
1892 case PT_FOLDER:
1893 case PT_FOLDER1:
1894 case PT_VALUE:
1895 case PT_IESPECIAL1:
1896 case PT_RAS_FOLDER:
1897 case PT_IESPECIAL2:
1898 return (LPSTR)&(pdata->u.file.szNames);
1900 case PT_WORKGRP:
1901 case PT_COMP:
1902 case PT_NETWORK:
1903 case PT_NETPROVIDER:
1904 case PT_SHARE:
1905 return (LPSTR)&(pdata->u.network.szNames);
1908 return NULL;
1911 /**************************************************************************
1912 * _ILGetSTextPointer()
1913 * gets a pointer to the short filename string stored in the pidl
1915 LPSTR _ILGetSTextPointer(LPCITEMIDLIST pidl)
1916 {/* TRACE(pidl,"(pidl%p)\n", pidl);*/
1918 LPPIDLDATA pdata =_ILGetDataPointer(pidl);
1920 if (pdata)
1922 switch (pdata->type)
1924 case PT_FOLDER:
1925 case PT_VALUE:
1926 case PT_IESPECIAL1:
1927 case PT_RAS_FOLDER:
1928 case PT_IESPECIAL2:
1929 return (LPSTR)(pdata->u.file.szNames + strlen (pdata->u.file.szNames) + 1);
1931 case PT_WORKGRP:
1932 return (LPSTR)(pdata->u.network.szNames + strlen (pdata->u.network.szNames) + 1);
1935 return NULL;
1938 /**************************************************************************
1939 * _ILGetGUIDPointer()
1941 * returns reference to guid stored in some pidls
1943 REFIID _ILGetGUIDPointer(LPCITEMIDLIST pidl)
1945 LPPIDLDATA pdata =_ILGetDataPointer(pidl);
1947 TRACE("%p\n", pidl);
1949 if (pdata)
1951 TRACE("pdata->type 0x%04x\n", pdata->type);
1952 switch (pdata->type)
1954 case PT_SHELLEXT:
1955 case PT_GUID:
1956 return (REFIID) &(pdata->u.guid.guid);
1958 default:
1959 TRACE("Unknown pidl type 0x%04x\n", pdata->type);
1960 break;
1963 return NULL;
1966 /*************************************************************************
1967 * _ILGetFileDateTime
1969 * Given the ItemIdList, get the FileTime
1971 * PARAMS
1972 * pidl [I] The ItemIDList
1973 * pFt [I] the resulted FILETIME of the file
1975 * RETURNS
1976 * True if Successful
1978 * NOTES
1981 BOOL _ILGetFileDateTime(LPCITEMIDLIST pidl, FILETIME *pFt)
1983 LPPIDLDATA pdata = _ILGetDataPointer(pidl);
1985 if(! pdata) return FALSE;
1987 switch (pdata->type)
1989 case PT_FOLDER:
1990 case PT_VALUE:
1991 DosDateTimeToFileTime(pdata->u.file.uFileDate, pdata->u.file.uFileTime, pFt);
1992 break;
1993 default:
1994 return FALSE;
1996 return TRUE;
1999 BOOL _ILGetFileDate (LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2001 FILETIME ft,lft;
2002 SYSTEMTIME time;
2003 BOOL ret;
2005 if (_ILGetFileDateTime( pidl, &ft )) {
2006 FileTimeToLocalFileTime(&ft, &lft);
2007 FileTimeToSystemTime (&lft, &time);
2008 ret = GetDateFormatA(LOCALE_USER_DEFAULT,DATE_SHORTDATE,&time, NULL, pOut, uOutSize);
2009 } else {
2010 pOut[0] = '\0';
2011 ret = FALSE;
2013 return ret;
2017 /*************************************************************************
2018 * _ILGetFileSize
2020 * Given the ItemIdList, get the FileSize
2022 * PARAMS
2023 * pidl [I] The ItemIDList
2024 * pOut [I] The buffer to save the result
2025 * uOutsize [I] The size of the buffer
2027 * RETURNS
2028 * The FileSize
2030 * NOTES
2031 * pOut can be null when no string is needed
2034 DWORD _ILGetFileSize (LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2036 LPPIDLDATA pdata = _ILGetDataPointer(pidl);
2037 DWORD dwSize;
2039 if(! pdata) return 0;
2041 switch (pdata->type)
2043 case PT_VALUE:
2044 dwSize = pdata->u.file.dwFileSize;
2045 if (pOut) StrFormatByteSizeA(dwSize, pOut, uOutSize);
2046 return dwSize;
2048 if (pOut) *pOut = 0x00;
2049 return 0;
2052 BOOL _ILGetExtension (LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2054 char szTemp[MAX_PATH];
2055 const char * pPoint;
2056 LPCITEMIDLIST pidlTemp=pidl;
2058 TRACE("pidl=%p\n",pidl);
2060 if (!pidl) return FALSE;
2062 pidlTemp = ILFindLastID(pidl);
2064 if (!_ILIsValue(pidlTemp)) return FALSE;
2065 if (!_ILSimpleGetText(pidlTemp, szTemp, MAX_PATH)) return FALSE;
2067 pPoint = PathFindExtensionA(szTemp);
2069 if (! *pPoint) return FALSE;
2071 pPoint++;
2072 lstrcpynA(pOut, pPoint, uOutSize);
2073 TRACE("%s\n",pOut);
2075 return TRUE;
2078 /*************************************************************************
2079 * _ILGetFileType
2081 * Given the ItemIdList, get the file type description
2083 * PARAMS
2084 * pidl [I] The ItemIDList (simple)
2085 * pOut [I] The buffer to save the result
2086 * uOutsize [I] The size of the buffer
2088 * RETURNS
2089 * nothing
2091 * NOTES
2092 * This function copies as much as possible into the buffer.
2094 void _ILGetFileType(LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2096 if(_ILIsValue(pidl))
2098 char sTemp[64];
2099 if(uOutSize > 0)
2101 pOut[0] = 0;
2103 if (_ILGetExtension (pidl, sTemp, 64))
2105 if (!( HCR_MapTypeToValueA(sTemp, sTemp, 64, TRUE)
2106 && HCR_MapTypeToValueA(sTemp, pOut, uOutSize, FALSE )))
2108 lstrcpynA (pOut, sTemp, uOutSize - 6);
2109 strcat (pOut, "-file");
2113 else
2115 lstrcpynA(pOut, "Folder", uOutSize);
2119 /*************************************************************************
2120 * _ILGetFileAttributes
2122 * Given the ItemIdList, get the Attrib string format
2124 * PARAMS
2125 * pidl [I] The ItemIDList
2126 * pOut [I] The buffer to save the result
2127 * uOutsize [I] The size of the Buffer
2129 * RETURNS
2130 * Attributes
2132 * FIXME
2133 * return value 0 in case of error is a valid return value
2136 DWORD _ILGetFileAttributes(LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2138 LPPIDLDATA pData = _ILGetDataPointer(pidl);
2139 WORD wAttrib = 0;
2140 int i;
2142 if(! pData) return 0;
2144 switch(pData->type)
2146 case PT_FOLDER:
2147 case PT_VALUE:
2148 wAttrib = pData->u.file.uFileAttribs;
2149 break;
2152 if(uOutSize >= 6)
2154 i=0;
2155 if(wAttrib & FILE_ATTRIBUTE_READONLY)
2157 pOut[i++] = 'R';
2159 if(wAttrib & FILE_ATTRIBUTE_HIDDEN)
2161 pOut[i++] = 'H';
2163 if(wAttrib & FILE_ATTRIBUTE_SYSTEM)
2165 pOut[i++] = 'S';
2167 if(wAttrib & FILE_ATTRIBUTE_ARCHIVE)
2169 pOut[i++] = 'A';
2171 if(wAttrib & FILE_ATTRIBUTE_COMPRESSED)
2173 pOut[i++] = 'C';
2175 pOut[i] = 0x00;
2177 return wAttrib;
2180 /*************************************************************************
2181 * ILFreeaPidl
2183 * free a aPidl struct
2185 void _ILFreeaPidl(LPITEMIDLIST * apidl, UINT cidl)
2187 UINT i;
2189 if (apidl)
2191 for (i = 0; i < cidl; i++) SHFree(apidl[i]);
2192 SHFree(apidl);
2196 /*************************************************************************
2197 * ILCopyaPidl
2199 * copies an aPidl struct
2201 LPITEMIDLIST* _ILCopyaPidl(LPCITEMIDLIST * apidlsrc, UINT cidl)
2203 UINT i;
2204 LPITEMIDLIST * apidldest = (LPITEMIDLIST*)SHAlloc(cidl * sizeof(LPITEMIDLIST));
2205 if(!apidlsrc) return NULL;
2207 for (i = 0; i < cidl; i++)
2208 apidldest[i] = ILClone(apidlsrc[i]);
2210 return apidldest;
2213 /*************************************************************************
2214 * _ILCopyCidaToaPidl
2216 * creates aPidl from CIDA
2218 LPITEMIDLIST* _ILCopyCidaToaPidl(LPITEMIDLIST* pidl, LPIDA cida)
2220 UINT i;
2221 LPITEMIDLIST * dst = (LPITEMIDLIST*)SHAlloc(cida->cidl * sizeof(LPITEMIDLIST));
2223 if(!dst) return NULL;
2225 if (pidl)
2226 *pidl = ILClone((LPITEMIDLIST)(&((LPBYTE)cida)[cida->aoffset[0]]));
2228 for (i = 0; i < cida->cidl; i++)
2229 dst[i] = ILClone((LPITEMIDLIST)(&((LPBYTE)cida)[cida->aoffset[i + 1]]));
2231 return dst;