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