Fix double-clicking control panel items
[reactos.git] / reactos / lib / shell32 / shellole.c
1 /*
2 * handling of SHELL32.DLL OLE-Objects
3 *
4 * Copyright 1997 Marcus Meissner
5 * Copyright 1998 Juergen Schmied <juergen.schmied@metronet.de>
6 *
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.
11 *
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.
16 *
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 #include "config.h"
23
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #define COBJMACROS
29
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"
39
40 #include "undocshell.h"
41 #include "wine/unicode.h"
42 #include "shell32_main.h"
43
44 #include "wine/debug.h"
45 #include "shlwapi.h"
46 #include "debughlp.h"
47
48 WINE_DEFAULT_DEBUG_CHANNEL(shell);
49
50 extern HRESULT WINAPI IFSFolder_Constructor(IUnknown * pUnkOuter, REFIID riid, LPVOID * ppv);
51
52 const WCHAR sShell32[12] = {'S','H','E','L','L','3','2','.','D','L','L','\0'};
53 const WCHAR sOLE32[10] = {'O','L','E','3','2','.','D','L','L','\0'};
54
55 HINSTANCE hShellOle32 = 0;
56 /**************************************************************************
57 * Default ClassFactory types
58 */
59 typedef HRESULT (CALLBACK *LPFNCREATEINSTANCE)(IUnknown* pUnkOuter, REFIID riid, LPVOID* ppvObject);
60 IClassFactory * IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI, PLONG pcRefDll, REFIID riidInst);
61
62 /* this table contains all CLSID's of shell32 objects */
63 struct {
64 REFIID riid;
65 LPFNCREATEINSTANCE lpfnCI;
66 } InterfaceTable[] = {
67 {&CLSID_ShellFSFolder, &IFSFolder_Constructor},
68 {&CLSID_MyComputer, &ISF_MyComputer_Constructor},
69 {&CLSID_ShellDesktop, &ISF_Desktop_Constructor},
70 {&CLSID_ShellLink, &IShellLink_Constructor},
71 {&CLSID_DragDropHelper, &IDropTargetHelper_Constructor},
72 {&CLSID_ControlPanel, &IControlPanel_Constructor},
73 {&CLSID_AutoComplete, &IAutoComplete_Constructor},
74 {NULL,NULL}
75 };
76
77 /*************************************************************************
78 * SHCoCreateInstance [SHELL32.102]
79 *
80 * NOTES
81 * exported by ordinal
82 */
83
84 /* FIXME: this should be SHLWAPI.24 since we can't yet import by ordinal */
85
86 DWORD WINAPI __SHGUIDToStringW (REFGUID guid, LPWSTR str)
87 {
88 WCHAR sFormat[52] = {'{','%','0','8','l','x','-','%','0','4',
89 'x','-','%','0','4','x','-','%','0','2',
90 'x','%','0','2','x','-','%','0','2','x',
91 '%','0','2','x','%','0','2','x','%','0',
92 '2','x','%','0','2','x','%','0','2','x',
93 '}','\0'};
94
95 return wsprintfW ( str, sFormat,
96 guid->Data1, guid->Data2, guid->Data3,
97 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
98 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7] );
99
100 }
101
102 /************************************************************************/
103
104 HRESULT WINAPI SHCoCreateInstance(
105 LPCWSTR aclsid,
106 const CLSID *clsid,
107 LPUNKNOWN pUnkOuter,
108 REFIID refiid,
109 LPVOID *ppv)
110 {
111 DWORD hres;
112 IID iid;
113 CLSID * myclsid = (CLSID*)clsid;
114 WCHAR sKeyName[MAX_PATH];
115 const WCHAR sCLSID[7] = {'C','L','S','I','D','\\','\0'};
116 WCHAR sClassID[60];
117 const WCHAR sInProcServer32[16] ={'\\','I','n','p','r','o','c','S','e','r','v','e','r','3','2','\0'};
118 const WCHAR sLoadWithoutCOM[15] ={'L','o','a','d','W','i','t','h','o','u','t','C','O','M','\0'};
119 WCHAR sDllPath[MAX_PATH];
120 HKEY hKey;
121 DWORD dwSize;
122 BOOLEAN bLoadFromShell32 = FALSE;
123 BOOLEAN bLoadWithoutCOM = FALSE;
124 IClassFactory * pcf = NULL;
125
126 if(!ppv) return E_POINTER;
127 *ppv=NULL;
128
129 /* if the clsid is a string, convert it */
130 if (!clsid)
131 {
132 if (!aclsid) return REGDB_E_CLASSNOTREG;
133 SHCLSIDFromStringW(aclsid, &iid);
134 myclsid = &iid;
135 }
136
137 TRACE("(%p,%s,unk:%p,%s,%p)\n",
138 aclsid,shdebugstr_guid(myclsid),pUnkOuter,shdebugstr_guid(refiid),ppv);
139
140 /* we look up the dll path in the registry */
141 __SHGUIDToStringW(myclsid, sClassID);
142 lstrcpyW(sKeyName, sCLSID);
143 lstrcatW(sKeyName, sClassID);
144 lstrcatW(sKeyName, sInProcServer32);
145
146 if (ERROR_SUCCESS == RegOpenKeyExW(HKEY_CLASSES_ROOT, sKeyName, 0, KEY_READ, &hKey)) {
147 dwSize = sizeof(sDllPath);
148 SHQueryValueExW(hKey, NULL, 0,0, sDllPath, &dwSize );
149
150 /* if a special registry key is set, we load a shell extension without help of OLE32 */
151 bLoadWithoutCOM = (ERROR_SUCCESS == SHQueryValueExW(hKey, sLoadWithoutCOM, 0, 0, 0, 0));
152
153 /* if the com object is inside shell32, omit use of ole32 */
154 bLoadFromShell32 = (0==lstrcmpiW( PathFindFileNameW(sDllPath), sShell32));
155
156 RegCloseKey (hKey);
157 } else {
158 /* since we can't find it in the registry we try internally */
159 bLoadFromShell32 = TRUE;
160 }
161
162 TRACE("WithoutCom=%u FromShell=%u\n", bLoadWithoutCOM, bLoadFromShell32);
163
164 /* now we create an instance */
165 if (bLoadFromShell32) {
166 if (! SUCCEEDED(DllGetClassObject(myclsid, &IID_IClassFactory,(LPVOID*)&pcf))) {
167 ERR("LoadFromShell failed for CLSID=%s\n", shdebugstr_guid(myclsid));
168 }
169 } else if (bLoadWithoutCOM) {
170
171 /* load an external dll without ole32 */
172 HANDLE hLibrary;
173 typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid, REFIID iid, LPVOID *ppv);
174 DllGetClassObjectFunc DllGetClassObject;
175
176 if ((hLibrary = LoadLibraryExW(sDllPath, 0, LOAD_WITH_ALTERED_SEARCH_PATH)) == 0) {
177 ERR("couldn't load InprocServer32 dll %s\n", debugstr_w(sDllPath));
178 hres = E_ACCESSDENIED;
179 goto end;
180 } else if (!(DllGetClassObject = (DllGetClassObjectFunc)GetProcAddress(hLibrary, "DllGetClassObject"))) {
181 ERR("couldn't find function DllGetClassObject in %s\n", debugstr_w(sDllPath));
182 FreeLibrary( hLibrary );
183 hres = E_ACCESSDENIED;
184 goto end;
185 } else if (! SUCCEEDED(hres = DllGetClassObject(myclsid, &IID_IClassFactory, (LPVOID*)&pcf))) {
186 TRACE("GetClassObject failed 0x%08lx\n", hres);
187 goto end;
188 }
189
190 } else {
191
192 /* load an external dll in the usual way */
193 hres = CoCreateInstance(myclsid, pUnkOuter, CLSCTX_INPROC_SERVER, refiid, ppv);
194 goto end;
195 }
196
197 /* here we should have a ClassFactory */
198 if (!pcf) return E_ACCESSDENIED;
199
200 hres = IClassFactory_CreateInstance(pcf, pUnkOuter, refiid, ppv);
201 IClassFactory_Release(pcf);
202 end:
203 if(hres!=S_OK)
204 {
205 ERR("failed (0x%08lx) to create CLSID:%s IID:%s\n",
206 hres, shdebugstr_guid(myclsid), shdebugstr_guid(refiid));
207 ERR("class not found in registry\n");
208 }
209
210 TRACE("-- instance: %p\n",*ppv);
211 return hres;
212 }
213
214 /*************************************************************************
215 * DllGetClassObject [SHELL32.@]
216 */
217 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
218 {
219 HRESULT hres = E_OUTOFMEMORY;
220 IClassFactory * pcf = NULL;
221 int i;
222
223 TRACE("CLSID:%s,IID:%s\n",shdebugstr_guid(rclsid),shdebugstr_guid(iid));
224
225 if (!ppv) return E_INVALIDARG;
226 *ppv = NULL;
227
228 /* search our internal interface table */
229 for(i=0;InterfaceTable[i].riid;i++) {
230 if(IsEqualIID(InterfaceTable[i].riid, rclsid)) {
231 TRACE("index[%u]\n", i);
232 pcf = IDefClF_fnConstructor(InterfaceTable[i].lpfnCI, NULL, NULL);
233 }
234 }
235
236 if (!pcf) {
237 FIXME("failed for CLSID=%s\n", shdebugstr_guid(rclsid));
238 return CLASS_E_CLASSNOTAVAILABLE;
239 }
240
241 hres = IClassFactory_QueryInterface(pcf, iid, ppv);
242 IClassFactory_Release(pcf);
243
244 TRACE("-- pointer to class factory: %p\n",*ppv);
245 return hres;
246 }
247
248 /*************************************************************************
249 * SHCLSIDFromString [SHELL32.147]
250 *
251 * NOTES
252 * exported by ordinal
253 */
254 DWORD WINAPI SHCLSIDFromStringA (LPCSTR clsid, CLSID *id)
255 {
256 WCHAR buffer[40];
257 TRACE("(%p(%s) %p)\n", clsid, clsid, id);
258 if (!MultiByteToWideChar( CP_ACP, 0, clsid, -1, buffer, sizeof(buffer)/sizeof(WCHAR) ))
259 return CO_E_CLASSSTRING;
260 return CLSIDFromString( buffer, id );
261 }
262 DWORD WINAPI SHCLSIDFromStringW (LPCWSTR clsid, CLSID *id)
263 {
264 TRACE("(%p(%s) %p)\n", clsid, debugstr_w(clsid), id);
265 return CLSIDFromString((LPWSTR)clsid, id);
266 }
267 DWORD WINAPI SHCLSIDFromStringAW (LPVOID clsid, CLSID *id)
268 {
269 if (SHELL_OsIsUnicode())
270 return SHCLSIDFromStringW (clsid, id);
271 return SHCLSIDFromStringA (clsid, id);
272 }
273
274 /*************************************************************************
275 * Shell Memory Allocator
276 */
277
278 /* set the vtable later */
279 static const IMallocVtbl VT_Shell_IMalloc32;
280
281 /* this is the static object instance */
282 typedef struct {
283 const IMallocVtbl *lpVtbl;
284 DWORD dummy;
285 } _ShellMalloc;
286
287 static _ShellMalloc Shell_Malloc = { &VT_Shell_IMalloc32,1};
288
289 /* this is the global allocator of shell32 */
290 static IMalloc * ShellTaskAllocator = NULL;
291
292 /******************************************************************************
293 * IShellMalloc_QueryInterface [VTABLE]
294 */
295 static HRESULT WINAPI IShellMalloc_fnQueryInterface(LPMALLOC iface, REFIID refiid, LPVOID *obj)
296 {
297 TRACE("(%s,%p)\n",shdebugstr_guid(refiid),obj);
298 if (IsEqualIID(refiid, &IID_IUnknown) || IsEqualIID(refiid, &IID_IMalloc)) {
299 *obj = (LPMALLOC) &Shell_Malloc;
300 return S_OK;
301 }
302 return E_NOINTERFACE;
303 }
304
305 /******************************************************************************
306 * IShellMalloc_AddRefRelease [VTABLE]
307 */
308 static ULONG WINAPI IShellMalloc_fnAddRefRelease(LPMALLOC iface)
309 {
310 return 1;
311 }
312
313 /******************************************************************************
314 * IShellMalloc_Alloc [VTABLE]
315 */
316 static LPVOID WINAPI IShellMalloc_fnAlloc(LPMALLOC iface, DWORD cb)
317 {
318 LPVOID addr;
319
320 addr = (LPVOID) LocalAlloc(LMEM_ZEROINIT, cb);
321 TRACE("(%p,%ld);\n",addr,cb);
322 return addr;
323 }
324
325 /******************************************************************************
326 * IShellMalloc_Realloc [VTABLE]
327 */
328 static LPVOID WINAPI IShellMalloc_fnRealloc(LPMALLOC iface, LPVOID pv, DWORD cb)
329 {
330 LPVOID addr;
331
332 if (pv) {
333 if (cb) {
334 addr = (LPVOID) LocalReAlloc((HANDLE) pv, cb, LMEM_ZEROINIT | LMEM_MOVEABLE);
335 } else {
336 LocalFree((HANDLE) pv);
337 addr = NULL;
338 }
339 } else {
340 if (cb) {
341 addr = (LPVOID) LocalAlloc(LMEM_ZEROINIT, cb);
342 } else {
343 addr = NULL;
344 }
345 }
346
347 TRACE("(%p->%p,%ld)\n",pv,addr,cb);
348 return addr;
349 }
350
351 /******************************************************************************
352 * IShellMalloc_Free [VTABLE]
353 */
354 static VOID WINAPI IShellMalloc_fnFree(LPMALLOC iface, LPVOID pv)
355 {
356 TRACE("(%p)\n",pv);
357 LocalFree((HANDLE) pv);
358 }
359
360 /******************************************************************************
361 * IShellMalloc_GetSize [VTABLE]
362 */
363 static DWORD WINAPI IShellMalloc_fnGetSize(LPMALLOC iface, LPVOID pv)
364 {
365 DWORD cb = (DWORD) LocalSize((HANDLE)pv);
366 TRACE("(%p,%ld)\n", pv, cb);
367 return cb;
368 }
369
370 /******************************************************************************
371 * IShellMalloc_DidAlloc [VTABLE]
372 */
373 static INT WINAPI IShellMalloc_fnDidAlloc(LPMALLOC iface, LPVOID pv)
374 {
375 TRACE("(%p)\n",pv);
376 return -1;
377 }
378
379 /******************************************************************************
380 * IShellMalloc_HeapMinimize [VTABLE]
381 */
382 static VOID WINAPI IShellMalloc_fnHeapMinimize(LPMALLOC iface)
383 {
384 TRACE("()\n");
385 }
386
387 static const IMallocVtbl VT_Shell_IMalloc32 =
388 {
389 IShellMalloc_fnQueryInterface,
390 IShellMalloc_fnAddRefRelease,
391 IShellMalloc_fnAddRefRelease,
392 IShellMalloc_fnAlloc,
393 IShellMalloc_fnRealloc,
394 IShellMalloc_fnFree,
395 IShellMalloc_fnGetSize,
396 IShellMalloc_fnDidAlloc,
397 IShellMalloc_fnHeapMinimize
398 };
399
400 /*************************************************************************
401 * SHGetMalloc [SHELL32.@]
402 *
403 * Return the shell IMalloc interface.
404 *
405 * PARAMS
406 * lpmal [O] Destination for IMalloc interface.
407 *
408 * RETURNS
409 * Success: S_OK. lpmal contains the shells IMalloc interface.
410 * Failure. An HRESULT error code.
411 *
412 * NOTES
413 * This function will use CoGetMalloc() if OLE32.DLL is already loaded.
414 * If not it uses an internal implementation as a fallback.
415 */
416 HRESULT WINAPI SHGetMalloc(LPMALLOC *lpmal)
417 {
418 TRACE("(%p)\n", lpmal);
419
420 if (!ShellTaskAllocator)
421 {
422 HMODULE hOle32 = GetModuleHandleA("OLE32.DLL");
423 /* this is very suspect. we should not being using a different
424 * allocator from deallocator based on something undeterministic
425 * like whether ole32 is loaded. as it happens currently, they
426 * both map to the same allocator deep down, but this could
427 * change in the future. */
428 if(hOle32) {
429 CoGetMalloc(MEMCTX_TASK, &ShellTaskAllocator);
430 TRACE("got ole32 IMalloc\n");
431 }
432 if(!ShellTaskAllocator) {
433 ShellTaskAllocator = (IMalloc* ) &Shell_Malloc;
434 TRACE("use fallback allocator\n");
435 }
436 }
437 *lpmal = ShellTaskAllocator;
438 return S_OK;
439 }
440
441 /*************************************************************************
442 * SHAlloc [SHELL32.196]
443 *
444 * NOTES
445 * exported by ordinal
446 */
447 LPVOID WINAPI SHAlloc(DWORD len)
448 {
449 IMalloc * ppv;
450 LPBYTE ret;
451
452 if (!ShellTaskAllocator) SHGetMalloc(&ppv);
453
454 ret = (LPVOID) IMalloc_Alloc(ShellTaskAllocator, len);
455 TRACE("%lu bytes at %p\n",len, ret);
456 return (LPVOID)ret;
457 }
458
459 /*************************************************************************
460 * SHFree [SHELL32.195]
461 *
462 * NOTES
463 * exported by ordinal
464 */
465 void WINAPI SHFree(LPVOID pv)
466 {
467 IMalloc * ppv;
468
469 TRACE("%p\n",pv);
470 if (!ShellTaskAllocator) SHGetMalloc(&ppv);
471 IMalloc_Free(ShellTaskAllocator, pv);
472 }
473
474 /*************************************************************************
475 * SHGetDesktopFolder [SHELL32.@]
476 */
477 HRESULT WINAPI SHGetDesktopFolder(IShellFolder **psf)
478 {
479 HRESULT hres = S_OK;
480 TRACE("\n");
481
482 if(!psf) return E_INVALIDARG;
483 *psf = NULL;
484 hres = ISF_Desktop_Constructor(NULL, &IID_IShellFolder,(LPVOID*)psf);
485
486 TRACE("-- %p->(%p)\n",psf, *psf);
487 return hres;
488 }
489 /**************************************************************************
490 * Default ClassFactory Implementation
491 *
492 * SHCreateDefClassObject
493 *
494 * NOTES
495 * Helper function for dlls without their own classfactory.
496 * A generic classfactory is returned.
497 * When the CreateInstance of the cf is called the callback is executed.
498 */
499
500 typedef struct
501 {
502 const IClassFactoryVtbl *lpVtbl;
503 LONG ref;
504 CLSID *rclsid;
505 LPFNCREATEINSTANCE lpfnCI;
506 const IID * riidInst;
507 LONG * pcRefDll; /* pointer to refcounter in external dll (ugrrr...) */
508 } IDefClFImpl;
509
510 static const IClassFactoryVtbl dclfvt;
511
512 /**************************************************************************
513 * IDefClF_fnConstructor
514 */
515
516 IClassFactory * IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI, PLONG pcRefDll, REFIID riidInst)
517 {
518 IDefClFImpl* lpclf;
519
520 lpclf = HeapAlloc(GetProcessHeap(),0,sizeof(IDefClFImpl));
521 lpclf->ref = 1;
522 lpclf->lpVtbl = &dclfvt;
523 lpclf->lpfnCI = lpfnCI;
524 lpclf->pcRefDll = pcRefDll;
525
526 if (pcRefDll) InterlockedIncrement(pcRefDll);
527 lpclf->riidInst = riidInst;
528
529 TRACE("(%p)%s\n",lpclf, shdebugstr_guid(riidInst));
530 return (LPCLASSFACTORY)lpclf;
531 }
532 /**************************************************************************
533 * IDefClF_fnQueryInterface
534 */
535 static HRESULT WINAPI IDefClF_fnQueryInterface(
536 LPCLASSFACTORY iface, REFIID riid, LPVOID *ppvObj)
537 {
538 IDefClFImpl *This = (IDefClFImpl *)iface;
539
540 TRACE("(%p)->(%s)\n",This,shdebugstr_guid(riid));
541
542 *ppvObj = NULL;
543
544 if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory)) {
545 *ppvObj = This;
546 InterlockedIncrement(&This->ref);
547 return S_OK;
548 }
549
550 TRACE("-- E_NOINTERFACE\n");
551 return E_NOINTERFACE;
552 }
553 /******************************************************************************
554 * IDefClF_fnAddRef
555 */
556 static ULONG WINAPI IDefClF_fnAddRef(LPCLASSFACTORY iface)
557 {
558 IDefClFImpl *This = (IDefClFImpl *)iface;
559 ULONG refCount = InterlockedIncrement(&This->ref);
560
561 TRACE("(%p)->(count=%lu)\n", This, refCount - 1);
562
563 return refCount;
564 }
565 /******************************************************************************
566 * IDefClF_fnRelease
567 */
568 static ULONG WINAPI IDefClF_fnRelease(LPCLASSFACTORY iface)
569 {
570 IDefClFImpl *This = (IDefClFImpl *)iface;
571 ULONG refCount = InterlockedDecrement(&This->ref);
572
573 TRACE("(%p)->(count=%lu)\n", This, refCount + 1);
574
575 if (!refCount)
576 {
577 if (This->pcRefDll) InterlockedDecrement(This->pcRefDll);
578
579 TRACE("-- destroying IClassFactory(%p)\n",This);
580 HeapFree(GetProcessHeap(),0,This);
581 return 0;
582 }
583 return refCount;
584 }
585 /******************************************************************************
586 * IDefClF_fnCreateInstance
587 */
588 static HRESULT WINAPI IDefClF_fnCreateInstance(
589 LPCLASSFACTORY iface, LPUNKNOWN pUnkOuter, REFIID riid, LPVOID *ppvObject)
590 {
591 IDefClFImpl *This = (IDefClFImpl *)iface;
592
593 TRACE("%p->(%p,%s,%p)\n",This,pUnkOuter,shdebugstr_guid(riid),ppvObject);
594
595 *ppvObject = NULL;
596
597 if ( This->riidInst==NULL ||
598 IsEqualCLSID(riid, This->riidInst) ||
599 IsEqualCLSID(riid, &IID_IUnknown) )
600 {
601 return This->lpfnCI(pUnkOuter, riid, ppvObject);
602 }
603
604 ERR("unknown IID requested %s\n",shdebugstr_guid(riid));
605 return E_NOINTERFACE;
606 }
607 /******************************************************************************
608 * IDefClF_fnLockServer
609 */
610 static HRESULT WINAPI IDefClF_fnLockServer(LPCLASSFACTORY iface, BOOL fLock)
611 {
612 IDefClFImpl *This = (IDefClFImpl *)iface;
613 TRACE("%p->(0x%x), not implemented\n",This, fLock);
614 return E_NOTIMPL;
615 }
616
617 static const IClassFactoryVtbl dclfvt =
618 {
619 IDefClF_fnQueryInterface,
620 IDefClF_fnAddRef,
621 IDefClF_fnRelease,
622 IDefClF_fnCreateInstance,
623 IDefClF_fnLockServer
624 };
625
626 /******************************************************************************
627 * SHCreateDefClassObject [SHELL32.70]
628 */
629 HRESULT WINAPI SHCreateDefClassObject(
630 REFIID riid,
631 LPVOID* ppv,
632 LPFNCREATEINSTANCE lpfnCI, /* [in] create instance callback entry */
633 LPDWORD pcRefDll, /* [in/out] ref count of the dll */
634 REFIID riidInst) /* [in] optional interface to the instance */
635 {
636 IClassFactory * pcf;
637
638 TRACE("%s %p %p %p %s\n",
639 shdebugstr_guid(riid), ppv, lpfnCI, pcRefDll, shdebugstr_guid(riidInst));
640
641 if (! IsEqualCLSID(riid, &IID_IClassFactory) ) return E_NOINTERFACE;
642 if (! (pcf = IDefClF_fnConstructor(lpfnCI, (PLONG)pcRefDll, riidInst))) return E_OUTOFMEMORY;
643 *ppv = pcf;
644 return NOERROR;
645 }
646
647 /*************************************************************************
648 * DragAcceptFiles [SHELL32.@]
649 */
650 void WINAPI DragAcceptFiles(HWND hWnd, BOOL b)
651 {
652 LONG exstyle;
653
654 if( !IsWindow(hWnd) ) return;
655 exstyle = GetWindowLongA(hWnd,GWL_EXSTYLE);
656 if (b)
657 exstyle |= WS_EX_ACCEPTFILES;
658 else
659 exstyle &= ~WS_EX_ACCEPTFILES;
660 SetWindowLongA(hWnd,GWL_EXSTYLE,exstyle);
661 }
662
663 /*************************************************************************
664 * DragFinish [SHELL32.@]
665 */
666 void WINAPI DragFinish(HDROP h)
667 {
668 TRACE("\n");
669 GlobalFree((HGLOBAL)h);
670 }
671
672 /*************************************************************************
673 * DragQueryPoint [SHELL32.@]
674 */
675 BOOL WINAPI DragQueryPoint(HDROP hDrop, POINT *p)
676 {
677 DROPFILES *lpDropFileStruct;
678 BOOL bRet;
679
680 TRACE("\n");
681
682 lpDropFileStruct = (DROPFILES *) GlobalLock(hDrop);
683
684 *p = lpDropFileStruct->pt;
685 bRet = lpDropFileStruct->fNC;
686
687 GlobalUnlock(hDrop);
688 return bRet;
689 }
690
691 /*************************************************************************
692 * DragQueryFile [SHELL32.@]
693 * DragQueryFileA [SHELL32.@]
694 */
695 UINT WINAPI DragQueryFileA(
696 HDROP hDrop,
697 UINT lFile,
698 LPSTR lpszFile,
699 UINT lLength)
700 {
701 LPSTR lpDrop;
702 UINT i = 0;
703 DROPFILES *lpDropFileStruct = (DROPFILES *) GlobalLock(hDrop);
704
705 TRACE("(%p, %x, %p, %u)\n", hDrop,lFile,lpszFile,lLength);
706
707 if(!lpDropFileStruct) goto end;
708
709 lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->pFiles;
710
711 if(lpDropFileStruct->fWide) {
712 LPWSTR lpszFileW = NULL;
713
714 if(lpszFile) {
715 lpszFileW = HeapAlloc(GetProcessHeap(), 0, lLength*sizeof(WCHAR));
716 if(lpszFileW == NULL) {
717 goto end;
718 }
719 }
720 i = DragQueryFileW(hDrop, lFile, lpszFileW, lLength);
721
722 if(lpszFileW) {
723 WideCharToMultiByte(CP_ACP, 0, lpszFileW, -1, lpszFile, lLength, 0, NULL);
724 HeapFree(GetProcessHeap(), 0, lpszFileW);
725 }
726 goto end;
727 }
728
729 while (i++ < lFile)
730 {
731 while (*lpDrop++); /* skip filename */
732 if (!*lpDrop)
733 {
734 i = (lFile == 0xFFFFFFFF) ? i : 0;
735 goto end;
736 }
737 }
738
739 i = strlen(lpDrop);
740 i++;
741 if (!lpszFile ) goto end; /* needed buffer size */
742 i = (lLength > i) ? i : lLength;
743 lstrcpynA (lpszFile, lpDrop, i);
744 end:
745 GlobalUnlock(hDrop);
746 return i;
747 }
748
749 /*************************************************************************
750 * DragQueryFileW [SHELL32.@]
751 */
752 UINT WINAPI DragQueryFileW(
753 HDROP hDrop,
754 UINT lFile,
755 LPWSTR lpszwFile,
756 UINT lLength)
757 {
758 LPWSTR lpwDrop;
759 UINT i = 0;
760 DROPFILES *lpDropFileStruct = (DROPFILES *) GlobalLock(hDrop);
761
762 TRACE("(%p, %x, %p, %u)\n", hDrop,lFile,lpszwFile,lLength);
763
764 if(!lpDropFileStruct) goto end;
765
766 lpwDrop = (LPWSTR) ((LPSTR)lpDropFileStruct + lpDropFileStruct->pFiles);
767
768 if(lpDropFileStruct->fWide == FALSE) {
769 LPSTR lpszFileA = NULL;
770
771 if(lpszwFile) {
772 lpszFileA = HeapAlloc(GetProcessHeap(), 0, lLength);
773 if(lpszFileA == NULL) {
774 goto end;
775 }
776 }
777 i = DragQueryFileA(hDrop, lFile, lpszFileA, lLength);
778
779 if(lpszFileA) {
780 MultiByteToWideChar(CP_ACP, 0, lpszFileA, -1, lpszwFile, lLength);
781 HeapFree(GetProcessHeap(), 0, lpszFileA);
782 }
783 goto end;
784 }
785
786 i = 0;
787 while (i++ < lFile)
788 {
789 while (*lpwDrop++); /* skip filename */
790 if (!*lpwDrop)
791 {
792 i = (lFile == 0xFFFFFFFF) ? i : 0;
793 goto end;
794 }
795 }
796
797 i = strlenW(lpwDrop);
798 i++;
799 if ( !lpszwFile) goto end; /* needed buffer size */
800
801 i = (lLength > i) ? i : lLength;
802 lstrcpynW (lpszwFile, lpwDrop, i);
803 end:
804 GlobalUnlock(hDrop);
805 return i;
806 }