user32: Fix user_thread_info for 64-bits
[wine/wine64.git] / dlls / shell32 / shellole.c
blobe67556f57c4ee068bd230b127393c971b09124fa
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 INT WINAPI SHStringFromGUIDW(REFGUID guid, LPWSTR lpszDest, INT cchMax); /* shlwapi.24 */
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}
81 /*************************************************************************
82 * SHCoCreateInstance [SHELL32.102]
84 * Equivalent to CoCreateInstance. Under Windows 9x this function could sometimes
85 * use the shell32 built-in "mini-COM" without the need to load ole32.dll - see
86 * SHLoadOLE for details.
88 * Under wine if a "LoadWithoutCOM" value is present or the object resides in
89 * shell32.dll the function will load the object manually without the help of ole32
91 * NOTES
92 * exported by ordinal
94 * SEE ALSO
95 * CoCreateInstace, SHLoadOLE
97 HRESULT WINAPI SHCoCreateInstance(
98 LPCWSTR aclsid,
99 const CLSID *clsid,
100 LPUNKNOWN pUnkOuter,
101 REFIID refiid,
102 LPVOID *ppv)
104 DWORD hres;
105 IID iid;
106 const CLSID * myclsid = clsid;
107 WCHAR sKeyName[MAX_PATH];
108 const WCHAR sCLSID[7] = {'C','L','S','I','D','\\','\0'};
109 WCHAR sClassID[60];
110 const WCHAR sInProcServer32[16] ={'\\','I','n','p','r','o','c','S','e','r','v','e','r','3','2','\0'};
111 const WCHAR sLoadWithoutCOM[15] ={'L','o','a','d','W','i','t','h','o','u','t','C','O','M','\0'};
112 WCHAR sDllPath[MAX_PATH];
113 HKEY hKey = 0;
114 DWORD dwSize;
115 IClassFactory * pcf = NULL;
117 if(!ppv) return E_POINTER;
118 *ppv=NULL;
120 /* if the clsid is a string, convert it */
121 if (!clsid)
123 if (!aclsid) return REGDB_E_CLASSNOTREG;
124 SHCLSIDFromStringW(aclsid, &iid);
125 myclsid = &iid;
128 TRACE("(%p,%s,unk:%p,%s,%p)\n",
129 aclsid,shdebugstr_guid(myclsid),pUnkOuter,shdebugstr_guid(refiid),ppv);
131 if (SUCCEEDED(DllGetClassObject(myclsid, &IID_IClassFactory,(LPVOID*)&pcf)))
133 hres = IClassFactory_CreateInstance(pcf, pUnkOuter, refiid, ppv);
134 IClassFactory_Release(pcf);
135 goto end;
138 /* we look up the dll path in the registry */
139 SHStringFromGUIDW(myclsid, sClassID, sizeof(sClassID)/sizeof(WCHAR));
140 lstrcpyW(sKeyName, sCLSID);
141 lstrcatW(sKeyName, sClassID);
142 lstrcatW(sKeyName, sInProcServer32);
144 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, sKeyName, 0, KEY_READ, &hKey))
145 return E_ACCESSDENIED;
147 /* if a special registry key is set, we load a shell extension without help of OLE32 */
148 if (!SHQueryValueExW(hKey, sLoadWithoutCOM, 0, 0, 0, 0))
150 /* load an external dll without ole32 */
151 HANDLE hLibrary;
152 typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid, REFIID iid, LPVOID *ppv);
153 DllGetClassObjectFunc DllGetClassObject;
155 dwSize = sizeof(sDllPath);
156 SHQueryValueExW(hKey, NULL, 0,0, sDllPath, &dwSize );
158 if ((hLibrary = LoadLibraryExW(sDllPath, 0, LOAD_WITH_ALTERED_SEARCH_PATH)) == 0) {
159 ERR("couldn't load InprocServer32 dll %s\n", debugstr_w(sDllPath));
160 hres = E_ACCESSDENIED;
161 goto end;
162 } else if (!(DllGetClassObject = (DllGetClassObjectFunc)GetProcAddress(hLibrary, "DllGetClassObject"))) {
163 ERR("couldn't find function DllGetClassObject in %s\n", debugstr_w(sDllPath));
164 FreeLibrary( hLibrary );
165 hres = E_ACCESSDENIED;
166 goto end;
167 } else if (FAILED(hres = DllGetClassObject(myclsid, &IID_IClassFactory, (LPVOID*)&pcf))) {
168 TRACE("GetClassObject failed 0x%08x\n", hres);
169 goto end;
172 hres = IClassFactory_CreateInstance(pcf, pUnkOuter, refiid, ppv);
173 IClassFactory_Release(pcf);
174 } else {
176 /* load an external dll in the usual way */
177 hres = CoCreateInstance(myclsid, pUnkOuter, CLSCTX_INPROC_SERVER, refiid, ppv);
180 end:
181 if (hKey) RegCloseKey(hKey);
182 if(hres!=S_OK)
184 ERR("failed (0x%08x) to create CLSID:%s IID:%s\n",
185 hres, shdebugstr_guid(myclsid), shdebugstr_guid(refiid));
186 ERR("class not found in registry\n");
189 TRACE("-- instance: %p\n",*ppv);
190 return hres;
193 /*************************************************************************
194 * DllGetClassObject [SHELL32.@]
195 * SHDllGetClassObject [SHELL32.128]
197 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
199 HRESULT hres = E_OUTOFMEMORY;
200 IClassFactory * pcf = NULL;
201 int i;
203 TRACE("CLSID:%s,IID:%s\n",shdebugstr_guid(rclsid),shdebugstr_guid(iid));
205 if (!ppv) return E_INVALIDARG;
206 *ppv = NULL;
208 /* search our internal interface table */
209 for(i=0;InterfaceTable[i].riid;i++) {
210 if(IsEqualIID(InterfaceTable[i].riid, rclsid)) {
211 TRACE("index[%u]\n", i);
212 pcf = IDefClF_fnConstructor(InterfaceTable[i].lpfnCI, NULL, NULL);
216 if (!pcf) {
217 FIXME("failed for CLSID=%s\n", shdebugstr_guid(rclsid));
218 return CLASS_E_CLASSNOTAVAILABLE;
221 hres = IClassFactory_QueryInterface(pcf, iid, ppv);
222 IClassFactory_Release(pcf);
224 TRACE("-- pointer to class factory: %p\n",*ppv);
225 return hres;
228 /*************************************************************************
229 * SHCLSIDFromString [SHELL32.147]
231 * Under Windows 9x this was an ANSI version of CLSIDFromString. It also allowed
232 * to avoid dependency on ole32.dll (see SHLoadOLE for details).
234 * Under Windows NT/2000/XP this is equivalent to CLSIDFromString
236 * NOTES
237 * exported by ordinal
239 * SEE ALSO
240 * CLSIDFromString, SHLoadOLE
242 DWORD WINAPI SHCLSIDFromStringA (LPCSTR clsid, CLSID *id)
244 WCHAR buffer[40];
245 TRACE("(%p(%s) %p)\n", clsid, clsid, id);
246 if (!MultiByteToWideChar( CP_ACP, 0, clsid, -1, buffer, sizeof(buffer)/sizeof(WCHAR) ))
247 return CO_E_CLASSSTRING;
248 return CLSIDFromString( buffer, id );
250 DWORD WINAPI SHCLSIDFromStringW (LPCWSTR clsid, CLSID *id)
252 TRACE("(%p(%s) %p)\n", clsid, debugstr_w(clsid), id);
253 return CLSIDFromString((LPWSTR)clsid, id);
255 DWORD WINAPI SHCLSIDFromStringAW (LPCVOID clsid, CLSID *id)
257 if (SHELL_OsIsUnicode())
258 return SHCLSIDFromStringW (clsid, id);
259 return SHCLSIDFromStringA (clsid, id);
262 /*************************************************************************
263 * SHGetMalloc [SHELL32.@]
265 * Equivalent to CoGetMalloc(MEMCTX_TASK, ...). Under Windows 9x this function
266 * could use the shell32 built-in "mini-COM" without the need to load ole32.dll -
267 * see SHLoadOLE for details.
269 * PARAMS
270 * lpmal [O] Destination for IMalloc interface.
272 * RETURNS
273 * Success: S_OK. lpmal contains the shells IMalloc interface.
274 * Failure. An HRESULT error code.
276 * SEE ALSO
277 * CoGetMalloc, SHLoadOLE
279 HRESULT WINAPI SHGetMalloc(LPMALLOC *lpmal)
281 TRACE("(%p)\n", lpmal);
282 return CoGetMalloc(MEMCTX_TASK, lpmal);
285 /*************************************************************************
286 * SHAlloc [SHELL32.196]
288 * Equivalent to CoTaskMemAlloc. Under Windows 9x this function could use
289 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
290 * see SHLoadOLE for details.
292 * NOTES
293 * exported by ordinal
295 * SEE ALSO
296 * CoTaskMemAlloc, SHLoadOLE
298 LPVOID WINAPI SHAlloc(DWORD len)
300 LPVOID ret;
302 ret = CoTaskMemAlloc(len);
303 TRACE("%u bytes at %p\n",len, ret);
304 return ret;
307 /*************************************************************************
308 * SHFree [SHELL32.195]
310 * Equivalent to CoTaskMemFree. Under Windows 9x this function could use
311 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
312 * see SHLoadOLE for details.
314 * NOTES
315 * exported by ordinal
317 * SEE ALSO
318 * CoTaskMemFree, SHLoadOLE
320 void WINAPI SHFree(LPVOID pv)
322 TRACE("%p\n",pv);
323 CoTaskMemFree(pv);
326 /*************************************************************************
327 * SHGetDesktopFolder [SHELL32.@]
329 HRESULT WINAPI SHGetDesktopFolder(IShellFolder **psf)
331 HRESULT hres = S_OK;
332 TRACE("\n");
334 if(!psf) return E_INVALIDARG;
335 *psf = NULL;
336 hres = ISF_Desktop_Constructor(NULL, &IID_IShellFolder,(LPVOID*)psf);
338 TRACE("-- %p->(%p)\n",psf, *psf);
339 return hres;
341 /**************************************************************************
342 * Default ClassFactory Implementation
344 * SHCreateDefClassObject
346 * NOTES
347 * Helper function for dlls without their own classfactory.
348 * A generic classfactory is returned.
349 * When the CreateInstance of the cf is called the callback is executed.
352 typedef struct
354 const IClassFactoryVtbl *lpVtbl;
355 LONG ref;
356 CLSID *rclsid;
357 LPFNCREATEINSTANCE lpfnCI;
358 const IID * riidInst;
359 LONG * pcRefDll; /* pointer to refcounter in external dll (ugrrr...) */
360 } IDefClFImpl;
362 static const IClassFactoryVtbl dclfvt;
364 /**************************************************************************
365 * IDefClF_fnConstructor
368 static IClassFactory * IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI, PLONG pcRefDll, REFIID riidInst)
370 IDefClFImpl* lpclf;
372 lpclf = HeapAlloc(GetProcessHeap(),0,sizeof(IDefClFImpl));
373 lpclf->ref = 1;
374 lpclf->lpVtbl = &dclfvt;
375 lpclf->lpfnCI = lpfnCI;
376 lpclf->pcRefDll = pcRefDll;
378 if (pcRefDll) InterlockedIncrement(pcRefDll);
379 lpclf->riidInst = riidInst;
381 TRACE("(%p)%s\n",lpclf, shdebugstr_guid(riidInst));
382 return (LPCLASSFACTORY)lpclf;
384 /**************************************************************************
385 * IDefClF_fnQueryInterface
387 static HRESULT WINAPI IDefClF_fnQueryInterface(
388 LPCLASSFACTORY iface, REFIID riid, LPVOID *ppvObj)
390 IDefClFImpl *This = (IDefClFImpl *)iface;
392 TRACE("(%p)->(%s)\n",This,shdebugstr_guid(riid));
394 *ppvObj = NULL;
396 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory)) {
397 *ppvObj = This;
398 InterlockedIncrement(&This->ref);
399 return S_OK;
402 TRACE("-- E_NOINTERFACE\n");
403 return E_NOINTERFACE;
405 /******************************************************************************
406 * IDefClF_fnAddRef
408 static ULONG WINAPI IDefClF_fnAddRef(LPCLASSFACTORY iface)
410 IDefClFImpl *This = (IDefClFImpl *)iface;
411 ULONG refCount = InterlockedIncrement(&This->ref);
413 TRACE("(%p)->(count=%u)\n", This, refCount - 1);
415 return refCount;
417 /******************************************************************************
418 * IDefClF_fnRelease
420 static ULONG WINAPI IDefClF_fnRelease(LPCLASSFACTORY iface)
422 IDefClFImpl *This = (IDefClFImpl *)iface;
423 ULONG refCount = InterlockedDecrement(&This->ref);
425 TRACE("(%p)->(count=%u)\n", This, refCount + 1);
427 if (!refCount)
429 if (This->pcRefDll) InterlockedDecrement(This->pcRefDll);
431 TRACE("-- destroying IClassFactory(%p)\n",This);
432 HeapFree(GetProcessHeap(),0,This);
433 return 0;
435 return refCount;
437 /******************************************************************************
438 * IDefClF_fnCreateInstance
440 static HRESULT WINAPI IDefClF_fnCreateInstance(
441 LPCLASSFACTORY iface, LPUNKNOWN pUnkOuter, REFIID riid, LPVOID *ppvObject)
443 IDefClFImpl *This = (IDefClFImpl *)iface;
445 TRACE("%p->(%p,%s,%p)\n",This,pUnkOuter,shdebugstr_guid(riid),ppvObject);
447 *ppvObject = NULL;
449 if ( This->riidInst==NULL ||
450 IsEqualCLSID(riid, This->riidInst) ||
451 IsEqualCLSID(riid, &IID_IUnknown) )
453 return This->lpfnCI(pUnkOuter, riid, ppvObject);
456 ERR("unknown IID requested %s\n",shdebugstr_guid(riid));
457 return E_NOINTERFACE;
459 /******************************************************************************
460 * IDefClF_fnLockServer
462 static HRESULT WINAPI IDefClF_fnLockServer(LPCLASSFACTORY iface, BOOL fLock)
464 IDefClFImpl *This = (IDefClFImpl *)iface;
465 TRACE("%p->(0x%x), not implemented\n",This, fLock);
466 return E_NOTIMPL;
469 static const IClassFactoryVtbl dclfvt =
471 IDefClF_fnQueryInterface,
472 IDefClF_fnAddRef,
473 IDefClF_fnRelease,
474 IDefClF_fnCreateInstance,
475 IDefClF_fnLockServer
478 /******************************************************************************
479 * SHCreateDefClassObject [SHELL32.70]
481 HRESULT WINAPI SHCreateDefClassObject(
482 REFIID riid,
483 LPVOID* ppv,
484 LPFNCREATEINSTANCE lpfnCI, /* [in] create instance callback entry */
485 LPDWORD pcRefDll, /* [in/out] ref count of the dll */
486 REFIID riidInst) /* [in] optional interface to the instance */
488 IClassFactory * pcf;
490 TRACE("%s %p %p %p %s\n",
491 shdebugstr_guid(riid), ppv, lpfnCI, pcRefDll, shdebugstr_guid(riidInst));
493 if (! IsEqualCLSID(riid, &IID_IClassFactory) ) return E_NOINTERFACE;
494 if (! (pcf = IDefClF_fnConstructor(lpfnCI, (PLONG)pcRefDll, riidInst))) return E_OUTOFMEMORY;
495 *ppv = pcf;
496 return NOERROR;
499 /*************************************************************************
500 * DragAcceptFiles [SHELL32.@]
502 void WINAPI DragAcceptFiles(HWND hWnd, BOOL b)
504 LONG exstyle;
506 if( !IsWindow(hWnd) ) return;
507 exstyle = GetWindowLongA(hWnd,GWL_EXSTYLE);
508 if (b)
509 exstyle |= WS_EX_ACCEPTFILES;
510 else
511 exstyle &= ~WS_EX_ACCEPTFILES;
512 SetWindowLongA(hWnd,GWL_EXSTYLE,exstyle);
515 /*************************************************************************
516 * DragFinish [SHELL32.@]
518 void WINAPI DragFinish(HDROP h)
520 TRACE("\n");
521 GlobalFree((HGLOBAL)h);
524 /*************************************************************************
525 * DragQueryPoint [SHELL32.@]
527 BOOL WINAPI DragQueryPoint(HDROP hDrop, POINT *p)
529 DROPFILES *lpDropFileStruct;
530 BOOL bRet;
532 TRACE("\n");
534 lpDropFileStruct = GlobalLock(hDrop);
536 *p = lpDropFileStruct->pt;
537 bRet = lpDropFileStruct->fNC;
539 GlobalUnlock(hDrop);
540 return bRet;
543 /*************************************************************************
544 * DragQueryFileA [SHELL32.@]
545 * DragQueryFile [SHELL32.@]
547 UINT WINAPI DragQueryFileA(
548 HDROP hDrop,
549 UINT lFile,
550 LPSTR lpszFile,
551 UINT lLength)
553 LPSTR lpDrop;
554 UINT i = 0;
555 DROPFILES *lpDropFileStruct = GlobalLock(hDrop);
557 TRACE("(%p, %x, %p, %u)\n", hDrop,lFile,lpszFile,lLength);
559 if(!lpDropFileStruct) goto end;
561 lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->pFiles;
563 if(lpDropFileStruct->fWide) {
564 LPWSTR lpszFileW = NULL;
566 if(lpszFile) {
567 lpszFileW = HeapAlloc(GetProcessHeap(), 0, lLength*sizeof(WCHAR));
568 if(lpszFileW == NULL) {
569 goto end;
572 i = DragQueryFileW(hDrop, lFile, lpszFileW, lLength);
574 if(lpszFileW) {
575 WideCharToMultiByte(CP_ACP, 0, lpszFileW, -1, lpszFile, lLength, 0, NULL);
576 HeapFree(GetProcessHeap(), 0, lpszFileW);
578 goto end;
581 while (i++ < lFile)
583 while (*lpDrop++); /* skip filename */
584 if (!*lpDrop)
586 i = (lFile == 0xFFFFFFFF) ? i : 0;
587 goto end;
591 i = strlen(lpDrop);
592 if (!lpszFile ) goto end; /* needed buffer size */
593 lstrcpynA (lpszFile, lpDrop, lLength);
594 end:
595 GlobalUnlock(hDrop);
596 return i;
599 /*************************************************************************
600 * DragQueryFileW [SHELL32.@]
602 UINT WINAPI DragQueryFileW(
603 HDROP hDrop,
604 UINT lFile,
605 LPWSTR lpszwFile,
606 UINT lLength)
608 LPWSTR lpwDrop;
609 UINT i = 0;
610 DROPFILES *lpDropFileStruct = GlobalLock(hDrop);
612 TRACE("(%p, %x, %p, %u)\n", hDrop,lFile,lpszwFile,lLength);
614 if(!lpDropFileStruct) goto end;
616 lpwDrop = (LPWSTR) ((LPSTR)lpDropFileStruct + lpDropFileStruct->pFiles);
618 if(lpDropFileStruct->fWide == FALSE) {
619 LPSTR lpszFileA = NULL;
621 if(lpszwFile) {
622 lpszFileA = HeapAlloc(GetProcessHeap(), 0, lLength);
623 if(lpszFileA == NULL) {
624 goto end;
627 i = DragQueryFileA(hDrop, lFile, lpszFileA, lLength);
629 if(lpszFileA) {
630 MultiByteToWideChar(CP_ACP, 0, lpszFileA, -1, lpszwFile, lLength);
631 HeapFree(GetProcessHeap(), 0, lpszFileA);
633 goto end;
636 i = 0;
637 while (i++ < lFile)
639 while (*lpwDrop++); /* skip filename */
640 if (!*lpwDrop)
642 i = (lFile == 0xFFFFFFFF) ? i : 0;
643 goto end;
647 i = strlenW(lpwDrop);
648 if ( !lpszwFile) goto end; /* needed buffer size */
649 lstrcpynW (lpszwFile, lpwDrop, lLength);
650 end:
651 GlobalUnlock(hDrop);
652 return i;