quartz: Reset EcCompleteCount before starting filters.
[wine/wine64.git] / dlls / shell32 / shellole.c
blob64b002481119f77a99cbfb73153024f0da1cb4de
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 static const WCHAR sShell32[12] = {'S','H','E','L','L','3','2','.','D','L','L','\0'};
54 /**************************************************************************
55 * Default ClassFactory types
57 typedef HRESULT (CALLBACK *LPFNCREATEINSTANCE)(IUnknown* pUnkOuter, REFIID riid, LPVOID* ppvObject);
58 static IClassFactory * IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI, PLONG pcRefDll, REFIID riidInst);
60 /* this table contains all CLSID's of shell32 objects */
61 static const struct {
62 REFIID riid;
63 LPFNCREATEINSTANCE lpfnCI;
64 } InterfaceTable[] = {
65 {&CLSID_ShellFSFolder, &IFSFolder_Constructor},
66 {&CLSID_MyComputer, &ISF_MyComputer_Constructor},
67 {&CLSID_NetworkPlaces, &ISF_NetworkPlaces_Constructor},
68 {&CLSID_ShellDesktop, &ISF_Desktop_Constructor},
69 {&CLSID_ShellLink, &IShellLink_Constructor},
70 {&CLSID_DragDropHelper, &IDropTargetHelper_Constructor},
71 {&CLSID_ControlPanel, &IControlPanel_Constructor},
72 {&CLSID_AutoComplete, &IAutoComplete_Constructor},
73 {&CLSID_UnixFolder, &UnixFolder_Constructor},
74 {&CLSID_UnixDosFolder, &UnixDosFolder_Constructor},
75 {&CLSID_FolderShortcut, &FolderShortcut_Constructor},
76 {&CLSID_MyDocuments, &MyDocuments_Constructor},
77 {&CLSID_RecycleBin, &RecycleBin_Constructor},
78 {NULL,NULL}
82 /* FIXME: this should be SHLWAPI.24 since we can't yet import by ordinal */
84 DWORD WINAPI __SHGUIDToStringW (REFGUID guid, LPWSTR str)
86 WCHAR sFormat[52] = {'{','%','0','8','l','x','-','%','0','4',
87 'x','-','%','0','4','x','-','%','0','2',
88 'x','%','0','2','x','-','%','0','2','x',
89 '%','0','2','x','%','0','2','x','%','0',
90 '2','x','%','0','2','x','%','0','2','x',
91 '}','\0'};
93 return wsprintfW ( str, sFormat,
94 guid->Data1, guid->Data2, guid->Data3,
95 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
96 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7] );
100 /*************************************************************************
101 * SHCoCreateInstance [SHELL32.102]
103 * Equivalent to CoCreateInstance. Under Windows 9x this function could sometimes
104 * use the shell32 built-in "mini-COM" without the need to load ole32.dll - see
105 * SHLoadOLE for details.
107 * Under wine if a "LoadWithoutCOM" value is present or the object resides in
108 * shell32.dll the function will load the object manually without the help of ole32
110 * NOTES
111 * exported by ordinal
113 * SEE ALSO
114 * CoCreateInstace, SHLoadOLE
116 HRESULT WINAPI SHCoCreateInstance(
117 LPCWSTR aclsid,
118 const CLSID *clsid,
119 LPUNKNOWN pUnkOuter,
120 REFIID refiid,
121 LPVOID *ppv)
123 DWORD hres;
124 IID iid;
125 const CLSID * myclsid = clsid;
126 WCHAR sKeyName[MAX_PATH];
127 const WCHAR sCLSID[7] = {'C','L','S','I','D','\\','\0'};
128 WCHAR sClassID[60];
129 const WCHAR sInProcServer32[16] ={'\\','I','n','p','r','o','c','S','e','r','v','e','r','3','2','\0'};
130 const WCHAR sLoadWithoutCOM[15] ={'L','o','a','d','W','i','t','h','o','u','t','C','O','M','\0'};
131 WCHAR sDllPath[MAX_PATH];
132 HKEY hKey = 0;
133 DWORD dwSize;
134 IClassFactory * pcf = NULL;
136 if(!ppv) return E_POINTER;
137 *ppv=NULL;
139 /* if the clsid is a string, convert it */
140 if (!clsid)
142 if (!aclsid) return REGDB_E_CLASSNOTREG;
143 SHCLSIDFromStringW(aclsid, &iid);
144 myclsid = &iid;
147 TRACE("(%p,%s,unk:%p,%s,%p)\n",
148 aclsid,shdebugstr_guid(myclsid),pUnkOuter,shdebugstr_guid(refiid),ppv);
150 if (SUCCEEDED(DllGetClassObject(myclsid, &IID_IClassFactory,(LPVOID*)&pcf)))
152 hres = IClassFactory_CreateInstance(pcf, pUnkOuter, refiid, ppv);
153 IClassFactory_Release(pcf);
154 goto end;
157 /* we look up the dll path in the registry */
158 __SHGUIDToStringW(myclsid, sClassID);
159 lstrcpyW(sKeyName, sCLSID);
160 lstrcatW(sKeyName, sClassID);
161 lstrcatW(sKeyName, sInProcServer32);
163 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, sKeyName, 0, KEY_READ, &hKey))
164 return E_ACCESSDENIED;
166 /* if a special registry key is set, we load a shell extension without help of OLE32 */
167 if (!SHQueryValueExW(hKey, sLoadWithoutCOM, 0, 0, 0, 0))
169 /* load an external dll without ole32 */
170 HANDLE hLibrary;
171 typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid, REFIID iid, LPVOID *ppv);
172 DllGetClassObjectFunc DllGetClassObject;
174 dwSize = sizeof(sDllPath);
175 SHQueryValueExW(hKey, NULL, 0,0, sDllPath, &dwSize );
177 if ((hLibrary = LoadLibraryExW(sDllPath, 0, LOAD_WITH_ALTERED_SEARCH_PATH)) == 0) {
178 ERR("couldn't load InprocServer32 dll %s\n", debugstr_w(sDllPath));
179 hres = E_ACCESSDENIED;
180 goto end;
181 } else if (!(DllGetClassObject = (DllGetClassObjectFunc)GetProcAddress(hLibrary, "DllGetClassObject"))) {
182 ERR("couldn't find function DllGetClassObject in %s\n", debugstr_w(sDllPath));
183 FreeLibrary( hLibrary );
184 hres = E_ACCESSDENIED;
185 goto end;
186 } else if (! SUCCEEDED(hres = DllGetClassObject(myclsid, &IID_IClassFactory, (LPVOID*)&pcf))) {
187 TRACE("GetClassObject failed 0x%08x\n", hres);
188 goto end;
191 hres = IClassFactory_CreateInstance(pcf, pUnkOuter, refiid, ppv);
192 IClassFactory_Release(pcf);
193 } else {
195 /* load an external dll in the usual way */
196 hres = CoCreateInstance(myclsid, pUnkOuter, CLSCTX_INPROC_SERVER, refiid, ppv);
199 end:
200 if (hKey) RegCloseKey(hKey);
201 if(hres!=S_OK)
203 ERR("failed (0x%08x) to create CLSID:%s IID:%s\n",
204 hres, shdebugstr_guid(myclsid), shdebugstr_guid(refiid));
205 ERR("class not found in registry\n");
208 TRACE("-- instance: %p\n",*ppv);
209 return hres;
212 /*************************************************************************
213 * DllGetClassObject [SHELL32.@]
214 * SHDllGetClassObject [SHELL32.128]
216 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
218 HRESULT hres = E_OUTOFMEMORY;
219 IClassFactory * pcf = NULL;
220 int i;
222 TRACE("CLSID:%s,IID:%s\n",shdebugstr_guid(rclsid),shdebugstr_guid(iid));
224 if (!ppv) return E_INVALIDARG;
225 *ppv = NULL;
227 /* search our internal interface table */
228 for(i=0;InterfaceTable[i].riid;i++) {
229 if(IsEqualIID(InterfaceTable[i].riid, rclsid)) {
230 TRACE("index[%u]\n", i);
231 pcf = IDefClF_fnConstructor(InterfaceTable[i].lpfnCI, NULL, NULL);
235 if (!pcf) {
236 FIXME("failed for CLSID=%s\n", shdebugstr_guid(rclsid));
237 return CLASS_E_CLASSNOTAVAILABLE;
240 hres = IClassFactory_QueryInterface(pcf, iid, ppv);
241 IClassFactory_Release(pcf);
243 TRACE("-- pointer to class factory: %p\n",*ppv);
244 return hres;
247 /*************************************************************************
248 * SHCLSIDFromString [SHELL32.147]
250 * Under Windows 9x this was an ANSI version of CLSIDFromString. It also allowed
251 * to avoid dependency on ole32.dll (see SHLoadOLE for details).
253 * Under Windows NT/2000/XP this is equivalent to CLSIDFromString
255 * NOTES
256 * exported by ordinal
258 * SEE ALSO
259 * CLSIDFromString, SHLoadOLE
261 DWORD WINAPI SHCLSIDFromStringA (LPCSTR clsid, CLSID *id)
263 WCHAR buffer[40];
264 TRACE("(%p(%s) %p)\n", clsid, clsid, id);
265 if (!MultiByteToWideChar( CP_ACP, 0, clsid, -1, buffer, sizeof(buffer)/sizeof(WCHAR) ))
266 return CO_E_CLASSSTRING;
267 return CLSIDFromString( buffer, id );
269 DWORD WINAPI SHCLSIDFromStringW (LPCWSTR clsid, CLSID *id)
271 TRACE("(%p(%s) %p)\n", clsid, debugstr_w(clsid), id);
272 return CLSIDFromString((LPWSTR)clsid, id);
274 DWORD WINAPI SHCLSIDFromStringAW (LPCVOID clsid, CLSID *id)
276 if (SHELL_OsIsUnicode())
277 return SHCLSIDFromStringW (clsid, id);
278 return SHCLSIDFromStringA (clsid, id);
281 /*************************************************************************
282 * SHGetMalloc [SHELL32.@]
284 * Equivalent to CoGetMalloc(MEMCTX_TASK, ...). Under Windows 9x this function
285 * could use the shell32 built-in "mini-COM" without the need to load ole32.dll -
286 * see SHLoadOLE for details.
288 * PARAMS
289 * lpmal [O] Destination for IMalloc interface.
291 * RETURNS
292 * Success: S_OK. lpmal contains the shells IMalloc interface.
293 * Failure. An HRESULT error code.
295 * SEE ALSO
296 * CoGetMalloc, SHLoadOLE
298 HRESULT WINAPI SHGetMalloc(LPMALLOC *lpmal)
300 TRACE("(%p)\n", lpmal);
301 return CoGetMalloc(MEMCTX_TASK, lpmal);
304 /*************************************************************************
305 * SHAlloc [SHELL32.196]
307 * Equivalent to CoTaskMemAlloc. Under Windows 9x this function could use
308 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
309 * see SHLoadOLE for details.
311 * NOTES
312 * exported by ordinal
314 * SEE ALSO
315 * CoTaskMemAlloc, SHLoadOLE
317 LPVOID WINAPI SHAlloc(DWORD len)
319 LPVOID ret;
321 ret = CoTaskMemAlloc(len);
322 TRACE("%u bytes at %p\n",len, ret);
323 return ret;
326 /*************************************************************************
327 * SHFree [SHELL32.195]
329 * Equivalent to CoTaskMemFree. Under Windows 9x this function could use
330 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
331 * see SHLoadOLE for details.
333 * NOTES
334 * exported by ordinal
336 * SEE ALSO
337 * CoTaskMemFree, SHLoadOLE
339 void WINAPI SHFree(LPVOID pv)
341 TRACE("%p\n",pv);
342 CoTaskMemFree(pv);
345 /*************************************************************************
346 * SHGetDesktopFolder [SHELL32.@]
348 HRESULT WINAPI SHGetDesktopFolder(IShellFolder **psf)
350 HRESULT hres = S_OK;
351 TRACE("\n");
353 if(!psf) return E_INVALIDARG;
354 *psf = NULL;
355 hres = ISF_Desktop_Constructor(NULL, &IID_IShellFolder,(LPVOID*)psf);
357 TRACE("-- %p->(%p)\n",psf, *psf);
358 return hres;
360 /**************************************************************************
361 * Default ClassFactory Implementation
363 * SHCreateDefClassObject
365 * NOTES
366 * Helper function for dlls without their own classfactory.
367 * A generic classfactory is returned.
368 * When the CreateInstance of the cf is called the callback is executed.
371 typedef struct
373 const IClassFactoryVtbl *lpVtbl;
374 LONG ref;
375 CLSID *rclsid;
376 LPFNCREATEINSTANCE lpfnCI;
377 const IID * riidInst;
378 LONG * pcRefDll; /* pointer to refcounter in external dll (ugrrr...) */
379 } IDefClFImpl;
381 static const IClassFactoryVtbl dclfvt;
383 /**************************************************************************
384 * IDefClF_fnConstructor
387 static IClassFactory * IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI, PLONG pcRefDll, REFIID riidInst)
389 IDefClFImpl* lpclf;
391 lpclf = HeapAlloc(GetProcessHeap(),0,sizeof(IDefClFImpl));
392 lpclf->ref = 1;
393 lpclf->lpVtbl = &dclfvt;
394 lpclf->lpfnCI = lpfnCI;
395 lpclf->pcRefDll = pcRefDll;
397 if (pcRefDll) InterlockedIncrement(pcRefDll);
398 lpclf->riidInst = riidInst;
400 TRACE("(%p)%s\n",lpclf, shdebugstr_guid(riidInst));
401 return (LPCLASSFACTORY)lpclf;
403 /**************************************************************************
404 * IDefClF_fnQueryInterface
406 static HRESULT WINAPI IDefClF_fnQueryInterface(
407 LPCLASSFACTORY iface, REFIID riid, LPVOID *ppvObj)
409 IDefClFImpl *This = (IDefClFImpl *)iface;
411 TRACE("(%p)->(%s)\n",This,shdebugstr_guid(riid));
413 *ppvObj = NULL;
415 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory)) {
416 *ppvObj = This;
417 InterlockedIncrement(&This->ref);
418 return S_OK;
421 TRACE("-- E_NOINTERFACE\n");
422 return E_NOINTERFACE;
424 /******************************************************************************
425 * IDefClF_fnAddRef
427 static ULONG WINAPI IDefClF_fnAddRef(LPCLASSFACTORY iface)
429 IDefClFImpl *This = (IDefClFImpl *)iface;
430 ULONG refCount = InterlockedIncrement(&This->ref);
432 TRACE("(%p)->(count=%u)\n", This, refCount - 1);
434 return refCount;
436 /******************************************************************************
437 * IDefClF_fnRelease
439 static ULONG WINAPI IDefClF_fnRelease(LPCLASSFACTORY iface)
441 IDefClFImpl *This = (IDefClFImpl *)iface;
442 ULONG refCount = InterlockedDecrement(&This->ref);
444 TRACE("(%p)->(count=%u)\n", This, refCount + 1);
446 if (!refCount)
448 if (This->pcRefDll) InterlockedDecrement(This->pcRefDll);
450 TRACE("-- destroying IClassFactory(%p)\n",This);
451 HeapFree(GetProcessHeap(),0,This);
452 return 0;
454 return refCount;
456 /******************************************************************************
457 * IDefClF_fnCreateInstance
459 static HRESULT WINAPI IDefClF_fnCreateInstance(
460 LPCLASSFACTORY iface, LPUNKNOWN pUnkOuter, REFIID riid, LPVOID *ppvObject)
462 IDefClFImpl *This = (IDefClFImpl *)iface;
464 TRACE("%p->(%p,%s,%p)\n",This,pUnkOuter,shdebugstr_guid(riid),ppvObject);
466 *ppvObject = NULL;
468 if ( This->riidInst==NULL ||
469 IsEqualCLSID(riid, This->riidInst) ||
470 IsEqualCLSID(riid, &IID_IUnknown) )
472 return This->lpfnCI(pUnkOuter, riid, ppvObject);
475 ERR("unknown IID requested %s\n",shdebugstr_guid(riid));
476 return E_NOINTERFACE;
478 /******************************************************************************
479 * IDefClF_fnLockServer
481 static HRESULT WINAPI IDefClF_fnLockServer(LPCLASSFACTORY iface, BOOL fLock)
483 IDefClFImpl *This = (IDefClFImpl *)iface;
484 TRACE("%p->(0x%x), not implemented\n",This, fLock);
485 return E_NOTIMPL;
488 static const IClassFactoryVtbl dclfvt =
490 IDefClF_fnQueryInterface,
491 IDefClF_fnAddRef,
492 IDefClF_fnRelease,
493 IDefClF_fnCreateInstance,
494 IDefClF_fnLockServer
497 /******************************************************************************
498 * SHCreateDefClassObject [SHELL32.70]
500 HRESULT WINAPI SHCreateDefClassObject(
501 REFIID riid,
502 LPVOID* ppv,
503 LPFNCREATEINSTANCE lpfnCI, /* [in] create instance callback entry */
504 LPDWORD pcRefDll, /* [in/out] ref count of the dll */
505 REFIID riidInst) /* [in] optional interface to the instance */
507 IClassFactory * pcf;
509 TRACE("%s %p %p %p %s\n",
510 shdebugstr_guid(riid), ppv, lpfnCI, pcRefDll, shdebugstr_guid(riidInst));
512 if (! IsEqualCLSID(riid, &IID_IClassFactory) ) return E_NOINTERFACE;
513 if (! (pcf = IDefClF_fnConstructor(lpfnCI, (PLONG)pcRefDll, riidInst))) return E_OUTOFMEMORY;
514 *ppv = pcf;
515 return NOERROR;
518 /*************************************************************************
519 * DragAcceptFiles [SHELL32.@]
521 void WINAPI DragAcceptFiles(HWND hWnd, BOOL b)
523 LONG exstyle;
525 if( !IsWindow(hWnd) ) return;
526 exstyle = GetWindowLongA(hWnd,GWL_EXSTYLE);
527 if (b)
528 exstyle |= WS_EX_ACCEPTFILES;
529 else
530 exstyle &= ~WS_EX_ACCEPTFILES;
531 SetWindowLongA(hWnd,GWL_EXSTYLE,exstyle);
534 /*************************************************************************
535 * DragFinish [SHELL32.@]
537 void WINAPI DragFinish(HDROP h)
539 TRACE("\n");
540 GlobalFree((HGLOBAL)h);
543 /*************************************************************************
544 * DragQueryPoint [SHELL32.@]
546 BOOL WINAPI DragQueryPoint(HDROP hDrop, POINT *p)
548 DROPFILES *lpDropFileStruct;
549 BOOL bRet;
551 TRACE("\n");
553 lpDropFileStruct = (DROPFILES *) GlobalLock(hDrop);
555 *p = lpDropFileStruct->pt;
556 bRet = lpDropFileStruct->fNC;
558 GlobalUnlock(hDrop);
559 return bRet;
562 /*************************************************************************
563 * DragQueryFileA [SHELL32.@]
564 * DragQueryFile [SHELL32.@]
566 UINT WINAPI DragQueryFileA(
567 HDROP hDrop,
568 UINT lFile,
569 LPSTR lpszFile,
570 UINT lLength)
572 LPSTR lpDrop;
573 UINT i = 0;
574 DROPFILES *lpDropFileStruct = (DROPFILES *) GlobalLock(hDrop);
576 TRACE("(%p, %x, %p, %u)\n", hDrop,lFile,lpszFile,lLength);
578 if(!lpDropFileStruct) goto end;
580 lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->pFiles;
582 if(lpDropFileStruct->fWide) {
583 LPWSTR lpszFileW = NULL;
585 if(lpszFile) {
586 lpszFileW = HeapAlloc(GetProcessHeap(), 0, lLength*sizeof(WCHAR));
587 if(lpszFileW == NULL) {
588 goto end;
591 i = DragQueryFileW(hDrop, lFile, lpszFileW, lLength);
593 if(lpszFileW) {
594 WideCharToMultiByte(CP_ACP, 0, lpszFileW, -1, lpszFile, lLength, 0, NULL);
595 HeapFree(GetProcessHeap(), 0, lpszFileW);
597 goto end;
600 while (i++ < lFile)
602 while (*lpDrop++); /* skip filename */
603 if (!*lpDrop)
605 i = (lFile == 0xFFFFFFFF) ? i : 0;
606 goto end;
610 i = strlen(lpDrop);
611 if (!lpszFile ) goto end; /* needed buffer size */
612 lstrcpynA (lpszFile, lpDrop, lLength);
613 end:
614 GlobalUnlock(hDrop);
615 return i;
618 /*************************************************************************
619 * DragQueryFileW [SHELL32.@]
621 UINT WINAPI DragQueryFileW(
622 HDROP hDrop,
623 UINT lFile,
624 LPWSTR lpszwFile,
625 UINT lLength)
627 LPWSTR lpwDrop;
628 UINT i = 0;
629 DROPFILES *lpDropFileStruct = (DROPFILES *) GlobalLock(hDrop);
631 TRACE("(%p, %x, %p, %u)\n", hDrop,lFile,lpszwFile,lLength);
633 if(!lpDropFileStruct) goto end;
635 lpwDrop = (LPWSTR) ((LPSTR)lpDropFileStruct + lpDropFileStruct->pFiles);
637 if(lpDropFileStruct->fWide == FALSE) {
638 LPSTR lpszFileA = NULL;
640 if(lpszwFile) {
641 lpszFileA = HeapAlloc(GetProcessHeap(), 0, lLength);
642 if(lpszFileA == NULL) {
643 goto end;
646 i = DragQueryFileA(hDrop, lFile, lpszFileA, lLength);
648 if(lpszFileA) {
649 MultiByteToWideChar(CP_ACP, 0, lpszFileA, -1, lpszwFile, lLength);
650 HeapFree(GetProcessHeap(), 0, lpszFileA);
652 goto end;
655 i = 0;
656 while (i++ < lFile)
658 while (*lpwDrop++); /* skip filename */
659 if (!*lpwDrop)
661 i = (lFile == 0xFFFFFFFF) ? i : 0;
662 goto end;
666 i = strlenW(lpwDrop);
667 if ( !lpszwFile) goto end; /* needed buffer size */
668 lstrcpynW (lpszwFile, lpwDrop, lLength);
669 end:
670 GlobalUnlock(hDrop);
671 return i;