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