d3d: Limit d3d8 and d3d9 vshader constants to 256.
[wine/multimedia.git] / dlls / shell32 / shellole.c
blob9e63c1edb9fd3dfb4447ed53ff8b0b6605c0af19
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_ShellItem, IShellItem_Constructor},
68 {&CLSID_ShellLink, IShellLink_Constructor},
69 {&CLSID_DragDropHelper, IDropTargetHelper_Constructor},
70 {&CLSID_ControlPanel, IControlPanel_Constructor},
71 {&CLSID_AutoComplete, IAutoComplete_Constructor},
72 {&CLSID_UnixFolder, UnixFolder_Constructor},
73 {&CLSID_UnixDosFolder, UnixDosFolder_Constructor},
74 {&CLSID_FolderShortcut, FolderShortcut_Constructor},
75 {&CLSID_MyDocuments, MyDocuments_Constructor},
76 {&CLSID_RecycleBin, RecycleBin_Constructor},
77 {NULL,NULL}
82 /*************************************************************************
83 * SHCoCreateInstance [SHELL32.102]
85 * Equivalent to CoCreateInstance. Under Windows 9x this function could sometimes
86 * use the shell32 built-in "mini-COM" without the need to load ole32.dll - see
87 * SHLoadOLE for details.
89 * Under wine if a "LoadWithoutCOM" value is present or the object resides in
90 * shell32.dll the function will load the object manually without the help of ole32
92 * NOTES
93 * exported by ordinal
95 * SEE ALSO
96 * CoCreateInstace, SHLoadOLE
98 HRESULT WINAPI SHCoCreateInstance(
99 LPCWSTR aclsid,
100 const CLSID *clsid,
101 LPUNKNOWN pUnkOuter,
102 REFIID refiid,
103 LPVOID *ppv)
105 DWORD hres;
106 IID iid;
107 const CLSID * myclsid = clsid;
108 WCHAR sKeyName[MAX_PATH];
109 const WCHAR sCLSID[7] = {'C','L','S','I','D','\\','\0'};
110 WCHAR sClassID[60];
111 const WCHAR sInProcServer32[16] ={'\\','I','n','p','r','o','c','S','e','r','v','e','r','3','2','\0'};
112 const WCHAR sLoadWithoutCOM[15] ={'L','o','a','d','W','i','t','h','o','u','t','C','O','M','\0'};
113 WCHAR sDllPath[MAX_PATH];
114 HKEY hKey = 0;
115 DWORD dwSize;
116 IClassFactory * pcf = NULL;
118 if(!ppv) return E_POINTER;
119 *ppv=NULL;
121 /* if the clsid is a string, convert it */
122 if (!clsid)
124 if (!aclsid) return REGDB_E_CLASSNOTREG;
125 SHCLSIDFromStringW(aclsid, &iid);
126 myclsid = &iid;
129 TRACE("(%p,%s,unk:%p,%s,%p)\n",
130 aclsid,shdebugstr_guid(myclsid),pUnkOuter,shdebugstr_guid(refiid),ppv);
132 if (SUCCEEDED(DllGetClassObject(myclsid, &IID_IClassFactory,(LPVOID*)&pcf)))
134 hres = IClassFactory_CreateInstance(pcf, pUnkOuter, refiid, ppv);
135 IClassFactory_Release(pcf);
136 goto end;
139 /* we look up the dll path in the registry */
140 SHStringFromGUIDW(myclsid, sClassID, sizeof(sClassID)/sizeof(WCHAR));
141 lstrcpyW(sKeyName, sCLSID);
142 lstrcatW(sKeyName, sClassID);
143 lstrcatW(sKeyName, sInProcServer32);
145 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, sKeyName, 0, KEY_READ, &hKey))
146 return E_ACCESSDENIED;
148 /* if a special registry key is set, we load a shell extension without help of OLE32 */
149 if (!SHQueryValueExW(hKey, sLoadWithoutCOM, 0, 0, 0, 0))
151 /* load an external dll without ole32 */
152 HANDLE hLibrary;
153 typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid, REFIID iid, LPVOID *ppv);
154 DllGetClassObjectFunc DllGetClassObject;
156 dwSize = sizeof(sDllPath);
157 SHQueryValueExW(hKey, NULL, 0,0, sDllPath, &dwSize );
159 if ((hLibrary = LoadLibraryExW(sDllPath, 0, LOAD_WITH_ALTERED_SEARCH_PATH)) == 0) {
160 ERR("couldn't load InprocServer32 dll %s\n", debugstr_w(sDllPath));
161 hres = E_ACCESSDENIED;
162 goto end;
163 } else if (!(DllGetClassObject = (DllGetClassObjectFunc)GetProcAddress(hLibrary, "DllGetClassObject"))) {
164 ERR("couldn't find function DllGetClassObject in %s\n", debugstr_w(sDllPath));
165 FreeLibrary( hLibrary );
166 hres = E_ACCESSDENIED;
167 goto end;
168 } else if (FAILED(hres = DllGetClassObject(myclsid, &IID_IClassFactory, (LPVOID*)&pcf))) {
169 TRACE("GetClassObject failed 0x%08x\n", hres);
170 goto end;
173 hres = IClassFactory_CreateInstance(pcf, pUnkOuter, refiid, ppv);
174 IClassFactory_Release(pcf);
175 } else {
177 /* load an external dll in the usual way */
178 hres = CoCreateInstance(myclsid, pUnkOuter, CLSCTX_INPROC_SERVER, refiid, ppv);
181 end:
182 if (hKey) RegCloseKey(hKey);
183 if(hres!=S_OK)
185 ERR("failed (0x%08x) to create CLSID:%s IID:%s\n",
186 hres, shdebugstr_guid(myclsid), shdebugstr_guid(refiid));
187 ERR("class not found in registry\n");
190 TRACE("-- instance: %p\n",*ppv);
191 return hres;
194 /*************************************************************************
195 * DllGetClassObject [SHELL32.@]
196 * SHDllGetClassObject [SHELL32.128]
198 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
200 HRESULT hres = E_OUTOFMEMORY;
201 IClassFactory * pcf = NULL;
202 int i;
204 TRACE("CLSID:%s,IID:%s\n",shdebugstr_guid(rclsid),shdebugstr_guid(iid));
206 if (!ppv) return E_INVALIDARG;
207 *ppv = NULL;
209 /* search our internal interface table */
210 for(i=0;InterfaceTable[i].riid;i++) {
211 if(IsEqualIID(InterfaceTable[i].riid, rclsid)) {
212 TRACE("index[%u]\n", i);
213 pcf = IDefClF_fnConstructor(InterfaceTable[i].lpfnCI, NULL, NULL);
217 if (!pcf) {
218 FIXME("failed for CLSID=%s\n", shdebugstr_guid(rclsid));
219 return CLASS_E_CLASSNOTAVAILABLE;
222 hres = IClassFactory_QueryInterface(pcf, iid, ppv);
223 IClassFactory_Release(pcf);
225 TRACE("-- pointer to class factory: %p\n",*ppv);
226 return hres;
229 /*************************************************************************
230 * SHCLSIDFromString [SHELL32.147]
232 * Under Windows 9x this was an ANSI version of CLSIDFromString. It also allowed
233 * to avoid dependency on ole32.dll (see SHLoadOLE for details).
235 * Under Windows NT/2000/XP this is equivalent to CLSIDFromString
237 * NOTES
238 * exported by ordinal
240 * SEE ALSO
241 * CLSIDFromString, SHLoadOLE
243 DWORD WINAPI SHCLSIDFromStringA (LPCSTR clsid, CLSID *id)
245 WCHAR buffer[40];
246 TRACE("(%p(%s) %p)\n", clsid, clsid, id);
247 if (!MultiByteToWideChar( CP_ACP, 0, clsid, -1, buffer, sizeof(buffer)/sizeof(WCHAR) ))
248 return CO_E_CLASSSTRING;
249 return CLSIDFromString( buffer, id );
251 DWORD WINAPI SHCLSIDFromStringW (LPCWSTR clsid, CLSID *id)
253 TRACE("(%p(%s) %p)\n", clsid, debugstr_w(clsid), id);
254 return CLSIDFromString((LPWSTR)clsid, id);
256 DWORD WINAPI SHCLSIDFromStringAW (LPCVOID clsid, CLSID *id)
258 if (SHELL_OsIsUnicode())
259 return SHCLSIDFromStringW (clsid, id);
260 return SHCLSIDFromStringA (clsid, id);
263 /*************************************************************************
264 * SHGetMalloc [SHELL32.@]
266 * Equivalent to CoGetMalloc(MEMCTX_TASK, ...). Under Windows 9x this function
267 * could use the shell32 built-in "mini-COM" without the need to load ole32.dll -
268 * see SHLoadOLE for details.
270 * PARAMS
271 * lpmal [O] Destination for IMalloc interface.
273 * RETURNS
274 * Success: S_OK. lpmal contains the shells IMalloc interface.
275 * Failure. An HRESULT error code.
277 * SEE ALSO
278 * CoGetMalloc, SHLoadOLE
280 HRESULT WINAPI SHGetMalloc(LPMALLOC *lpmal)
282 TRACE("(%p)\n", lpmal);
283 return CoGetMalloc(MEMCTX_TASK, lpmal);
286 /*************************************************************************
287 * SHAlloc [SHELL32.196]
289 * Equivalent to CoTaskMemAlloc. Under Windows 9x this function could use
290 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
291 * see SHLoadOLE for details.
293 * NOTES
294 * exported by ordinal
296 * SEE ALSO
297 * CoTaskMemAlloc, SHLoadOLE
299 LPVOID WINAPI SHAlloc(DWORD len)
301 LPVOID ret;
303 ret = CoTaskMemAlloc(len);
304 TRACE("%u bytes at %p\n",len, ret);
305 return ret;
308 /*************************************************************************
309 * SHFree [SHELL32.195]
311 * Equivalent to CoTaskMemFree. Under Windows 9x this function could use
312 * the shell32 built-in "mini-COM" without the need to load ole32.dll -
313 * see SHLoadOLE for details.
315 * NOTES
316 * exported by ordinal
318 * SEE ALSO
319 * CoTaskMemFree, SHLoadOLE
321 void WINAPI SHFree(LPVOID pv)
323 TRACE("%p\n",pv);
324 CoTaskMemFree(pv);
327 /*************************************************************************
328 * SHGetDesktopFolder [SHELL32.@]
330 HRESULT WINAPI SHGetDesktopFolder(IShellFolder **psf)
332 HRESULT hres = S_OK;
333 TRACE("\n");
335 if(!psf) return E_INVALIDARG;
336 *psf = NULL;
337 hres = ISF_Desktop_Constructor(NULL, &IID_IShellFolder,(LPVOID*)psf);
339 TRACE("-- %p->(%p)\n",psf, *psf);
340 return hres;
342 /**************************************************************************
343 * Default ClassFactory Implementation
345 * SHCreateDefClassObject
347 * NOTES
348 * Helper function for dlls without their own classfactory.
349 * A generic classfactory is returned.
350 * When the CreateInstance of the cf is called the callback is executed.
353 typedef struct
355 const IClassFactoryVtbl *lpVtbl;
356 LONG ref;
357 CLSID *rclsid;
358 LPFNCREATEINSTANCE lpfnCI;
359 const IID * riidInst;
360 LONG * pcRefDll; /* pointer to refcounter in external dll (ugrrr...) */
361 } IDefClFImpl;
363 static const IClassFactoryVtbl dclfvt;
365 /**************************************************************************
366 * IDefClF_fnConstructor
369 static IClassFactory * IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI, PLONG pcRefDll, REFIID riidInst)
371 IDefClFImpl* lpclf;
373 lpclf = HeapAlloc(GetProcessHeap(),0,sizeof(IDefClFImpl));
374 lpclf->ref = 1;
375 lpclf->lpVtbl = &dclfvt;
376 lpclf->lpfnCI = lpfnCI;
377 lpclf->pcRefDll = pcRefDll;
379 if (pcRefDll) InterlockedIncrement(pcRefDll);
380 lpclf->riidInst = riidInst;
382 TRACE("(%p)%s\n",lpclf, shdebugstr_guid(riidInst));
383 return (LPCLASSFACTORY)lpclf;
385 /**************************************************************************
386 * IDefClF_fnQueryInterface
388 static HRESULT WINAPI IDefClF_fnQueryInterface(
389 LPCLASSFACTORY iface, REFIID riid, LPVOID *ppvObj)
391 IDefClFImpl *This = (IDefClFImpl *)iface;
393 TRACE("(%p)->(%s)\n",This,shdebugstr_guid(riid));
395 *ppvObj = NULL;
397 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory)) {
398 *ppvObj = This;
399 InterlockedIncrement(&This->ref);
400 return S_OK;
403 TRACE("-- E_NOINTERFACE\n");
404 return E_NOINTERFACE;
406 /******************************************************************************
407 * IDefClF_fnAddRef
409 static ULONG WINAPI IDefClF_fnAddRef(LPCLASSFACTORY iface)
411 IDefClFImpl *This = (IDefClFImpl *)iface;
412 ULONG refCount = InterlockedIncrement(&This->ref);
414 TRACE("(%p)->(count=%u)\n", This, refCount - 1);
416 return refCount;
418 /******************************************************************************
419 * IDefClF_fnRelease
421 static ULONG WINAPI IDefClF_fnRelease(LPCLASSFACTORY iface)
423 IDefClFImpl *This = (IDefClFImpl *)iface;
424 ULONG refCount = InterlockedDecrement(&This->ref);
426 TRACE("(%p)->(count=%u)\n", This, refCount + 1);
428 if (!refCount)
430 if (This->pcRefDll) InterlockedDecrement(This->pcRefDll);
432 TRACE("-- destroying IClassFactory(%p)\n",This);
433 HeapFree(GetProcessHeap(),0,This);
434 return 0;
436 return refCount;
438 /******************************************************************************
439 * IDefClF_fnCreateInstance
441 static HRESULT WINAPI IDefClF_fnCreateInstance(
442 LPCLASSFACTORY iface, LPUNKNOWN pUnkOuter, REFIID riid, LPVOID *ppvObject)
444 IDefClFImpl *This = (IDefClFImpl *)iface;
446 TRACE("%p->(%p,%s,%p)\n",This,pUnkOuter,shdebugstr_guid(riid),ppvObject);
448 *ppvObject = NULL;
450 if ( This->riidInst==NULL ||
451 IsEqualCLSID(riid, This->riidInst) ||
452 IsEqualCLSID(riid, &IID_IUnknown) )
454 return This->lpfnCI(pUnkOuter, riid, ppvObject);
457 ERR("unknown IID requested %s\n",shdebugstr_guid(riid));
458 return E_NOINTERFACE;
460 /******************************************************************************
461 * IDefClF_fnLockServer
463 static HRESULT WINAPI IDefClF_fnLockServer(LPCLASSFACTORY iface, BOOL fLock)
465 IDefClFImpl *This = (IDefClFImpl *)iface;
466 TRACE("%p->(0x%x), not implemented\n",This, fLock);
467 return E_NOTIMPL;
470 static const IClassFactoryVtbl dclfvt =
472 IDefClF_fnQueryInterface,
473 IDefClF_fnAddRef,
474 IDefClF_fnRelease,
475 IDefClF_fnCreateInstance,
476 IDefClF_fnLockServer
479 /******************************************************************************
480 * SHCreateDefClassObject [SHELL32.70]
482 HRESULT WINAPI SHCreateDefClassObject(
483 REFIID riid,
484 LPVOID* ppv,
485 LPFNCREATEINSTANCE lpfnCI, /* [in] create instance callback entry */
486 LPDWORD pcRefDll, /* [in/out] ref count of the dll */
487 REFIID riidInst) /* [in] optional interface to the instance */
489 IClassFactory * pcf;
491 TRACE("%s %p %p %p %s\n",
492 shdebugstr_guid(riid), ppv, lpfnCI, pcRefDll, shdebugstr_guid(riidInst));
494 if (! IsEqualCLSID(riid, &IID_IClassFactory) ) return E_NOINTERFACE;
495 if (! (pcf = IDefClF_fnConstructor(lpfnCI, (PLONG)pcRefDll, riidInst))) return E_OUTOFMEMORY;
496 *ppv = pcf;
497 return NOERROR;
500 /*************************************************************************
501 * DragAcceptFiles [SHELL32.@]
503 void WINAPI DragAcceptFiles(HWND hWnd, BOOL b)
505 LONG exstyle;
507 if( !IsWindow(hWnd) ) return;
508 exstyle = GetWindowLongA(hWnd,GWL_EXSTYLE);
509 if (b)
510 exstyle |= WS_EX_ACCEPTFILES;
511 else
512 exstyle &= ~WS_EX_ACCEPTFILES;
513 SetWindowLongA(hWnd,GWL_EXSTYLE,exstyle);
516 /*************************************************************************
517 * DragFinish [SHELL32.@]
519 void WINAPI DragFinish(HDROP h)
521 TRACE("\n");
522 GlobalFree(h);
525 /*************************************************************************
526 * DragQueryPoint [SHELL32.@]
528 BOOL WINAPI DragQueryPoint(HDROP hDrop, POINT *p)
530 DROPFILES *lpDropFileStruct;
531 BOOL bRet;
533 TRACE("\n");
535 lpDropFileStruct = GlobalLock(hDrop);
537 *p = lpDropFileStruct->pt;
538 bRet = lpDropFileStruct->fNC;
540 GlobalUnlock(hDrop);
541 return bRet;
544 /*************************************************************************
545 * DragQueryFileA [SHELL32.@]
546 * DragQueryFile [SHELL32.@]
548 UINT WINAPI DragQueryFileA(
549 HDROP hDrop,
550 UINT lFile,
551 LPSTR lpszFile,
552 UINT lLength)
554 LPSTR lpDrop;
555 UINT i = 0;
556 DROPFILES *lpDropFileStruct = GlobalLock(hDrop);
558 TRACE("(%p, %x, %p, %u)\n", hDrop,lFile,lpszFile,lLength);
560 if(!lpDropFileStruct) goto end;
562 lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->pFiles;
564 if(lpDropFileStruct->fWide) {
565 LPWSTR lpszFileW = NULL;
567 if(lpszFile) {
568 lpszFileW = HeapAlloc(GetProcessHeap(), 0, lLength*sizeof(WCHAR));
569 if(lpszFileW == NULL) {
570 goto end;
573 i = DragQueryFileW(hDrop, lFile, lpszFileW, lLength);
575 if(lpszFileW) {
576 WideCharToMultiByte(CP_ACP, 0, lpszFileW, -1, lpszFile, lLength, 0, NULL);
577 HeapFree(GetProcessHeap(), 0, lpszFileW);
579 goto end;
582 while (i++ < lFile)
584 while (*lpDrop++); /* skip filename */
585 if (!*lpDrop)
587 i = (lFile == 0xFFFFFFFF) ? i : 0;
588 goto end;
592 i = strlen(lpDrop);
593 if (!lpszFile ) goto end; /* needed buffer size */
594 lstrcpynA (lpszFile, lpDrop, lLength);
595 end:
596 GlobalUnlock(hDrop);
597 return i;
600 /*************************************************************************
601 * DragQueryFileW [SHELL32.@]
603 UINT WINAPI DragQueryFileW(
604 HDROP hDrop,
605 UINT lFile,
606 LPWSTR lpszwFile,
607 UINT lLength)
609 LPWSTR lpwDrop;
610 UINT i = 0;
611 DROPFILES *lpDropFileStruct = GlobalLock(hDrop);
613 TRACE("(%p, %x, %p, %u)\n", hDrop,lFile,lpszwFile,lLength);
615 if(!lpDropFileStruct) goto end;
617 lpwDrop = (LPWSTR) ((LPSTR)lpDropFileStruct + lpDropFileStruct->pFiles);
619 if(lpDropFileStruct->fWide == FALSE) {
620 LPSTR lpszFileA = NULL;
622 if(lpszwFile) {
623 lpszFileA = HeapAlloc(GetProcessHeap(), 0, lLength);
624 if(lpszFileA == NULL) {
625 goto end;
628 i = DragQueryFileA(hDrop, lFile, lpszFileA, lLength);
630 if(lpszFileA) {
631 MultiByteToWideChar(CP_ACP, 0, lpszFileA, -1, lpszwFile, lLength);
632 HeapFree(GetProcessHeap(), 0, lpszFileA);
634 goto end;
637 i = 0;
638 while (i++ < lFile)
640 while (*lpwDrop++); /* skip filename */
641 if (!*lpwDrop)
643 i = (lFile == 0xFFFFFFFF) ? i : 0;
644 goto end;
648 i = strlenW(lpwDrop);
649 if ( !lpszwFile) goto end; /* needed buffer size */
650 lstrcpynW (lpszwFile, lpwDrop, lLength);
651 end:
652 GlobalUnlock(hDrop);
653 return i;