kernel: Trying to lock an empty global memory block gives an ERROR_DISCARDED.
[wine/hacks.git] / dlls / shell32 / folders.c
blobf7f749b11dea655cadb2f69c7a815fa355a3e3f0
1 /*
2 * Copyright 1997 Marcus Meissner
3 * Copyright 1998 Juergen Schmied
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "config.h"
21 #include "wine/port.h"
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <string.h>
28 #define COBJMACROS
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winerror.h"
33 #include "objbase.h"
34 #include "undocshell.h"
35 #include "shlguid.h"
36 #include "winreg.h"
38 #include "wine/debug.h"
40 #include "pidl.h"
41 #include "shell32_main.h"
42 #include "shfldr.h"
43 #include "shresdef.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(shell);
47 /***********************************************************************
48 * IExtractIconW implementation
50 typedef struct
52 const IExtractIconWVtbl *lpVtbl;
53 LONG ref;
54 const IPersistFileVtbl *lpvtblPersistFile;
55 const IExtractIconAVtbl *lpvtblExtractIconA;
56 LPITEMIDLIST pidl;
57 } IExtractIconWImpl;
59 static const IExtractIconAVtbl eiavt;
60 static const IExtractIconWVtbl eivt;
61 static const IPersistFileVtbl pfvt;
63 static inline IExtractIconW *impl_from_IPersistFile( IPersistFile *iface )
65 return (IExtractIconW *)((char*)iface - FIELD_OFFSET(IExtractIconWImpl, lpvtblPersistFile));
68 static inline IExtractIconW *impl_from_IExtractIconA( IExtractIconA *iface )
70 return (IExtractIconW *)((char*)iface - FIELD_OFFSET(IExtractIconWImpl, lpvtblExtractIconA));
74 /**************************************************************************
75 * IExtractIconW_Constructor
77 IExtractIconW* IExtractIconW_Constructor(LPCITEMIDLIST pidl)
79 IExtractIconWImpl* ei;
81 TRACE("%p\n", pidl);
83 ei = HeapAlloc(GetProcessHeap(),0,sizeof(IExtractIconWImpl));
84 ei->ref=1;
85 ei->lpVtbl = &eivt;
86 ei->lpvtblPersistFile = &pfvt;
87 ei->lpvtblExtractIconA = &eiavt;
88 ei->pidl=ILClone(pidl);
90 pdump(pidl);
92 TRACE("(%p)\n", ei);
93 return (IExtractIconW *)ei;
95 /**************************************************************************
96 * IExtractIconW_QueryInterface
98 static HRESULT WINAPI IExtractIconW_fnQueryInterface(IExtractIconW *iface, REFIID riid, LPVOID *ppvObj)
100 IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
102 TRACE("(%p)->(\n\tIID:\t%s,%p)\n", This, debugstr_guid(riid), ppvObj);
104 *ppvObj = NULL;
106 if (IsEqualIID(riid, &IID_IUnknown)) /*IUnknown*/
108 *ppvObj = This;
110 else if (IsEqualIID(riid, &IID_IPersistFile)) /*IExtractIcon*/
112 *ppvObj = (IPersistFile*)&(This->lpvtblPersistFile);
114 else if (IsEqualIID(riid, &IID_IExtractIconA)) /*IExtractIcon*/
116 *ppvObj = (IExtractIconA*)&(This->lpvtblExtractIconA);
118 else if (IsEqualIID(riid, &IID_IExtractIconW)) /*IExtractIcon*/
120 *ppvObj = (IExtractIconW*)This;
123 if(*ppvObj)
125 IExtractIconW_AddRef((IExtractIconW*) *ppvObj);
126 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
127 return S_OK;
129 TRACE("-- Interface: E_NOINTERFACE\n");
130 return E_NOINTERFACE;
133 /**************************************************************************
134 * IExtractIconW_AddRef
136 static ULONG WINAPI IExtractIconW_fnAddRef(IExtractIconW * iface)
138 IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
139 ULONG refCount = InterlockedIncrement(&This->ref);
141 TRACE("(%p)->(count=%lu)\n", This, refCount - 1);
143 return refCount;
145 /**************************************************************************
146 * IExtractIconW_Release
148 static ULONG WINAPI IExtractIconW_fnRelease(IExtractIconW * iface)
150 IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
151 ULONG refCount = InterlockedDecrement(&This->ref);
153 TRACE("(%p)->(count=%lu)\n", This, refCount + 1);
155 if (!refCount)
157 TRACE(" destroying IExtractIcon(%p)\n",This);
158 SHFree(This->pidl);
159 HeapFree(GetProcessHeap(),0,This);
160 return 0;
162 return refCount;
165 static HRESULT getIconLocationForFolder(IExtractIconW *iface, UINT uFlags,
166 LPWSTR szIconFile, UINT cchMax, int *piIndex, UINT *pwFlags)
168 IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
169 DWORD dwNr;
170 WCHAR wszPath[MAX_PATH];
171 WCHAR wszCLSIDValue[CHARS_IN_GUID];
172 static const WCHAR shellClassInfo[] = { '.','S','h','e','l','l','C','l','a','s','s','I','n','f','o',0 };
173 static const WCHAR iconFile[] = { 'I','c','o','n','F','i','l','e',0 };
174 static const WCHAR clsid[] = { 'C','L','S','I','D',0 };
175 static const WCHAR clsid2[] = { 'C','L','S','I','D','2',0 };
176 static const WCHAR iconIndex[] = { 'I','c','o','n','I','n','d','e','x',0 };
178 if (SHELL32_GetCustomFolderAttribute(This->pidl, shellClassInfo, iconFile,
179 wszPath, MAX_PATH))
181 WCHAR wszIconIndex[10];
182 SHELL32_GetCustomFolderAttribute(This->pidl, shellClassInfo, iconIndex,
183 wszIconIndex, 10);
184 *piIndex = atoiW(wszIconIndex);
186 else if (SHELL32_GetCustomFolderAttribute(This->pidl, shellClassInfo, clsid,
187 wszCLSIDValue, CHARS_IN_GUID) &&
188 HCR_GetDefaultIconW(wszCLSIDValue, szIconFile, cchMax, &dwNr))
190 *piIndex = dwNr;
192 else if (SHELL32_GetCustomFolderAttribute(This->pidl, shellClassInfo, clsid2,
193 wszCLSIDValue, CHARS_IN_GUID) &&
194 HCR_GetDefaultIconW(wszCLSIDValue, szIconFile, cchMax, &dwNr))
196 *piIndex = dwNr;
198 else
200 static const WCHAR folder[] = { 'F','o','l','d','e','r',0 };
202 if (!HCR_GetDefaultIconW(folder, szIconFile, cchMax, &dwNr))
204 lstrcpynW(szIconFile, swShell32Name, cchMax);
205 dwNr = IDI_SHELL_FOLDER;
207 *piIndex = -((uFlags & GIL_OPENICON) ? dwNr + 1 : dwNr);
209 return S_OK;
212 WCHAR swShell32Name[MAX_PATH];
214 /**************************************************************************
215 * IExtractIconW_GetIconLocation
217 * mapping filetype to icon
219 static HRESULT WINAPI IExtractIconW_fnGetIconLocation(
220 IExtractIconW * iface,
221 UINT uFlags, /* GIL_ flags */
222 LPWSTR szIconFile,
223 UINT cchMax,
224 int * piIndex,
225 UINT * pwFlags) /* returned GIL_ flags */
227 IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
229 char sTemp[MAX_PATH];
230 DWORD dwNr;
231 GUID const * riid;
232 LPITEMIDLIST pSimplePidl = ILFindLastID(This->pidl);
234 TRACE("(%p) (flags=%u %p %u %p %p)\n", This, uFlags, szIconFile, cchMax, piIndex, pwFlags);
236 if (pwFlags)
237 *pwFlags = 0;
239 if (_ILIsDesktop(pSimplePidl))
241 lstrcpynW(szIconFile, swShell32Name, cchMax);
242 *piIndex = -IDI_SHELL_DESKTOP;
245 /* my computer and other shell extensions */
246 else if ((riid = _ILGetGUIDPointer(pSimplePidl)))
248 static const WCHAR fmt[] = { 'C','L','S','I','D','\\',
249 '{','%','0','8','l','x','-','%','0','4','x','-','%','0','4','x','-',
250 '%','0','2','x','%','0','2','x','-','%','0','2','x', '%','0','2','x',
251 '%','0','2','x','%','0','2','x','%','0','2','x','%','0','2','x','}',0 };
252 WCHAR xriid[50];
254 sprintfW(xriid, fmt,
255 riid->Data1, riid->Data2, riid->Data3,
256 riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
257 riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7]);
259 if (HCR_GetDefaultIconW(xriid, szIconFile, cchMax, &dwNr))
261 *piIndex = dwNr;
263 else
265 lstrcpynW(szIconFile, swShell32Name, cchMax);
266 if(IsEqualGUID(riid, &CLSID_MyComputer))
267 *piIndex = -IDI_SHELL_MY_COMPUTER;
268 else if(IsEqualGUID(riid, &CLSID_MyDocuments))
269 *piIndex = -IDI_SHELL_MY_DOCUMENTS;
270 else if(IsEqualGUID(riid, &CLSID_NetworkPlaces))
271 *piIndex = -IDI_SHELL_MY_NETWORK_PLACES;
272 else if(IsEqualGUID(riid, &CLSID_UnixFolder) ||
273 IsEqualGUID(riid, &CLSID_UnixDosFolder))
274 *piIndex = -IDI_SHELL_DRIVE;
275 else
276 *piIndex = -IDI_SHELL_FOLDER;
280 else if (_ILIsDrive (pSimplePidl))
282 static const WCHAR drive[] = { 'D','r','i','v','e',0 };
284 int icon_idx = -1;
286 if (_ILGetDrive(pSimplePidl, sTemp, MAX_PATH))
288 switch(GetDriveTypeA(sTemp))
290 case DRIVE_REMOVABLE: icon_idx = IDI_SHELL_FLOPPY; break;
291 case DRIVE_CDROM: icon_idx = IDI_SHELL_CDROM; break;
292 case DRIVE_REMOTE: icon_idx = IDI_SHELL_NETDRIVE; break;
293 case DRIVE_RAMDISK: icon_idx = IDI_SHELL_RAMDISK; break;
297 if (icon_idx != -1)
299 lstrcpynW(szIconFile, swShell32Name, cchMax);
300 *piIndex = -icon_idx;
302 else
304 if (HCR_GetDefaultIconW(drive, szIconFile, cchMax, &dwNr))
306 *piIndex = dwNr;
308 else
310 lstrcpynW(szIconFile, swShell32Name, cchMax);
311 *piIndex = -IDI_SHELL_DRIVE;
315 else if (_ILIsFolder (pSimplePidl))
317 getIconLocationForFolder(iface, uFlags, szIconFile, cchMax, piIndex,
318 pwFlags);
320 else
322 BOOL found = FALSE;
324 if (_ILIsCPanelStruct(pSimplePidl))
326 if (SUCCEEDED(CPanel_GetIconLocationW(pSimplePidl, szIconFile, cchMax, piIndex)))
327 found = TRUE;
329 else if (_ILGetExtension(pSimplePidl, sTemp, MAX_PATH))
331 if (HCR_MapTypeToValueA(sTemp, sTemp, MAX_PATH, TRUE)
332 && HCR_GetDefaultIconA(sTemp, sTemp, MAX_PATH, &dwNr))
334 if (!lstrcmpA("%1", sTemp)) /* icon is in the file */
336 SHGetPathFromIDListW(This->pidl, szIconFile);
337 *piIndex = 0;
339 else
341 MultiByteToWideChar(CP_ACP, 0, sTemp, -1, szIconFile, cchMax);
342 *piIndex = dwNr;
345 found = TRUE;
347 else if (!lstrcmpiA(sTemp, "lnkfile"))
349 /* extract icon from shell shortcut */
350 IShellFolder* dsf;
351 IShellLinkW* psl;
353 if (SUCCEEDED(SHGetDesktopFolder(&dsf)))
355 HRESULT hr = IShellFolder_GetUIObjectOf(dsf, NULL, 1, (LPCITEMIDLIST*)&This->pidl, &IID_IShellLinkW, NULL, (LPVOID*)&psl);
357 if (SUCCEEDED(hr))
359 hr = IShellLinkW_GetIconLocation(psl, szIconFile, MAX_PATH, piIndex);
361 if (SUCCEEDED(hr) && *szIconFile)
362 found = TRUE;
364 IShellLinkW_Release(psl);
367 IShellFolder_Release(dsf);
372 if (!found) /* default icon */
374 lstrcpynW(szIconFile, swShell32Name, cchMax);
375 *piIndex = 0;
379 TRACE("-- %s %x\n", debugstr_w(szIconFile), *piIndex);
380 return NOERROR;
383 /**************************************************************************
384 * IExtractIconW_Extract
386 static HRESULT WINAPI IExtractIconW_fnExtract(IExtractIconW * iface, LPCWSTR pszFile, UINT nIconIndex, HICON *phiconLarge, HICON *phiconSmall, UINT nIconSize)
388 IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
389 int index;
391 FIXME("(%p) (file=%p index=%d %p %p size=%08x) semi-stub\n", This, debugstr_w(pszFile), (signed)nIconIndex,
392 phiconLarge, phiconSmall, nIconSize);
394 index = SIC_GetIconIndex(pszFile, nIconIndex, 0);
396 if (phiconLarge)
397 *phiconLarge = ImageList_GetIcon(ShellBigIconList, index, ILD_TRANSPARENT);
399 if (phiconSmall)
400 *phiconSmall = ImageList_GetIcon(ShellSmallIconList, index, ILD_TRANSPARENT);
402 return S_OK;
405 static const IExtractIconWVtbl eivt =
407 IExtractIconW_fnQueryInterface,
408 IExtractIconW_fnAddRef,
409 IExtractIconW_fnRelease,
410 IExtractIconW_fnGetIconLocation,
411 IExtractIconW_fnExtract
414 /**************************************************************************
415 * IExtractIconA_Constructor
417 IExtractIconA* IExtractIconA_Constructor(LPCITEMIDLIST pidl)
419 IExtractIconWImpl *This = (IExtractIconWImpl *)IExtractIconW_Constructor(pidl);
420 IExtractIconA *eia = (IExtractIconA *)&This->lpvtblExtractIconA;
422 TRACE("(%p)->(%p)\n", This, eia);
423 return eia;
425 /**************************************************************************
426 * IExtractIconA_QueryInterface
428 static HRESULT WINAPI IExtractIconA_fnQueryInterface(IExtractIconA * iface, REFIID riid, LPVOID *ppvObj)
430 IExtractIconW *This = impl_from_IExtractIconA(iface);
432 return IExtractIconW_QueryInterface(This, riid, ppvObj);
435 /**************************************************************************
436 * IExtractIconA_AddRef
438 static ULONG WINAPI IExtractIconA_fnAddRef(IExtractIconA * iface)
440 IExtractIconW *This = impl_from_IExtractIconA(iface);
442 return IExtractIconW_AddRef(This);
444 /**************************************************************************
445 * IExtractIconA_Release
447 static ULONG WINAPI IExtractIconA_fnRelease(IExtractIconA * iface)
449 IExtractIconW *This = impl_from_IExtractIconA(iface);
451 return IExtractIconW_AddRef(This);
453 /**************************************************************************
454 * IExtractIconA_GetIconLocation
456 * mapping filetype to icon
458 static HRESULT WINAPI IExtractIconA_fnGetIconLocation(
459 IExtractIconA * iface,
460 UINT uFlags,
461 LPSTR szIconFile,
462 UINT cchMax,
463 int * piIndex,
464 UINT * pwFlags)
466 HRESULT ret;
467 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, cchMax * sizeof(WCHAR));
468 IExtractIconW *This = impl_from_IExtractIconA(iface);
470 TRACE("(%p) (flags=%u %p %u %p %p)\n", This, uFlags, szIconFile, cchMax, piIndex, pwFlags);
472 ret = IExtractIconW_GetIconLocation(This, uFlags, lpwstrFile, cchMax, piIndex, pwFlags);
473 WideCharToMultiByte(CP_ACP, 0, lpwstrFile, -1, szIconFile, cchMax, NULL, NULL);
474 HeapFree(GetProcessHeap(), 0, lpwstrFile);
476 TRACE("-- %s %x\n", szIconFile, *piIndex);
477 return ret;
479 /**************************************************************************
480 * IExtractIconA_Extract
482 static HRESULT WINAPI IExtractIconA_fnExtract(IExtractIconA * iface, LPCSTR pszFile, UINT nIconIndex, HICON *phiconLarge, HICON *phiconSmall, UINT nIconSize)
484 HRESULT ret;
485 INT len = MultiByteToWideChar(CP_ACP, 0, pszFile, -1, NULL, 0);
486 LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
487 IExtractIconW *This = impl_from_IExtractIconA(iface);
489 TRACE("(%p) (file=%p index=%u %p %p size=%u)\n", This, pszFile, nIconIndex, phiconLarge, phiconSmall, nIconSize);
491 MultiByteToWideChar(CP_ACP, 0, pszFile, -1, lpwstrFile, len);
492 ret = IExtractIconW_Extract(This, lpwstrFile, nIconIndex, phiconLarge, phiconSmall, nIconSize);
493 HeapFree(GetProcessHeap(), 0, lpwstrFile);
494 return ret;
497 static const IExtractIconAVtbl eiavt =
499 IExtractIconA_fnQueryInterface,
500 IExtractIconA_fnAddRef,
501 IExtractIconA_fnRelease,
502 IExtractIconA_fnGetIconLocation,
503 IExtractIconA_fnExtract
506 /************************************************************************
507 * IEIPersistFile_QueryInterface (IUnknown)
509 static HRESULT WINAPI IEIPersistFile_fnQueryInterface(
510 IPersistFile *iface,
511 REFIID iid,
512 LPVOID *ppvObj)
514 IExtractIconW *This = impl_from_IPersistFile(iface);
516 return IExtractIconW_QueryInterface(This, iid, ppvObj);
519 /************************************************************************
520 * IEIPersistFile_AddRef (IUnknown)
522 static ULONG WINAPI IEIPersistFile_fnAddRef(
523 IPersistFile *iface)
525 IExtractIconW *This = impl_from_IPersistFile(iface);
527 return IExtractIconW_AddRef(This);
530 /************************************************************************
531 * IEIPersistFile_Release (IUnknown)
533 static ULONG WINAPI IEIPersistFile_fnRelease(
534 IPersistFile *iface)
536 IExtractIconW *This = impl_from_IPersistFile(iface);
538 return IExtractIconW_Release(This);
541 /************************************************************************
542 * IEIPersistFile_GetClassID (IPersist)
544 static HRESULT WINAPI IEIPersistFile_fnGetClassID(
545 IPersistFile *iface,
546 LPCLSID lpClassId)
548 CLSID StdFolderID = { 0x00000000, 0x0000, 0x0000, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} };
550 if (lpClassId==NULL)
551 return E_POINTER;
553 memcpy(lpClassId, &StdFolderID, sizeof(StdFolderID));
555 return S_OK;
558 /************************************************************************
559 * IEIPersistFile_Load (IPersistFile)
561 static HRESULT WINAPI IEIPersistFile_fnLoad(IPersistFile* iface, LPCOLESTR pszFileName, DWORD dwMode)
563 IExtractIconW *This = impl_from_IPersistFile(iface);
564 FIXME("%p\n", This);
565 return E_NOTIMPL;
569 static const IPersistFileVtbl pfvt =
571 IEIPersistFile_fnQueryInterface,
572 IEIPersistFile_fnAddRef,
573 IEIPersistFile_fnRelease,
574 IEIPersistFile_fnGetClassID,
575 (void *) 0xdeadbeef /* IEIPersistFile_fnIsDirty */,
576 IEIPersistFile_fnLoad,
577 (void *) 0xdeadbeef /* IEIPersistFile_fnSave */,
578 (void *) 0xdeadbeef /* IEIPersistFile_fnSaveCompleted */,
579 (void *) 0xdeadbeef /* IEIPersistFile_fnGetCurFile */