msi: Don't flag an error in the InstallServices action if the component does not...
[wine] / dlls / 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 #define NONAMELESSUNION
30
31 #include "windef.h"
32 #include "winbase.h"
33 #include "shellapi.h"
34 #include "wingdi.h"
35 #include "winuser.h"
36 #include "shlobj.h"
37 #include "shlguid.h"
38 #include "winreg.h"
39 #include "winerror.h"
40
41 #include "undocshell.h"
42 #include "wine/unicode.h"
43 #include "shell32_main.h"
44
45 #include "wine/debug.h"
46 #include "shlwapi.h"
47 #include "debughlp.h"
48
49 WINE_DEFAULT_DEBUG_CHANNEL(shell);
50
51 extern INT WINAPI SHStringFromGUIDW(REFGUID guid, LPWSTR lpszDest, INT cchMax);  /* shlwapi.24 */
52
53 /**************************************************************************
54  * Default ClassFactory types
55  */
56 typedef HRESULT (CALLBACK *LPFNCREATEINSTANCE)(IUnknown* pUnkOuter, REFIID riid, LPVOID* ppvObject);
57 static IClassFactory * IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI, PLONG pcRefDll, REFIID riidInst);
58
59 /* this table contains all CLSID's of shell32 objects */
60 static const struct {
61         REFIID                  riid;
62         LPFNCREATEINSTANCE      lpfnCI;
63 } InterfaceTable[] = {
64
65         {&CLSID_AutoComplete,   IAutoComplete_Constructor},
66         {&CLSID_ControlPanel,   IControlPanel_Constructor},
67         {&CLSID_DragDropHelper, IDropTargetHelper_Constructor},
68         {&CLSID_FolderShortcut, FolderShortcut_Constructor},
69         {&CLSID_MyComputer,     ISF_MyComputer_Constructor},
70         {&CLSID_MyDocuments,    MyDocuments_Constructor},
71         {&CLSID_NetworkPlaces,  ISF_NetworkPlaces_Constructor},
72         {&CLSID_Printers,       Printers_Constructor},
73         {&CLSID_QueryAssociations, QueryAssociations_Constructor},
74         {&CLSID_RecycleBin,     RecycleBin_Constructor},
75         {&CLSID_ShellDesktop,   ISF_Desktop_Constructor},
76         {&CLSID_ShellFSFolder,  IFSFolder_Constructor},
77         {&CLSID_ShellItem,      IShellItem_Constructor},
78         {&CLSID_ShellLink,      IShellLink_Constructor},
79         {&CLSID_UnixDosFolder,  UnixDosFolder_Constructor},
80         {&CLSID_UnixFolder,     UnixFolder_Constructor},
81         {&CLSID_ExplorerBrowser,ExplorerBrowser_Constructor},
82         {&CLSID_KnownFolderManager, KnownFolderManager_Constructor},
83         {NULL, NULL}
84 };
85
86 /*************************************************************************
87  * SHCoCreateInstance [SHELL32.102]
88  *
89  * Equivalent to CoCreateInstance. Under Windows 9x this function could sometimes
90  * use the shell32 built-in "mini-COM" without the need to load ole32.dll - see
91  * SHLoadOLE for details.
92  *
93  * Under wine if a "LoadWithoutCOM" value is present or the object resides in
94  * shell32.dll the function will load the object manually without the help of ole32
95  *
96  * NOTES
97  *     exported by ordinal
98  *
99  * SEE ALSO
100  *     CoCreateInstace, SHLoadOLE
101  */
102 HRESULT WINAPI SHCoCreateInstance(
103         LPCWSTR aclsid,
104         const CLSID *clsid,
105         LPUNKNOWN pUnkOuter,
106         REFIID refiid,
107         LPVOID *ppv)
108 {
109         DWORD   hres;
110         IID     iid;
111         const   CLSID * myclsid = clsid;
112         WCHAR   sKeyName[MAX_PATH];
113         const   WCHAR sCLSID[7] = {'C','L','S','I','D','\\','\0'};
114         WCHAR   sClassID[60];
115         const WCHAR sInProcServer32[16] ={'\\','I','n','p','r','o','c','S','e','r','v','e','r','3','2','\0'};
116         const WCHAR sLoadWithoutCOM[15] ={'L','o','a','d','W','i','t','h','o','u','t','C','O','M','\0'};
117         WCHAR   sDllPath[MAX_PATH];
118         HKEY    hKey = 0;
119         DWORD   dwSize;
120         IClassFactory * pcf = NULL;
121
122         if(!ppv) return E_POINTER;
123         *ppv=NULL;
124
125         /* if the clsid is a string, convert it */
126         if (!clsid)
127         {
128           if (!aclsid) return REGDB_E_CLASSNOTREG;
129           SHCLSIDFromStringW(aclsid, &iid);
130           myclsid = &iid;
131         }
132
133         TRACE("(%p,%s,unk:%p,%s,%p)\n",
134                 aclsid,shdebugstr_guid(myclsid),pUnkOuter,shdebugstr_guid(refiid),ppv);
135
136         if (SUCCEEDED(DllGetClassObject(myclsid, &IID_IClassFactory,(LPVOID*)&pcf)))
137         {
138             hres = IClassFactory_CreateInstance(pcf, pUnkOuter, refiid, ppv);
139             IClassFactory_Release(pcf);
140             goto end;
141         }
142
143         /* we look up the dll path in the registry */
144         SHStringFromGUIDW(myclsid, sClassID, sizeof(sClassID)/sizeof(WCHAR));
145         lstrcpyW(sKeyName, sCLSID);
146         lstrcatW(sKeyName, sClassID);
147         lstrcatW(sKeyName, sInProcServer32);
148
149         if (RegOpenKeyExW(HKEY_CLASSES_ROOT, sKeyName, 0, KEY_READ, &hKey))
150             return E_ACCESSDENIED;
151
152         /* if a special registry key is set, we load a shell extension without help of OLE32 */
153         if (!SHQueryValueExW(hKey, sLoadWithoutCOM, 0, 0, 0, 0))
154         {
155             /* load an external dll without ole32 */
156             HANDLE hLibrary;
157             typedef HRESULT (CALLBACK *DllGetClassObjectFunc)(REFCLSID clsid, REFIID iid, LPVOID *ppv);
158             DllGetClassObjectFunc DllGetClassObject;
159
160             dwSize = sizeof(sDllPath);
161             SHQueryValueExW(hKey, NULL, 0,0, sDllPath, &dwSize );
162
163             if ((hLibrary = LoadLibraryExW(sDllPath, 0, LOAD_WITH_ALTERED_SEARCH_PATH)) == 0) {
164                 ERR("couldn't load InprocServer32 dll %s\n", debugstr_w(sDllPath));
165                 hres = E_ACCESSDENIED;
166                 goto end;
167             } else if (!(DllGetClassObject = (DllGetClassObjectFunc)GetProcAddress(hLibrary, "DllGetClassObject"))) {
168                 ERR("couldn't find function DllGetClassObject in %s\n", debugstr_w(sDllPath));
169                 FreeLibrary( hLibrary );
170                 hres = E_ACCESSDENIED;
171                 goto end;
172             } else if (FAILED(hres = DllGetClassObject(myclsid, &IID_IClassFactory, (LPVOID*)&pcf))) {
173                     TRACE("GetClassObject failed 0x%08x\n", hres);
174                     goto end;
175             }
176
177             hres = IClassFactory_CreateInstance(pcf, pUnkOuter, refiid, ppv);
178             IClassFactory_Release(pcf);
179         } else {
180
181             /* load an external dll in the usual way */
182             hres = CoCreateInstance(myclsid, pUnkOuter, CLSCTX_INPROC_SERVER, refiid, ppv);
183         }
184
185 end:
186         if (hKey) RegCloseKey(hKey);
187         if(hres!=S_OK)
188         {
189           ERR("failed (0x%08x) to create CLSID:%s IID:%s\n",
190               hres, shdebugstr_guid(myclsid), shdebugstr_guid(refiid));
191           ERR("class not found in registry\n");
192         }
193
194         TRACE("-- instance: %p\n",*ppv);
195         return hres;
196 }
197
198 /*************************************************************************
199  * DllGetClassObject     [SHELL32.@]
200  * SHDllGetClassObject   [SHELL32.128]
201  */
202 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
203 {
204         HRESULT hres = E_OUTOFMEMORY;
205         IClassFactory * pcf = NULL;
206         int i;
207
208         TRACE("CLSID:%s,IID:%s\n",shdebugstr_guid(rclsid),shdebugstr_guid(iid));
209
210         if (!ppv) return E_INVALIDARG;
211         *ppv = NULL;
212
213         /* search our internal interface table */
214         for(i=0;InterfaceTable[i].riid;i++) {
215             if(IsEqualIID(InterfaceTable[i].riid, rclsid)) {
216                 TRACE("index[%u]\n", i);
217                 pcf = IDefClF_fnConstructor(InterfaceTable[i].lpfnCI, NULL, NULL);
218             }
219         }
220
221         if (!pcf) {
222             FIXME("failed for CLSID=%s\n", shdebugstr_guid(rclsid));
223             return CLASS_E_CLASSNOTAVAILABLE;
224         }
225
226         hres = IClassFactory_QueryInterface(pcf, iid, ppv);
227         IClassFactory_Release(pcf);
228
229         TRACE("-- pointer to class factory: %p\n",*ppv);
230         return hres;
231 }
232
233 /*************************************************************************
234  * SHCLSIDFromString                            [SHELL32.147]
235  *
236  * Under Windows 9x this was an ANSI version of CLSIDFromString. It also allowed
237  * to avoid dependency on ole32.dll (see SHLoadOLE for details).
238  *
239  * Under Windows NT/2000/XP this is equivalent to CLSIDFromString
240  *
241  * NOTES
242  *     exported by ordinal
243  *
244  * SEE ALSO
245  *     CLSIDFromString, SHLoadOLE
246  */
247 DWORD WINAPI SHCLSIDFromStringA (LPCSTR clsid, CLSID *id)
248 {
249     WCHAR buffer[40];
250     TRACE("(%p(%s) %p)\n", clsid, clsid, id);
251     if (!MultiByteToWideChar( CP_ACP, 0, clsid, -1, buffer, sizeof(buffer)/sizeof(WCHAR) ))
252         return CO_E_CLASSSTRING;
253     return CLSIDFromString( buffer, id );
254 }
255 DWORD WINAPI SHCLSIDFromStringW (LPCWSTR clsid, CLSID *id)
256 {
257         TRACE("(%p(%s) %p)\n", clsid, debugstr_w(clsid), id);
258         return CLSIDFromString(clsid, id);
259 }
260 DWORD WINAPI SHCLSIDFromStringAW (LPCVOID clsid, CLSID *id)
261 {
262         if (SHELL_OsIsUnicode())
263           return SHCLSIDFromStringW (clsid, id);
264         return SHCLSIDFromStringA (clsid, id);
265 }
266
267 /*************************************************************************
268  *                       SHGetMalloc                    [SHELL32.@]
269  *
270  * Equivalent to CoGetMalloc(MEMCTX_TASK, ...). Under Windows 9x this function
271  * could use the shell32 built-in "mini-COM" without the need to load ole32.dll -
272  * see SHLoadOLE for details. 
273  *
274  * PARAMS
275  *  lpmal [O] Destination for IMalloc interface.
276  *
277  * RETURNS
278  *  Success: S_OK. lpmal contains the shells IMalloc interface.
279  *  Failure. An HRESULT error code.
280  *
281  * SEE ALSO
282  *  CoGetMalloc, SHLoadOLE
283  */
284 HRESULT WINAPI SHGetMalloc(LPMALLOC *lpmal)
285 {
286         TRACE("(%p)\n", lpmal);
287         return CoGetMalloc(MEMCTX_TASK, lpmal);
288 }
289
290 /*************************************************************************
291  * SHAlloc                                      [SHELL32.196]
292  *
293  * Equivalent to CoTaskMemAlloc. Under Windows 9x this function could use
294  * the shell32 built-in "mini-COM" without the need to load ole32.dll -
295  * see SHLoadOLE for details. 
296  *
297  * NOTES
298  *     exported by ordinal
299  *
300  * SEE ALSO
301  *     CoTaskMemAlloc, SHLoadOLE
302  */
303 LPVOID WINAPI SHAlloc(DWORD len)
304 {
305         LPVOID ret;
306
307         ret = CoTaskMemAlloc(len);
308         TRACE("%u bytes at %p\n",len, ret);
309         return ret;
310 }
311
312 /*************************************************************************
313  * SHFree                                       [SHELL32.195]
314  *
315  * Equivalent to CoTaskMemFree. Under Windows 9x this function could use
316  * the shell32 built-in "mini-COM" without the need to load ole32.dll -
317  * see SHLoadOLE for details. 
318  *
319  * NOTES
320  *     exported by ordinal
321  *
322  * SEE ALSO
323  *     CoTaskMemFree, SHLoadOLE
324  */
325 void WINAPI SHFree(LPVOID pv)
326 {
327         TRACE("%p\n",pv);
328         CoTaskMemFree(pv);
329 }
330
331 /*************************************************************************
332  * SHGetDesktopFolder                   [SHELL32.@]
333  */
334 HRESULT WINAPI SHGetDesktopFolder(IShellFolder **psf)
335 {
336         HRESULT hres;
337
338         TRACE("(%p)\n", psf);
339
340         if(!psf) return E_INVALIDARG;
341
342         *psf = NULL;
343         hres = ISF_Desktop_Constructor(NULL, &IID_IShellFolder, (LPVOID*)psf);
344
345         TRACE("-- %p->(%p) 0x%08x\n", psf, *psf, hres);
346         return hres;
347 }
348 /**************************************************************************
349  * Default ClassFactory Implementation
350  *
351  * SHCreateDefClassObject
352  *
353  * NOTES
354  *  Helper function for dlls without their own classfactory.
355  *  A generic classfactory is returned.
356  *  When the CreateInstance of the cf is called the callback is executed.
357  */
358
359 typedef struct
360 {
361     IClassFactory               IClassFactory_iface;
362     LONG                        ref;
363     CLSID                       *rclsid;
364     LPFNCREATEINSTANCE          lpfnCI;
365     const IID *                 riidInst;
366     LONG *                      pcRefDll; /* pointer to refcounter in external dll (ugrrr...) */
367 } IDefClFImpl;
368
369 static inline IDefClFImpl *impl_from_IClassFactory(IClassFactory *iface)
370 {
371         return CONTAINING_RECORD(iface, IDefClFImpl, IClassFactory_iface);
372 }
373
374 static const IClassFactoryVtbl dclfvt;
375
376 /**************************************************************************
377  *  IDefClF_fnConstructor
378  */
379
380 static IClassFactory * IDefClF_fnConstructor(LPFNCREATEINSTANCE lpfnCI, PLONG pcRefDll, REFIID riidInst)
381 {
382         IDefClFImpl* lpclf;
383
384         lpclf = HeapAlloc(GetProcessHeap(),0,sizeof(IDefClFImpl));
385         lpclf->ref = 1;
386         lpclf->IClassFactory_iface.lpVtbl = &dclfvt;
387         lpclf->lpfnCI = lpfnCI;
388         lpclf->pcRefDll = pcRefDll;
389
390         if (pcRefDll) InterlockedIncrement(pcRefDll);
391         lpclf->riidInst = riidInst;
392
393         TRACE("(%p)%s\n",lpclf, shdebugstr_guid(riidInst));
394         return (LPCLASSFACTORY)lpclf;
395 }
396 /**************************************************************************
397  *  IDefClF_fnQueryInterface
398  */
399 static HRESULT WINAPI IDefClF_fnQueryInterface(
400   LPCLASSFACTORY iface, REFIID riid, LPVOID *ppvObj)
401 {
402         IDefClFImpl *This = impl_from_IClassFactory(iface);
403
404         TRACE("(%p)->(%s)\n",This,shdebugstr_guid(riid));
405
406         *ppvObj = NULL;
407
408         if(IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory)) {
409           *ppvObj = This;
410           InterlockedIncrement(&This->ref);
411           return S_OK;
412         }
413
414         TRACE("-- E_NOINTERFACE\n");
415         return E_NOINTERFACE;
416 }
417 /******************************************************************************
418  * IDefClF_fnAddRef
419  */
420 static ULONG WINAPI IDefClF_fnAddRef(LPCLASSFACTORY iface)
421 {
422         IDefClFImpl *This = impl_from_IClassFactory(iface);
423         ULONG refCount = InterlockedIncrement(&This->ref);
424
425         TRACE("(%p)->(count=%u)\n", This, refCount - 1);
426
427         return refCount;
428 }
429 /******************************************************************************
430  * IDefClF_fnRelease
431  */
432 static ULONG WINAPI IDefClF_fnRelease(LPCLASSFACTORY iface)
433 {
434         IDefClFImpl *This = impl_from_IClassFactory(iface);
435         ULONG refCount = InterlockedDecrement(&This->ref);
436
437         TRACE("(%p)->(count=%u)\n", This, refCount + 1);
438
439         if (!refCount)
440         {
441           if (This->pcRefDll) InterlockedDecrement(This->pcRefDll);
442
443           TRACE("-- destroying IClassFactory(%p)\n",This);
444           HeapFree(GetProcessHeap(),0,This);
445           return 0;
446         }
447         return refCount;
448 }
449 /******************************************************************************
450  * IDefClF_fnCreateInstance
451  */
452 static HRESULT WINAPI IDefClF_fnCreateInstance(
453   LPCLASSFACTORY iface, LPUNKNOWN pUnkOuter, REFIID riid, LPVOID *ppvObject)
454 {
455         IDefClFImpl *This = impl_from_IClassFactory(iface);
456
457         TRACE("%p->(%p,%s,%p)\n",This,pUnkOuter,shdebugstr_guid(riid),ppvObject);
458
459         *ppvObject = NULL;
460
461         if ( This->riidInst==NULL ||
462              IsEqualCLSID(riid, This->riidInst) ||
463              IsEqualCLSID(riid, &IID_IUnknown) )
464         {
465           return This->lpfnCI(pUnkOuter, riid, ppvObject);
466         }
467
468         ERR("unknown IID requested %s\n",shdebugstr_guid(riid));
469         return E_NOINTERFACE;
470 }
471 /******************************************************************************
472  * IDefClF_fnLockServer
473  */
474 static HRESULT WINAPI IDefClF_fnLockServer(LPCLASSFACTORY iface, BOOL fLock)
475 {
476         IDefClFImpl *This = impl_from_IClassFactory(iface);
477         TRACE("%p->(0x%x), not implemented\n",This, fLock);
478         return E_NOTIMPL;
479 }
480
481 static const IClassFactoryVtbl dclfvt =
482 {
483     IDefClF_fnQueryInterface,
484     IDefClF_fnAddRef,
485   IDefClF_fnRelease,
486   IDefClF_fnCreateInstance,
487   IDefClF_fnLockServer
488 };
489
490 /******************************************************************************
491  * SHCreateDefClassObject                       [SHELL32.70]
492  */
493 HRESULT WINAPI SHCreateDefClassObject(
494         REFIID  riid,
495         LPVOID* ppv,
496         LPFNCREATEINSTANCE lpfnCI,      /* [in] create instance callback entry */
497         LPDWORD pcRefDll,               /* [in/out] ref count of the dll */
498         REFIID  riidInst)               /* [in] optional interface to the instance */
499 {
500         IClassFactory * pcf;
501
502         TRACE("%s %p %p %p %s\n",
503               shdebugstr_guid(riid), ppv, lpfnCI, pcRefDll, shdebugstr_guid(riidInst));
504
505         if (! IsEqualCLSID(riid, &IID_IClassFactory) ) return E_NOINTERFACE;
506         if (! (pcf = IDefClF_fnConstructor(lpfnCI, (PLONG)pcRefDll, riidInst))) return E_OUTOFMEMORY;
507         *ppv = pcf;
508         return NOERROR;
509 }
510
511 /*************************************************************************
512  *  DragAcceptFiles             [SHELL32.@]
513  */
514 void WINAPI DragAcceptFiles(HWND hWnd, BOOL b)
515 {
516         LONG exstyle;
517
518         if( !IsWindow(hWnd) ) return;
519         exstyle = GetWindowLongA(hWnd,GWL_EXSTYLE);
520         if (b)
521           exstyle |= WS_EX_ACCEPTFILES;
522         else
523           exstyle &= ~WS_EX_ACCEPTFILES;
524         SetWindowLongA(hWnd,GWL_EXSTYLE,exstyle);
525 }
526
527 /*************************************************************************
528  * DragFinish           [SHELL32.@]
529  */
530 void WINAPI DragFinish(HDROP h)
531 {
532         TRACE("\n");
533         GlobalFree(h);
534 }
535
536 /*************************************************************************
537  * DragQueryPoint               [SHELL32.@]
538  */
539 BOOL WINAPI DragQueryPoint(HDROP hDrop, POINT *p)
540 {
541         DROPFILES *lpDropFileStruct;
542         BOOL bRet;
543
544         TRACE("\n");
545
546         lpDropFileStruct = GlobalLock(hDrop);
547
548         *p = lpDropFileStruct->pt;
549         bRet = lpDropFileStruct->fNC;
550
551         GlobalUnlock(hDrop);
552         return bRet;
553 }
554
555 /*************************************************************************
556  *  DragQueryFileA              [SHELL32.@]
557  *  DragQueryFile               [SHELL32.@]
558  */
559 UINT WINAPI DragQueryFileA(
560         HDROP hDrop,
561         UINT lFile,
562         LPSTR lpszFile,
563         UINT lLength)
564 {
565         LPSTR lpDrop;
566         UINT i = 0;
567         DROPFILES *lpDropFileStruct = GlobalLock(hDrop);
568
569         TRACE("(%p, %x, %p, %u)\n",     hDrop,lFile,lpszFile,lLength);
570
571         if(!lpDropFileStruct) goto end;
572
573         lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->pFiles;
574
575         if(lpDropFileStruct->fWide) {
576             LPWSTR lpszFileW = NULL;
577
578             if(lpszFile) {
579                 lpszFileW = HeapAlloc(GetProcessHeap(), 0, lLength*sizeof(WCHAR));
580                 if(lpszFileW == NULL) {
581                     goto end;
582                 }
583             }
584             i = DragQueryFileW(hDrop, lFile, lpszFileW, lLength);
585
586             if(lpszFileW) {
587                 WideCharToMultiByte(CP_ACP, 0, lpszFileW, -1, lpszFile, lLength, 0, NULL);
588                 HeapFree(GetProcessHeap(), 0, lpszFileW);
589             }
590             goto end;
591         }
592
593         while (i++ < lFile)
594         {
595           while (*lpDrop++); /* skip filename */
596           if (!*lpDrop)
597           {
598             i = (lFile == 0xFFFFFFFF) ? i : 0;
599             goto end;
600           }
601         }
602
603         i = strlen(lpDrop);
604         if (!lpszFile ) goto end;   /* needed buffer size */
605         lstrcpynA (lpszFile, lpDrop, lLength);
606 end:
607         GlobalUnlock(hDrop);
608         return i;
609 }
610
611 /*************************************************************************
612  *  DragQueryFileW              [SHELL32.@]
613  */
614 UINT WINAPI DragQueryFileW(
615         HDROP hDrop,
616         UINT lFile,
617         LPWSTR lpszwFile,
618         UINT lLength)
619 {
620         LPWSTR lpwDrop;
621         UINT i = 0;
622         DROPFILES *lpDropFileStruct = GlobalLock(hDrop);
623
624         TRACE("(%p, %x, %p, %u)\n", hDrop,lFile,lpszwFile,lLength);
625
626         if(!lpDropFileStruct) goto end;
627
628         lpwDrop = (LPWSTR) ((LPSTR)lpDropFileStruct + lpDropFileStruct->pFiles);
629
630         if(lpDropFileStruct->fWide == FALSE) {
631             LPSTR lpszFileA = NULL;
632
633             if(lpszwFile) {
634                 lpszFileA = HeapAlloc(GetProcessHeap(), 0, lLength);
635                 if(lpszFileA == NULL) {
636                     goto end;
637                 }
638             }
639             i = DragQueryFileA(hDrop, lFile, lpszFileA, lLength);
640
641             if(lpszFileA) {
642                 MultiByteToWideChar(CP_ACP, 0, lpszFileA, -1, lpszwFile, lLength);
643                 HeapFree(GetProcessHeap(), 0, lpszFileA);
644             }
645             goto end;
646         }
647
648         i = 0;
649         while (i++ < lFile)
650         {
651           while (*lpwDrop++); /* skip filename */
652           if (!*lpwDrop)
653           {
654             i = (lFile == 0xFFFFFFFF) ? i : 0;
655             goto end;
656           }
657         }
658
659         i = strlenW(lpwDrop);
660         if ( !lpszwFile) goto end;   /* needed buffer size */
661         lstrcpynW (lpszwFile, lpwDrop, lLength);
662 end:
663         GlobalUnlock(hDrop);
664         return i;
665 }
666
667 /*************************************************************************
668  *  SHPropStgCreate             [SHELL32.685]
669  */
670 HRESULT WINAPI SHPropStgCreate(IPropertySetStorage *psstg, REFFMTID fmtid,
671         const CLSID *pclsid, DWORD grfFlags, DWORD grfMode,
672         DWORD dwDisposition, IPropertyStorage **ppstg, UINT *puCodePage)
673 {
674     PROPSPEC prop;
675     PROPVARIANT ret;
676     HRESULT hres;
677
678     TRACE("%p %s %s %x %x %x %p %p\n", psstg, debugstr_guid(fmtid), debugstr_guid(pclsid),
679             grfFlags, grfMode, dwDisposition, ppstg, puCodePage);
680
681     hres = IPropertySetStorage_Open(psstg, fmtid, grfMode, ppstg);
682
683     switch(dwDisposition) {
684     case CREATE_ALWAYS:
685         if(SUCCEEDED(hres)) {
686             IPropertyStorage_Release(*ppstg);
687             hres = IPropertySetStorage_Delete(psstg, fmtid);
688             if(FAILED(hres))
689                 return hres;
690             hres = E_FAIL;
691         }
692
693     case OPEN_ALWAYS:
694     case CREATE_NEW:
695         if(FAILED(hres))
696             hres = IPropertySetStorage_Create(psstg, fmtid, pclsid,
697                     grfFlags, grfMode, ppstg);
698
699     case OPEN_EXISTING:
700         if(FAILED(hres))
701             return hres;
702
703         if(puCodePage) {
704             prop.ulKind = PRSPEC_PROPID;
705             prop.u.propid = PID_CODEPAGE;
706             hres = IPropertyStorage_ReadMultiple(*ppstg, 1, &prop, &ret);
707             if(FAILED(hres) || ret.vt!=VT_I2)
708                 *puCodePage = 0;
709             else
710                 *puCodePage = ret.u.iVal;
711         }
712     }
713
714     return S_OK;
715 }
716
717 /*************************************************************************
718  *  SHPropStgReadMultiple       [SHELL32.688]
719  */
720 HRESULT WINAPI SHPropStgReadMultiple(IPropertyStorage *pps, UINT uCodePage,
721         ULONG cpspec, const PROPSPEC *rgpspec, PROPVARIANT *rgvar)
722 {
723     STATPROPSETSTG stat;
724     HRESULT hres;
725
726     FIXME("%p %u %u %p %p\n", pps, uCodePage, cpspec, rgpspec, rgvar);
727
728     memset(rgvar, 0, cpspec*sizeof(PROPVARIANT));
729     hres = IPropertyStorage_ReadMultiple(pps, cpspec, rgpspec, rgvar);
730     if(FAILED(hres))
731         return hres;
732
733     if(!uCodePage) {
734         PROPSPEC prop;
735         PROPVARIANT ret;
736
737         prop.ulKind = PRSPEC_PROPID;
738         prop.u.propid = PID_CODEPAGE;
739         hres = IPropertyStorage_ReadMultiple(pps, 1, &prop, &ret);
740         if(FAILED(hres) || ret.vt!=VT_I2)
741             return S_OK;
742
743         uCodePage = ret.u.iVal;
744     }
745
746     hres = IPropertyStorage_Stat(pps, &stat);
747     if(FAILED(hres))
748         return S_OK;
749
750     /* TODO: do something with codepage and stat */
751     return S_OK;
752 }
753
754 /*************************************************************************
755  *  SHPropStgWriteMultiple      [SHELL32.689]
756  */
757 HRESULT WINAPI SHPropStgWriteMultiple(IPropertyStorage *pps, UINT *uCodePage,
758         ULONG cpspec, const PROPSPEC *rgpspec, PROPVARIANT *rgvar, PROPID propidNameFirst)
759 {
760     STATPROPSETSTG stat;
761     UINT codepage;
762     HRESULT hres;
763
764     FIXME("%p %p %u %p %p %d\n", pps, uCodePage, cpspec, rgpspec, rgvar, propidNameFirst);
765
766     hres = IPropertyStorage_Stat(pps, &stat);
767     if(FAILED(hres))
768         return hres;
769
770     if(uCodePage && *uCodePage)
771         codepage = *uCodePage;
772     else {
773         PROPSPEC prop;
774         PROPVARIANT ret;
775
776         prop.ulKind = PRSPEC_PROPID;
777         prop.u.propid = PID_CODEPAGE;
778         hres = IPropertyStorage_ReadMultiple(pps, 1, &prop, &ret);
779         if(FAILED(hres))
780             return hres;
781         if(ret.vt!=VT_I2 || !ret.u.iVal)
782             return E_FAIL;
783
784         codepage = ret.u.iVal;
785         if(uCodePage)
786             *uCodePage = codepage;
787     }
788
789     /* TODO: do something with codepage and stat */
790
791     hres = IPropertyStorage_WriteMultiple(pps, cpspec, rgpspec, rgvar, propidNameFirst);
792     return hres;
793 }