Merge r55012 adding Wine3D control panel as per Amine's request.
[reactos.git] / dll / win32 / shell32 / pidl.cpp
1 /*
2 * pidl Handling
3 *
4 * Copyright 1998 Juergen Schmied
5 *
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.
10 *
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.
15 *
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
19 *
20 * NOTES
21 * a pidl == NULL means desktop and is legal
22 *
23 */
24
25 #include <precomp.h>
26
27 WINE_DEFAULT_DEBUG_CHANNEL(pidl);
28 WINE_DECLARE_DEBUG_CHANNEL(shell);
29
30 /* from comctl32.dll */
31 EXTERN_C LPVOID WINAPI Alloc(INT);
32 EXTERN_C BOOL WINAPI Free(LPVOID);
33
34 static LPSTR _ILGetSTextPointer(LPCITEMIDLIST pidl);
35 static LPWSTR _ILGetTextPointerW(LPCITEMIDLIST pidl);
36
37 /*************************************************************************
38 * ILGetDisplayNameExA [SHELL32.186]
39 *
40 * Retrieves the display name of an ItemIDList
41 *
42 * PARAMS
43 * psf [I] Shell Folder to start with, if NULL the desktop is used
44 * pidl [I] ItemIDList relative to the psf to get the display name for
45 * path [O] Filled in with the display name, assumed to be at least MAX_PATH long
46 * type [I] Type of display name to retrieve
47 * 0 = SHGDN_FORPARSING | SHGDN_FORADDRESSBAR uses always the desktop as root
48 * 1 = SHGDN_NORMAL relative to the root folder
49 * 2 = SHGDN_INFOLDER relative to the root folder, only the last name
50 *
51 * RETURNS
52 * True if the display name could be retrieved successfully, False otherwise
53 */
54 static BOOL ILGetDisplayNameExA(LPSHELLFOLDER psf, LPCITEMIDLIST pidl, LPSTR path, DWORD type)
55 {
56 BOOL ret = FALSE;
57 WCHAR wPath[MAX_PATH];
58
59 TRACE("%p %p %p %d\n", psf, pidl, path, type);
60
61 if (!pidl || !path)
62 return FALSE;
63
64 ret = ILGetDisplayNameExW(psf, pidl, wPath, type);
65 WideCharToMultiByte(CP_ACP, 0, wPath, -1, path, MAX_PATH, NULL, NULL);
66 TRACE("%p %p %s\n", psf, pidl, debugstr_a(path));
67
68 return ret;
69 }
70
71 BOOL WINAPI ILGetDisplayNameExW(LPSHELLFOLDER psf, LPCITEMIDLIST pidl, LPWSTR path, DWORD type)
72 {
73 CComPtr<IShellFolder> psfParent;
74 LPSHELLFOLDER lsf = psf;
75 HRESULT ret = NO_ERROR;
76 LPCITEMIDLIST pidllast;
77 STRRET strret;
78 DWORD flag;
79
80 TRACE("%p %p %p %d\n", psf, pidl, path, type);
81
82 if (!pidl || !path)
83 return FALSE;
84
85 if (!lsf)
86 {
87 ret = SHGetDesktopFolder(&lsf);
88 if (FAILED(ret))
89 return FALSE;
90 }
91
92 if (type <= 2)
93 {
94 switch (type)
95 {
96 case ILGDN_FORPARSING:
97 flag = SHGDN_FORPARSING | SHGDN_FORADDRESSBAR;
98 break;
99 case ILGDN_NORMAL:
100 flag = SHGDN_NORMAL;
101 break;
102 case ILGDN_INFOLDER:
103 flag = SHGDN_INFOLDER;
104 break;
105 default:
106 FIXME("Unknown type parameter = %x\n", type);
107 flag = SHGDN_FORPARSING | SHGDN_FORADDRESSBAR;
108 break;
109 }
110 if (!*(const WORD*)pidl || type == ILGDN_FORPARSING)
111 {
112 ret = lsf->GetDisplayNameOf(pidl, flag, &strret);
113 if (SUCCEEDED(ret))
114 {
115 if(!StrRetToStrNW(path, MAX_PATH, &strret, pidl))
116 ret = E_FAIL;
117 }
118 }
119 else
120 {
121 ret = SHBindToParent(pidl, IID_IShellFolder, (LPVOID*)&psfParent, &pidllast);
122 if (SUCCEEDED(ret))
123 {
124 ret = psfParent->GetDisplayNameOf(pidllast, flag, &strret);
125 if (SUCCEEDED(ret))
126 {
127 if(!StrRetToStrNW(path, MAX_PATH, &strret, pidllast))
128 ret = E_FAIL;
129 }
130 }
131 }
132 }
133
134 TRACE("%p %p %s\n", psf, pidl, debugstr_w(path));
135
136 if (!psf)
137 lsf->Release();
138 return SUCCEEDED(ret);
139 }
140
141 /*************************************************************************
142 * ILGetDisplayNameEx [SHELL32.186]
143 */
144 BOOL WINAPI ILGetDisplayNameEx(LPSHELLFOLDER psf, LPCITEMIDLIST pidl, LPVOID path, DWORD type)
145 {
146 TRACE_(shell)("%p %p %p %d\n", psf, pidl, path, type);
147
148 if (SHELL_OsIsUnicode())
149 return ILGetDisplayNameExW(psf, pidl, (LPWSTR)path, type);
150 return ILGetDisplayNameExA(psf, pidl, (LPSTR)path, type);
151 }
152
153 /*************************************************************************
154 * ILGetDisplayName [SHELL32.15]
155 */
156 BOOL WINAPI ILGetDisplayName(LPCITEMIDLIST pidl, LPVOID path)
157 {
158 TRACE_(shell)("%p %p\n", pidl, path);
159
160 if (SHELL_OsIsUnicode())
161 return ILGetDisplayNameExW(NULL, pidl, (LPWSTR)path, ILGDN_FORPARSING);
162 return ILGetDisplayNameExA(NULL, pidl, (LPSTR)path, ILGDN_FORPARSING);
163 }
164
165 /*************************************************************************
166 * ILFindLastID [SHELL32.16]
167 *
168 * NOTES
169 * observed: pidl=Desktop return=pidl
170 */
171 LPITEMIDLIST WINAPI ILFindLastID(LPCITEMIDLIST pidl)
172 {
173 LPCITEMIDLIST pidlLast = pidl;
174
175 TRACE("(pidl=%p)\n", pidl);
176
177 if (!pidl)
178 return NULL;
179
180 while (pidl->mkid.cb)
181 {
182 pidlLast = pidl;
183 pidl = ILGetNext(pidl);
184 }
185 return (LPITEMIDLIST)pidlLast;
186 }
187
188 /*************************************************************************
189 * ILRemoveLastID [SHELL32.17]
190 *
191 * NOTES
192 * when pidl=Desktop return=FALSE
193 */
194 BOOL WINAPI ILRemoveLastID(LPITEMIDLIST pidl)
195 {
196 TRACE_(shell)("pidl=%p\n", pidl);
197
198 if (!pidl || !pidl->mkid.cb)
199 return 0;
200 ILFindLastID(pidl)->mkid.cb = 0;
201 return 1;
202 }
203
204 /*************************************************************************
205 * ILClone [SHELL32.18]
206 *
207 * NOTES
208 * duplicate an idlist
209 */
210 LPITEMIDLIST WINAPI ILClone (LPCITEMIDLIST pidl)
211 {
212 DWORD len;
213 LPITEMIDLIST newpidl;
214
215 if (!pidl)
216 return NULL;
217
218 len = ILGetSize(pidl);
219 newpidl = (LPITEMIDLIST)SHAlloc(len);
220 if (newpidl)
221 memcpy(newpidl, pidl, len);
222
223 TRACE("pidl=%p newpidl=%p\n", pidl, newpidl);
224 pdump(pidl);
225
226 return newpidl;
227 }
228
229 /*************************************************************************
230 * ILCloneFirst [SHELL32.19]
231 *
232 * NOTES
233 * duplicates the first idlist of a complex pidl
234 */
235 LPITEMIDLIST WINAPI ILCloneFirst(LPCITEMIDLIST pidl)
236 {
237 DWORD len;
238 LPITEMIDLIST pidlNew = NULL;
239
240 TRACE("pidl=%p\n", pidl);
241 pdump(pidl);
242
243 if (pidl)
244 {
245 len = pidl->mkid.cb;
246 pidlNew = (LPITEMIDLIST)SHAlloc(len + 2);
247 if (pidlNew)
248 {
249 memcpy(pidlNew, pidl, len + 2); /* 2 -> mind a desktop pidl */
250
251 if (len)
252 ILGetNext(pidlNew)->mkid.cb = 0x00;
253 }
254 }
255 TRACE("-- newpidl=%p\n", pidlNew);
256
257 return pidlNew;
258 }
259
260 /*************************************************************************
261 * ILLoadFromStream (SHELL32.26)
262 *
263 * NOTES
264 * the first two bytes are the len, the pidl is following then
265 */
266 HRESULT WINAPI ILLoadFromStream (IStream * pStream, LPITEMIDLIST * ppPidl)
267 {
268 WORD wLen = 0;
269 DWORD dwBytesRead;
270 HRESULT ret = E_FAIL;
271
272
273 TRACE_(shell)("%p %p\n", pStream , ppPidl);
274
275 SHFree(*ppPidl);
276 *ppPidl = NULL;
277
278 pStream->AddRef ();
279
280 if (SUCCEEDED(pStream->Read(&wLen, 2, &dwBytesRead)))
281 {
282 TRACE("PIDL length is %d\n", wLen);
283 if (wLen != 0)
284 {
285 *ppPidl = (LPITEMIDLIST)SHAlloc(wLen);
286 if (SUCCEEDED(pStream->Read(*ppPidl , wLen, &dwBytesRead)))
287 {
288 TRACE("Stream read OK\n");
289 ret = S_OK;
290 }
291 else
292 {
293 WARN("reading pidl failed\n");
294 SHFree(*ppPidl);
295 *ppPidl = NULL;
296 }
297 }
298 else
299 {
300 *ppPidl = NULL;
301 ret = S_OK;
302 }
303 }
304
305 /* we are not yet fully compatible */
306 if (*ppPidl && !pcheck(*ppPidl))
307 {
308 WARN("Check failed\n");
309 SHFree(*ppPidl);
310 *ppPidl = NULL;
311 }
312
313 pStream->Release ();
314 TRACE("done\n");
315 return ret;
316 }
317
318 /*************************************************************************
319 * ILSaveToStream (SHELL32.27)
320 *
321 * NOTES
322 * the first two bytes are the len, the pidl is following then
323 */
324 HRESULT WINAPI ILSaveToStream (IStream * pStream, LPCITEMIDLIST pPidl)
325 {
326 WORD wLen = 0;
327 HRESULT ret = E_FAIL;
328
329 TRACE_(shell)("%p %p\n", pStream, pPidl);
330
331 pStream->AddRef ();
332
333 wLen = ILGetSize(pPidl);
334
335 if (SUCCEEDED(pStream->Write(&wLen, 2, NULL)))
336 {
337 if (SUCCEEDED(pStream->Write(pPidl, wLen, NULL)))
338 ret = S_OK;
339 }
340 pStream->Release ();
341
342 return ret;
343 }
344
345 /*************************************************************************
346 * SHILCreateFromPath [SHELL32.28]
347 *
348 * Create an ItemIDList from a path
349 *
350 * PARAMS
351 * path [I]
352 * ppidl [O]
353 * attributes [I/O] requested attributes on call and actual attributes when
354 * the function returns
355 *
356 * RETURNS
357 * NO_ERROR if successful, or an OLE errer code otherwise
358 *
359 * NOTES
360 * Wrapper for IShellFolder_ParseDisplayName().
361 */
362 HRESULT WINAPI SHILCreateFromPathA(LPCSTR path, LPITEMIDLIST * ppidl, DWORD * attributes)
363 {
364 WCHAR lpszDisplayName[MAX_PATH];
365
366 TRACE_(shell)("%s %p 0x%08x\n", path, ppidl, attributes ? *attributes : 0);
367
368 if (!MultiByteToWideChar(CP_ACP, 0, path, -1, lpszDisplayName, MAX_PATH))
369 lpszDisplayName[MAX_PATH-1] = 0;
370
371 return SHILCreateFromPathW(lpszDisplayName, ppidl, attributes);
372 }
373
374 HRESULT WINAPI SHILCreateFromPathW(LPCWSTR path, LPITEMIDLIST * ppidl, DWORD * attributes)
375 {
376 CComPtr<IShellFolder> sf;
377 DWORD pchEaten;
378 HRESULT ret = E_FAIL;
379
380 TRACE_(shell)("%s %p 0x%08x\n", debugstr_w(path), ppidl, attributes ? *attributes : 0);
381
382 if (SUCCEEDED (SHGetDesktopFolder(&sf)))
383 ret = sf->ParseDisplayName(0, NULL, (LPWSTR)path, &pchEaten, ppidl, attributes);
384 return ret;
385 }
386
387 EXTERN_C HRESULT WINAPI SHILCreateFromPathAW (LPCVOID path, LPITEMIDLIST * ppidl, DWORD * attributes)
388 {
389 if ( SHELL_OsIsUnicode())
390 return SHILCreateFromPathW ((LPCWSTR)path, ppidl, attributes);
391 return SHILCreateFromPathA ((LPCSTR)path, ppidl, attributes);
392 }
393
394 /*************************************************************************
395 * SHCloneSpecialIDList [SHELL32.89]
396 *
397 * Create an ItemIDList to one of the special folders.
398
399 * PARAMS
400 * hwndOwner [in]
401 * nFolder [in] CSIDL_xxxxx
402 * fCreate [in] Create folder if it does not exist
403 *
404 * RETURNS
405 * Success: The newly created pidl
406 * Failure: NULL, if inputs are invalid.
407 *
408 * NOTES
409 * exported by ordinal.
410 * Caller is responsible for deallocating the returned ItemIDList with the
411 * shells IMalloc interface, aka ILFree.
412 */
413 LPITEMIDLIST WINAPI SHCloneSpecialIDList(HWND hwndOwner, int nFolder, BOOL fCreate)
414 {
415 LPITEMIDLIST ppidl;
416 TRACE_(shell)("(hwnd=%p,csidl=0x%x,%s).\n", hwndOwner, nFolder, fCreate ? "T" : "F");
417
418 if (fCreate)
419 nFolder |= CSIDL_FLAG_CREATE;
420
421 SHGetSpecialFolderLocation(hwndOwner, nFolder, &ppidl);
422 return ppidl;
423 }
424
425 /*************************************************************************
426 * ILGlobalClone [SHELL32.20]
427 *
428 * Clones an ItemIDList using Alloc.
429 *
430 * PARAMS
431 * pidl [I] ItemIDList to clone
432 *
433 * RETURNS
434 * Newly allocated ItemIDList.
435 *
436 * NOTES
437 * exported by ordinal.
438 */
439 LPITEMIDLIST WINAPI ILGlobalClone(LPCITEMIDLIST pidl)
440 {
441 DWORD len;
442 LPITEMIDLIST newpidl;
443
444 if (!pidl)
445 return NULL;
446
447 len = ILGetSize(pidl);
448 newpidl = (LPITEMIDLIST)Alloc(len);
449 if (newpidl)
450 memcpy(newpidl, pidl, len);
451
452 TRACE("pidl=%p newpidl=%p\n", pidl, newpidl);
453 pdump(pidl);
454
455 return newpidl;
456 }
457
458 /*************************************************************************
459 * ILIsEqual [SHELL32.21]
460 *
461 */
462 BOOL WINAPI ILIsEqual(LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
463 {
464 char szData1[MAX_PATH];
465 char szData2[MAX_PATH];
466
467 LPCITEMIDLIST pidltemp1 = pidl1;
468 LPCITEMIDLIST pidltemp2 = pidl2;
469
470 TRACE("pidl1=%p pidl2=%p\n", pidl1, pidl2);
471
472 /*
473 * Explorer reads from registry directly (StreamMRU),
474 * so we can only check here
475 */
476 if (!pcheck(pidl1) || !pcheck (pidl2))
477 return FALSE;
478
479 pdump (pidl1);
480 pdump (pidl2);
481
482 if (!pidl1 || !pidl2)
483 return FALSE;
484
485 while (pidltemp1->mkid.cb && pidltemp2->mkid.cb)
486 {
487 _ILSimpleGetText(pidltemp1, szData1, MAX_PATH);
488 _ILSimpleGetText(pidltemp2, szData2, MAX_PATH);
489
490 if (strcmp( szData1, szData2 ))
491 return FALSE;
492
493 pidltemp1 = ILGetNext(pidltemp1);
494 pidltemp2 = ILGetNext(pidltemp2);
495 }
496
497 if (!pidltemp1->mkid.cb && !pidltemp2->mkid.cb)
498 return TRUE;
499
500 return FALSE;
501 }
502
503 /*************************************************************************
504 * ILIsParent [SHELL32.23]
505 *
506 * Verifies that pidlParent is indeed the (immediate) parent of pidlChild.
507 *
508 * PARAMS
509 * pidlParent [I]
510 * pidlChild [I]
511 * bImmediate [I] only return true if the parent is the direct parent
512 * of the child
513 *
514 * RETURNS
515 * True if the parent ItemIDlist is a complete part of the child ItemIdList,
516 * False otherwise.
517 *
518 * NOTES
519 * parent = a/b, child = a/b/c -> true, c is in folder a/b
520 * child = a/b/c/d -> false if bImmediate is true, d is not in folder a/b
521 * child = a/b/c/d -> true if bImmediate is false, d is in a subfolder of a/b
522 */
523 BOOL WINAPI ILIsParent(LPCITEMIDLIST pidlParent, LPCITEMIDLIST pidlChild, BOOL bImmediate)
524 {
525 char szData1[MAX_PATH];
526 char szData2[MAX_PATH];
527 LPCITEMIDLIST pParent = pidlParent;
528 LPCITEMIDLIST pChild = pidlChild;
529
530 TRACE("%p %p %x\n", pidlParent, pidlChild, bImmediate);
531
532 if (!pParent || !pChild)
533 return FALSE;
534
535 while (pParent->mkid.cb && pChild->mkid.cb)
536 {
537 _ILSimpleGetText(pParent, szData1, MAX_PATH);
538 _ILSimpleGetText(pChild, szData2, MAX_PATH);
539
540 if (strcmp( szData1, szData2 ))
541 return FALSE;
542
543 pParent = ILGetNext(pParent);
544 pChild = ILGetNext(pChild);
545 }
546
547 /* child shorter or has equal length to parent */
548 if (pParent->mkid.cb || !pChild->mkid.cb)
549 return FALSE;
550
551 /* not immediate descent */
552 if ( ILGetNext(pChild)->mkid.cb && bImmediate)
553 return FALSE;
554
555 return TRUE;
556 }
557
558 /*************************************************************************
559 * ILFindChild [SHELL32.24]
560 *
561 * Compares elements from pidl1 and pidl2.
562 *
563 * PARAMS
564 * pidl1 [I]
565 * pidl2 [I]
566 *
567 * RETURNS
568 * pidl1 is desktop pidl2
569 * pidl1 shorter pidl2 pointer to first different element of pidl2
570 * if there was at least one equal element
571 * pidl2 shorter pidl1 0
572 * pidl2 equal pidl1 pointer to last 0x00-element of pidl2
573 *
574 * NOTES
575 * exported by ordinal.
576 */
577 LPITEMIDLIST WINAPI ILFindChild(LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
578 {
579 char szData1[MAX_PATH];
580 char szData2[MAX_PATH];
581
582 LPCITEMIDLIST pidltemp1 = pidl1;
583 LPCITEMIDLIST pidltemp2 = pidl2;
584 LPCITEMIDLIST ret = NULL;
585
586 TRACE("pidl1=%p pidl2=%p\n", pidl1, pidl2);
587
588 /* explorer reads from registry directly (StreamMRU),
589 so we can only check here */
590 if ((!pcheck (pidl1)) || (!pcheck (pidl2)))
591 return FALSE;
592
593 pdump (pidl1);
594 pdump (pidl2);
595
596 if (_ILIsDesktop(pidl1))
597 {
598 ret = pidl2;
599 }
600 else
601 {
602 while (pidltemp1->mkid.cb && pidltemp2->mkid.cb)
603 {
604 _ILSimpleGetText(pidltemp1, szData1, MAX_PATH);
605 _ILSimpleGetText(pidltemp2, szData2, MAX_PATH);
606
607 if (strcmp(szData1, szData2))
608 break;
609
610 pidltemp1 = ILGetNext(pidltemp1);
611 pidltemp2 = ILGetNext(pidltemp2);
612 ret = pidltemp2;
613 }
614
615 if (pidltemp1->mkid.cb)
616 ret = NULL; /* elements of pidl1 left*/
617 }
618 TRACE_(shell)("--- %p\n", ret);
619 return (LPITEMIDLIST)ret; /* pidl 1 is shorter */
620 }
621
622 /*************************************************************************
623 * ILCombine [SHELL32.25]
624 *
625 * Concatenates two complex ItemIDLists.
626 *
627 * PARAMS
628 * pidl1 [I] first complex ItemIDLists
629 * pidl2 [I] complex ItemIDLists to append
630 *
631 * RETURNS
632 * if both pidl's == NULL NULL
633 * if pidl1 == NULL cloned pidl2
634 * if pidl2 == NULL cloned pidl1
635 * otherwise new pidl with pidl2 appended to pidl1
636 *
637 * NOTES
638 * exported by ordinal.
639 * Does not destroy the passed in ItemIDLists!
640 */
641 LPITEMIDLIST WINAPI ILCombine(LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
642 {
643 DWORD len1, len2;
644 LPITEMIDLIST pidlNew;
645
646 TRACE("pidl=%p pidl=%p\n", pidl1, pidl2);
647
648 if (!pidl1 && !pidl2) return NULL;
649
650 pdump (pidl1);
651 pdump (pidl2);
652
653 if (!pidl1)
654 {
655 pidlNew = ILClone(pidl2);
656 return pidlNew;
657 }
658
659 if (!pidl2)
660 {
661 pidlNew = ILClone(pidl1);
662 return pidlNew;
663 }
664
665 len1 = ILGetSize(pidl1) - 2;
666 len2 = ILGetSize(pidl2);
667 pidlNew = (LPITEMIDLIST)SHAlloc(len1 + len2);
668
669 if (pidlNew)
670 {
671 memcpy(pidlNew, pidl1, len1);
672 memcpy(((BYTE *)pidlNew) + len1, pidl2, len2);
673 }
674
675 /* TRACE(pidl,"--new pidl=%p\n",pidlNew);*/
676 return pidlNew;
677 }
678
679 /*************************************************************************
680 * SHGetRealIDL [SHELL32.98]
681 *
682 * NOTES
683 */
684 HRESULT WINAPI SHGetRealIDL(LPSHELLFOLDER lpsf, LPCITEMIDLIST pidlSimple, LPITEMIDLIST *pidlReal)
685 {
686 CComPtr<IDataObject> pDataObj;
687 HRESULT hr;
688
689 hr = lpsf->GetUIObjectOf(0, 1, &pidlSimple,
690 IID_IDataObject, 0, (LPVOID*)&pDataObj);
691 if (SUCCEEDED(hr))
692 {
693 STGMEDIUM medium;
694 FORMATETC fmt;
695
696 fmt.cfFormat = RegisterClipboardFormatW(CFSTR_SHELLIDLIST);
697 fmt.ptd = NULL;
698 fmt.dwAspect = DVASPECT_CONTENT;
699 fmt.lindex = -1;
700 fmt.tymed = TYMED_HGLOBAL;
701
702 hr = pDataObj->GetData(&fmt, &medium);
703
704 if (SUCCEEDED(hr))
705 {
706 /*assert(pida->cidl==1);*/
707 LPIDA pida = (LPIDA)GlobalLock(medium.hGlobal);
708
709 LPCITEMIDLIST pidl_folder = (LPCITEMIDLIST) ((LPBYTE)pida + pida->aoffset[0]);
710 LPCITEMIDLIST pidl_child = (LPCITEMIDLIST) ((LPBYTE)pida + pida->aoffset[1]);
711
712 *pidlReal = ILCombine(pidl_folder, pidl_child);
713
714 if (!*pidlReal)
715 hr = E_OUTOFMEMORY;
716
717 GlobalUnlock(medium.hGlobal);
718 GlobalFree(medium.hGlobal);
719 }
720 }
721
722 return hr;
723 }
724
725 /*************************************************************************
726 * SHLogILFromFSIL [SHELL32.95]
727 *
728 * NOTES
729 * pild = CSIDL_DESKTOP ret = 0
730 * pild = CSIDL_DRIVES ret = 0
731 */
732 EXTERN_C LPITEMIDLIST WINAPI SHLogILFromFSIL(LPITEMIDLIST pidl)
733 {
734 FIXME("(pidl=%p)\n", pidl);
735
736 pdump(pidl);
737
738 return 0;
739 }
740
741 /*************************************************************************
742 * ILGetSize [SHELL32.152]
743 *
744 * Gets the byte size of an ItemIDList including zero terminator
745 *
746 * PARAMS
747 * pidl [I] ItemIDList
748 *
749 * RETURNS
750 * size of pidl in bytes
751 *
752 * NOTES
753 * exported by ordinal
754 */
755 UINT WINAPI ILGetSize(LPCITEMIDLIST pidl)
756 {
757 LPCSHITEMID si = &(pidl->mkid);
758 UINT len = 0;
759
760 if (pidl)
761 {
762 while (si->cb)
763 {
764 len += si->cb;
765 si = (LPCSHITEMID)(((const BYTE*)si) + si->cb);
766 }
767 len += 2;
768 }
769 TRACE("pidl=%p size=%u\n", pidl, len);
770 return len;
771 }
772
773 /*************************************************************************
774 * ILGetNext [SHELL32.153]
775 *
776 * Gets the next ItemID of an ItemIDList
777 *
778 * PARAMS
779 * pidl [I] ItemIDList
780 *
781 * RETURNS
782 * null -> null
783 * desktop -> null
784 * simple pidl -> pointer to 0x0000 element
785 *
786 * NOTES
787 * exported by ordinal.
788 */
789 LPITEMIDLIST WINAPI ILGetNext(LPCITEMIDLIST pidl)
790 {
791 WORD len;
792
793 TRACE("%p\n", pidl);
794
795 if (pidl)
796 {
797 len = pidl->mkid.cb;
798 if (len)
799 {
800 pidl = (LPCITEMIDLIST) (((const BYTE*)pidl) + len);
801 TRACE("-- %p\n", pidl);
802 return (LPITEMIDLIST)pidl;
803 }
804 }
805 return NULL;
806 }
807
808 /*************************************************************************
809 * ILAppend [SHELL32.154]
810 *
811 * Adds the single ItemID item to the ItemIDList indicated by pidl.
812 * If bEnd is FALSE, inserts the item in the front of the list,
813 * otherwise it adds the item to the end. (???)
814 *
815 * PARAMS
816 * pidl [I] ItemIDList to extend
817 * item [I] ItemID to prepend/append
818 * bEnd [I] Indicates if the item should be appended
819 *
820 * NOTES
821 * Destroys the passed in idlist! (???)
822 */
823 EXTERN_C LPITEMIDLIST WINAPI ILAppend(LPITEMIDLIST pidl, LPCITEMIDLIST item, BOOL bEnd)
824 {
825 LPITEMIDLIST idlRet;
826
827 WARN("(pidl=%p,pidl=%p,%08u)semi-stub\n", pidl, item, bEnd);
828
829 pdump (pidl);
830 pdump (item);
831
832 if (_ILIsDesktop(pidl))
833 {
834 idlRet = ILClone(item);
835 SHFree (pidl);
836 return idlRet;
837 }
838
839 if (bEnd)
840 idlRet = ILCombine(pidl, item);
841 else
842 idlRet = ILCombine(item, pidl);
843
844 SHFree(pidl);
845 return idlRet;
846 }
847
848 /*************************************************************************
849 * ILFree [SHELL32.155]
850 *
851 * Frees memory (if not NULL) allocated by SHMalloc allocator
852 *
853 * PARAMS
854 * pidl [I]
855 *
856 * RETURNS
857 * Nothing
858 *
859 * NOTES
860 * exported by ordinal
861 */
862 void WINAPI ILFree(LPITEMIDLIST pidl)
863 {
864 TRACE("(pidl=%p)\n", pidl);
865 SHFree(pidl);
866 }
867
868 /*************************************************************************
869 * ILGlobalFree [SHELL32.156]
870 *
871 * Frees memory (if not NULL) allocated by Alloc allocator
872 *
873 * PARAMS
874 * pidl [I]
875 *
876 * RETURNS
877 * Nothing
878 *
879 * NOTES
880 * exported by ordinal.
881 */
882 void WINAPI ILGlobalFree( LPITEMIDLIST pidl)
883 {
884 TRACE("%p\n", pidl);
885
886 Free(pidl);
887 }
888
889 /*************************************************************************
890 * ILCreateFromPathA [SHELL32.189]
891 *
892 * Creates a complex ItemIDList from a path and returns it.
893 *
894 * PARAMS
895 * path [I]
896 *
897 * RETURNS
898 * the newly created complex ItemIDList or NULL if failed
899 *
900 * NOTES
901 * exported by ordinal.
902 */
903 LPITEMIDLIST WINAPI ILCreateFromPathA (LPCSTR path)
904 {
905 LPITEMIDLIST pidlnew = NULL;
906
907 TRACE_(shell)("%s\n", debugstr_a(path));
908
909 if (SUCCEEDED(SHILCreateFromPathA(path, &pidlnew, NULL)))
910 return pidlnew;
911 return NULL;
912 }
913
914 /*************************************************************************
915 * ILCreateFromPathW [SHELL32.190]
916 *
917 * See ILCreateFromPathA.
918 */
919 LPITEMIDLIST WINAPI ILCreateFromPathW (LPCWSTR path)
920 {
921 LPITEMIDLIST pidlnew = NULL;
922
923 TRACE_(shell)("%s\n", debugstr_w(path));
924
925 if (SUCCEEDED(SHILCreateFromPathW(path, &pidlnew, NULL)))
926 return pidlnew;
927 return NULL;
928 }
929
930 /*************************************************************************
931 * ILCreateFromPath [SHELL32.157]
932 */
933 EXTERN_C LPITEMIDLIST WINAPI ILCreateFromPathAW (LPCVOID path)
934 {
935 if ( SHELL_OsIsUnicode())
936 return ILCreateFromPathW ((LPCWSTR)path);
937 return ILCreateFromPathA ((LPCSTR)path);
938 }
939
940 /*************************************************************************
941 * _ILParsePathW [internal]
942 *
943 * Creates an ItemIDList from a path and returns it.
944 *
945 * PARAMS
946 * path [I] path to parse and convert into an ItemIDList
947 * lpFindFile [I] pointer to buffer to initialize the FileSystem
948 * Bind Data object with
949 * bBindCtx [I] indicates to create a BindContext and assign a
950 * FileSystem Bind Data object
951 * ppidl [O] the newly create ItemIDList
952 * prgfInOut [I/O] requested attributes on input and actual
953 * attributes on return
954 *
955 * RETURNS
956 * NO_ERROR on success or an OLE error code
957 *
958 * NOTES
959 * If either lpFindFile is non-NULL or bBindCtx is TRUE, this function
960 * creates a BindContext object and assigns a FileSystem Bind Data object
961 * to it, passing the BindContext to IShellFolder_ParseDisplayName. Each
962 * IShellFolder uses that FileSystem Bind Data object of the BindContext
963 * to pass data about the current path element to the next object. This
964 * is used to avoid having to verify the current path element on disk, so
965 * that creating an ItemIDList from a nonexistent path still can work.
966 */
967 static HRESULT _ILParsePathW(LPCWSTR path, LPWIN32_FIND_DATAW lpFindFile,
968 BOOL bBindCtx, LPITEMIDLIST *ppidl, LPDWORD prgfInOut)
969 {
970 CComPtr<IShellFolder> pSF;
971 CComPtr<IBindCtx> pBC;
972 HRESULT ret;
973
974 TRACE("%s %p %d (%p)->%p (%p)->0x%x\n", debugstr_w(path), lpFindFile, bBindCtx,
975 ppidl, ppidl ? *ppidl : NULL,
976 prgfInOut, prgfInOut ? *prgfInOut : 0);
977
978 ret = SHGetDesktopFolder(&pSF);
979 if (FAILED(ret))
980 return ret;
981
982 if (lpFindFile || bBindCtx)
983 ret = IFileSystemBindData_Constructor(lpFindFile, &pBC);
984
985 if (SUCCEEDED(ret))
986 {
987 ret = pSF->ParseDisplayName(0, pBC, (LPOLESTR)path, NULL, ppidl, prgfInOut);
988 }
989
990 if (FAILED(ret) && ppidl)
991 *ppidl = NULL;
992
993 TRACE("%s %p 0x%x\n", debugstr_w(path), ppidl ? *ppidl : NULL, prgfInOut ? *prgfInOut : 0);
994
995 return ret;
996 }
997
998 /*************************************************************************
999 * SHSimpleIDListFromPath [SHELL32.162]
1000 *
1001 * Creates a simple ItemIDList from a path and returns it. This function
1002 * does not fail on nonexistent paths.
1003 *
1004 * PARAMS
1005 * path [I] path to parse and convert into an ItemIDList
1006 *
1007 * RETURNS
1008 * the newly created simple ItemIDList
1009 *
1010 * NOTES
1011 * Simple in the name does not mean a relative ItemIDList but rather a
1012 * fully qualified list, where only the file name is filled in and the
1013 * directory flag for those ItemID elements this is known about, eg.
1014 * it is not the last element in the ItemIDList or the actual directory
1015 * exists on disk.
1016 * exported by ordinal.
1017 */
1018 LPITEMIDLIST WINAPI SHSimpleIDListFromPathA(LPCSTR lpszPath)
1019 {
1020 LPITEMIDLIST pidl = NULL;
1021 LPWSTR wPath = NULL;
1022 int len;
1023
1024 TRACE("%s\n", debugstr_a(lpszPath));
1025
1026 if (lpszPath)
1027 {
1028 len = MultiByteToWideChar(CP_ACP, 0, lpszPath, -1, NULL, 0);
1029 wPath = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1030 MultiByteToWideChar(CP_ACP, 0, lpszPath, -1, wPath, len);
1031 }
1032
1033 _ILParsePathW(wPath, NULL, TRUE, &pidl, NULL);
1034
1035 HeapFree(GetProcessHeap(), 0, wPath);
1036 TRACE("%s %p\n", debugstr_a(lpszPath), pidl);
1037 return pidl;
1038 }
1039
1040 LPITEMIDLIST WINAPI SHSimpleIDListFromPathW(LPCWSTR lpszPath)
1041 {
1042 LPITEMIDLIST pidl = NULL;
1043
1044 TRACE("%s\n", debugstr_w(lpszPath));
1045
1046 _ILParsePathW(lpszPath, NULL, TRUE, &pidl, NULL);
1047 TRACE("%s %p\n", debugstr_w(lpszPath), pidl);
1048 return pidl;
1049 }
1050
1051 EXTERN_C LPITEMIDLIST WINAPI SHSimpleIDListFromPathAW(LPCVOID lpszPath)
1052 {
1053 if ( SHELL_OsIsUnicode())
1054 return SHSimpleIDListFromPathW ((LPCWSTR)lpszPath);
1055 return SHSimpleIDListFromPathA ((LPCSTR)lpszPath);
1056 }
1057
1058 /*************************************************************************
1059 * SHGetDataFromIDListA [SHELL32.247]
1060 *
1061 * NOTES
1062 * the pidl can be a simple one. since we can't get the path out of the pidl
1063 * we have to take all data from the pidl
1064 */
1065 HRESULT WINAPI SHGetDataFromIDListA(LPSHELLFOLDER psf, LPCITEMIDLIST pidl,
1066 int nFormat, LPVOID dest, int len)
1067 {
1068 LPSTR filename, shortname;
1069 WIN32_FIND_DATAA * pfd;
1070
1071 TRACE_(shell)("sf=%p pidl=%p 0x%04x %p 0x%04x stub\n", psf, pidl, nFormat, dest, len);
1072
1073 pdump(pidl);
1074 if (!psf || !dest)
1075 return E_INVALIDARG;
1076
1077 switch (nFormat)
1078 {
1079 case SHGDFIL_FINDDATA:
1080 pfd = (WIN32_FIND_DATAA *)dest;
1081
1082 if (_ILIsDrive(pidl) || _ILIsSpecialFolder(pidl))
1083 return E_INVALIDARG;
1084
1085 if (len < (int)sizeof(WIN32_FIND_DATAA))
1086 return E_INVALIDARG;
1087
1088 ZeroMemory(pfd, sizeof (WIN32_FIND_DATAA));
1089 _ILGetFileDateTime( pidl, &(pfd->ftLastWriteTime));
1090 pfd->dwFileAttributes = _ILGetFileAttributes(pidl, NULL, 0);
1091 pfd->nFileSizeLow = _ILGetFileSize ( pidl, NULL, 0);
1092
1093 filename = _ILGetTextPointer(pidl);
1094 shortname = _ILGetSTextPointer(pidl);
1095
1096 if (filename)
1097 lstrcpynA(pfd->cFileName, filename, sizeof(pfd->cFileName));
1098 else
1099 pfd->cFileName[0] = '\0';
1100
1101 if (shortname)
1102 lstrcpynA(pfd->cAlternateFileName, shortname, sizeof(pfd->cAlternateFileName));
1103 else
1104 pfd->cAlternateFileName[0] = '\0';
1105 return NOERROR;
1106
1107 case SHGDFIL_NETRESOURCE:
1108 case SHGDFIL_DESCRIPTIONID:
1109 FIXME_(shell)("SHGDFIL %i stub\n", nFormat);
1110 break;
1111
1112 default:
1113 ERR_(shell)("Unknown SHGDFIL %i, please report\n", nFormat);
1114 }
1115
1116 return E_INVALIDARG;
1117 }
1118
1119 /*************************************************************************
1120 * SHGetDataFromIDListW [SHELL32.248]
1121 *
1122 */
1123 HRESULT WINAPI SHGetDataFromIDListW(LPSHELLFOLDER psf, LPCITEMIDLIST pidl,
1124 int nFormat, LPVOID dest, int len)
1125 {
1126 LPSTR filename, shortname;
1127 WIN32_FIND_DATAW * pfd = (WIN32_FIND_DATAW *)dest;
1128
1129 TRACE_(shell)("sf=%p pidl=%p 0x%04x %p 0x%04x stub\n", psf, pidl, nFormat, dest, len);
1130
1131 pdump(pidl);
1132
1133 if (!psf || !dest)
1134 return E_INVALIDARG;
1135
1136 switch (nFormat)
1137 {
1138 case SHGDFIL_FINDDATA:
1139 pfd = (WIN32_FIND_DATAW *)dest;
1140
1141 if (_ILIsDrive(pidl))
1142 return E_INVALIDARG;
1143
1144 if (len < (int)sizeof(WIN32_FIND_DATAW))
1145 return E_INVALIDARG;
1146
1147 ZeroMemory(pfd, sizeof (WIN32_FIND_DATAA));
1148 _ILGetFileDateTime( pidl, &(pfd->ftLastWriteTime));
1149 pfd->dwFileAttributes = _ILGetFileAttributes(pidl, NULL, 0);
1150 pfd->nFileSizeLow = _ILGetFileSize ( pidl, NULL, 0);
1151
1152 filename = _ILGetTextPointer(pidl);
1153 shortname = _ILGetSTextPointer(pidl);
1154
1155 if (!filename)
1156 pfd->cFileName[0] = '\0';
1157 else if (!MultiByteToWideChar(CP_ACP, 0, filename, -1, pfd->cFileName, MAX_PATH))
1158 pfd->cFileName[MAX_PATH-1] = 0;
1159
1160 if (!shortname)
1161 pfd->cAlternateFileName[0] = '\0';
1162 else if (!MultiByteToWideChar(CP_ACP, 0, shortname, -1, pfd->cAlternateFileName, 14))
1163 pfd->cAlternateFileName[13] = 0;
1164 return NOERROR;
1165
1166 case SHGDFIL_NETRESOURCE:
1167 case SHGDFIL_DESCRIPTIONID:
1168 FIXME_(shell)("SHGDFIL %i stub\n", nFormat);
1169 break;
1170
1171 default:
1172 ERR_(shell)("Unknown SHGDFIL %i, please report\n", nFormat);
1173 }
1174
1175 return E_INVALIDARG;
1176 }
1177
1178 /*************************************************************************
1179 * SHGetPathFromIDListA [SHELL32.@][NT 4.0: SHELL32.220]
1180 *
1181 * PARAMETERS
1182 * pidl, [IN] pidl
1183 * pszPath [OUT] path
1184 *
1185 * RETURNS
1186 * path from a passed PIDL.
1187 *
1188 * NOTES
1189 * NULL returns FALSE
1190 * desktop pidl gives path to desktop directory back
1191 * special pidls returning FALSE
1192 */
1193 BOOL WINAPI SHGetPathFromIDListA(LPCITEMIDLIST pidl, LPSTR pszPath)
1194 {
1195 WCHAR wszPath[MAX_PATH];
1196 BOOL bSuccess;
1197
1198 bSuccess = SHGetPathFromIDListW(pidl, wszPath);
1199 WideCharToMultiByte(CP_ACP, 0, wszPath, -1, pszPath, MAX_PATH, NULL, NULL);
1200
1201 return bSuccess;
1202 }
1203
1204 /*************************************************************************
1205 * SHGetPathFromIDListW [SHELL32.@]
1206 *
1207 * See SHGetPathFromIDListA.
1208 */
1209 BOOL WINAPI SHGetPathFromIDListW(LPCITEMIDLIST pidl, LPWSTR pszPath)
1210 {
1211 HRESULT hr;
1212 LPCITEMIDLIST pidlLast;
1213 CComPtr<IShellFolder> psfFolder;
1214 DWORD dwAttributes;
1215 STRRET strret;
1216
1217 TRACE_(shell)("(pidl=%p,%p)\n", pidl, pszPath);
1218 pdump(pidl);
1219
1220 *pszPath = '\0';
1221 if (!pidl)
1222 return FALSE;
1223
1224 hr = SHBindToParent(pidl, IID_IShellFolder, (VOID**)&psfFolder, &pidlLast);
1225 if (FAILED(hr))
1226 {
1227 ERR("SHBindToParent failed: %x\n", hr);
1228 return FALSE;
1229 }
1230
1231 dwAttributes = SFGAO_FILESYSTEM;
1232 hr = psfFolder->GetAttributesOf(1, &pidlLast, &dwAttributes);
1233 if (FAILED(hr) || !(dwAttributes & SFGAO_FILESYSTEM))
1234 {
1235 WARN("Wrong dwAttributes or GetAttributesOf failed: %x\n", hr);
1236 return FALSE;
1237 }
1238
1239 hr = psfFolder->GetDisplayNameOf(pidlLast, SHGDN_FORPARSING, &strret);
1240 if (FAILED(hr)) return FALSE;
1241
1242 hr = StrRetToBufW(&strret, pidlLast, pszPath, MAX_PATH);
1243
1244 TRACE_(shell)("-- %s, 0x%08x\n", debugstr_w(pszPath), hr);
1245 return SUCCEEDED(hr);
1246 }
1247
1248 /*************************************************************************
1249 * SHBindToParent [shell version 5.0]
1250 */
1251 HRESULT WINAPI SHBindToParent(LPCITEMIDLIST pidl, REFIID riid, LPVOID *ppv, LPCITEMIDLIST *ppidlLast)
1252 {
1253 CComPtr<IShellFolder> psfDesktop;
1254 HRESULT hr = E_FAIL;
1255
1256 TRACE_(shell)("pidl=%p\n", pidl);
1257 pdump(pidl);
1258
1259 if (!pidl || !ppv)
1260 return E_INVALIDARG;
1261
1262 *ppv = NULL;
1263 if (ppidlLast)
1264 *ppidlLast = NULL;
1265
1266 hr = SHGetDesktopFolder(&psfDesktop);
1267 if (FAILED(hr))
1268 return hr;
1269
1270 if (_ILIsPidlSimple(pidl))
1271 {
1272 /* we are on desktop level */
1273 hr = psfDesktop->QueryInterface(riid, ppv);
1274 }
1275 else
1276 {
1277 LPITEMIDLIST pidlParent = ILClone(pidl);
1278 ILRemoveLastID(pidlParent);
1279 hr = psfDesktop->BindToObject(pidlParent, NULL, riid, ppv);
1280 SHFree (pidlParent);
1281 }
1282
1283 if (SUCCEEDED(hr) && ppidlLast)
1284 *ppidlLast = ILFindLastID(pidl);
1285
1286 TRACE_(shell)("-- psf=%p pidl=%p ret=0x%08x\n", *ppv, (ppidlLast) ? *ppidlLast : NULL, hr);
1287 return hr;
1288 }
1289
1290 /**************************************************************************
1291 *
1292 * internal functions
1293 *
1294 * ### 1. section creating pidls ###
1295 *
1296 *************************************************************************
1297 */
1298
1299 /* Basic PIDL constructor. Allocates size + 5 bytes, where:
1300 * - two bytes are SHITEMID.cb
1301 * - one byte is PIDLDATA.type
1302 * - two bytes are the NULL PIDL terminator
1303 * Sets type of the returned PIDL to type.
1304 */
1305 static LPITEMIDLIST _ILAlloc(PIDLTYPE type, unsigned int size)
1306 {
1307 LPITEMIDLIST pidlOut = NULL;
1308
1309 pidlOut = (LPITEMIDLIST)SHAlloc(size + 5);
1310 if(pidlOut)
1311 {
1312 LPPIDLDATA pData;
1313 LPITEMIDLIST pidlNext;
1314
1315 ZeroMemory(pidlOut, size + 5);
1316 pidlOut->mkid.cb = size + 3;
1317
1318 pData = _ILGetDataPointer(pidlOut);
1319 if (pData)
1320 pData->type = type;
1321
1322 pidlNext = ILGetNext(pidlOut);
1323 if (pidlNext)
1324 pidlNext->mkid.cb = 0x00;
1325 TRACE("-- (pidl=%p, size=%u)\n", pidlOut, size);
1326 }
1327
1328 return pidlOut;
1329 }
1330
1331 LPITEMIDLIST _ILCreateDesktop(void)
1332 {
1333 LPITEMIDLIST ret;
1334
1335 TRACE("()\n");
1336 ret = (LPITEMIDLIST)SHAlloc(2);
1337 if (ret)
1338 ret->mkid.cb = 0;
1339 return ret;
1340 }
1341
1342 LPITEMIDLIST _ILCreateMyComputer(void)
1343 {
1344 TRACE("()\n");
1345 return _ILCreateGuid(PT_GUID, CLSID_MyComputer);
1346 }
1347
1348 LPITEMIDLIST _ILCreateMyDocuments(void)
1349 {
1350 TRACE("()\n");
1351 return _ILCreateGuid(PT_GUID, CLSID_MyDocuments);
1352 }
1353
1354 LPITEMIDLIST _ILCreateIExplore(void)
1355 {
1356 TRACE("()\n");
1357 return _ILCreateGuid(PT_GUID, CLSID_Internet);
1358 }
1359
1360 LPITEMIDLIST _ILCreateControlPanel(void)
1361 {
1362 LPITEMIDLIST parent = _ILCreateGuid(PT_GUID, CLSID_MyComputer), ret = NULL;
1363
1364 TRACE("()\n");
1365 if (parent)
1366 {
1367 LPITEMIDLIST cpl = _ILCreateGuid(PT_SHELLEXT, CLSID_ControlPanel);
1368
1369 if (cpl)
1370 {
1371 ret = ILCombine(parent, cpl);
1372 SHFree(cpl);
1373 }
1374 SHFree(parent);
1375 }
1376 return ret;
1377 }
1378
1379 LPITEMIDLIST _ILCreatePrinters(void)
1380 {
1381 LPITEMIDLIST parent = _ILCreateGuid(PT_GUID, CLSID_MyComputer), ret = NULL;
1382
1383 TRACE("()\n");
1384 if (parent)
1385 {
1386 LPITEMIDLIST printers = _ILCreateGuid(PT_YAGUID, CLSID_Printers);
1387
1388 if (printers)
1389 {
1390 ret = ILCombine(parent, printers);
1391 SHFree(printers);
1392 }
1393 SHFree(parent);
1394 }
1395 return ret;
1396 }
1397
1398 LPITEMIDLIST _ILCreateNetwork(void)
1399 {
1400 TRACE("()\n");
1401 return _ILCreateGuid(PT_GUID, CLSID_NetworkPlaces);
1402 }
1403
1404 LPITEMIDLIST _ILCreateBitBucket(void)
1405 {
1406 TRACE("()\n");
1407 return _ILCreateGuid(PT_GUID, CLSID_RecycleBin);
1408 }
1409
1410 LPITEMIDLIST _ILCreateAdminTools(void)
1411 {
1412 TRACE("()\n");
1413 return _ILCreateGuid(PT_GUID, CLSID_AdminFolderShortcut); //FIXME
1414 }
1415
1416 LPITEMIDLIST _ILCreateGuid(PIDLTYPE type, REFIID guid)
1417 {
1418 LPITEMIDLIST pidlOut;
1419
1420 if (type == PT_SHELLEXT || type == PT_GUID || type == PT_YAGUID)
1421 {
1422 pidlOut = _ILAlloc(type, sizeof(GUIDStruct));
1423 if (pidlOut)
1424 {
1425 LPPIDLDATA pData = _ILGetDataPointer(pidlOut);
1426
1427 pData->u.guid.guid = guid;
1428 TRACE("-- create GUID-pidl %s\n",
1429 debugstr_guid(&(pData->u.guid.guid)));
1430 }
1431 }
1432 else
1433 {
1434 WARN("%d: invalid type for GUID\n", type);
1435 pidlOut = NULL;
1436 }
1437 return pidlOut;
1438 }
1439
1440 LPITEMIDLIST _ILCreateGuidFromStrA(LPCSTR szGUID)
1441 {
1442 IID iid;
1443
1444 if (FAILED(SHCLSIDFromStringA(szGUID, &iid)))
1445 {
1446 ERR("%s is not a GUID\n", szGUID);
1447 return NULL;
1448 }
1449 return _ILCreateGuid(PT_GUID, iid);
1450 }
1451
1452 LPITEMIDLIST _ILCreateGuidFromStrW(LPCWSTR szGUID)
1453 {
1454 IID iid;
1455
1456 if (FAILED(CLSIDFromString((LPOLESTR)szGUID, &iid)))
1457 {
1458 ERR("%s is not a GUID\n", debugstr_w(szGUID));
1459 return NULL;
1460 }
1461 return _ILCreateGuid(PT_GUID, iid);
1462 }
1463
1464 LPITEMIDLIST _ILCreateFromFindDataW( const WIN32_FIND_DATAW *wfd )
1465 {
1466 char buff[MAX_PATH + 14 +1]; /* see WIN32_FIND_DATA */
1467 DWORD len, len1, wlen, alen;
1468 LPITEMIDLIST pidl;
1469 PIDLTYPE type;
1470
1471 if (!wfd)
1472 return NULL;
1473
1474 TRACE("(%s, %s)\n", debugstr_w(wfd->cAlternateFileName), debugstr_w(wfd->cFileName));
1475
1476 /* prepare buffer with both names */
1477 len = WideCharToMultiByte(CP_ACP, 0, wfd->cFileName, -1, buff, MAX_PATH, NULL, NULL);
1478 len1 = WideCharToMultiByte(CP_ACP, 0, wfd->cAlternateFileName, -1, buff + len, sizeof(buff) - len, NULL, NULL);
1479 alen = len + len1;
1480
1481 type = (wfd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? PT_FOLDER : PT_VALUE;
1482
1483 wlen = wcslen(wfd->cFileName) + 1;
1484 pidl = _ILAlloc(type, sizeof(FileStruct) + (alen + (alen & 1)) +
1485 sizeof(FileStructW) + wlen * sizeof(WCHAR) + sizeof(WORD));
1486 if (pidl)
1487 {
1488 LPPIDLDATA pData = _ILGetDataPointer(pidl);
1489 FileStruct *fs = &pData->u.file;
1490 FileStructW *fsw;
1491 WORD *pOffsetW;
1492
1493 FileTimeToDosDateTime( &wfd->ftLastWriteTime, &fs->uFileDate, &fs->uFileTime);
1494 fs->dwFileSize = wfd->nFileSizeLow;
1495 fs->uFileAttribs = wfd->dwFileAttributes;
1496 memcpy(fs->szNames, buff, alen);
1497
1498 fsw = (FileStructW*)(pData->u.file.szNames + alen + (alen & 0x1));
1499 fsw->cbLen = sizeof(FileStructW) + wlen * sizeof(WCHAR) + sizeof(WORD);
1500 FileTimeToDosDateTime( &wfd->ftCreationTime, &fsw->uCreationDate, &fsw->uCreationTime);
1501 FileTimeToDosDateTime( &wfd->ftLastAccessTime, &fsw->uLastAccessDate, &fsw->uLastAccessTime);
1502 memcpy(fsw->wszName, wfd->cFileName, wlen * sizeof(WCHAR));
1503
1504 pOffsetW = (WORD*)((LPBYTE)pidl + pidl->mkid.cb - sizeof(WORD));
1505 *pOffsetW = (LPBYTE)fsw - (LPBYTE)pidl;
1506 TRACE("-- Set Value: %s\n", debugstr_w(fsw->wszName));
1507 }
1508 return pidl;
1509
1510 }
1511
1512 HRESULT _ILCreateFromPathW(LPCWSTR szPath, LPITEMIDLIST* ppidl)
1513 {
1514 HANDLE hFile;
1515 WIN32_FIND_DATAW stffile;
1516
1517 if (!ppidl)
1518 return E_INVALIDARG;
1519
1520 hFile = FindFirstFileW(szPath, &stffile);
1521 if (hFile == INVALID_HANDLE_VALUE)
1522 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
1523
1524 FindClose(hFile);
1525
1526 *ppidl = _ILCreateFromFindDataW(&stffile);
1527
1528 return *ppidl ? S_OK : E_OUTOFMEMORY;
1529 }
1530
1531 LPITEMIDLIST _ILCreateDrive(LPCWSTR lpszNew)
1532 {
1533 LPITEMIDLIST pidlOut;
1534
1535 TRACE("(%s)\n", debugstr_w(lpszNew));
1536
1537 pidlOut = _ILAlloc(PT_DRIVE, sizeof(DriveStruct));
1538 if (pidlOut)
1539 {
1540 LPSTR pszDest;
1541
1542 pszDest = _ILGetTextPointer(pidlOut);
1543 if (pszDest)
1544 {
1545 strcpy(pszDest, "x:\\");
1546 pszDest[0] = towupper(lpszNew[0]);
1547 TRACE("-- create Drive: %s\n", debugstr_a(pszDest));
1548 }
1549 }
1550 return pidlOut;
1551 }
1552
1553 /**************************************************************************
1554 * _ILGetDrive()
1555 *
1556 * Gets the text for the drive eg. 'c:\'
1557 *
1558 * RETURNS
1559 * strlen (lpszText)
1560 */
1561 DWORD _ILGetDrive(LPCITEMIDLIST pidl, LPSTR pOut, UINT uSize)
1562 {
1563 TRACE("(%p,%p,%u)\n", pidl, pOut, uSize);
1564
1565 if(_ILIsMyComputer(pidl))
1566 pidl = ILGetNext(pidl);
1567
1568 if (pidl && _ILIsDrive(pidl))
1569 return _ILSimpleGetText(pidl, pOut, uSize);
1570
1571 return 0;
1572 }
1573
1574 /**************************************************************************
1575 *
1576 * ### 2. section testing pidls ###
1577 *
1578 **************************************************************************
1579 * _ILIsUnicode()
1580 * _ILIsDesktop()
1581 * _ILIsMyComputer()
1582 * _ILIsSpecialFolder()
1583 * _ILIsDrive()
1584 * _ILIsFolder()
1585 * _ILIsValue()
1586 * _ILIsPidlSimple()
1587 */
1588 BOOL _ILIsUnicode(LPCITEMIDLIST pidl)
1589 {
1590 LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1591
1592 TRACE("(%p)\n", pidl);
1593
1594 return (pidl && lpPData && PT_VALUEW == lpPData->type);
1595 }
1596
1597 BOOL _ILIsDesktop(LPCITEMIDLIST pidl)
1598 {
1599 TRACE("(%p)\n", pidl);
1600
1601 return pidl && pidl->mkid.cb ? 0 : 1;
1602 }
1603
1604 BOOL _ILIsMyDocuments(LPCITEMIDLIST pidl)
1605 {
1606 IID *iid = _ILGetGUIDPointer(pidl);
1607
1608 TRACE("(%p)\n", pidl);
1609
1610 if (iid)
1611 return IsEqualIID(*iid, CLSID_MyDocuments);
1612 return FALSE;
1613 }
1614
1615 BOOL _ILIsControlPanel(LPCITEMIDLIST pidl)
1616 {
1617 IID *iid = _ILGetGUIDPointer(pidl);
1618
1619 TRACE("(%p)\n", pidl);
1620
1621 if (iid)
1622 return IsEqualIID(*iid, CLSID_ControlPanel);
1623 return FALSE;
1624 }
1625
1626 BOOL _ILIsNetHood(LPCITEMIDLIST pidl)
1627 {
1628 IID *iid = _ILGetGUIDPointer(pidl);
1629
1630 TRACE("(%p)\n", pidl);
1631
1632 if (iid)
1633 return IsEqualIID(*iid, CLSID_NetworkPlaces);
1634 return FALSE;
1635 }
1636
1637
1638 LPITEMIDLIST _ILCreateNetHood(void)
1639 {
1640 return _ILCreateGuid(PT_GUID, CLSID_NetworkPlaces);
1641 }
1642
1643 LPITEMIDLIST _ILCreateFont(void)
1644 {
1645 return _ILCreateGuid(PT_GUID, CLSID_FontsFolderShortcut);
1646 }
1647
1648 BOOL _ILIsMyComputer(LPCITEMIDLIST pidl)
1649 {
1650 IID *iid = _ILGetGUIDPointer(pidl);
1651
1652 TRACE("(%p)\n", pidl);
1653
1654 if (iid)
1655 return IsEqualIID(*iid, CLSID_MyComputer);
1656 return FALSE;
1657 }
1658
1659 BOOL _ILIsPrinter(LPCITEMIDLIST pidl)
1660 {
1661 IID *iid = _ILGetGUIDPointer(pidl);
1662
1663 TRACE("(%p)\n", pidl);
1664
1665 if (iid)
1666 return IsEqualIID(*iid, CLSID_Printers);
1667 return FALSE;
1668 }
1669
1670 BOOL _ILIsBitBucket(LPCITEMIDLIST pidl)
1671 {
1672 IID *iid = _ILGetGUIDPointer(pidl);
1673
1674 TRACE("(%p)\n", pidl);
1675
1676 if (iid)
1677 return IsEqualIID(*iid, CLSID_RecycleBin);
1678 return FALSE;
1679 }
1680
1681 BOOL _ILIsAdminTools(LPCITEMIDLIST pidl)
1682 {
1683 IID *iid = _ILGetGUIDPointer(pidl);
1684
1685 TRACE("(%p)\n", pidl);
1686
1687 if (iid)
1688 return IsEqualIID(*iid, CLSID_AdminFolderShortcut);
1689 else
1690 return FALSE;
1691 }
1692
1693 BOOL _ILIsSpecialFolder (LPCITEMIDLIST pidl)
1694 {
1695 LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1696
1697 TRACE("(%p)\n", pidl);
1698
1699 return (pidl && ( (lpPData && (PT_GUID == lpPData->type || PT_SHELLEXT == lpPData->type || PT_YAGUID == lpPData->type)) ||
1700 (pidl && pidl->mkid.cb == 0x00)
1701 ));
1702 }
1703
1704 BOOL _ILIsDrive(LPCITEMIDLIST pidl)
1705 {
1706 LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1707
1708 TRACE("(%p)\n", pidl);
1709
1710 return (pidl && lpPData && (PT_DRIVE == lpPData->type ||
1711 PT_DRIVE1 == lpPData->type ||
1712 PT_DRIVE2 == lpPData->type ||
1713 PT_DRIVE3 == lpPData->type));
1714 }
1715
1716 BOOL _ILIsFolder(LPCITEMIDLIST pidl)
1717 {
1718 LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1719
1720 TRACE("(%p)\n", pidl);
1721
1722 return (pidl && lpPData && (PT_FOLDER == lpPData->type || PT_FOLDER1 == lpPData->type));
1723 }
1724
1725 BOOL _ILIsValue(LPCITEMIDLIST pidl)
1726 {
1727 LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1728
1729 TRACE("(%p)\n", pidl);
1730
1731 return (pidl && lpPData && PT_VALUE == lpPData->type);
1732 }
1733
1734 BOOL _ILIsCPanelStruct(LPCITEMIDLIST pidl)
1735 {
1736 LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1737
1738 TRACE("(%p)\n", pidl);
1739
1740 return (pidl && lpPData && (lpPData->type == 0));
1741 }
1742
1743 /**************************************************************************
1744 * _ILIsPidlSimple
1745 */
1746 BOOL _ILIsPidlSimple(LPCITEMIDLIST pidl)
1747 {
1748 BOOL ret = TRUE;
1749
1750 if(! _ILIsDesktop(pidl)) /* pidl=NULL or mkid.cb=0 */
1751 {
1752 WORD len = pidl->mkid.cb;
1753 LPCITEMIDLIST pidlnext = (LPCITEMIDLIST) (((const BYTE*)pidl) + len );
1754
1755 if (pidlnext->mkid.cb)
1756 ret = FALSE;
1757 }
1758
1759 TRACE("%s\n", ret ? "Yes" : "No");
1760 return ret;
1761 }
1762
1763 /**************************************************************************
1764 *
1765 * ### 3. section getting values from pidls ###
1766 */
1767
1768 /**************************************************************************
1769 * _ILSimpleGetText
1770 *
1771 * gets the text for the first item in the pidl (eg. simple pidl)
1772 *
1773 * returns the length of the string
1774 */
1775 DWORD _ILSimpleGetText (LPCITEMIDLIST pidl, LPSTR szOut, UINT uOutSize)
1776 {
1777 DWORD dwReturn = 0;
1778 LPSTR szSrc;
1779 LPWSTR szSrcW;
1780 GUID const * riid;
1781 char szTemp[MAX_PATH];
1782
1783 TRACE("(%p %p %x)\n", pidl, szOut, uOutSize);
1784
1785 if (!pidl)
1786 return 0;
1787
1788 if (szOut)
1789 *szOut = 0;
1790
1791 if (_ILIsDesktop(pidl))
1792 {
1793 /* desktop */
1794 if (HCR_GetClassNameA(CLSID_ShellDesktop, szTemp, MAX_PATH))
1795 {
1796 if (szOut)
1797 lstrcpynA(szOut, szTemp, uOutSize);
1798
1799 dwReturn = strlen (szTemp);
1800 }
1801 }
1802 else if (( szSrc = _ILGetTextPointer(pidl) ))
1803 {
1804 /* filesystem */
1805 if (szOut)
1806 lstrcpynA(szOut, szSrc, uOutSize);
1807
1808 dwReturn = strlen(szSrc);
1809 }
1810 else if (( szSrcW = _ILGetTextPointerW(pidl) ))
1811 {
1812 /* unicode filesystem */
1813 WideCharToMultiByte(CP_ACP, 0, szSrcW, -1, szTemp, MAX_PATH, NULL, NULL);
1814
1815 if (szOut)
1816 lstrcpynA(szOut, szTemp, uOutSize);
1817
1818 dwReturn = strlen (szTemp);
1819 }
1820 else if (( riid = _ILGetGUIDPointer(pidl) ))
1821 {
1822 /* special folder */
1823 if (HCR_GetClassNameA(*riid, szTemp, MAX_PATH) )
1824 {
1825 if (szOut)
1826 lstrcpynA(szOut, szTemp, uOutSize);
1827
1828 dwReturn = strlen (szTemp);
1829 }
1830 }
1831 else
1832 {
1833 ERR("-- no text\n");
1834 }
1835
1836 TRACE("-- (%p=%s 0x%08x)\n", szOut, debugstr_a(szOut), dwReturn);
1837 return dwReturn;
1838 }
1839
1840 /**************************************************************************
1841 * _ILSimpleGetTextW
1842 *
1843 * gets the text for the first item in the pidl (eg. simple pidl)
1844 *
1845 * returns the length of the string
1846 */
1847 DWORD _ILSimpleGetTextW (LPCITEMIDLIST pidl, LPWSTR szOut, UINT uOutSize)
1848 {
1849 DWORD dwReturn;
1850 FileStructW *pFileStructW = _ILGetFileStructW(pidl);
1851
1852 TRACE("(%p %p %x)\n", pidl, szOut, uOutSize);
1853
1854 if (pFileStructW) {
1855 lstrcpynW(szOut, pFileStructW->wszName, uOutSize);
1856 dwReturn = wcslen(pFileStructW->wszName);
1857 } else {
1858 GUID const * riid;
1859 WCHAR szTemp[MAX_PATH];
1860 LPSTR szSrc;
1861 LPWSTR szSrcW;
1862 dwReturn = 0;
1863
1864 if (!pidl)
1865 return 0;
1866
1867 if (szOut)
1868 *szOut = 0;
1869
1870 if (_ILIsDesktop(pidl))
1871 {
1872 /* desktop */
1873 if (HCR_GetClassNameW(CLSID_ShellDesktop, szTemp, MAX_PATH))
1874 {
1875 if (szOut)
1876 lstrcpynW(szOut, szTemp, uOutSize);
1877
1878 dwReturn = wcslen (szTemp);
1879 }
1880 }
1881 else if (( szSrcW = _ILGetTextPointerW(pidl) ))
1882 {
1883 /* unicode filesystem */
1884 if (szOut)
1885 lstrcpynW(szOut, szSrcW, uOutSize);
1886
1887 dwReturn = wcslen(szSrcW);
1888 }
1889 else if (( szSrc = _ILGetTextPointer(pidl) ))
1890 {
1891 /* filesystem */
1892 MultiByteToWideChar(CP_ACP, 0, szSrc, -1, szTemp, MAX_PATH);
1893
1894 if (szOut)
1895 lstrcpynW(szOut, szTemp, uOutSize);
1896
1897 dwReturn = wcslen (szTemp);
1898 }
1899 else if (( riid = _ILGetGUIDPointer(pidl) ))
1900 {
1901 /* special folder */
1902 if ( HCR_GetClassNameW(*riid, szTemp, MAX_PATH) )
1903 {
1904 if (szOut)
1905 lstrcpynW(szOut, szTemp, uOutSize);
1906
1907 dwReturn = wcslen (szTemp);
1908 }
1909 }
1910 else
1911 {
1912 ERR("-- no text\n");
1913 }
1914 }
1915
1916 TRACE("-- (%p=%s 0x%08x)\n", szOut, debugstr_w(szOut), dwReturn);
1917 return dwReturn;
1918 }
1919
1920 /**************************************************************************
1921 *
1922 * ### 4. getting pointers to parts of pidls ###
1923 *
1924 **************************************************************************
1925 * _ILGetDataPointer()
1926 */
1927 LPPIDLDATA _ILGetDataPointer(LPCITEMIDLIST pidl)
1928 {
1929 if(pidl && pidl->mkid.cb != 0x00)
1930 return (LPPIDLDATA)pidl->mkid.abID;
1931 return NULL;
1932 }
1933
1934 /**************************************************************************
1935 * _ILGetTextPointerW()
1936 * gets a pointer to the unicode long filename string stored in the pidl
1937 */
1938 static LPWSTR _ILGetTextPointerW(LPCITEMIDLIST pidl)
1939 {
1940 /* TRACE(pidl,"(pidl%p)\n", pidl);*/
1941
1942 LPPIDLDATA pdata = _ILGetDataPointer(pidl);
1943
1944 if (!pdata)
1945 return NULL;
1946
1947 switch (pdata->type)
1948 {
1949 case PT_GUID:
1950 case PT_SHELLEXT:
1951 case PT_YAGUID:
1952 return NULL;
1953
1954 case PT_DRIVE:
1955 case PT_DRIVE1:
1956 case PT_DRIVE2:
1957 case PT_DRIVE3:
1958 /*return (LPSTR)&(pdata->u.drive.szDriveName);*/
1959 return NULL;
1960
1961 case PT_FOLDER:
1962 case PT_FOLDER1:
1963 case PT_VALUE:
1964 case PT_IESPECIAL1:
1965 case PT_IESPECIAL2:
1966 /*return (LPSTR)&(pdata->u.file.szNames);*/
1967 return NULL;
1968
1969 case PT_WORKGRP:
1970 case PT_COMP:
1971 case PT_NETWORK:
1972 case PT_NETPROVIDER:
1973 case PT_SHARE:
1974 /*return (LPSTR)&(pdata->u.network.szNames);*/
1975 return NULL;
1976
1977 case PT_VALUEW:
1978 return (LPWSTR)pdata->u.file.szNames;
1979 }
1980 return NULL;
1981 }
1982
1983
1984 /**************************************************************************
1985 * _ILGetTextPointer()
1986 * gets a pointer to the long filename string stored in the pidl
1987 */
1988 LPSTR _ILGetTextPointer(LPCITEMIDLIST pidl)
1989 {
1990 /* TRACE(pidl,"(pidl%p)\n", pidl);*/
1991
1992 LPPIDLDATA pdata = _ILGetDataPointer(pidl);
1993
1994 if (!pdata)
1995 return NULL;
1996
1997 switch (pdata->type)
1998 {
1999 case PT_GUID:
2000 case PT_SHELLEXT:
2001 case PT_YAGUID:
2002 return NULL;
2003
2004 case PT_DRIVE:
2005 case PT_DRIVE1:
2006 case PT_DRIVE2:
2007 case PT_DRIVE3:
2008 return pdata->u.drive.szDriveName;
2009
2010 case PT_FOLDER:
2011 case PT_FOLDER1:
2012 case PT_VALUE:
2013 case PT_IESPECIAL1:
2014 case PT_IESPECIAL2:
2015 return pdata->u.file.szNames;
2016
2017 case PT_WORKGRP:
2018 case PT_COMP:
2019 case PT_NETWORK:
2020 case PT_NETPROVIDER:
2021 case PT_SHARE:
2022 return pdata->u.network.szNames;
2023
2024 case PT_CPLAPPLET:
2025 return pdata->u.cpanel.szName;
2026 }
2027 return NULL;
2028 }
2029
2030 /**************************************************************************
2031 * _ILGetSTextPointer()
2032 * gets a pointer to the short filename string stored in the pidl
2033 */
2034 static LPSTR _ILGetSTextPointer(LPCITEMIDLIST pidl)
2035 {
2036 /* TRACE(pidl,"(pidl%p)\n", pidl); */
2037
2038 LPPIDLDATA pdata = _ILGetDataPointer(pidl);
2039
2040 if (!pdata)
2041 return NULL;
2042
2043 switch (pdata->type)
2044 {
2045 case PT_FOLDER:
2046 case PT_VALUE:
2047 case PT_IESPECIAL1:
2048 case PT_IESPECIAL2:
2049 return pdata->u.file.szNames + strlen (pdata->u.file.szNames) + 1;
2050
2051 case PT_WORKGRP:
2052 return pdata->u.network.szNames + strlen (pdata->u.network.szNames) + 1;
2053 }
2054 return NULL;
2055 }
2056
2057 /**************************************************************************
2058 * _ILGetGUIDPointer()
2059 *
2060 * returns reference to guid stored in some pidls
2061 */
2062 IID* _ILGetGUIDPointer(LPCITEMIDLIST pidl)
2063 {
2064 LPPIDLDATA pdata = _ILGetDataPointer(pidl);
2065
2066 TRACE("%p\n", pidl);
2067
2068 if (!pdata)
2069 return NULL;
2070
2071 TRACE("pdata->type 0x%04x\n", pdata->type);
2072 switch (pdata->type)
2073 {
2074 case PT_SHELLEXT:
2075 case PT_GUID:
2076 case PT_YAGUID:
2077 return &(pdata->u.guid.guid);
2078
2079 default:
2080 TRACE("Unknown pidl type 0x%04x\n", pdata->type);
2081 break;
2082 }
2083 return NULL;
2084 }
2085
2086 /******************************************************************************
2087 * _ILGetFileStructW [Internal]
2088 *
2089 * Get pointer the a SHITEMID's FileStructW field if present
2090 *
2091 * PARAMS
2092 * pidl [I] The SHITEMID
2093 *
2094 * RETURNS
2095 * Success: Pointer to pidl's FileStructW field.
2096 * Failure: NULL
2097 */
2098 FileStructW* _ILGetFileStructW(LPCITEMIDLIST pidl) {
2099 FileStructW *pFileStructW;
2100 WORD cbOffset;
2101
2102 if (!(_ILIsValue(pidl) || _ILIsFolder(pidl)))
2103 return NULL;
2104
2105 cbOffset = *(const WORD *)((const BYTE *)pidl + pidl->mkid.cb - sizeof(WORD));
2106 pFileStructW = (FileStructW*)((LPBYTE)pidl + cbOffset);
2107
2108 /* Currently I don't see a fool prove way to figure out if a pidl is for sure of WinXP
2109 * style with a FileStructW member. If we switch all our shellfolder-implementations to
2110 * the new format, this won't be a problem. For now, we do as many sanity checks as possible. */
2111 if (cbOffset & 0x1 || /* FileStructW member is word aligned in the pidl */
2112 /* FileStructW is positioned after FileStruct */
2113 cbOffset < sizeof(pidl->mkid.cb) + sizeof(PIDLTYPE) + sizeof(FileStruct) ||
2114 /* There has to be enough space at cbOffset in the pidl to hold FileStructW and cbOffset */
2115 cbOffset > pidl->mkid.cb - sizeof(cbOffset) - sizeof(FileStructW) ||
2116 pidl->mkid.cb != cbOffset + pFileStructW->cbLen)
2117 {
2118 WARN("Invalid pidl format (cbOffset = %d)!\n", cbOffset);
2119 return NULL;
2120 }
2121
2122 return pFileStructW;
2123 }
2124
2125 /*************************************************************************
2126 * _ILGetFileDateTime
2127 *
2128 * Given the ItemIdList, get the FileTime
2129 *
2130 * PARAMS
2131 * pidl [I] The ItemIDList
2132 * pFt [I] the resulted FILETIME of the file
2133 *
2134 * RETURNS
2135 * True if Successful
2136 *
2137 * NOTES
2138 *
2139 */
2140 BOOL _ILGetFileDateTime(LPCITEMIDLIST pidl, FILETIME *pFt)
2141 {
2142 LPPIDLDATA pdata = _ILGetDataPointer(pidl);
2143
2144 if (!pdata)
2145 return FALSE;
2146
2147 switch (pdata->type)
2148 {
2149 case PT_FOLDER:
2150 case PT_VALUE:
2151 DosDateTimeToFileTime(pdata->u.file.uFileDate, pdata->u.file.uFileTime, pFt);
2152 break;
2153 default:
2154 return FALSE;
2155 }
2156 return TRUE;
2157 }
2158
2159 BOOL _ILGetFileDate (LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2160 {
2161 FILETIME ft, lft;
2162 SYSTEMTIME time;
2163 BOOL ret;
2164
2165 if (_ILGetFileDateTime( pidl, &ft ))
2166 {
2167 FileTimeToLocalFileTime(&ft, &lft);
2168 FileTimeToSystemTime (&lft, &time);
2169
2170 ret = GetDateFormatA(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &time, NULL, pOut, uOutSize);
2171 if (ret)
2172 {
2173 /* Append space + time without seconds */
2174 pOut[ret-1] = ' ';
2175 GetTimeFormatA(LOCALE_USER_DEFAULT, TIME_NOSECONDS, &time, NULL, &pOut[ret], uOutSize - ret);
2176 }
2177 }
2178 else
2179 {
2180 pOut[0] = '\0';
2181 ret = FALSE;
2182 }
2183 return ret;
2184 }
2185
2186 /*************************************************************************
2187 * _ILGetFileSize
2188 *
2189 * Given the ItemIdList, get the FileSize
2190 *
2191 * PARAMS
2192 * pidl [I] The ItemIDList
2193 * pOut [I] The buffer to save the result
2194 * uOutsize [I] The size of the buffer
2195 *
2196 * RETURNS
2197 * The FileSize
2198 *
2199 * NOTES
2200 * pOut can be null when no string is needed
2201 *
2202 */
2203 DWORD _ILGetFileSize (LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2204 {
2205 LPPIDLDATA pdata = _ILGetDataPointer(pidl);
2206 DWORD dwSize;
2207
2208 if (!pdata)
2209 return 0;
2210
2211 switch (pdata->type)
2212 {
2213 case PT_VALUE:
2214 dwSize = pdata->u.file.dwFileSize;
2215 if (pOut)
2216 StrFormatKBSizeA(dwSize, pOut, uOutSize);
2217 return dwSize;
2218 }
2219 if (pOut)
2220 *pOut = 0x00;
2221 return 0;
2222 }
2223
2224 BOOL _ILGetExtension (LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2225 {
2226 char szTemp[MAX_PATH];
2227 const char * pPoint;
2228 LPCITEMIDLIST pidlTemp = pidl;
2229
2230 TRACE("pidl=%p\n", pidl);
2231
2232 if (!pidl)
2233 return FALSE;
2234
2235 pidlTemp = ILFindLastID(pidl);
2236
2237 if (!_ILIsValue(pidlTemp))
2238 return FALSE;
2239 if (!_ILSimpleGetText(pidlTemp, szTemp, MAX_PATH))
2240 return FALSE;
2241
2242 pPoint = PathFindExtensionA(szTemp);
2243
2244 if (!*pPoint)
2245 return FALSE;
2246
2247 pPoint++;
2248 lstrcpynA(pOut, pPoint, uOutSize);
2249 TRACE("%s\n", pOut);
2250
2251 return TRUE;
2252 }
2253
2254 /*************************************************************************
2255 * _ILGetFileType
2256 *
2257 * Given the ItemIdList, get the file type description
2258 *
2259 * PARAMS
2260 * pidl [I] The ItemIDList (simple)
2261 * pOut [I] The buffer to save the result
2262 * uOutsize [I] The size of the buffer
2263 *
2264 * RETURNS
2265 * nothing
2266 *
2267 * NOTES
2268 * This function copies as much as possible into the buffer.
2269 */
2270 void _ILGetFileType(LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2271 {
2272 char sType[64];
2273
2274 if(_ILIsValue(pidl))
2275 {
2276 char sTemp[64];
2277
2278 if(uOutSize > 0)
2279 pOut[0] = 0;
2280 if (_ILGetExtension (pidl, sType, 64))
2281 {
2282 if (HCR_MapTypeToValueA(sType, sTemp, 64, TRUE))
2283 {
2284 /* retrieve description */
2285 if(HCR_MapTypeToValueA(sTemp, pOut, uOutSize, FALSE ))
2286 return;
2287 }
2288 /* display Ext-file as description */
2289 strcpy(pOut, sType);
2290 _strupr(pOut);
2291 /* load localized file string */
2292 sTemp[0] = '\0';
2293 if(LoadStringA(shell32_hInstance, IDS_SHV_COLUMN1, sTemp, 64))
2294 {
2295 sTemp[63] = '\0';
2296 strcat(pOut, "-");
2297 strcat(pOut, sTemp);
2298 }
2299 }
2300 }
2301 else
2302 {
2303 pOut[0] = '\0';
2304 LoadStringA(shell32_hInstance, IDS_DIRECTORY, pOut, uOutSize);
2305 /* make sure its null terminated */
2306 pOut[uOutSize-1] = '\0';
2307 }
2308 }
2309
2310 /*************************************************************************
2311 * _ILGetFileAttributes
2312 *
2313 * Given the ItemIdList, get the Attrib string format
2314 *
2315 * PARAMS
2316 * pidl [I] The ItemIDList
2317 * pOut [I] The buffer to save the result
2318 * uOutsize [I] The size of the Buffer
2319 *
2320 * RETURNS
2321 * Attributes
2322 *
2323 * FIXME
2324 * return value 0 in case of error is a valid return value
2325 *
2326 */
2327 DWORD _ILGetFileAttributes(LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2328 {
2329 LPPIDLDATA pData = _ILGetDataPointer(pidl);
2330 WORD wAttrib = 0;
2331 int i;
2332
2333 if (!pData)
2334 return 0;
2335
2336 switch(pData->type)
2337 {
2338 case PT_FOLDER:
2339 case PT_VALUE:
2340 wAttrib = pData->u.file.uFileAttribs;
2341 break;
2342 }
2343
2344 if(uOutSize >= 6)
2345 {
2346 i = 0;
2347 if(wAttrib & FILE_ATTRIBUTE_READONLY)
2348 pOut[i++] = 'R';
2349 if(wAttrib & FILE_ATTRIBUTE_HIDDEN)
2350 pOut[i++] = 'H';
2351 if(wAttrib & FILE_ATTRIBUTE_SYSTEM)
2352 pOut[i++] = 'S';
2353 if(wAttrib & FILE_ATTRIBUTE_ARCHIVE)
2354 pOut[i++] = 'A';
2355 if(wAttrib & FILE_ATTRIBUTE_COMPRESSED)
2356 pOut[i++] = 'C';
2357 pOut[i] = 0x00;
2358 }
2359 return wAttrib;
2360 }
2361
2362 /*************************************************************************
2363 * ILFreeaPidl
2364 *
2365 * free a aPidl struct
2366 */
2367 void _ILFreeaPidl(LPITEMIDLIST * apidl, UINT cidl)
2368 {
2369 UINT i;
2370
2371 if (apidl)
2372 {
2373 for (i = 0; i < cidl; i++)
2374 SHFree(apidl[i]);
2375 SHFree(apidl);
2376 }
2377 }
2378
2379 /*************************************************************************
2380 * ILCopyaPidl
2381 *
2382 * copies an aPidl struct
2383 */
2384 LPITEMIDLIST* _ILCopyaPidl(const LPCITEMIDLIST * apidlsrc, UINT cidl)
2385 {
2386 UINT i;
2387 LPITEMIDLIST *apidldest;
2388
2389 apidldest = (LPITEMIDLIST *)SHAlloc(cidl * sizeof(LPITEMIDLIST));
2390 if (!apidlsrc)
2391 return NULL;
2392
2393 for (i = 0; i < cidl; i++)
2394 apidldest[i] = ILClone(apidlsrc[i]);
2395
2396 return apidldest;
2397 }
2398
2399 /*************************************************************************
2400 * _ILCopyCidaToaPidl
2401 *
2402 * creates aPidl from CIDA
2403 */
2404 LPITEMIDLIST* _ILCopyCidaToaPidl(LPITEMIDLIST* pidl, const CIDA * cida)
2405 {
2406 UINT i;
2407 LPITEMIDLIST *dst;
2408
2409 dst = (LPITEMIDLIST *)SHAlloc(cida->cidl * sizeof(LPITEMIDLIST));
2410 if (!dst)
2411 return NULL;
2412
2413 if (pidl)
2414 *pidl = ILClone((LPCITEMIDLIST)(&((const BYTE*)cida)[cida->aoffset[0]]));
2415
2416 for (i = 0; i < cida->cidl; i++)
2417 dst[i] = ILClone((LPCITEMIDLIST)(&((const BYTE*)cida)[cida->aoffset[i + 1]]));
2418
2419 return dst;
2420 }