ntdll: Do not report non-reserved memory areas as free since we don't know what's...
[wine/multimedia.git] / dlls / shell32 / shellole.c
blobeae8bc3d8c08aacdde0a330643ab62a7a9b4cadd
1 /*
2 * handling of SHELL32.DLL OLE-Objects
4 * Copyright 1997 Marcus Meissner
5 * Copyright 1998 Juergen Schmied <juergen.schmied@metronet.de>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <string.h>
28 #define COBJMACROS
30 #include "windef.h"
31 #include "winbase.h"
32 #include "shellapi.h"
33 #include "wingdi.h"
34 #include "winuser.h"
35 #include "shlobj.h"
36 #include "shlguid.h"
37 #include "winreg.h"
38 #include "winerror.h"
40 #include "undocshell.h"
41 #include "wine/unicode.h"
42 #include "shell32_main.h"
44 #include "wine/debug.h"
45 #include "shlwapi.h"
46 #include "debughlp.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(shell);
50 extern HRESULT WINAPI IFSFolder_Constructor(IUnknown * pUnkOuter, REFIID riid, LPVOID * ppv);
52 /**************************************************************************
53 * Default ClassFactory types
55 typedef HRESULT (CALLBACK *LPFNCREATEINSTANCE)(IUnknown* pUnkOuter, REFIID riid, LPVOID* ppvObject);
56 static IClassFactory * IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI, PLONG pcRefDll, REFIID riidInst);
58 /* this table contains all CLSID's of shell32 objects */
59 static const struct {
60 REFIID riid;
61 LPFNCREATEINSTANCE lpfnCI;
62 } InterfaceTable[] = {
63 {&CLSID_ShellFSFolder, IFSFolder_Constructor},
64 {&CLSID_MyComputer, ISF_MyComputer_Constructor},
65 {&CLSID_NetworkPlaces, ISF_NetworkPlaces_Constructor},
66 {&CLSID_ShellDesktop, ISF_Desktop_Constructor},
67 {&CLSID_ShellLink, IShellLink_Constructor},
68 {&CLSID_DragDropHelper, IDropTargetHelper_Constructor},
69 {&CLSID_ControlPanel, IControlPanel_Constructor},
70 {&CLSID_AutoComplete, IAutoComplete_Constructor},
71 {&CLSID_UnixFolder, UnixFolder_Constructor},
72 {&CLSID_UnixDosFolder, UnixDosFolder_Constructor},
73 {&CLSID_FolderShortcut, FolderShortcut_Constructor},
74 {&CLSID_MyDocuments, MyDocuments_Constructor},
75 {&CLSID_RecycleBin, RecycleBin_Constructor},
76 {NULL,NULL}
80 /* FIXME: this should be SHLWAPI.24 since we can't yet import by ordinal */
82 DWORD WINAPI __SHGUIDToStringW (REFGUID guid, LPWSTR str)
84 WCHAR sFormat[52] = {'{','%','0','8','l','x','-','%','0','4',
85 'x','-','%','0','4','x','-','%','0','2',
86 'x','%','0','2','x','-','%','0','2','x',
87 '%','0','2','x','%','0','2','x','%','0',
88 '2','x','%','0','2','x','%','0','2','x',
89 '}','\0'};
91 return wsprintfW ( str, sFormat,
92 guid->Data1, guid->Data2, guid->Data3,
93 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
94 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7] );
98 /*************************************************************************
99 * SHCoCreateInstance [SHELL32.102]
101 * Equivalent to CoCreateInstance. Under Windows 9x this function could sometimes
102 * use the shell32 built-in "mini-COM" without the need to load ole32.dll - see
103 * SHLoadOLE for details.
105 * Under wine if a "LoadWithoutCOM" value is present or the object resides in
106 * shell32.dll the function will load the object manually without the help of ole32
108 * NOTES
109 * exported by ordinal
111 * SEE ALSO
112 * CoCreateInstace, SHLoadOLE
114 HRESULT WINAPI SHCoCreateInstance(
115 LPCWSTR aclsid,
116 const CLSID *clsid,
117 LPUNKNOWN pUnkOuter,
118 REFIID refiid,
119 LPVOID *ppv)
121 DWORD hres;
122 IID iid;
123 const CLSID * myclsid = clsid;
124 WCHAR sKeyName[MAX_PATH];
125 const WCHAR sCLSID[7] = {'C','L','S','I','D','\\','\0'};
126 WCHAR sClassID[60];
127 const WCHAR sInProcServer32[16] ={'\\','I','n','p','r','o','c','S','e','r','v','e','r','3','2','\0'};
128 const WCHAR sLoadWithoutCOM[15] ={'L','o','a','d','W','i','t','h','o','u','t','C','O','M','\0'};
129 WCHAR sDllPath[MAX_PATH];
130 HKEY hKey = 0;
131 DWORD dwSize;
132 IClassFactory * pcf = NULL;
134 if(!ppv) return E_POINTER;
135 *ppv=NULL;
137 /* if the clsid is a string, convert it */
138 if (!clsid)
140 if (!aclsid) return REGDB_E_CLASSNOTREG;
141 SHCLSIDFromStringW(aclsid, &iid);
142 myclsid = &iid;
145 TRACE("(%p,%s,unk:%p,%s,%p)\n",
146 aclsid,shdebugstr_guid(myclsid),pUnkOuter,shdebugstr_guid(refiid),ppv);
148 if (SUCCEEDED(DllGetClassObject(myclsid, &IID_IClassFactory,(LPVOID*)&pcf)))
150 hres = IClassFactory_CreateInstance(pcf, pUnkOuter, refiid, ppv);
151 IClassFactory_Release(pcf);
152 goto end;
155 /* we look up the dll path in the registry */
156 __SHGUIDToStringW(myclsid, sClassID);
157 lstrcpyW(sKeyName, sCLSID);
158 lstrcatW(sKeyName, sClassID);
159 lstrcatW(sKeyName, sInProcServer32);
161 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, sKeyName, 0, KEY_READ, &hKey))
162 return E_ACCESSDENIED;
164 /* if a special registry key is set, we load a shell extension without help of OLE32 */
165 if (!SHQueryValueExW(hKey, sLoadWithoutCOM, 0, 0, 0, 0))
167 /* load an external dll without ole32 */
168 HANDLE hLibrary;
169 typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid, REFIID iid, LPVOID *ppv);
170 DllGetClassObjectFunc DllGetClassObject;
172 dwSize = sizeof(sDllPath);
173 SHQueryValueExW(hKey, NULL, 0,0, sDllPath, &dwSize );
175 if ((hLibrary = LoadLibraryExW(sDllPath, 0, LOAD_WITH_ALTERED_SEARCH_PATH)) == 0) {
176 ERR("couldn't load InprocServer32 dll %s\n", debugstr_w(sDllPath));
177 hres = E_ACCESSDENIED;
178 goto end;
179 } else if (!(DllGetClassObject = (DllGetClassObjectFunc)GetProcAddress(hLibrary, "DllGetClassObject"))) {
180 ERR("couldn't find function DllGetClassObject in %s\n", debugstr_w(sDllPath));
181 FreeLibrary( hLibrary );
182 hres = E_ACCESSDENIED;
183 goto end;
184 } else if (FAILED(hres = DllGetClassObject(myclsid, &IID_IClassFactory, (LPVOID*)&pcf))) {
185 TRACE("GetClassObject failed 0x%08x\n", hres);
186 goto end;
189 hres = IClassFactory_CreateInstance(pcf, pUnkOuter, refiid, ppv);
190 IClassFactory_Release(pcf);
191 } else {
193 /* load an external dll in the usual way */
194 hres = CoCreateInstance(myclsid, pUnkOuter, CLSCTX_INPROC_SERVER, refiid, ppv);
197 end:
198 if (hKey) RegCloseKey(hKey);
199 if(hres!=S_OK)
201 ERR("failed (0x%08x) to create CLSID:%s IID:%s\n",
202 hres, shdebugstr_guid(myclsid), shdebugstr_guid(refiid));
203 ERR("class not found in registry\n");
206 TRACE("-- instance: %p\n",*ppv);
207 return hres;
210 /*************************************************************************
211 * DllGetClassObject [SHELL32.@]
212 * SHDllGetClassObject [SHELL32.128]
214 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
216 HRESULT hres = E_OUTOFMEMORY;
217 IClassFactory * pcf = NULL;
218 int i;
220 TRACE("CLSID:%s,IID:%s\n",shdebugstr_guid(rclsid),shdebugstr_guid(iid));
222 if (!ppv) return E_INVALIDARG;
223 *ppv = NULL;
225 /* search our internal interface table */
226 for(i=0;InterfaceTable[i].riid;i++) {
227 if(IsEqualIID(InterfaceTable[i].riid, rclsid)) {
228 TRACE("index[%u]\n", i);
229 pcf = IDefClF_fnConstructor(InterfaceTable[i].lpfnCI, NULL, NULL);
233 if (!pcf) {
234 FIXME("failed for CLSID=%s\n", shdebugstr_guid(rclsid));
235 return CLASS_E_CLASSNOTAVAILABLE;
238 hres = IClassFactory_QueryInterface(pcf, iid, ppv);
239 IClassFactory_Release(pcf);
241 TRACE("-- pointer to class factory: %p\n",*ppv);
242 return hres;
245 /*************************************************************************
246 * SHCLSIDFromString [SHELL32.147]
248 * Under Windows 9x this was an ANSI version of CLSIDFromString. It also allowed
249 * to avoid dependency on ole32.dll (see SHLoadOLE for details).
251 * Under Windows NT/2000/XP this is equivalent to CLSIDFromString
253 * NOTES
254 * exported by ordinal
256 * SEE ALSO
257 * CLSIDFromString, SHLoadOLE
259 DWORD WINAPI SHCLSIDFromStringA (LPCSTR clsid, CLSID *id)
261 WCHAR buffer[40];
262 TRACE("(%p(%s) %p)\n", clsid, clsid, id);
263 if (!MultiByteToWideChar( CP_ACP, 0, clsid, -1, buffer, sizeof(buffer)/sizeof(WCHAR) ))
264 return CO_E_CLASSSTRING;
265 return CLSIDFromString( buffer, id );
267 DWORD WINAPI SHCLSIDFromStringW (LPCWSTR clsid, CLSID *id)
269 TRACE("(%p(%s) %p)\n", clsid, debugstr_w(clsid), id);
270 return CLSIDFromString((LPWSTR)clsid, id);
272 DWORD WINAPI SHCLSIDFromStringAW (LPCVOID clsid, CLSID *id)
274 if (SHELL_OsIsUnicode())
275 return SHCLSIDFromStringW (clsid, id);
276 return SHCLSIDFromStringA (clsid, id);
279 /*************************************************************************
280 * SHGetMalloc [SHELL32.@]
282 * Equivalent to CoGetMalloc(MEMCTX_TASK, ...). Under Windows 9x this function
283 * could use the shell32 built-in "mini-COM" without the need to load ole32.dll -
284 * see SHLoadOLE for details.
286 * PARAMS
287 * lpmal [O] Destination for IMalloc interface.
289 * RETURNS
290 * Success: S_OK. lpmal contains the shells IMalloc interface.
291 * Failure. An HRESULT error code.
293 * SEE ALSO
294 * CoGetMalloc, SHLoadOLE
296 HRESULT WINAPI SHGetMalloc(LPMALLOC *lpmal)
298 TRACE("(%p)\n", lpmal);
299 return CoGetMalloc(MEMCTX_TASK, lpmal);
302 /*************************************************************************
303 * SHAlloc [SHELL32.196]
305 * Equivalent to CoTaskMemAlloc. Under Windows 9x this function could use
306 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
307 * see SHLoadOLE for details.
309 * NOTES
310 * exported by ordinal
312 * SEE ALSO
313 * CoTaskMemAlloc, SHLoadOLE
315 LPVOID WINAPI SHAlloc(DWORD len)
317 LPVOID ret;
319 ret = CoTaskMemAlloc(len);
320 TRACE("%u bytes at %p\n",len, ret);
321 return ret;
324 /*************************************************************************
325 * SHFree [SHELL32.195]
327 * Equivalent to CoTaskMemFree. Under Windows 9x this function could use
328 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
329 * see SHLoadOLE for details.
331 * NOTES
332 * exported by ordinal
334 * SEE ALSO
335 * CoTaskMemFree, SHLoadOLE
337 void WINAPI SHFree(LPVOID pv)
339 TRACE("%p\n",pv);
340 CoTaskMemFree(pv);
343 /*************************************************************************
344 * SHGetDesktopFolder [SHELL32.@]
346 HRESULT WINAPI SHGetDesktopFolder(IShellFolder **psf)
348 HRESULT hres = S_OK;
349 TRACE("\n");
351 if(!psf) return E_INVALIDARG;
352 *psf = NULL;
353 hres = ISF_Desktop_Constructor(NULL, &IID_IShellFolder,(LPVOID*)psf);
355 TRACE("-- %p->(%p)\n",psf, *psf);
356 return hres;
358 /**************************************************************************
359 * Default ClassFactory Implementation
361 * SHCreateDefClassObject
363 * NOTES
364 * Helper function for dlls without their own classfactory.
365 * A generic classfactory is returned.
366 * When the CreateInstance of the cf is called the callback is executed.
369 typedef struct
371 const IClassFactoryVtbl *lpVtbl;
372 LONG ref;
373 CLSID *rclsid;
374 LPFNCREATEINSTANCE lpfnCI;
375 const IID * riidInst;
376 LONG * pcRefDll; /* pointer to refcounter in external dll (ugrrr...) */
377 } IDefClFImpl;
379 static const IClassFactoryVtbl dclfvt;
381 /**************************************************************************
382 * IDefClF_fnConstructor
385 static IClassFactory * IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI, PLONG pcRefDll, REFIID riidInst)
387 IDefClFImpl* lpclf;
389 lpclf = HeapAlloc(GetProcessHeap(),0,sizeof(IDefClFImpl));
390 lpclf->ref = 1;
391 lpclf->lpVtbl = &dclfvt;
392 lpclf->lpfnCI = lpfnCI;
393 lpclf->pcRefDll = pcRefDll;
395 if (pcRefDll) InterlockedIncrement(pcRefDll);
396 lpclf->riidInst = riidInst;
398 TRACE("(%p)%s\n",lpclf, shdebugstr_guid(riidInst));
399 return (LPCLASSFACTORY)lpclf;
401 /**************************************************************************
402 * IDefClF_fnQueryInterface
404 static HRESULT WINAPI IDefClF_fnQueryInterface(
405 LPCLASSFACTORY iface, REFIID riid, LPVOID *ppvObj)
407 IDefClFImpl *This = (IDefClFImpl *)iface;
409 TRACE("(%p)->(%s)\n",This,shdebugstr_guid(riid));
411 *ppvObj = NULL;
413 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory)) {
414 *ppvObj = This;
415 InterlockedIncrement(&This->ref);
416 return S_OK;
419 TRACE("-- E_NOINTERFACE\n");
420 return E_NOINTERFACE;
422 /******************************************************************************
423 * IDefClF_fnAddRef
425 static ULONG WINAPI IDefClF_fnAddRef(LPCLASSFACTORY iface)
427 IDefClFImpl *This = (IDefClFImpl *)iface;
428 ULONG refCount = InterlockedIncrement(&This->ref);
430 TRACE("(%p)->(count=%u)\n", This, refCount - 1);
432 return refCount;
434 /******************************************************************************
435 * IDefClF_fnRelease
437 static ULONG WINAPI IDefClF_fnRelease(LPCLASSFACTORY iface)
439 IDefClFImpl *This = (IDefClFImpl *)iface;
440 ULONG refCount = InterlockedDecrement(&This->ref);
442 TRACE("(%p)->(count=%u)\n", This, refCount + 1);
444 if (!refCount)
446 if (This->pcRefDll) InterlockedDecrement(This->pcRefDll);
448 TRACE("-- destroying IClassFactory(%p)\n",This);
449 HeapFree(GetProcessHeap(),0,This);
450 return 0;
452 return refCount;
454 /******************************************************************************
455 * IDefClF_fnCreateInstance
457 static HRESULT WINAPI IDefClF_fnCreateInstance(
458 LPCLASSFACTORY iface, LPUNKNOWN pUnkOuter, REFIID riid, LPVOID *ppvObject)
460 IDefClFImpl *This = (IDefClFImpl *)iface;
462 TRACE("%p->(%p,%s,%p)\n",This,pUnkOuter,shdebugstr_guid(riid),ppvObject);
464 *ppvObject = NULL;
466 if ( This->riidInst==NULL ||
467 IsEqualCLSID(riid, This->riidInst) ||
468 IsEqualCLSID(riid, &IID_IUnknown) )
470 return This->lpfnCI(pUnkOuter, riid, ppvObject);
473 ERR("unknown IID requested %s\n",shdebugstr_guid(riid));
474 return E_NOINTERFACE;
476 /******************************************************************************
477 * IDefClF_fnLockServer
479 static HRESULT WINAPI IDefClF_fnLockServer(LPCLASSFACTORY iface, BOOL fLock)
481 IDefClFImpl *This = (IDefClFImpl *)iface;
482 TRACE("%p->(0x%x), not implemented\n",This, fLock);
483 return E_NOTIMPL;
486 static const IClassFactoryVtbl dclfvt =
488 IDefClF_fnQueryInterface,
489 IDefClF_fnAddRef,
490 IDefClF_fnRelease,
491 IDefClF_fnCreateInstance,
492 IDefClF_fnLockServer
495 /******************************************************************************
496 * SHCreateDefClassObject [SHELL32.70]
498 HRESULT WINAPI SHCreateDefClassObject(
499 REFIID riid,
500 LPVOID* ppv,
501 LPFNCREATEINSTANCE lpfnCI, /* [in] create instance callback entry */
502 LPDWORD pcRefDll, /* [in/out] ref count of the dll */
503 REFIID riidInst) /* [in] optional interface to the instance */
505 IClassFactory * pcf;
507 TRACE("%s %p %p %p %s\n",
508 shdebugstr_guid(riid), ppv, lpfnCI, pcRefDll, shdebugstr_guid(riidInst));
510 if (! IsEqualCLSID(riid, &IID_IClassFactory) ) return E_NOINTERFACE;
511 if (! (pcf = IDefClF_fnConstructor(lpfnCI, (PLONG)pcRefDll, riidInst))) return E_OUTOFMEMORY;
512 *ppv = pcf;
513 return NOERROR;
516 /*************************************************************************
517 * DragAcceptFiles [SHELL32.@]
519 void WINAPI DragAcceptFiles(HWND hWnd, BOOL b)
521 LONG exstyle;
523 if( !IsWindow(hWnd) ) return;
524 exstyle = GetWindowLongA(hWnd,GWL_EXSTYLE);
525 if (b)
526 exstyle |= WS_EX_ACCEPTFILES;
527 else
528 exstyle &= ~WS_EX_ACCEPTFILES;
529 SetWindowLongA(hWnd,GWL_EXSTYLE,exstyle);
532 /*************************************************************************
533 * DragFinish [SHELL32.@]
535 void WINAPI DragFinish(HDROP h)
537 TRACE("\n");
538 GlobalFree((HGLOBAL)h);
541 /*************************************************************************
542 * DragQueryPoint [SHELL32.@]
544 BOOL WINAPI DragQueryPoint(HDROP hDrop, POINT *p)
546 DROPFILES *lpDropFileStruct;
547 BOOL bRet;
549 TRACE("\n");
551 lpDropFileStruct = GlobalLock(hDrop);
553 *p = lpDropFileStruct->pt;
554 bRet = lpDropFileStruct->fNC;
556 GlobalUnlock(hDrop);
557 return bRet;
560 /*************************************************************************
561 * DragQueryFileA [SHELL32.@]
562 * DragQueryFile [SHELL32.@]
564 UINT WINAPI DragQueryFileA(
565 HDROP hDrop,
566 UINT lFile,
567 LPSTR lpszFile,
568 UINT lLength)
570 LPSTR lpDrop;
571 UINT i = 0;
572 DROPFILES *lpDropFileStruct = GlobalLock(hDrop);
574 TRACE("(%p, %x, %p, %u)\n", hDrop,lFile,lpszFile,lLength);
576 if(!lpDropFileStruct) goto end;
578 lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->pFiles;
580 if(lpDropFileStruct->fWide) {
581 LPWSTR lpszFileW = NULL;
583 if(lpszFile) {
584 lpszFileW = HeapAlloc(GetProcessHeap(), 0, lLength*sizeof(WCHAR));
585 if(lpszFileW == NULL) {
586 goto end;
589 i = DragQueryFileW(hDrop, lFile, lpszFileW, lLength);
591 if(lpszFileW) {
592 WideCharToMultiByte(CP_ACP, 0, lpszFileW, -1, lpszFile, lLength, 0, NULL);
593 HeapFree(GetProcessHeap(), 0, lpszFileW);
595 goto end;
598 while (i++ < lFile)
600 while (*lpDrop++); /* skip filename */
601 if (!*lpDrop)
603 i = (lFile == 0xFFFFFFFF) ? i : 0;
604 goto end;
608 i = strlen(lpDrop);
609 if (!lpszFile ) goto end; /* needed buffer size */
610 lstrcpynA (lpszFile, lpDrop, lLength);
611 end:
612 GlobalUnlock(hDrop);
613 return i;
616 /*************************************************************************
617 * DragQueryFileW [SHELL32.@]
619 UINT WINAPI DragQueryFileW(
620 HDROP hDrop,
621 UINT lFile,
622 LPWSTR lpszwFile,
623 UINT lLength)
625 LPWSTR lpwDrop;
626 UINT i = 0;
627 DROPFILES *lpDropFileStruct = GlobalLock(hDrop);
629 TRACE("(%p, %x, %p, %u)\n", hDrop,lFile,lpszwFile,lLength);
631 if(!lpDropFileStruct) goto end;
633 lpwDrop = (LPWSTR) ((LPSTR)lpDropFileStruct + lpDropFileStruct->pFiles);
635 if(lpDropFileStruct->fWide == FALSE) {
636 LPSTR lpszFileA = NULL;
638 if(lpszwFile) {
639 lpszFileA = HeapAlloc(GetProcessHeap(), 0, lLength);
640 if(lpszFileA == NULL) {
641 goto end;
644 i = DragQueryFileA(hDrop, lFile, lpszFileA, lLength);
646 if(lpszFileA) {
647 MultiByteToWideChar(CP_ACP, 0, lpszFileA, -1, lpszwFile, lLength);
648 HeapFree(GetProcessHeap(), 0, lpszFileA);
650 goto end;
653 i = 0;
654 while (i++ < lFile)
656 while (*lpwDrop++); /* skip filename */
657 if (!*lpwDrop)
659 i = (lFile == 0xFFFFFFFF) ? i : 0;
660 goto end;
664 i = strlenW(lpwDrop);
665 if ( !lpszwFile) goto end; /* needed buffer size */
666 lstrcpynW (lpszwFile, lpwDrop, lLength);
667 end:
668 GlobalUnlock(hDrop);
669 return i;