Fix snooping of 16-bit dlls being loaded at the same address.
[wine] / dlls / shell32 / folders.c
1 /*
2  *      Copyright 1997  Marcus Meissner
3  *      Copyright 1998  Juergen Schmied
4  *
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.
9  *
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.
14  *
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
18  */
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23
24 #include "windef.h"
25 #include "wine/obj_base.h"
26 #include "wine/obj_extracticon.h"
27 #include "undocshell.h"
28 #include "shlguid.h"
29
30 #include "wine/debug.h"
31 #include "winerror.h"
32
33 #include "pidl.h"
34 #include "shell32_main.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(shell);
37
38
39 /***********************************************************************
40 *   IExtractIconA implementation
41 */
42
43 typedef struct
44 {       ICOM_VFIELD(IExtractIconA);
45         DWORD   ref;
46         ICOM_VTABLE(IPersistFile)*      lpvtblPersistFile;
47         LPITEMIDLIST    pidl;
48 } IExtractIconAImpl;
49
50 static struct ICOM_VTABLE(IExtractIconA) eivt;
51 static struct ICOM_VTABLE(IPersistFile) pfvt;
52
53 #define _IPersistFile_Offset ((int)(&(((IExtractIconAImpl*)0)->lpvtblPersistFile)))
54 #define _ICOM_THIS_From_IPersistFile(class, name) class* This = (class*)(((char*)name)-_IPersistFile_Offset);
55
56 /**************************************************************************
57 *  IExtractIconA_Constructor
58 */
59 IExtractIconA* IExtractIconA_Constructor(LPCITEMIDLIST pidl)
60 {
61         IExtractIconAImpl* ei;
62
63         ei=(IExtractIconAImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(IExtractIconAImpl));
64         ei->ref=1;
65         ICOM_VTBL(ei) = &eivt;
66         ei->lpvtblPersistFile = &pfvt;
67         ei->pidl=ILClone(pidl);
68
69         pdump(pidl);
70
71         TRACE("(%p)\n",ei);
72         return (IExtractIconA *)ei;
73 }
74 /**************************************************************************
75  *  IExtractIconA_QueryInterface
76  */
77 static HRESULT WINAPI IExtractIconA_fnQueryInterface( IExtractIconA * iface, REFIID riid, LPVOID *ppvObj)
78 {
79         ICOM_THIS(IExtractIconAImpl,iface);
80
81          TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
82
83         *ppvObj = NULL;
84
85         if(IsEqualIID(riid, &IID_IUnknown))             /*IUnknown*/
86         { *ppvObj = This;
87         }
88         else if(IsEqualIID(riid, &IID_IPersistFile))    /*IExtractIcon*/
89         {    *ppvObj = (IPersistFile*)&(This->lpvtblPersistFile);
90         }
91         else if(IsEqualIID(riid, &IID_IExtractIconA))   /*IExtractIcon*/
92         {    *ppvObj = (IExtractIconA*)This;
93         }
94
95         if(*ppvObj)
96         { IExtractIconA_AddRef((IExtractIconA*) *ppvObj);
97           TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
98           return S_OK;
99         }
100         TRACE("-- Interface: E_NOINTERFACE\n");
101         return E_NOINTERFACE;
102 }
103
104 /**************************************************************************
105 *  IExtractIconA_AddRef
106 */
107 static ULONG WINAPI IExtractIconA_fnAddRef(IExtractIconA * iface)
108 {
109         ICOM_THIS(IExtractIconAImpl,iface);
110
111         TRACE("(%p)->(count=%lu)\n",This, This->ref );
112
113         return ++(This->ref);
114 }
115 /**************************************************************************
116 *  IExtractIconA_Release
117 */
118 static ULONG WINAPI IExtractIconA_fnRelease(IExtractIconA * iface)
119 {
120         ICOM_THIS(IExtractIconAImpl,iface);
121
122         TRACE("(%p)->()\n",This);
123
124         if (!--(This->ref))
125         { TRACE(" destroying IExtractIcon(%p)\n",This);
126           SHFree(This->pidl);
127           HeapFree(GetProcessHeap(),0,This);
128           return 0;
129         }
130         return This->ref;
131 }
132 /**************************************************************************
133 *  IExtractIconA_GetIconLocation
134 *
135 * mapping filetype to icon
136 */
137 static HRESULT WINAPI IExtractIconA_fnGetIconLocation(
138         IExtractIconA * iface,
139         UINT uFlags,
140         LPSTR szIconFile,
141         UINT cchMax,
142         int * piIndex,
143         UINT * pwFlags)
144 {
145         ICOM_THIS(IExtractIconAImpl,iface);
146
147         char    sTemp[MAX_PATH];
148         DWORD   dwNr;
149         GUID const * riid;
150         LPITEMIDLIST    pSimplePidl = ILFindLastID(This->pidl);
151
152         TRACE("(%p) (flags=%u %p %u %p %p)\n", This, uFlags, szIconFile, cchMax, piIndex, pwFlags);
153
154         if (pwFlags)
155           *pwFlags = 0;
156
157         if (_ILIsDesktop(pSimplePidl))
158         {
159           lstrcpynA(szIconFile, "shell32.dll", cchMax);
160           *piIndex = 34;
161         }
162
163         /* my computer and other shell extensions */
164         else if ( (riid = _ILGetGUIDPointer(pSimplePidl)) )
165         {
166           char xriid[50];
167           sprintf( xriid, "CLSID\\{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
168                    riid->Data1, riid->Data2, riid->Data3,
169                    riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
170                    riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7] );
171
172           if (HCR_GetDefaultIcon(xriid, sTemp, MAX_PATH, &dwNr))
173           {
174             lstrcpynA(szIconFile, sTemp, cchMax);
175             *piIndex = dwNr;
176           }
177           else
178           {
179             lstrcpynA(szIconFile, "shell32.dll", cchMax);
180             *piIndex = 15;
181           }
182         }
183
184         else if (_ILIsDrive (pSimplePidl))
185         {
186           if (HCR_GetDefaultIcon("Drive", sTemp, MAX_PATH, &dwNr))
187           {
188             lstrcpynA(szIconFile, sTemp, cchMax);
189             *piIndex = dwNr;
190           }
191           else
192           {
193             lstrcpynA(szIconFile, "shell32.dll", cchMax);
194             *piIndex = 8;
195           }
196         }
197         else if (_ILIsFolder (pSimplePidl))
198         {
199           if (HCR_GetDefaultIcon("Folder", sTemp, MAX_PATH, &dwNr))
200           {
201             lstrcpynA(szIconFile, sTemp, cchMax);
202             *piIndex = dwNr;
203           }
204           else
205           {
206             lstrcpynA(szIconFile, "shell32.dll", cchMax);
207             *piIndex = (uFlags & GIL_OPENICON)? 4 : 3;
208           }
209         }
210         else    /* object is file */
211         {
212           if (_ILGetExtension (pSimplePidl, sTemp, MAX_PATH)
213               && HCR_MapTypeToValue(sTemp, sTemp, MAX_PATH, TRUE)
214               && HCR_GetDefaultIcon(sTemp, sTemp, MAX_PATH, &dwNr))
215           {
216             if (!strcmp("%1",sTemp))            /* icon is in the file */
217             {
218               SHGetPathFromIDListA(This->pidl, sTemp);
219               dwNr = 0;
220             }
221             lstrcpynA(szIconFile, sTemp, cchMax);
222             *piIndex = dwNr;
223           }
224           else                                  /* default icon */
225           {
226             lstrcpynA(szIconFile, "shell32.dll", cchMax);
227             *piIndex = 0;
228           }
229         }
230
231         TRACE("-- %s %x\n", szIconFile, *piIndex);
232         return NOERROR;
233 }
234 /**************************************************************************
235 *  IExtractIconA_Extract
236 */
237 static HRESULT WINAPI IExtractIconA_fnExtract(IExtractIconA * iface, LPCSTR pszFile, UINT nIconIndex, HICON *phiconLarge, HICON *phiconSmall, UINT nIconSize)
238 {
239         ICOM_THIS(IExtractIconAImpl,iface);
240
241         FIXME("(%p) (file=%p index=%u %p %p size=%u) semi-stub\n", This, pszFile, nIconIndex, phiconLarge, phiconSmall, nIconSize);
242
243         if (phiconLarge)
244           *phiconLarge = ImageList_GetIcon(ShellBigIconList, nIconIndex, ILD_TRANSPARENT);
245
246         if (phiconSmall)
247           *phiconSmall = ImageList_GetIcon(ShellSmallIconList, nIconIndex, ILD_TRANSPARENT);
248
249         return S_OK;
250 }
251
252 static struct ICOM_VTABLE(IExtractIconA) eivt =
253 {
254         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
255         IExtractIconA_fnQueryInterface,
256         IExtractIconA_fnAddRef,
257         IExtractIconA_fnRelease,
258         IExtractIconA_fnGetIconLocation,
259         IExtractIconA_fnExtract
260 };
261
262 /************************************************************************
263  * IEIPersistFile_QueryInterface (IUnknown)
264  */
265 static HRESULT WINAPI IEIPersistFile_fnQueryInterface(
266         IPersistFile    *iface,
267         REFIID          iid,
268         LPVOID          *ppvObj)
269 {
270         _ICOM_THIS_From_IPersistFile(IExtractIconA, iface);
271
272         return IShellFolder_QueryInterface((IExtractIconA*)This, iid, ppvObj);
273 }
274
275 /************************************************************************
276  * IEIPersistFile_AddRef (IUnknown)
277  */
278 static ULONG WINAPI IEIPersistFile_fnAddRef(
279         IPersistFile    *iface)
280 {
281         _ICOM_THIS_From_IPersistFile(IExtractIconA, iface);
282
283         return IExtractIconA_AddRef((IExtractIconA*)This);
284 }
285
286 /************************************************************************
287  * IEIPersistFile_Release (IUnknown)
288  */
289 static ULONG WINAPI IEIPersistFile_fnRelease(
290         IPersistFile    *iface)
291 {
292         _ICOM_THIS_From_IPersistFile(IExtractIconA, iface);
293
294         return IExtractIconA_Release((IExtractIconA*)This);
295 }
296
297 /************************************************************************
298  * IEIPersistFile_GetClassID (IPersist)
299  */
300 static HRESULT WINAPI IEIPersistFile_fnGetClassID(
301         IPersistFile    *iface,
302         LPCLSID         lpClassId)
303 {
304         CLSID StdFolderID = { 0x00000000, 0x0000, 0x0000, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} };
305
306         if (lpClassId==NULL)
307           return E_POINTER;
308
309         memcpy(lpClassId, &StdFolderID, sizeof(StdFolderID));
310
311         return S_OK;
312 }
313
314 /************************************************************************
315  * IEIPersistFile_Load (IPersistFile)
316  */
317 static HRESULT WINAPI IEIPersistFile_fnLoad(IPersistFile* iface, LPCOLESTR pszFileName, DWORD dwMode)
318 {
319         _ICOM_THIS_From_IPersistFile(IExtractIconA, iface);
320         FIXME("%p\n", This);
321         return E_NOTIMPL;
322
323 }
324
325 static struct ICOM_VTABLE(IPersistFile) pfvt =
326 {
327         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
328         IEIPersistFile_fnQueryInterface,
329         IEIPersistFile_fnAddRef,
330         IEIPersistFile_fnRelease,
331         IEIPersistFile_fnGetClassID,
332         (void *) 0xdeadbeef /* IEIPersistFile_fnIsDirty */,
333         IEIPersistFile_fnLoad,
334         (void *) 0xdeadbeef /* IEIPersistFile_fnSave */,
335         (void *) 0xdeadbeef /* IEIPersistFile_fnSaveCompleted */,
336         (void *) 0xdeadbeef /* IEIPersistFile_fnGetCurFile */
337 };