ole32: Register the CLSID_Picture_Metafile and CLSID_Picture_Dib ProgIDs.
[wine] / dlls / ole32 / ole2.c
1 /*
2  *      OLE2 library
3  *
4  * Copyright 1995 Martin von Loewis
5  * Copyright 1999 Francis Beaudet
6  * Copyright 1999 Noel Borthwick
7  * Copyright 1999, 2000 Marcus Meissner
8  * Copyright 2005 Juan Lang
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24
25 #include "config.h"
26
27 #include <assert.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <string.h>
32
33 #define COBJMACROS
34 #define NONAMELESSUNION
35 #define NONAMELESSSTRUCT
36
37 #include "windef.h"
38 #include "winbase.h"
39 #include "winerror.h"
40 #include "wingdi.h"
41 #include "winuser.h"
42 #include "winnls.h"
43 #include "winreg.h"
44 #include "commctrl.h"
45 #include "ole2.h"
46 #include "ole2ver.h"
47 #include "wownt32.h"
48
49 #include "wine/unicode.h"
50 #include "compobj_private.h"
51 #include "wine/list.h"
52
53 #include "wine/debug.h"
54
55 WINE_DEFAULT_DEBUG_CHANNEL(ole);
56 WINE_DECLARE_DEBUG_CHANNEL(accel);
57
58 /******************************************************************************
59  * These are static/global variables and internal data structures that the
60  * OLE module uses to maintain it's state.
61  */
62 typedef struct tagDropTargetNode
63 {
64   HWND          hwndTarget;
65   IDropTarget*  dropTarget;
66   struct list   entry;
67 } DropTargetNode;
68
69 typedef struct tagTrackerWindowInfo
70 {
71   IDataObject* dataObject;
72   IDropSource* dropSource;
73   DWORD        dwOKEffect;
74   DWORD*       pdwEffect;
75   BOOL       trackingDone;
76   HRESULT      returnValue;
77
78   BOOL       escPressed;
79   HWND       curTargetHWND;     /* window the mouse is hovering over */
80   HWND       curDragTargetHWND; /* might be a ancestor of curTargetHWND */
81   IDropTarget* curDragTarget;
82   POINTL     curMousePos;       /* current position of the mouse in screen coordinates */
83   DWORD      dwKeyState;        /* current state of the shift and ctrl keys and the mouse buttons */
84 } TrackerWindowInfo;
85
86 typedef struct tagOleMenuDescriptor  /* OleMenuDescriptor */
87 {
88   HWND               hwndFrame;         /* The containers frame window */
89   HWND               hwndActiveObject;  /* The active objects window */
90   OLEMENUGROUPWIDTHS mgw;               /* OLE menu group widths for the shared menu */
91   HMENU              hmenuCombined;     /* The combined menu */
92   BOOL               bIsServerItem;     /* True if the currently open popup belongs to the server */
93 } OleMenuDescriptor;
94
95 typedef struct tagOleMenuHookItem   /* OleMenu hook item in per thread hook list */
96 {
97   DWORD tid;                /* Thread Id  */
98   HANDLE hHeap;             /* Heap this is allocated from */
99   HHOOK GetMsg_hHook;       /* message hook for WH_GETMESSAGE */
100   HHOOK CallWndProc_hHook;  /* message hook for WH_CALLWNDPROC */
101   struct tagOleMenuHookItem *next;
102 } OleMenuHookItem;
103
104 static OleMenuHookItem *hook_list;
105
106 /*
107  * This is the lock count on the OLE library. It is controlled by the
108  * OLEInitialize/OLEUninitialize methods.
109  */
110 static ULONG OLE_moduleLockCount = 0;
111
112 /*
113  * Name of our registered window class.
114  */
115 static const char OLEDD_DRAGTRACKERCLASS[] = "WineDragDropTracker32";
116
117 /*
118  * This is the head of the Drop target container.
119  */
120 static struct list targetListHead = LIST_INIT(targetListHead);
121
122 /******************************************************************************
123  * These are the prototypes of miscelaneous utility methods
124  */
125 static void OLEUTL_ReadRegistryDWORDValue(HKEY regKey, DWORD* pdwValue);
126
127 /******************************************************************************
128  * These are the prototypes of the utility methods used to manage a shared menu
129  */
130 static void OLEMenu_Initialize(void);
131 static void OLEMenu_UnInitialize(void);
132 static BOOL OLEMenu_InstallHooks( DWORD tid );
133 static BOOL OLEMenu_UnInstallHooks( DWORD tid );
134 static OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid );
135 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos );
136 static BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor );
137 static LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam);
138 static LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam);
139
140 /******************************************************************************
141  * These are the prototypes of the OLE Clipboard initialization methods (in clipboard.c)
142  */
143 extern void OLEClipbrd_UnInitialize(void);
144 extern void OLEClipbrd_Initialize(void);
145
146 /******************************************************************************
147  * These are the prototypes of the utility methods used for OLE Drag n Drop
148  */
149 static void            OLEDD_Initialize(void);
150 static void            OLEDD_UnInitialize(void);
151 static DropTargetNode* OLEDD_FindDropTarget(
152                          HWND hwndOfTarget);
153 static void            OLEDD_FreeDropTarget(DropTargetNode*);
154 static LRESULT WINAPI  OLEDD_DragTrackerWindowProc(
155                          HWND   hwnd,
156                          UINT   uMsg,
157                          WPARAM wParam,
158                          LPARAM   lParam);
159 static void OLEDD_TrackMouseMove(
160                          TrackerWindowInfo* trackerInfo);
161 static void OLEDD_TrackStateChange(
162                          TrackerWindowInfo* trackerInfo);
163 static DWORD OLEDD_GetButtonState(void);
164
165
166 /******************************************************************************
167  *              OleBuildVersion [OLE2.1]
168  *              OleBuildVersion [OLE32.@]
169  */
170 DWORD WINAPI OleBuildVersion(void)
171 {
172     TRACE("Returning version %d, build %d.\n", rmm, rup);
173     return (rmm<<16)+rup;
174 }
175
176 /***********************************************************************
177  *           OleInitialize       (OLE2.2)
178  *           OleInitialize       (OLE32.@)
179  */
180 HRESULT WINAPI OleInitialize(LPVOID reserved)
181 {
182   HRESULT hr;
183
184   TRACE("(%p)\n", reserved);
185
186   /*
187    * The first duty of the OleInitialize is to initialize the COM libraries.
188    */
189   hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
190
191   /*
192    * If the CoInitializeEx call failed, the OLE libraries can't be
193    * initialized.
194    */
195   if (FAILED(hr))
196     return hr;
197
198   /*
199    * Then, it has to initialize the OLE specific modules.
200    * This includes:
201    *     Clipboard
202    *     Drag and Drop
203    *     Object linking and Embedding
204    *     In-place activation
205    */
206   if (OLE_moduleLockCount==0)
207   {
208     /*
209      * Initialize the libraries.
210      */
211     TRACE("() - Initializing the OLE libraries\n");
212
213     /*
214      * OLE Clipboard
215      */
216     OLEClipbrd_Initialize();
217
218     /*
219      * Drag and Drop
220      */
221     OLEDD_Initialize();
222
223     /*
224      * OLE shared menu
225      */
226     OLEMenu_Initialize();
227   }
228
229   /*
230    * Then, we increase the lock count on the OLE module.
231    */
232   OLE_moduleLockCount++;
233
234   return hr;
235 }
236
237 /******************************************************************************
238  *              OleUninitialize [OLE2.3]
239  *              OleUninitialize [OLE32.@]
240  */
241 void WINAPI OleUninitialize(void)
242 {
243   TRACE("()\n");
244
245   /*
246    * Decrease the lock count on the OLE module.
247    */
248   OLE_moduleLockCount--;
249
250   /*
251    * If we hit the bottom of the lock stack, free the libraries.
252    */
253   if (OLE_moduleLockCount==0)
254   {
255     /*
256      * Actually free the libraries.
257      */
258     TRACE("() - Freeing the last reference count\n");
259
260     /*
261      * OLE Clipboard
262      */
263     OLEClipbrd_UnInitialize();
264
265     /*
266      * Drag and Drop
267      */
268     OLEDD_UnInitialize();
269
270     /*
271      * OLE shared menu
272      */
273     OLEMenu_UnInitialize();
274   }
275
276   /*
277    * Then, uninitialize the COM libraries.
278    */
279   CoUninitialize();
280 }
281
282 /******************************************************************************
283  *              OleInitializeWOW        [OLE32.@]
284  */
285 HRESULT WINAPI OleInitializeWOW(DWORD x, DWORD y) {
286         FIXME("(0x%08lx, 0x%08lx),stub!\n",x, y);
287         return 0;
288 }
289
290 /***********************************************************************
291  *           RegisterDragDrop (OLE32.@)
292  */
293 HRESULT WINAPI RegisterDragDrop(
294         HWND hwnd,
295         LPDROPTARGET pDropTarget)
296 {
297   DropTargetNode* dropTargetInfo;
298
299   TRACE("(%p,%p)\n", hwnd, pDropTarget);
300
301   if (!pDropTarget)
302     return E_INVALIDARG;
303   
304   /*
305    * First, check if the window is already registered.
306    */
307   dropTargetInfo = OLEDD_FindDropTarget(hwnd);
308
309   if (dropTargetInfo!=NULL)
310     return DRAGDROP_E_ALREADYREGISTERED;
311
312   /*
313    * If it's not there, we can add it. We first create a node for it.
314    */
315   dropTargetInfo = HeapAlloc(GetProcessHeap(), 0, sizeof(DropTargetNode));
316
317   if (dropTargetInfo==NULL)
318     return E_OUTOFMEMORY;
319
320   dropTargetInfo->hwndTarget     = hwnd;
321
322   /*
323    * Don't forget that this is an interface pointer, need to nail it down since
324    * we keep a copy of it.
325    */
326   IDropTarget_AddRef(pDropTarget);
327   dropTargetInfo->dropTarget  = pDropTarget;
328
329   list_add_tail(&targetListHead, &dropTargetInfo->entry);
330
331   return S_OK;
332 }
333
334 /***********************************************************************
335  *           RevokeDragDrop (OLE32.@)
336  */
337 HRESULT WINAPI RevokeDragDrop(
338         HWND hwnd)
339 {
340   DropTargetNode* dropTargetInfo;
341
342   TRACE("(%p)\n", hwnd);
343
344   /*
345    * First, check if the window is already registered.
346    */
347   dropTargetInfo = OLEDD_FindDropTarget(hwnd);
348
349   /*
350    * If it ain't in there, it's an error.
351    */
352   if (dropTargetInfo==NULL)
353     return DRAGDROP_E_NOTREGISTERED;
354
355   OLEDD_FreeDropTarget(dropTargetInfo);
356
357   return S_OK;
358 }
359
360 /***********************************************************************
361  *           OleRegGetUserType (OLE32.@)
362  *
363  * This implementation of OleRegGetUserType ignores the dwFormOfType
364  * parameter and always returns the full name of the object. This is
365  * not too bad since this is the case for many objects because of the
366  * way they are registered.
367  */
368 HRESULT WINAPI OleRegGetUserType(
369         REFCLSID clsid,
370         DWORD dwFormOfType,
371         LPOLESTR* pszUserType)
372 {
373   char    keyName[60];
374   DWORD   dwKeyType;
375   DWORD   cbData;
376   HKEY    clsidKey;
377   LONG    hres;
378   LPSTR   buffer;
379   HRESULT retVal;
380   /*
381    * Initialize the out parameter.
382    */
383   *pszUserType = NULL;
384
385   /*
386    * Build the key name we're looking for
387    */
388   sprintf( keyName, "CLSID\\{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\",
389            clsid->Data1, clsid->Data2, clsid->Data3,
390            clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
391            clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );
392
393   TRACE("(%s, %ld, %p)\n", keyName, dwFormOfType, pszUserType);
394
395   /*
396    * Open the class id Key
397    */
398   hres = RegOpenKeyA(HKEY_CLASSES_ROOT,
399                      keyName,
400                      &clsidKey);
401
402   if (hres != ERROR_SUCCESS)
403     return REGDB_E_CLASSNOTREG;
404
405   /*
406    * Retrieve the size of the name string.
407    */
408   cbData = 0;
409
410   hres = RegQueryValueExA(clsidKey,
411                           "",
412                           NULL,
413                           &dwKeyType,
414                           NULL,
415                           &cbData);
416
417   if (hres!=ERROR_SUCCESS)
418   {
419     RegCloseKey(clsidKey);
420     return REGDB_E_READREGDB;
421   }
422
423   /*
424    * Allocate a buffer for the registry value.
425    */
426   *pszUserType = CoTaskMemAlloc(cbData*2);
427
428   if (*pszUserType==NULL)
429   {
430     RegCloseKey(clsidKey);
431     return E_OUTOFMEMORY;
432   }
433
434   buffer = HeapAlloc(GetProcessHeap(), 0, cbData);
435
436   if (buffer == NULL)
437   {
438     RegCloseKey(clsidKey);
439     CoTaskMemFree(*pszUserType);
440     *pszUserType=NULL;
441     return E_OUTOFMEMORY;
442   }
443
444   hres = RegQueryValueExA(clsidKey,
445                           "",
446                           NULL,
447                           &dwKeyType,
448                           (LPBYTE) buffer,
449                           &cbData);
450
451   RegCloseKey(clsidKey);
452
453
454   if (hres!=ERROR_SUCCESS)
455   {
456     CoTaskMemFree(*pszUserType);
457     *pszUserType=NULL;
458
459     retVal = REGDB_E_READREGDB;
460   }
461   else
462   {
463     MultiByteToWideChar( CP_ACP, 0, buffer, -1, *pszUserType, cbData /*FIXME*/ );
464     retVal = S_OK;
465   }
466   HeapFree(GetProcessHeap(), 0, buffer);
467
468   return retVal;
469 }
470
471 /***********************************************************************
472  * DoDragDrop [OLE32.@]
473  */
474 HRESULT WINAPI DoDragDrop (
475   IDataObject *pDataObject,  /* [in] ptr to the data obj           */
476   IDropSource* pDropSource,  /* [in] ptr to the source obj         */
477   DWORD       dwOKEffect,    /* [in] effects allowed by the source */
478   DWORD       *pdwEffect)    /* [out] ptr to effects of the source */
479 {
480   TrackerWindowInfo trackerInfo;
481   HWND            hwndTrackWindow;
482   MSG             msg;
483
484   TRACE("(DataObject %p, DropSource %p)\n", pDataObject, pDropSource);
485
486   /*
487    * Setup the drag n drop tracking window.
488    */
489   if (!IsValidInterface((LPUNKNOWN)pDropSource))
490       return E_INVALIDARG;
491
492   trackerInfo.dataObject        = pDataObject;
493   trackerInfo.dropSource        = pDropSource;
494   trackerInfo.dwOKEffect        = dwOKEffect;
495   trackerInfo.pdwEffect         = pdwEffect;
496   trackerInfo.trackingDone      = FALSE;
497   trackerInfo.escPressed        = FALSE;
498   trackerInfo.curDragTargetHWND = 0;
499   trackerInfo.curTargetHWND     = 0;
500   trackerInfo.curDragTarget     = 0;
501
502   hwndTrackWindow = CreateWindowA(OLEDD_DRAGTRACKERCLASS,
503                                     "TrackerWindow",
504                                     WS_POPUP,
505                                     CW_USEDEFAULT, CW_USEDEFAULT,
506                                     CW_USEDEFAULT, CW_USEDEFAULT,
507                                     0,
508                                     0,
509                                     0,
510                                     (LPVOID)&trackerInfo);
511
512   if (hwndTrackWindow!=0)
513   {
514     /*
515      * Capture the mouse input
516      */
517     SetCapture(hwndTrackWindow);
518
519     msg.message = 0;
520
521     /*
522      * Pump messages. All mouse input should go the the capture window.
523      */
524     while (!trackerInfo.trackingDone && GetMessageA(&msg, 0, 0, 0) )
525     {
526       trackerInfo.curMousePos.x = msg.pt.x;
527       trackerInfo.curMousePos.y = msg.pt.y;
528       trackerInfo.dwKeyState = OLEDD_GetButtonState();
529             
530       if ( (msg.message >= WM_KEYFIRST) &&
531            (msg.message <= WM_KEYLAST) )
532       {
533         /*
534          * When keyboard messages are sent to windows on this thread, we
535          * want to ignore notify the drop source that the state changed.
536          * in the case of the Escape key, we also notify the drop source
537          * we give it a special meaning.
538          */
539         if ( (msg.message==WM_KEYDOWN) &&
540              (msg.wParam==VK_ESCAPE) )
541         {
542           trackerInfo.escPressed = TRUE;
543         }
544
545         /*
546          * Notify the drop source.
547          */
548         OLEDD_TrackStateChange(&trackerInfo);
549       }
550       else
551       {
552         /*
553          * Dispatch the messages only when it's not a keyboard message.
554          */
555         DispatchMessageA(&msg);
556       }
557     }
558
559     /* re-post the quit message to outer message loop */
560     if (msg.message == WM_QUIT)
561         PostQuitMessage(msg.wParam);
562     /*
563      * Destroy the temporary window.
564      */
565     DestroyWindow(hwndTrackWindow);
566
567     return trackerInfo.returnValue;
568   }
569
570   return E_FAIL;
571 }
572
573 /***********************************************************************
574  * OleQueryLinkFromData [OLE32.@]
575  */
576 HRESULT WINAPI OleQueryLinkFromData(
577   IDataObject* pSrcDataObject)
578 {
579   FIXME("(%p),stub!\n", pSrcDataObject);
580   return S_OK;
581 }
582
583 /***********************************************************************
584  * OleRegGetMiscStatus [OLE32.@]
585  */
586 HRESULT WINAPI OleRegGetMiscStatus(
587   REFCLSID clsid,
588   DWORD    dwAspect,
589   DWORD*   pdwStatus)
590 {
591   char    keyName[60];
592   HKEY    clsidKey;
593   HKEY    miscStatusKey;
594   HKEY    aspectKey;
595   LONG    result;
596
597   /*
598    * Initialize the out parameter.
599    */
600   *pdwStatus = 0;
601
602   /*
603    * Build the key name we're looking for
604    */
605   sprintf( keyName, "CLSID\\{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\",
606            clsid->Data1, clsid->Data2, clsid->Data3,
607            clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
608            clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );
609
610   TRACE("(%s, %ld, %p)\n", keyName, dwAspect, pdwStatus);
611
612   /*
613    * Open the class id Key
614    */
615   result = RegOpenKeyA(HKEY_CLASSES_ROOT,
616                        keyName,
617                        &clsidKey);
618
619   if (result != ERROR_SUCCESS)
620     return REGDB_E_CLASSNOTREG;
621
622   /*
623    * Get the MiscStatus
624    */
625   result = RegOpenKeyA(clsidKey,
626                        "MiscStatus",
627                        &miscStatusKey);
628
629
630   if (result != ERROR_SUCCESS)
631   {
632     RegCloseKey(clsidKey);
633     return REGDB_E_READREGDB;
634   }
635
636   /*
637    * Read the default value
638    */
639   OLEUTL_ReadRegistryDWORDValue(miscStatusKey, pdwStatus);
640
641   /*
642    * Open the key specific to the requested aspect.
643    */
644   sprintf(keyName, "%ld", dwAspect);
645
646   result = RegOpenKeyA(miscStatusKey,
647                        keyName,
648                        &aspectKey);
649
650   if (result == ERROR_SUCCESS)
651   {
652     OLEUTL_ReadRegistryDWORDValue(aspectKey, pdwStatus);
653     RegCloseKey(aspectKey);
654   }
655
656   /*
657    * Cleanup
658    */
659   RegCloseKey(miscStatusKey);
660   RegCloseKey(clsidKey);
661
662   return S_OK;
663 }
664
665 static HRESULT EnumOLEVERB_Construct(HKEY hkeyVerb, ULONG index, IEnumOLEVERB **ppenum);
666
667 typedef struct
668 {
669     const IEnumOLEVERBVtbl *lpvtbl;
670     LONG ref;
671
672     HKEY hkeyVerb;
673     ULONG index;
674 } EnumOLEVERB;
675
676 static HRESULT WINAPI EnumOLEVERB_QueryInterface(
677     IEnumOLEVERB *iface, REFIID riid, void **ppv)
678 {
679     TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
680     if (IsEqualIID(riid, &IID_IUnknown) ||
681         IsEqualIID(riid, &IID_IEnumOLEVERB))
682     {
683         IUnknown_AddRef(iface);
684         *ppv = iface;
685         return S_OK;
686     }
687     return E_NOINTERFACE;
688 }
689
690 static ULONG WINAPI EnumOLEVERB_AddRef(
691     IEnumOLEVERB *iface)
692 {
693     EnumOLEVERB *This = (EnumOLEVERB *)iface;
694     TRACE("()\n");
695     return InterlockedIncrement(&This->ref);
696 }
697
698 static ULONG WINAPI EnumOLEVERB_Release(
699     IEnumOLEVERB *iface)
700 {
701     EnumOLEVERB *This = (EnumOLEVERB *)iface;
702     LONG refs = InterlockedDecrement(&This->ref);
703     TRACE("()\n");
704     if (!refs)
705     {
706         RegCloseKey(This->hkeyVerb);
707         HeapFree(GetProcessHeap(), 0, This);
708     }
709     return refs;
710 }
711
712 static HRESULT WINAPI EnumOLEVERB_Next(
713     IEnumOLEVERB *iface, ULONG celt, LPOLEVERB rgelt,
714     ULONG *pceltFetched)
715 {
716     EnumOLEVERB *This = (EnumOLEVERB *)iface;
717     HRESULT hr = S_OK;
718
719     TRACE("(%ld, %p, %p)\n", celt, rgelt, pceltFetched);
720
721     if (pceltFetched)
722         *pceltFetched = 0;
723
724     for (; celt; celt--, rgelt++)
725     {
726         WCHAR wszSubKey[20];
727         LONG cbData;
728         LPWSTR pwszOLEVERB;
729         LPWSTR pwszMenuFlags;
730         LPWSTR pwszAttribs;
731         LONG res = RegEnumKeyW(This->hkeyVerb, This->index, wszSubKey, sizeof(wszSubKey)/sizeof(wszSubKey[0]));
732         if (res == ERROR_NO_MORE_ITEMS)
733         {
734             hr = S_FALSE;
735             break;
736         }
737         else if (res != ERROR_SUCCESS)
738         {
739             ERR("RegEnumKeyW failed with error %ld\n", res);
740             hr = REGDB_E_READREGDB;
741             break;
742         }
743         res = RegQueryValueW(This->hkeyVerb, wszSubKey, NULL, &cbData);
744         if (res != ERROR_SUCCESS)
745         {
746             ERR("RegQueryValueW failed with error %ld\n", res);
747             hr = REGDB_E_READREGDB;
748             break;
749         }
750         pwszOLEVERB = CoTaskMemAlloc(cbData);
751         if (!pwszOLEVERB)
752         {
753             hr = E_OUTOFMEMORY;
754             break;
755         }
756         res = RegQueryValueW(This->hkeyVerb, wszSubKey, pwszOLEVERB, &cbData);
757         if (res != ERROR_SUCCESS)
758         {
759             ERR("RegQueryValueW failed with error %ld\n", res);
760             hr = REGDB_E_READREGDB;
761             CoTaskMemFree(pwszOLEVERB);
762             break;
763         }
764
765         TRACE("verb string: %s\n", debugstr_w(pwszOLEVERB));
766         pwszMenuFlags = strchrW(pwszOLEVERB, ',');
767         if (!pwszMenuFlags)
768         {
769             hr = OLEOBJ_E_INVALIDVERB;
770             CoTaskMemFree(pwszOLEVERB);
771             break;
772         }
773         /* nul terminate the name string and advance to first character */
774         *pwszMenuFlags = '\0';
775         pwszMenuFlags++;
776         pwszAttribs = strchrW(pwszMenuFlags, ',');
777         if (!pwszAttribs)
778         {
779             hr = OLEOBJ_E_INVALIDVERB;
780             CoTaskMemFree(pwszOLEVERB);
781             break;
782         }
783         /* nul terminate the menu string and advance to first character */
784         *pwszAttribs = '\0';
785         pwszAttribs++;
786
787         /* fill out structure for this verb */
788         rgelt->lVerb = atolW(wszSubKey);
789         rgelt->lpszVerbName = pwszOLEVERB; /* user should free */
790         rgelt->fuFlags = atolW(pwszMenuFlags);
791         rgelt->grfAttribs = atolW(pwszAttribs);
792
793         if (pceltFetched)
794             (*pceltFetched)++;
795         This->index++;
796     }
797     return hr;
798 }
799
800 static HRESULT WINAPI EnumOLEVERB_Skip(
801     IEnumOLEVERB *iface, ULONG celt)
802 {
803     EnumOLEVERB *This = (EnumOLEVERB *)iface;
804
805     TRACE("(%ld)\n", celt);
806
807     This->index += celt;
808     return S_OK;
809 }
810
811 static HRESULT WINAPI EnumOLEVERB_Reset(
812     IEnumOLEVERB *iface)
813 {
814     EnumOLEVERB *This = (EnumOLEVERB *)iface;
815
816     TRACE("()\n");
817
818     This->index = 0;
819     return S_OK;
820 }
821
822 static HRESULT WINAPI EnumOLEVERB_Clone(
823     IEnumOLEVERB *iface,
824     IEnumOLEVERB **ppenum)
825 {
826     EnumOLEVERB *This = (EnumOLEVERB *)iface;
827     HKEY hkeyVerb;
828     TRACE("(%p)\n", ppenum);
829     if (!DuplicateHandle(GetCurrentProcess(), This->hkeyVerb, GetCurrentProcess(), (HANDLE *)&hkeyVerb, 0, FALSE, DUPLICATE_SAME_ACCESS))
830         return HRESULT_FROM_WIN32(GetLastError());
831     return EnumOLEVERB_Construct(hkeyVerb, This->index, ppenum);
832 }
833
834 static const IEnumOLEVERBVtbl EnumOLEVERB_VTable =
835 {
836     EnumOLEVERB_QueryInterface,
837     EnumOLEVERB_AddRef,
838     EnumOLEVERB_Release,
839     EnumOLEVERB_Next,
840     EnumOLEVERB_Skip,
841     EnumOLEVERB_Reset,
842     EnumOLEVERB_Clone
843 };
844
845 static HRESULT EnumOLEVERB_Construct(HKEY hkeyVerb, ULONG index, IEnumOLEVERB **ppenum)
846 {
847     EnumOLEVERB *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
848     if (!This)
849     {
850         RegCloseKey(hkeyVerb);
851         return E_OUTOFMEMORY;
852     }
853     This->lpvtbl = &EnumOLEVERB_VTable;
854     This->ref = 1;
855     This->index = index;
856     This->hkeyVerb = hkeyVerb;
857     *ppenum = (IEnumOLEVERB *)&This->lpvtbl;
858     return S_OK;    
859 }
860
861 /***********************************************************************
862  *           OleRegEnumVerbs    [OLE32.@]
863  *
864  * Enumerates verbs associated with a class stored in the registry.
865  *
866  * PARAMS
867  *  clsid  [I] Class ID to enumerate the verbs for.
868  *  ppenum [O] Enumerator.
869  *
870  * RETURNS
871  *  S_OK: Success.
872  *  REGDB_E_CLASSNOTREG: The specified class does not have a key in the registry.
873  *  REGDB_E_READREGDB: The class key could not be opened for some other reason.
874  *  OLE_E_REGDB_KEY: The Verb subkey for the class is not present.
875  *  OLEOBJ_E_NOVERBS: The Verb subkey for the class is empty.
876  */
877 HRESULT WINAPI OleRegEnumVerbs (REFCLSID clsid, LPENUMOLEVERB* ppenum)
878 {
879     LONG res;
880     HKEY hkeyVerb;
881     DWORD dwSubKeys;
882     static const WCHAR wszVerb[] = {'V','e','r','b',0};
883
884     TRACE("(%s, %p)\n", debugstr_guid(clsid), ppenum);
885
886     res = COM_OpenKeyForCLSID(clsid, wszVerb, KEY_READ, &hkeyVerb);
887     if (FAILED(res))
888     {
889         if (res == REGDB_E_CLASSNOTREG)
890             ERR("CLSID %s not registered\n", debugstr_guid(clsid));
891         else if (res == REGDB_E_KEYMISSING)
892             ERR("no Verbs key for class %s\n", debugstr_guid(clsid));
893         else
894             ERR("failed to open Verbs key for CLSID %s with error %ld\n",
895                 debugstr_guid(clsid), res);
896         return res;
897     }
898
899     res = RegQueryInfoKeyW(hkeyVerb, NULL, NULL, NULL, &dwSubKeys, NULL,
900                           NULL, NULL, NULL, NULL, NULL, NULL);
901     if (res != ERROR_SUCCESS)
902     {
903         ERR("failed to get subkey count with error %ld\n", GetLastError());
904         return REGDB_E_READREGDB;
905     }
906
907     if (!dwSubKeys)
908     {
909         WARN("class %s has no verbs\n", debugstr_guid(clsid));
910         RegCloseKey(hkeyVerb);
911         return OLEOBJ_E_NOVERBS;
912     }
913
914     return EnumOLEVERB_Construct(hkeyVerb, 0, ppenum);
915 }
916
917 /******************************************************************************
918  *              OleSetContainedObject        [OLE32.@]
919  */
920 HRESULT WINAPI OleSetContainedObject(
921   LPUNKNOWN pUnknown,
922   BOOL      fContained)
923 {
924   IRunnableObject* runnable = NULL;
925   HRESULT          hres;
926
927   TRACE("(%p,%x)\n", pUnknown, fContained);
928
929   hres = IUnknown_QueryInterface(pUnknown,
930                                  &IID_IRunnableObject,
931                                  (void**)&runnable);
932
933   if (SUCCEEDED(hres))
934   {
935     hres = IRunnableObject_SetContainedObject(runnable, fContained);
936
937     IRunnableObject_Release(runnable);
938
939     return hres;
940   }
941
942   return S_OK;
943 }
944
945 /******************************************************************************
946  *              OleLoad        [OLE32.@]
947  */
948 HRESULT WINAPI OleLoad(
949   LPSTORAGE       pStg,
950   REFIID          riid,
951   LPOLECLIENTSITE pClientSite,
952   LPVOID*         ppvObj)
953 {
954   IPersistStorage* persistStorage = NULL;
955   IOleObject*      oleObject      = NULL;
956   STATSTG          storageInfo;
957   HRESULT          hres;
958
959   TRACE("(%p,%p,%p,%p)\n", pStg, riid, pClientSite, ppvObj);
960
961   /*
962    * TODO, Conversion ... OleDoAutoConvert
963    */
964
965   /*
966    * Get the class ID for the object.
967    */
968   hres = IStorage_Stat(pStg, &storageInfo, STATFLAG_NONAME);
969
970   /*
971    * Now, try and create the handler for the object
972    */
973   hres = CoCreateInstance(&storageInfo.clsid,
974                           NULL,
975                           CLSCTX_INPROC_HANDLER,
976                           &IID_IOleObject,
977                           (void**)&oleObject);
978
979   /*
980    * If that fails, as it will most times, load the default
981    * OLE handler.
982    */
983   if (FAILED(hres))
984   {
985     hres = OleCreateDefaultHandler(&storageInfo.clsid,
986                                    NULL,
987                                    &IID_IOleObject,
988                                    (void**)&oleObject);
989   }
990
991   /*
992    * If we couldn't find a handler... this is bad. Abort the whole thing.
993    */
994   if (FAILED(hres))
995     return hres;
996
997   /*
998    * Inform the new object of it's client site.
999    */
1000   hres = IOleObject_SetClientSite(oleObject, pClientSite);
1001
1002   /*
1003    * Initialize the object with it's IPersistStorage interface.
1004    */
1005   hres = IOleObject_QueryInterface(oleObject,
1006                                    &IID_IPersistStorage,
1007                                    (void**)&persistStorage);
1008
1009   if (SUCCEEDED(hres))
1010   {
1011     IPersistStorage_Load(persistStorage, pStg);
1012
1013     IPersistStorage_Release(persistStorage);
1014     persistStorage = NULL;
1015   }
1016
1017   /*
1018    * Return the requested interface to the caller.
1019    */
1020   hres = IOleObject_QueryInterface(oleObject, riid, ppvObj);
1021
1022   /*
1023    * Cleanup interfaces used internally
1024    */
1025   IOleObject_Release(oleObject);
1026
1027   return hres;
1028 }
1029
1030 /***********************************************************************
1031  *           OleSave     [OLE32.@]
1032  */
1033 HRESULT WINAPI OleSave(
1034   LPPERSISTSTORAGE pPS,
1035   LPSTORAGE        pStg,
1036   BOOL             fSameAsLoad)
1037 {
1038   HRESULT hres;
1039   CLSID   objectClass;
1040
1041   TRACE("(%p,%p,%x)\n", pPS, pStg, fSameAsLoad);
1042
1043   /*
1044    * First, we transfer the class ID (if available)
1045    */
1046   hres = IPersistStorage_GetClassID(pPS, &objectClass);
1047
1048   if (SUCCEEDED(hres))
1049   {
1050     WriteClassStg(pStg, &objectClass);
1051   }
1052
1053   /*
1054    * Then, we ask the object to save itself to the
1055    * storage. If it is successful, we commit the storage.
1056    */
1057   hres = IPersistStorage_Save(pPS, pStg, fSameAsLoad);
1058
1059   if (SUCCEEDED(hres))
1060   {
1061     IStorage_Commit(pStg,
1062                     STGC_DEFAULT);
1063   }
1064
1065   return hres;
1066 }
1067
1068
1069 /******************************************************************************
1070  *              OleLockRunning        [OLE32.@]
1071  */
1072 HRESULT WINAPI OleLockRunning(LPUNKNOWN pUnknown, BOOL fLock, BOOL fLastUnlockCloses)
1073 {
1074   IRunnableObject* runnable = NULL;
1075   HRESULT          hres;
1076
1077   TRACE("(%p,%x,%x)\n", pUnknown, fLock, fLastUnlockCloses);
1078
1079   hres = IUnknown_QueryInterface(pUnknown,
1080                                  &IID_IRunnableObject,
1081                                  (void**)&runnable);
1082
1083   if (SUCCEEDED(hres))
1084   {
1085     hres = IRunnableObject_LockRunning(runnable, fLock, fLastUnlockCloses);
1086
1087     IRunnableObject_Release(runnable);
1088
1089     return hres;
1090   }
1091   else
1092     return E_INVALIDARG;
1093 }
1094
1095
1096 /**************************************************************************
1097  * Internal methods to manage the shared OLE menu in response to the
1098  * OLE***MenuDescriptor API
1099  */
1100
1101 /***
1102  * OLEMenu_Initialize()
1103  *
1104  * Initializes the OLEMENU data structures.
1105  */
1106 static void OLEMenu_Initialize(void)
1107 {
1108 }
1109
1110 /***
1111  * OLEMenu_UnInitialize()
1112  *
1113  * Releases the OLEMENU data structures.
1114  */
1115 static void OLEMenu_UnInitialize(void)
1116 {
1117 }
1118
1119 /*************************************************************************
1120  * OLEMenu_InstallHooks
1121  * Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
1122  *
1123  * RETURNS: TRUE if message hooks were successfully installed
1124  *          FALSE on failure
1125  */
1126 static BOOL OLEMenu_InstallHooks( DWORD tid )
1127 {
1128   OleMenuHookItem *pHookItem = NULL;
1129
1130   /* Create an entry for the hook table */
1131   if ( !(pHookItem = HeapAlloc(GetProcessHeap(), 0,
1132                                sizeof(OleMenuHookItem)) ) )
1133     return FALSE;
1134
1135   pHookItem->tid = tid;
1136   pHookItem->hHeap = GetProcessHeap();
1137
1138   /* Install a thread scope message hook for WH_GETMESSAGE */
1139   pHookItem->GetMsg_hHook = SetWindowsHookExA( WH_GETMESSAGE, OLEMenu_GetMsgProc,
1140                                                0, GetCurrentThreadId() );
1141   if ( !pHookItem->GetMsg_hHook )
1142     goto CLEANUP;
1143
1144   /* Install a thread scope message hook for WH_CALLWNDPROC */
1145   pHookItem->CallWndProc_hHook = SetWindowsHookExA( WH_CALLWNDPROC, OLEMenu_CallWndProc,
1146                                                     0, GetCurrentThreadId() );
1147   if ( !pHookItem->CallWndProc_hHook )
1148     goto CLEANUP;
1149
1150   /* Insert the hook table entry */
1151   pHookItem->next = hook_list;
1152   hook_list = pHookItem;
1153
1154   return TRUE;
1155
1156 CLEANUP:
1157   /* Unhook any hooks */
1158   if ( pHookItem->GetMsg_hHook )
1159     UnhookWindowsHookEx( pHookItem->GetMsg_hHook );
1160   if ( pHookItem->CallWndProc_hHook )
1161     UnhookWindowsHookEx( pHookItem->CallWndProc_hHook );
1162   /* Release the hook table entry */
1163   HeapFree(pHookItem->hHeap, 0, pHookItem );
1164
1165   return FALSE;
1166 }
1167
1168 /*************************************************************************
1169  * OLEMenu_UnInstallHooks
1170  * UnInstall thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
1171  *
1172  * RETURNS: TRUE if message hooks were successfully installed
1173  *          FALSE on failure
1174  */
1175 static BOOL OLEMenu_UnInstallHooks( DWORD tid )
1176 {
1177   OleMenuHookItem *pHookItem = NULL;
1178   OleMenuHookItem **ppHook = &hook_list;
1179
1180   while (*ppHook)
1181   {
1182       if ((*ppHook)->tid == tid)
1183       {
1184           pHookItem = *ppHook;
1185           *ppHook = pHookItem->next;
1186           break;
1187       }
1188       ppHook = &(*ppHook)->next;
1189   }
1190   if (!pHookItem) return FALSE;
1191
1192   /* Uninstall the hooks installed for this thread */
1193   if ( !UnhookWindowsHookEx( pHookItem->GetMsg_hHook ) )
1194     goto CLEANUP;
1195   if ( !UnhookWindowsHookEx( pHookItem->CallWndProc_hHook ) )
1196     goto CLEANUP;
1197
1198   /* Release the hook table entry */
1199   HeapFree(pHookItem->hHeap, 0, pHookItem );
1200
1201   return TRUE;
1202
1203 CLEANUP:
1204   /* Release the hook table entry */
1205   HeapFree(pHookItem->hHeap, 0, pHookItem );
1206
1207   return FALSE;
1208 }
1209
1210 /*************************************************************************
1211  * OLEMenu_IsHookInstalled
1212  * Tests if OLEMenu hooks have been installed for a thread
1213  *
1214  * RETURNS: The pointer and index of the hook table entry for the tid
1215  *          NULL and -1 for the index if no hooks were installed for this thread
1216  */
1217 static OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid )
1218 {
1219   OleMenuHookItem *pHookItem = NULL;
1220
1221   /* Do a simple linear search for an entry whose tid matches ours.
1222    * We really need a map but efficiency is not a concern here. */
1223   for (pHookItem = hook_list; pHookItem; pHookItem = pHookItem->next)
1224   {
1225     if ( tid == pHookItem->tid )
1226       return pHookItem;
1227   }
1228
1229   return NULL;
1230 }
1231
1232 /***********************************************************************
1233  *           OLEMenu_FindMainMenuIndex
1234  *
1235  * Used by OLEMenu API to find the top level group a menu item belongs to.
1236  * On success pnPos contains the index of the item in the top level menu group
1237  *
1238  * RETURNS: TRUE if the ID was found, FALSE on failure
1239  */
1240 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos )
1241 {
1242   UINT i, nItems;
1243
1244   nItems = GetMenuItemCount( hMainMenu );
1245
1246   for (i = 0; i < nItems; i++)
1247   {
1248     HMENU hsubmenu;
1249
1250     /*  Is the current item a submenu? */
1251     if ( (hsubmenu = GetSubMenu(hMainMenu, i)) )
1252     {
1253       /* If the handle is the same we're done */
1254       if ( hsubmenu == hPopupMenu )
1255       {
1256         if (pnPos)
1257           *pnPos = i;
1258         return TRUE;
1259       }
1260       /* Recursively search without updating pnPos */
1261       else if ( OLEMenu_FindMainMenuIndex( hsubmenu, hPopupMenu, NULL ) )
1262       {
1263         if (pnPos)
1264           *pnPos = i;
1265         return TRUE;
1266       }
1267     }
1268   }
1269
1270   return FALSE;
1271 }
1272
1273 /***********************************************************************
1274  *           OLEMenu_SetIsServerMenu
1275  *
1276  * Checks whether a popup menu belongs to a shared menu group which is
1277  * owned by the server, and sets the menu descriptor state accordingly.
1278  * All menu messages from these groups should be routed to the server.
1279  *
1280  * RETURNS: TRUE if the popup menu is part of a server owned group
1281  *          FALSE if the popup menu is part of a container owned group
1282  */
1283 static BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor )
1284 {
1285   UINT nPos = 0, nWidth, i;
1286
1287   pOleMenuDescriptor->bIsServerItem = FALSE;
1288
1289   /* Don't bother searching if the popup is the combined menu itself */
1290   if ( hmenu == pOleMenuDescriptor->hmenuCombined )
1291     return FALSE;
1292
1293   /* Find the menu item index in the shared OLE menu that this item belongs to */
1294   if ( !OLEMenu_FindMainMenuIndex( pOleMenuDescriptor->hmenuCombined, hmenu,  &nPos ) )
1295     return FALSE;
1296
1297   /* The group widths array has counts for the number of elements
1298    * in the groups File, Edit, Container, Object, Window, Help.
1299    * The Edit, Object & Help groups belong to the server object
1300    * and the other three belong to the container.
1301    * Loop through the group widths and locate the group we are a member of.
1302    */
1303   for ( i = 0, nWidth = 0; i < 6; i++ )
1304   {
1305     nWidth += pOleMenuDescriptor->mgw.width[i];
1306     if ( nPos < nWidth )
1307     {
1308       /* Odd elements are server menu widths */
1309       pOleMenuDescriptor->bIsServerItem = (i%2) ? TRUE : FALSE;
1310       break;
1311     }
1312   }
1313
1314   return pOleMenuDescriptor->bIsServerItem;
1315 }
1316
1317 /*************************************************************************
1318  * OLEMenu_CallWndProc
1319  * Thread scope WH_CALLWNDPROC hook proc filter function (callback)
1320  * This is invoked from a message hook installed in OleSetMenuDescriptor.
1321  */
1322 static LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam)
1323 {
1324   LPCWPSTRUCT pMsg = NULL;
1325   HOLEMENU hOleMenu = 0;
1326   OleMenuDescriptor *pOleMenuDescriptor = NULL;
1327   OleMenuHookItem *pHookItem = NULL;
1328   WORD fuFlags;
1329
1330   TRACE("%i, %04x, %08x\n", code, wParam, (unsigned)lParam );
1331
1332   /* Check if we're being asked to process the message */
1333   if ( HC_ACTION != code )
1334     goto NEXTHOOK;
1335
1336   /* Retrieve the current message being dispatched from lParam */
1337   pMsg = (LPCWPSTRUCT)lParam;
1338
1339   /* Check if the message is destined for a window we are interested in:
1340    * If the window has an OLEMenu property we may need to dispatch
1341    * the menu message to its active objects window instead. */
1342
1343   hOleMenu = (HOLEMENU)GetPropA( pMsg->hwnd, "PROP_OLEMenuDescriptor" );
1344   if ( !hOleMenu )
1345     goto NEXTHOOK;
1346
1347   /* Get the menu descriptor */
1348   pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1349   if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1350     goto NEXTHOOK;
1351
1352   /* Process menu messages */
1353   switch( pMsg->message )
1354   {
1355     case WM_INITMENU:
1356     {
1357       /* Reset the menu descriptor state */
1358       pOleMenuDescriptor->bIsServerItem = FALSE;
1359
1360       /* Send this message to the server as well */
1361       SendMessageA( pOleMenuDescriptor->hwndActiveObject,
1362                   pMsg->message, pMsg->wParam, pMsg->lParam );
1363       goto NEXTHOOK;
1364     }
1365
1366     case WM_INITMENUPOPUP:
1367     {
1368       /* Save the state for whether this is a server owned menu */
1369       OLEMenu_SetIsServerMenu( (HMENU)pMsg->wParam, pOleMenuDescriptor );
1370       break;
1371     }
1372
1373     case WM_MENUSELECT:
1374     {
1375       fuFlags = HIWORD(pMsg->wParam);  /* Get flags */
1376       if ( fuFlags & MF_SYSMENU )
1377          goto NEXTHOOK;
1378
1379       /* Save the state for whether this is a server owned popup menu */
1380       else if ( fuFlags & MF_POPUP )
1381         OLEMenu_SetIsServerMenu( (HMENU)pMsg->lParam, pOleMenuDescriptor );
1382
1383       break;
1384     }
1385
1386     case WM_DRAWITEM:
1387     {
1388       LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT) pMsg->lParam;
1389       if ( pMsg->wParam != 0 || lpdis->CtlType != ODT_MENU )
1390         goto NEXTHOOK;  /* Not a menu message */
1391
1392       break;
1393     }
1394
1395     default:
1396       goto NEXTHOOK;
1397   }
1398
1399   /* If the message was for the server dispatch it accordingly */
1400   if ( pOleMenuDescriptor->bIsServerItem )
1401   {
1402     SendMessageA( pOleMenuDescriptor->hwndActiveObject,
1403                   pMsg->message, pMsg->wParam, pMsg->lParam );
1404   }
1405
1406 NEXTHOOK:
1407   if ( pOleMenuDescriptor )
1408     GlobalUnlock( hOleMenu );
1409
1410   /* Lookup the hook item for the current thread */
1411   if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1412   {
1413     /* This should never fail!! */
1414     WARN("could not retrieve hHook for current thread!\n" );
1415     return 0;
1416   }
1417
1418   /* Pass on the message to the next hooker */
1419   return CallNextHookEx( pHookItem->CallWndProc_hHook, code, wParam, lParam );
1420 }
1421
1422 /*************************************************************************
1423  * OLEMenu_GetMsgProc
1424  * Thread scope WH_GETMESSAGE hook proc filter function (callback)
1425  * This is invoked from a message hook installed in OleSetMenuDescriptor.
1426  */
1427 static LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam)
1428 {
1429   LPMSG pMsg = NULL;
1430   HOLEMENU hOleMenu = 0;
1431   OleMenuDescriptor *pOleMenuDescriptor = NULL;
1432   OleMenuHookItem *pHookItem = NULL;
1433   WORD wCode;
1434
1435   TRACE("%i, %04x, %08x\n", code, wParam, (unsigned)lParam );
1436
1437   /* Check if we're being asked to process a  messages */
1438   if ( HC_ACTION != code )
1439     goto NEXTHOOK;
1440
1441   /* Retrieve the current message being dispatched from lParam */
1442   pMsg = (LPMSG)lParam;
1443
1444   /* Check if the message is destined for a window we are interested in:
1445    * If the window has an OLEMenu property we may need to dispatch
1446    * the menu message to its active objects window instead. */
1447
1448   hOleMenu = (HOLEMENU)GetPropA( pMsg->hwnd, "PROP_OLEMenuDescriptor" );
1449   if ( !hOleMenu )
1450     goto NEXTHOOK;
1451
1452   /* Process menu messages */
1453   switch( pMsg->message )
1454   {
1455     case WM_COMMAND:
1456     {
1457       wCode = HIWORD(pMsg->wParam);  /* Get notification code */
1458       if ( wCode )
1459         goto NEXTHOOK;  /* Not a menu message */
1460       break;
1461     }
1462     default:
1463       goto NEXTHOOK;
1464   }
1465
1466   /* Get the menu descriptor */
1467   pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1468   if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1469     goto NEXTHOOK;
1470
1471   /* If the message was for the server dispatch it accordingly */
1472   if ( pOleMenuDescriptor->bIsServerItem )
1473   {
1474     /* Change the hWnd in the message to the active objects hWnd.
1475      * The message loop which reads this message will automatically
1476      * dispatch it to the embedded objects window. */
1477     pMsg->hwnd = pOleMenuDescriptor->hwndActiveObject;
1478   }
1479
1480 NEXTHOOK:
1481   if ( pOleMenuDescriptor )
1482     GlobalUnlock( hOleMenu );
1483
1484   /* Lookup the hook item for the current thread */
1485   if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1486   {
1487     /* This should never fail!! */
1488     WARN("could not retrieve hHook for current thread!\n" );
1489     return FALSE;
1490   }
1491
1492   /* Pass on the message to the next hooker */
1493   return CallNextHookEx( pHookItem->GetMsg_hHook, code, wParam, lParam );
1494 }
1495
1496 /***********************************************************************
1497  * OleCreateMenuDescriptor [OLE32.@]
1498  * Creates an OLE menu descriptor for OLE to use when dispatching
1499  * menu messages and commands.
1500  *
1501  * PARAMS:
1502  *    hmenuCombined  -  Handle to the objects combined menu
1503  *    lpMenuWidths   -  Pointer to array of 6 LONG's indicating menus per group
1504  *
1505  */
1506 HOLEMENU WINAPI OleCreateMenuDescriptor(
1507   HMENU                hmenuCombined,
1508   LPOLEMENUGROUPWIDTHS lpMenuWidths)
1509 {
1510   HOLEMENU hOleMenu;
1511   OleMenuDescriptor *pOleMenuDescriptor;
1512   int i;
1513
1514   if ( !hmenuCombined || !lpMenuWidths )
1515     return 0;
1516
1517   /* Create an OLE menu descriptor */
1518   if ( !(hOleMenu = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT,
1519                                 sizeof(OleMenuDescriptor) ) ) )
1520   return 0;
1521
1522   pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1523   if ( !pOleMenuDescriptor )
1524     return 0;
1525
1526   /* Initialize menu group widths and hmenu */
1527   for ( i = 0; i < 6; i++ )
1528     pOleMenuDescriptor->mgw.width[i] = lpMenuWidths->width[i];
1529
1530   pOleMenuDescriptor->hmenuCombined = hmenuCombined;
1531   pOleMenuDescriptor->bIsServerItem = FALSE;
1532   GlobalUnlock( hOleMenu );
1533
1534   return hOleMenu;
1535 }
1536
1537 /***********************************************************************
1538  * OleDestroyMenuDescriptor [OLE32.@]
1539  * Destroy the shared menu descriptor
1540  */
1541 HRESULT WINAPI OleDestroyMenuDescriptor(
1542   HOLEMENU hmenuDescriptor)
1543 {
1544   if ( hmenuDescriptor )
1545     GlobalFree( hmenuDescriptor );
1546         return S_OK;
1547 }
1548
1549 /***********************************************************************
1550  * OleSetMenuDescriptor [OLE32.@]
1551  * Installs or removes OLE dispatching code for the containers frame window.
1552  *
1553  * PARAMS
1554  *     hOleMenu         Handle to composite menu descriptor
1555  *     hwndFrame        Handle to containers frame window
1556  *     hwndActiveObject Handle to objects in-place activation window
1557  *     lpFrame          Pointer to IOleInPlaceFrame on containers window
1558  *     lpActiveObject   Pointer to IOleInPlaceActiveObject on active in-place object
1559  *
1560  * RETURNS
1561  *      S_OK                               - menu installed correctly
1562  *      E_FAIL, E_INVALIDARG, E_UNEXPECTED - failure
1563  *
1564  * FIXME
1565  *      The lpFrame and lpActiveObject parameters are currently ignored
1566  *      OLE should install context sensitive help F1 filtering for the app when
1567  *      these are non null.
1568  */
1569 HRESULT WINAPI OleSetMenuDescriptor(
1570   HOLEMENU               hOleMenu,
1571   HWND                   hwndFrame,
1572   HWND                   hwndActiveObject,
1573   LPOLEINPLACEFRAME        lpFrame,
1574   LPOLEINPLACEACTIVEOBJECT lpActiveObject)
1575 {
1576   OleMenuDescriptor *pOleMenuDescriptor = NULL;
1577
1578   /* Check args */
1579   if ( !hwndFrame || (hOleMenu && !hwndActiveObject) )
1580     return E_INVALIDARG;
1581
1582   if ( lpFrame || lpActiveObject )
1583   {
1584      FIXME("(%p, %p, %p, %p, %p), Context sensitive help filtering not implemented!\n",
1585         hOleMenu,
1586         hwndFrame,
1587         hwndActiveObject,
1588         lpFrame,
1589         lpActiveObject);
1590   }
1591
1592   /* Set up a message hook to intercept the containers frame window messages.
1593    * The message filter is responsible for dispatching menu messages from the
1594    * shared menu which are intended for the object.
1595    */
1596
1597   if ( hOleMenu )  /* Want to install dispatching code */
1598   {
1599     /* If OLEMenu hooks are already installed for this thread, fail
1600      * Note: This effectively means that OleSetMenuDescriptor cannot
1601      * be called twice in succession on the same frame window
1602      * without first calling it with a null hOleMenu to uninstall */
1603     if ( OLEMenu_IsHookInstalled( GetCurrentThreadId() ) )
1604   return E_FAIL;
1605
1606     /* Get the menu descriptor */
1607     pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1608     if ( !pOleMenuDescriptor )
1609       return E_UNEXPECTED;
1610
1611     /* Update the menu descriptor */
1612     pOleMenuDescriptor->hwndFrame = hwndFrame;
1613     pOleMenuDescriptor->hwndActiveObject = hwndActiveObject;
1614
1615     GlobalUnlock( hOleMenu );
1616     pOleMenuDescriptor = NULL;
1617
1618     /* Add a menu descriptor windows property to the frame window */
1619     SetPropA( hwndFrame, "PROP_OLEMenuDescriptor", hOleMenu );
1620
1621     /* Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC */
1622     if ( !OLEMenu_InstallHooks( GetCurrentThreadId() ) )
1623       return E_FAIL;
1624   }
1625   else  /* Want to uninstall dispatching code */
1626   {
1627     /* Uninstall the hooks */
1628     if ( !OLEMenu_UnInstallHooks( GetCurrentThreadId() ) )
1629       return E_FAIL;
1630
1631     /* Remove the menu descriptor property from the frame window */
1632     RemovePropA( hwndFrame, "PROP_OLEMenuDescriptor" );
1633   }
1634
1635   return S_OK;
1636 }
1637
1638 /******************************************************************************
1639  *              IsAccelerator        [OLE32.@]
1640  * Mostly copied from controls/menu.c TranslateAccelerator implementation
1641  */
1642 BOOL WINAPI IsAccelerator(HACCEL hAccel, int cAccelEntries, LPMSG lpMsg, WORD* lpwCmd)
1643 {
1644     LPACCEL lpAccelTbl;
1645     int i;
1646
1647     if(!lpMsg) return FALSE;
1648     if (!hAccel)
1649     {
1650         WARN_(accel)("NULL accel handle\n");
1651         return FALSE;
1652     }
1653     if((lpMsg->message != WM_KEYDOWN &&
1654         lpMsg->message != WM_KEYUP &&
1655         lpMsg->message != WM_SYSKEYDOWN &&
1656         lpMsg->message != WM_SYSKEYUP &&
1657         lpMsg->message != WM_CHAR)) return FALSE;
1658     lpAccelTbl = HeapAlloc(GetProcessHeap(), 0, cAccelEntries * sizeof(ACCEL));
1659     if (NULL == lpAccelTbl)
1660     {
1661         return FALSE;
1662     }
1663     if (CopyAcceleratorTableW(hAccel, lpAccelTbl, cAccelEntries) != cAccelEntries)
1664     {
1665         WARN_(accel)("CopyAcceleratorTableW failed\n");
1666         HeapFree(GetProcessHeap(), 0, lpAccelTbl);
1667         return FALSE;
1668     }
1669
1670     TRACE_(accel)("hAccel=%p, cAccelEntries=%d,"
1671                 "msg->hwnd=%p, msg->message=%04x, wParam=%08x, lParam=%08lx\n",
1672                 hAccel, cAccelEntries,
1673                 lpMsg->hwnd, lpMsg->message, lpMsg->wParam, lpMsg->lParam);
1674     for(i = 0; i < cAccelEntries; i++)
1675     {
1676         if(lpAccelTbl[i].key != lpMsg->wParam)
1677             continue;
1678
1679         if(lpMsg->message == WM_CHAR)
1680         {
1681             if(!(lpAccelTbl[i].fVirt & FALT) && !(lpAccelTbl[i].fVirt & FVIRTKEY))
1682             {
1683                 TRACE_(accel)("found accel for WM_CHAR: ('%c')\n", lpMsg->wParam & 0xff);
1684                 goto found;
1685             }
1686         }
1687         else
1688         {
1689             if(lpAccelTbl[i].fVirt & FVIRTKEY)
1690             {
1691                 INT mask = 0;
1692                 TRACE_(accel)("found accel for virt_key %04x (scan %04x)\n",
1693                                 lpMsg->wParam, HIWORD(lpMsg->lParam) & 0xff);
1694                 if(GetKeyState(VK_SHIFT) & 0x8000) mask |= FSHIFT;
1695                 if(GetKeyState(VK_CONTROL) & 0x8000) mask |= FCONTROL;
1696                 if(GetKeyState(VK_MENU) & 0x8000) mask |= FALT;
1697                 if(mask == (lpAccelTbl[i].fVirt & (FSHIFT | FCONTROL | FALT))) goto found;
1698                 TRACE_(accel)("incorrect SHIFT/CTRL/ALT-state\n");
1699             }
1700             else
1701             {
1702                 if(!(lpMsg->lParam & 0x01000000))  /* no special_key */
1703                 {
1704                     if((lpAccelTbl[i].fVirt & FALT) && (lpMsg->lParam & 0x20000000))
1705                     {                                                  /* ^^ ALT pressed */
1706                         TRACE_(accel)("found accel for Alt-%c\n", lpMsg->wParam & 0xff);
1707                         goto found;
1708                     }
1709                 }
1710             }
1711         }
1712     }
1713
1714     WARN_(accel)("couldn't translate accelerator key\n");
1715     HeapFree(GetProcessHeap(), 0, lpAccelTbl);
1716     return FALSE;
1717
1718 found:
1719     if(lpwCmd) *lpwCmd = lpAccelTbl[i].cmd;
1720     HeapFree(GetProcessHeap(), 0, lpAccelTbl);
1721     return TRUE;
1722 }
1723
1724 /***********************************************************************
1725  * ReleaseStgMedium [OLE32.@]
1726  */
1727 void WINAPI ReleaseStgMedium(
1728   STGMEDIUM* pmedium)
1729 {
1730   switch (pmedium->tymed)
1731   {
1732     case TYMED_HGLOBAL:
1733     {
1734       if ( (pmedium->pUnkForRelease==0) &&
1735            (pmedium->u.hGlobal!=0) )
1736         GlobalFree(pmedium->u.hGlobal);
1737       break;
1738     }
1739     case TYMED_FILE:
1740     {
1741       if (pmedium->u.lpszFileName!=0)
1742       {
1743         if (pmedium->pUnkForRelease==0)
1744         {
1745           DeleteFileW(pmedium->u.lpszFileName);
1746         }
1747
1748         CoTaskMemFree(pmedium->u.lpszFileName);
1749       }
1750       break;
1751     }
1752     case TYMED_ISTREAM:
1753     {
1754       if (pmedium->u.pstm!=0)
1755       {
1756         IStream_Release(pmedium->u.pstm);
1757       }
1758       break;
1759     }
1760     case TYMED_ISTORAGE:
1761     {
1762       if (pmedium->u.pstg!=0)
1763       {
1764         IStorage_Release(pmedium->u.pstg);
1765       }
1766       break;
1767     }
1768     case TYMED_GDI:
1769     {
1770       if ( (pmedium->pUnkForRelease==0) &&
1771            (pmedium->u.hBitmap!=0) )
1772         DeleteObject(pmedium->u.hBitmap);
1773       break;
1774     }
1775     case TYMED_MFPICT:
1776     {
1777       if ( (pmedium->pUnkForRelease==0) &&
1778            (pmedium->u.hMetaFilePict!=0) )
1779       {
1780         LPMETAFILEPICT pMP = GlobalLock(pmedium->u.hMetaFilePict);
1781         DeleteMetaFile(pMP->hMF);
1782         GlobalUnlock(pmedium->u.hMetaFilePict);
1783         GlobalFree(pmedium->u.hMetaFilePict);
1784       }
1785       break;
1786     }
1787     case TYMED_ENHMF:
1788     {
1789       if ( (pmedium->pUnkForRelease==0) &&
1790            (pmedium->u.hEnhMetaFile!=0) )
1791       {
1792         DeleteEnhMetaFile(pmedium->u.hEnhMetaFile);
1793       }
1794       break;
1795     }
1796     case TYMED_NULL:
1797     default:
1798       break;
1799   }
1800   pmedium->tymed=TYMED_NULL;
1801
1802   /*
1803    * After cleaning up, the unknown is released
1804    */
1805   if (pmedium->pUnkForRelease!=0)
1806   {
1807     IUnknown_Release(pmedium->pUnkForRelease);
1808     pmedium->pUnkForRelease = 0;
1809   }
1810 }
1811
1812 /***
1813  * OLEDD_Initialize()
1814  *
1815  * Initializes the OLE drag and drop data structures.
1816  */
1817 static void OLEDD_Initialize()
1818 {
1819     WNDCLASSA wndClass;
1820
1821     ZeroMemory (&wndClass, sizeof(WNDCLASSA));
1822     wndClass.style         = CS_GLOBALCLASS;
1823     wndClass.lpfnWndProc   = OLEDD_DragTrackerWindowProc;
1824     wndClass.cbClsExtra    = 0;
1825     wndClass.cbWndExtra    = sizeof(TrackerWindowInfo*);
1826     wndClass.hCursor       = 0;
1827     wndClass.hbrBackground = 0;
1828     wndClass.lpszClassName = OLEDD_DRAGTRACKERCLASS;
1829
1830     RegisterClassA (&wndClass);
1831 }
1832
1833 /***
1834  * OLEDD_FreeDropTarget()
1835  *
1836  * Frees the drag and drop data structure
1837  */
1838 static void OLEDD_FreeDropTarget(DropTargetNode *dropTargetInfo)
1839 {
1840   list_remove(&dropTargetInfo->entry);
1841   IDropTarget_Release(dropTargetInfo->dropTarget);
1842   HeapFree(GetProcessHeap(), 0, dropTargetInfo);
1843 }
1844
1845 /***
1846  * OLEDD_UnInitialize()
1847  *
1848  * Releases the OLE drag and drop data structures.
1849  */
1850 static void OLEDD_UnInitialize(void)
1851 {
1852   /*
1853    * Simply empty the list.
1854    */
1855   while (!list_empty(&targetListHead))
1856   {
1857     DropTargetNode* curNode;
1858     curNode = LIST_ENTRY(list_head(&targetListHead), DropTargetNode, entry);
1859     OLEDD_FreeDropTarget(curNode);
1860   }
1861 }
1862
1863 /***
1864  * OLEDD_FindDropTarget()
1865  *
1866  * Finds information about the drop target.
1867  */
1868 static DropTargetNode* OLEDD_FindDropTarget(HWND hwndOfTarget)
1869 {
1870   DropTargetNode*  curNode;
1871
1872   /*
1873    * Iterate the list to find the HWND value.
1874    */
1875   LIST_FOR_EACH_ENTRY(curNode, &targetListHead, DropTargetNode, entry)
1876     if (hwndOfTarget==curNode->hwndTarget)
1877       return curNode;
1878
1879   /*
1880    * If we get here, the item is not in the list
1881    */
1882   return NULL;
1883 }
1884
1885 /***
1886  * OLEDD_DragTrackerWindowProc()
1887  *
1888  * This method is the WindowProcedure of the drag n drop tracking
1889  * window. During a drag n Drop operation, an invisible window is created
1890  * to receive the user input and act upon it. This procedure is in charge
1891  * of this behavior.
1892  */
1893
1894 #define DRAG_TIMER_ID 1
1895
1896 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(
1897                          HWND   hwnd,
1898                          UINT   uMsg,
1899                          WPARAM wParam,
1900                          LPARAM   lParam)
1901 {
1902   switch (uMsg)
1903   {
1904     case WM_CREATE:
1905     {
1906       LPCREATESTRUCTA createStruct = (LPCREATESTRUCTA)lParam;
1907
1908       SetWindowLongA(hwnd, 0, (LONG)createStruct->lpCreateParams);
1909       SetTimer(hwnd, DRAG_TIMER_ID, 50, NULL);
1910
1911       break;
1912     }
1913     case WM_TIMER:
1914     case WM_MOUSEMOVE:
1915     {
1916       OLEDD_TrackMouseMove((TrackerWindowInfo*)GetWindowLongA(hwnd, 0));
1917       break;
1918     }
1919     case WM_LBUTTONUP:
1920     case WM_MBUTTONUP:
1921     case WM_RBUTTONUP:
1922     case WM_LBUTTONDOWN:
1923     case WM_MBUTTONDOWN:
1924     case WM_RBUTTONDOWN:
1925     {
1926       OLEDD_TrackStateChange((TrackerWindowInfo*)GetWindowLongA(hwnd, 0));
1927       break;
1928     }
1929     case WM_DESTROY:
1930     {
1931       KillTimer(hwnd, DRAG_TIMER_ID);
1932       break;
1933     }
1934   }
1935
1936   /*
1937    * This is a window proc after all. Let's call the default.
1938    */
1939   return DefWindowProcA (hwnd, uMsg, wParam, lParam);
1940 }
1941
1942 /***
1943  * OLEDD_TrackMouseMove()
1944  *
1945  * This method is invoked while a drag and drop operation is in effect.
1946  * it will generate the appropriate callbacks in the drop source
1947  * and drop target. It will also provide the expected feedback to
1948  * the user.
1949  *
1950  * params:
1951  *    trackerInfo - Pointer to the structure identifying the
1952  *                  drag & drop operation that is currently
1953  *                  active.
1954  */
1955 static void OLEDD_TrackMouseMove(TrackerWindowInfo* trackerInfo)
1956 {
1957   HWND   hwndNewTarget = 0;
1958   HRESULT  hr = S_OK;
1959   POINT pt;
1960
1961   /*
1962    * Get the handle of the window under the mouse
1963    */
1964   pt.x = trackerInfo->curMousePos.x;
1965   pt.y = trackerInfo->curMousePos.y;
1966   hwndNewTarget = WindowFromPoint(pt);
1967
1968   /*
1969    * Every time, we re-initialize the effects passed to the
1970    * IDropTarget to the effects allowed by the source.
1971    */
1972   *trackerInfo->pdwEffect = trackerInfo->dwOKEffect;
1973
1974   /*
1975    * If we are hovering over the same target as before, send the
1976    * DragOver notification
1977    */
1978   if ( (trackerInfo->curDragTarget != 0) &&
1979        (trackerInfo->curTargetHWND == hwndNewTarget) )
1980   {
1981     IDropTarget_DragOver(trackerInfo->curDragTarget,
1982                          trackerInfo->dwKeyState,
1983                          trackerInfo->curMousePos,
1984                          trackerInfo->pdwEffect);
1985   }
1986   else
1987   {
1988     DropTargetNode* newDropTargetNode = 0;
1989
1990     /*
1991      * If we changed window, we have to notify our old target and check for
1992      * the new one.
1993      */
1994     if (trackerInfo->curDragTarget!=0)
1995     {
1996       IDropTarget_DragLeave(trackerInfo->curDragTarget);
1997     }
1998
1999     /*
2000      * Make sure we're hovering over a window.
2001      */
2002     if (hwndNewTarget!=0)
2003     {
2004       /*
2005        * Find-out if there is a drag target under the mouse
2006        */
2007       HWND nexttar = hwndNewTarget;
2008       trackerInfo->curTargetHWND = hwndNewTarget;
2009
2010       do {
2011         newDropTargetNode = OLEDD_FindDropTarget(nexttar);
2012       } while (!newDropTargetNode && (nexttar = GetParent(nexttar)) != 0);
2013       if(nexttar) hwndNewTarget = nexttar;
2014
2015       trackerInfo->curDragTargetHWND = hwndNewTarget;
2016       trackerInfo->curDragTarget     = newDropTargetNode ? newDropTargetNode->dropTarget : 0;
2017
2018       /*
2019        * If there is, notify it that we just dragged-in
2020        */
2021       if (trackerInfo->curDragTarget!=0)
2022       {
2023         IDropTarget_DragEnter(trackerInfo->curDragTarget,
2024                               trackerInfo->dataObject,
2025                               trackerInfo->dwKeyState,
2026                               trackerInfo->curMousePos,
2027                               trackerInfo->pdwEffect);
2028       }
2029     }
2030     else
2031     {
2032       /*
2033        * The mouse is not over a window so we don't track anything.
2034        */
2035       trackerInfo->curDragTargetHWND = 0;
2036       trackerInfo->curTargetHWND     = 0;
2037       trackerInfo->curDragTarget     = 0;
2038     }
2039   }
2040
2041   /*
2042    * Now that we have done that, we have to tell the source to give
2043    * us feedback on the work being done by the target.  If we don't
2044    * have a target, simulate no effect.
2045    */
2046   if (trackerInfo->curDragTarget==0)
2047   {
2048     *trackerInfo->pdwEffect = DROPEFFECT_NONE;
2049   }
2050
2051   hr = IDropSource_GiveFeedback(trackerInfo->dropSource,
2052                                 *trackerInfo->pdwEffect);
2053
2054   /*
2055    * When we ask for feedback from the drop source, sometimes it will
2056    * do all the necessary work and sometimes it will not handle it
2057    * when that's the case, we must display the standard drag and drop
2058    * cursors.
2059    */
2060   if (hr==DRAGDROP_S_USEDEFAULTCURSORS)
2061   {
2062     if (*trackerInfo->pdwEffect & DROPEFFECT_MOVE)
2063     {
2064       SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(1)));
2065     }
2066     else if (*trackerInfo->pdwEffect & DROPEFFECT_COPY)
2067     {
2068       SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(2)));
2069     }
2070     else if (*trackerInfo->pdwEffect & DROPEFFECT_LINK)
2071     {
2072       SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(3)));
2073     }
2074     else
2075     {
2076       SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(0)));
2077     }
2078   }
2079 }
2080
2081 /***
2082  * OLEDD_TrackStateChange()
2083  *
2084  * This method is invoked while a drag and drop operation is in effect.
2085  * It is used to notify the drop target/drop source callbacks when
2086  * the state of the keyboard or mouse button change.
2087  *
2088  * params:
2089  *    trackerInfo - Pointer to the structure identifying the
2090  *                  drag & drop operation that is currently
2091  *                  active.
2092  */
2093 static void OLEDD_TrackStateChange(TrackerWindowInfo* trackerInfo)
2094 {
2095   /*
2096    * Ask the drop source what to do with the operation.
2097    */
2098   trackerInfo->returnValue = IDropSource_QueryContinueDrag(
2099                                trackerInfo->dropSource,
2100                                trackerInfo->escPressed,
2101                                trackerInfo->dwKeyState);
2102
2103   /*
2104    * All the return valued will stop the operation except the S_OK
2105    * return value.
2106    */
2107   if (trackerInfo->returnValue!=S_OK)
2108   {
2109     /*
2110      * Make sure the message loop in DoDragDrop stops
2111      */
2112     trackerInfo->trackingDone = TRUE;
2113
2114     /*
2115      * Release the mouse in case the drop target decides to show a popup
2116      * or a menu or something.
2117      */
2118     ReleaseCapture();
2119
2120     /*
2121      * If we end-up over a target, drop the object in the target or
2122      * inform the target that the operation was cancelled.
2123      */
2124     if (trackerInfo->curDragTarget!=0)
2125     {
2126       switch (trackerInfo->returnValue)
2127       {
2128         /*
2129          * If the source wants us to complete the operation, we tell
2130          * the drop target that we just dropped the object in it.
2131          */
2132         case DRAGDROP_S_DROP:
2133         {
2134           IDropTarget_Drop(trackerInfo->curDragTarget,
2135                            trackerInfo->dataObject,
2136                            trackerInfo->dwKeyState,
2137                            trackerInfo->curMousePos,
2138                            trackerInfo->pdwEffect);
2139           break;
2140         }
2141         /*
2142          * If the source told us that we should cancel, fool the drop
2143          * target by telling it that the mouse left it's window.
2144          * Also set the drop effect to "NONE" in case the application
2145          * ignores the result of DoDragDrop.
2146          */
2147         case DRAGDROP_S_CANCEL:
2148           IDropTarget_DragLeave(trackerInfo->curDragTarget);
2149           *trackerInfo->pdwEffect = DROPEFFECT_NONE;
2150           break;
2151       }
2152     }
2153   }
2154 }
2155
2156 /***
2157  * OLEDD_GetButtonState()
2158  *
2159  * This method will use the current state of the keyboard to build
2160  * a button state mask equivalent to the one passed in the
2161  * WM_MOUSEMOVE wParam.
2162  */
2163 static DWORD OLEDD_GetButtonState()
2164 {
2165   BYTE  keyboardState[256];
2166   DWORD keyMask = 0;
2167
2168   GetKeyboardState(keyboardState);
2169
2170   if ( (keyboardState[VK_SHIFT] & 0x80) !=0)
2171     keyMask |= MK_SHIFT;
2172
2173   if ( (keyboardState[VK_CONTROL] & 0x80) !=0)
2174     keyMask |= MK_CONTROL;
2175
2176   if ( (keyboardState[VK_LBUTTON] & 0x80) !=0)
2177     keyMask |= MK_LBUTTON;
2178
2179   if ( (keyboardState[VK_RBUTTON] & 0x80) !=0)
2180     keyMask |= MK_RBUTTON;
2181
2182   if ( (keyboardState[VK_MBUTTON] & 0x80) !=0)
2183     keyMask |= MK_MBUTTON;
2184
2185   return keyMask;
2186 }
2187
2188 /***
2189  * OLEDD_GetButtonState()
2190  *
2191  * This method will read the default value of the registry key in
2192  * parameter and extract a DWORD value from it. The registry key value
2193  * can be in a string key or a DWORD key.
2194  *
2195  * params:
2196  *     regKey   - Key to read the default value from
2197  *     pdwValue - Pointer to the location where the DWORD
2198  *                value is returned. This value is not modified
2199  *                if the value is not found.
2200  */
2201
2202 static void OLEUTL_ReadRegistryDWORDValue(
2203   HKEY   regKey,
2204   DWORD* pdwValue)
2205 {
2206   char  buffer[20];
2207   DWORD dwKeyType;
2208   DWORD cbData = 20;
2209   LONG  lres;
2210
2211   lres = RegQueryValueExA(regKey,
2212                           "",
2213                           NULL,
2214                           &dwKeyType,
2215                           (LPBYTE)buffer,
2216                           &cbData);
2217
2218   if (lres==ERROR_SUCCESS)
2219   {
2220     switch (dwKeyType)
2221     {
2222       case REG_DWORD:
2223         *pdwValue = *(DWORD*)buffer;
2224         break;
2225       case REG_EXPAND_SZ:
2226       case REG_MULTI_SZ:
2227       case REG_SZ:
2228         *pdwValue = (DWORD)strtoul(buffer, NULL, 10);
2229         break;
2230     }
2231   }
2232 }
2233
2234 /******************************************************************************
2235  * OleDraw (OLE32.@)
2236  *
2237  * The operation of this function is documented literally in the WinAPI
2238  * documentation to involve a QueryInterface for the IViewObject interface,
2239  * followed by a call to IViewObject::Draw.
2240  */
2241 HRESULT WINAPI OleDraw(
2242         IUnknown *pUnk,
2243         DWORD dwAspect,
2244         HDC hdcDraw,
2245         LPCRECT lprcBounds)
2246 {
2247   HRESULT hres;
2248   IViewObject *viewobject;
2249
2250   hres = IUnknown_QueryInterface(pUnk,
2251                                  &IID_IViewObject,
2252                                  (void**)&viewobject);
2253
2254   if (SUCCEEDED(hres))
2255   {
2256     RECTL rectl;
2257
2258     rectl.left = lprcBounds->left;
2259     rectl.right = lprcBounds->right;
2260     rectl.top = lprcBounds->top;
2261     rectl.bottom = lprcBounds->bottom;
2262     hres = IViewObject_Draw(viewobject, dwAspect, -1, 0, 0, 0, hdcDraw, &rectl, 0, 0, 0);
2263
2264     IViewObject_Release(viewobject);
2265     return hres;
2266   }
2267   else
2268   {
2269     return DV_E_NOIVIEWOBJECT;
2270   }
2271 }
2272
2273 /***********************************************************************
2274  *             OleTranslateAccelerator [OLE32.@]
2275  */
2276 HRESULT WINAPI OleTranslateAccelerator (LPOLEINPLACEFRAME lpFrame,
2277                    LPOLEINPLACEFRAMEINFO lpFrameInfo, LPMSG lpmsg)
2278 {
2279     WORD wID;
2280
2281     TRACE("(%p,%p,%p)\n", lpFrame, lpFrameInfo, lpmsg);
2282
2283     if (IsAccelerator(lpFrameInfo->haccel,lpFrameInfo->cAccelEntries,lpmsg,&wID))
2284         return IOleInPlaceFrame_TranslateAccelerator(lpFrame,lpmsg,wID);
2285
2286     return S_FALSE;
2287 }
2288
2289 /******************************************************************************
2290  *              OleCreate        [OLE32.@]
2291  *
2292  */
2293 HRESULT WINAPI OleCreate(
2294         REFCLSID rclsid,
2295         REFIID riid,
2296         DWORD renderopt,
2297         LPFORMATETC pFormatEtc,
2298         LPOLECLIENTSITE pClientSite,
2299         LPSTORAGE pStg,
2300         LPVOID* ppvObj)
2301 {
2302     HRESULT hres, hres1;
2303     IUnknown * pUnk = NULL;
2304
2305     FIXME("\n\t%s\n\t%s semi-stub!\n", debugstr_guid(rclsid), debugstr_guid(riid));
2306
2307     if (SUCCEEDED((hres = CoCreateInstance(rclsid, 0, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER|CLSCTX_LOCAL_SERVER , riid, (LPVOID*)&pUnk))))
2308     {
2309         if (pClientSite)
2310         {
2311             IOleObject * pOE;
2312             IPersistStorage * pPS;
2313             if (SUCCEEDED((hres = IUnknown_QueryInterface( pUnk, &IID_IOleObject, (LPVOID*)&pOE))))
2314             {
2315                 TRACE("trying to set clientsite %p\n", pClientSite);
2316                 hres1 = IOleObject_SetClientSite(pOE, pClientSite);
2317                 TRACE("-- result 0x%08lx\n", hres1);
2318                 IOleObject_Release(pOE);
2319             }
2320             if (SUCCEEDED((hres = IUnknown_QueryInterface( pUnk, &IID_IPersistStorage, (LPVOID*)&pPS))))
2321             {
2322                 TRACE("trying to set stg %p\n", pStg);
2323                 hres1 = IPersistStorage_InitNew(pPS, pStg);
2324                 TRACE("-- result 0x%08lx\n", hres1);
2325                 IPersistStorage_Release(pPS);
2326             }
2327         }
2328     }
2329
2330     *ppvObj = pUnk;
2331
2332     TRACE("-- %p\n", pUnk);
2333     return hres;
2334 }
2335
2336 /******************************************************************************
2337  *              OleSetAutoConvert        [OLE32.@]
2338  */
2339 HRESULT WINAPI OleSetAutoConvert(REFCLSID clsidOld, REFCLSID clsidNew)
2340 {
2341     static const WCHAR wszAutoConvertTo[] = {'A','u','t','o','C','o','n','v','e','r','t','T','o',0};
2342     HKEY hkey = NULL;
2343     WCHAR szClsidNew[CHARS_IN_GUID];
2344     HRESULT res = S_OK;
2345
2346     TRACE("(%s,%s)\n", debugstr_guid(clsidOld), debugstr_guid(clsidNew));
2347     
2348     res = COM_OpenKeyForCLSID(clsidOld, NULL, KEY_READ | KEY_WRITE, &hkey);
2349     if (FAILED(res))
2350         goto done;
2351     StringFromGUID2(clsidNew, szClsidNew, CHARS_IN_GUID);
2352     if (RegSetValueW(hkey, wszAutoConvertTo, REG_SZ, szClsidNew, (strlenW(szClsidNew)+1) * sizeof(WCHAR)))
2353     {
2354         res = REGDB_E_WRITEREGDB;
2355         goto done;
2356     }
2357
2358 done:
2359     if (hkey) RegCloseKey(hkey);
2360     return res;
2361 }
2362
2363 /******************************************************************************
2364  *              OleDoAutoConvert        [OLE32.@]
2365  */
2366 HRESULT WINAPI OleDoAutoConvert(LPSTORAGE pStg, LPCLSID pClsidNew)
2367 {
2368     FIXME("(%p,%p) : stub\n",pStg,pClsidNew);
2369     return E_NOTIMPL;
2370 }
2371
2372 /******************************************************************************
2373  *              OleIsRunning        [OLE32.@]
2374  */
2375 BOOL WINAPI OleIsRunning(LPOLEOBJECT pObject)
2376 {
2377     IRunnableObject *pRunnable;
2378     HRESULT hr;
2379     BOOL running;
2380
2381     TRACE("(%p)\n", pObject);
2382
2383     hr = IOleObject_QueryInterface(pObject, &IID_IRunnableObject, (void **)&pRunnable);
2384     if (FAILED(hr))
2385         return FALSE;
2386     running = IRunnableObject_IsRunning(pRunnable);
2387     IRunnableObject_Release(pRunnable);
2388     return running;
2389 }
2390
2391 /***********************************************************************
2392  *           OleNoteObjectVisible                           [OLE32.@]
2393  */
2394 HRESULT WINAPI OleNoteObjectVisible(LPUNKNOWN pUnknown, BOOL bVisible)
2395 {
2396     TRACE("(%p, %s)\n", pUnknown, bVisible ? "TRUE" : "FALSE");
2397     return CoLockObjectExternal(pUnknown, bVisible, TRUE);
2398 }
2399
2400
2401 /***********************************************************************
2402  *           OLE_FreeClipDataArray   [internal]
2403  *
2404  * NOTES:
2405  *  frees the data associated with an array of CLIPDATAs
2406  */
2407 static void OLE_FreeClipDataArray(ULONG count, CLIPDATA * pClipDataArray)
2408 {
2409     ULONG i;
2410     for (i = 0; i < count; i++)
2411         if (pClipDataArray[i].pClipData)
2412             CoTaskMemFree(pClipDataArray[i].pClipData);
2413 }
2414
2415 /***********************************************************************
2416  *           PropSysAllocString                     [OLE32.@]
2417  * NOTES:
2418  *  Basically a copy of SysAllocStringLen.
2419  */
2420 BSTR WINAPI PropSysAllocString(LPCOLESTR str)
2421 {
2422     DWORD  bufferSize;
2423     DWORD* newBuffer;
2424     WCHAR* stringBuffer;
2425     int len;
2426
2427     if (!str) return 0;
2428
2429     len = lstrlenW(str);
2430     /*
2431      * Find the length of the buffer passed-in in bytes.
2432      */
2433     bufferSize = len * sizeof (WCHAR);
2434
2435     /*
2436      * Allocate a new buffer to hold the string.
2437      * don't forget to keep an empty spot at the beginning of the
2438      * buffer for the character count and an extra character at the
2439      * end for the NULL.
2440      */
2441     newBuffer = HeapAlloc(GetProcessHeap(), 0,
2442                           bufferSize + sizeof(WCHAR) + sizeof(DWORD));
2443
2444     /*
2445      * If the memory allocation failed, return a null pointer.
2446      */
2447     if (newBuffer==0)
2448       return 0;
2449
2450     /*
2451      * Copy the length of the string in the placeholder.
2452      */
2453     *newBuffer = bufferSize;
2454
2455     /*
2456      * Skip the byte count.
2457      */
2458     newBuffer++;
2459
2460     /*
2461      * Copy the information in the buffer.
2462      * Since it is valid to pass a NULL pointer here, we'll initialize the
2463      * buffer to nul if it is the case.
2464      */
2465     if (str != 0)
2466       memcpy(newBuffer, str, bufferSize);
2467     else
2468       memset(newBuffer, 0, bufferSize);
2469
2470     /*
2471      * Make sure that there is a nul character at the end of the
2472      * string.
2473      */
2474     stringBuffer = (WCHAR*)newBuffer;
2475     stringBuffer[len] = L'\0';
2476
2477     return (LPWSTR)stringBuffer;
2478 }
2479
2480 /***********************************************************************
2481  *           PropSysFreeString                      [OLE32.@]
2482  * NOTES
2483  *  Copy of SysFreeString.
2484  */
2485 void WINAPI PropSysFreeString(LPOLESTR str)
2486 {
2487     DWORD* bufferPointer;
2488
2489     /* NULL is a valid parameter */
2490     if(!str) return;
2491
2492     /*
2493      * We have to be careful when we free a BSTR pointer, it points to
2494      * the beginning of the string but it skips the byte count contained
2495      * before the string.
2496      */
2497     bufferPointer = (DWORD*)str;
2498
2499     bufferPointer--;
2500
2501     /*
2502      * Free the memory from its "real" origin.
2503      */
2504     HeapFree(GetProcessHeap(), 0, bufferPointer);
2505 }
2506
2507 /******************************************************************************
2508  * Check if a PROPVARIANT's type is valid.
2509  */
2510 static inline HRESULT PROPVARIANT_ValidateType(VARTYPE vt)
2511 {
2512     switch (vt)
2513     {
2514     case VT_EMPTY:
2515     case VT_NULL:
2516     case VT_I2:
2517     case VT_I4:
2518     case VT_R4:
2519     case VT_R8:
2520     case VT_CY:
2521     case VT_DATE:
2522     case VT_BSTR:
2523     case VT_ERROR:
2524     case VT_BOOL:
2525     case VT_UI1:
2526     case VT_UI2:
2527     case VT_UI4:
2528     case VT_I8:
2529     case VT_UI8:
2530     case VT_LPSTR:
2531     case VT_LPWSTR:
2532     case VT_FILETIME:
2533     case VT_BLOB:
2534     case VT_STREAM:
2535     case VT_STORAGE:
2536     case VT_STREAMED_OBJECT:
2537     case VT_STORED_OBJECT:
2538     case VT_BLOB_OBJECT:
2539     case VT_CF:
2540     case VT_CLSID:
2541     case VT_I2|VT_VECTOR:
2542     case VT_I4|VT_VECTOR:
2543     case VT_R4|VT_VECTOR:
2544     case VT_R8|VT_VECTOR:
2545     case VT_CY|VT_VECTOR:
2546     case VT_DATE|VT_VECTOR:
2547     case VT_BSTR|VT_VECTOR:
2548     case VT_ERROR|VT_VECTOR:
2549     case VT_BOOL|VT_VECTOR:
2550     case VT_VARIANT|VT_VECTOR:
2551     case VT_UI1|VT_VECTOR:
2552     case VT_UI2|VT_VECTOR:
2553     case VT_UI4|VT_VECTOR:
2554     case VT_I8|VT_VECTOR:
2555     case VT_UI8|VT_VECTOR:
2556     case VT_LPSTR|VT_VECTOR:
2557     case VT_LPWSTR|VT_VECTOR:
2558     case VT_FILETIME|VT_VECTOR:
2559     case VT_CF|VT_VECTOR:
2560     case VT_CLSID|VT_VECTOR:
2561         return S_OK;
2562     }
2563     WARN("Bad type %d\n", vt);
2564     return STG_E_INVALIDPARAMETER;
2565 }
2566
2567 /***********************************************************************
2568  *           PropVariantClear                       [OLE32.@]
2569  */
2570 HRESULT WINAPI PropVariantClear(PROPVARIANT * pvar) /* [in/out] */
2571 {
2572     HRESULT hr;
2573
2574     TRACE("(%p)\n", pvar);
2575
2576     if (!pvar)
2577         return S_OK;
2578
2579     hr = PROPVARIANT_ValidateType(pvar->vt);
2580     if (FAILED(hr))
2581         return hr;
2582
2583     switch(pvar->vt)
2584     {
2585     case VT_EMPTY:
2586     case VT_NULL:
2587     case VT_I2:
2588     case VT_I4:
2589     case VT_R4:
2590     case VT_R8:
2591     case VT_CY:
2592     case VT_DATE:
2593     case VT_ERROR:
2594     case VT_BOOL:
2595     case VT_UI1:
2596     case VT_UI2:
2597     case VT_UI4:
2598     case VT_I8:
2599     case VT_UI8:
2600     case VT_FILETIME:
2601         break;
2602     case VT_STREAM:
2603     case VT_STREAMED_OBJECT:
2604     case VT_STORAGE:
2605     case VT_STORED_OBJECT:
2606         if (pvar->u.pStream)
2607             IUnknown_Release(pvar->u.pStream);
2608         break;
2609     case VT_CLSID:
2610     case VT_LPSTR:
2611     case VT_LPWSTR:
2612         /* pick an arbitary typed pointer - we don't care about the type
2613          * as we are just freeing it */
2614         CoTaskMemFree(pvar->u.puuid);
2615         break;
2616     case VT_BLOB:
2617     case VT_BLOB_OBJECT:
2618         CoTaskMemFree(pvar->u.blob.pBlobData);
2619         break;
2620     case VT_BSTR:
2621         if (pvar->u.bstrVal)
2622             PropSysFreeString(pvar->u.bstrVal);
2623         break;
2624     case VT_CF:
2625         if (pvar->u.pclipdata)
2626         {
2627             OLE_FreeClipDataArray(1, pvar->u.pclipdata);
2628             CoTaskMemFree(pvar->u.pclipdata);
2629         }
2630         break;
2631     default:
2632         if (pvar->vt & VT_VECTOR)
2633         {
2634             ULONG i;
2635
2636             switch (pvar->vt & ~VT_VECTOR)
2637             {
2638             case VT_VARIANT:
2639                 FreePropVariantArray(pvar->u.capropvar.cElems, pvar->u.capropvar.pElems);
2640                 break;
2641             case VT_CF:
2642                 OLE_FreeClipDataArray(pvar->u.caclipdata.cElems, pvar->u.caclipdata.pElems);
2643                 break;
2644             case VT_BSTR:
2645                 for (i = 0; i < pvar->u.cabstr.cElems; i++)
2646                     PropSysFreeString(pvar->u.cabstr.pElems[i]);
2647                 break;
2648             case VT_LPSTR:
2649                 for (i = 0; i < pvar->u.calpstr.cElems; i++)
2650                     CoTaskMemFree(pvar->u.calpstr.pElems[i]);
2651                 break;
2652             case VT_LPWSTR:
2653                 for (i = 0; i < pvar->u.calpwstr.cElems; i++)
2654                     CoTaskMemFree(pvar->u.calpwstr.pElems[i]);
2655                 break;
2656             }
2657             if (pvar->vt & ~VT_VECTOR)
2658             {
2659                 /* pick an arbitary VT_VECTOR structure - they all have the same
2660                  * memory layout */
2661                 CoTaskMemFree(pvar->u.capropvar.pElems);
2662             }
2663         }
2664         else
2665             WARN("Invalid/unsupported type %d\n", pvar->vt);
2666     }
2667
2668     ZeroMemory(pvar, sizeof(*pvar));
2669
2670     return S_OK;
2671 }
2672
2673 /***********************************************************************
2674  *           PropVariantCopy                        [OLE32.@]
2675  */
2676 HRESULT WINAPI PropVariantCopy(PROPVARIANT *pvarDest,      /* [out] */
2677                                const PROPVARIANT *pvarSrc) /* [in] */
2678 {
2679     ULONG len;
2680     HRESULT hr;
2681
2682     TRACE("(%p, %p)\n", pvarDest, pvarSrc);
2683
2684     hr = PROPVARIANT_ValidateType(pvarSrc->vt);
2685     if (FAILED(hr))
2686         return hr;
2687
2688     /* this will deal with most cases */
2689     CopyMemory(pvarDest, pvarSrc, sizeof(*pvarDest));
2690
2691     switch(pvarSrc->vt)
2692     {
2693     case VT_STREAM:
2694     case VT_STREAMED_OBJECT:
2695     case VT_STORAGE:
2696     case VT_STORED_OBJECT:
2697         IUnknown_AddRef((LPUNKNOWN)pvarDest->u.pStream);
2698         break;
2699     case VT_CLSID:
2700         pvarDest->u.puuid = CoTaskMemAlloc(sizeof(CLSID));
2701         CopyMemory(pvarDest->u.puuid, pvarSrc->u.puuid, sizeof(CLSID));
2702         break;
2703     case VT_LPSTR:
2704         len = strlen(pvarSrc->u.pszVal);
2705         pvarDest->u.pszVal = CoTaskMemAlloc((len+1)*sizeof(CHAR));
2706         CopyMemory(pvarDest->u.pszVal, pvarSrc->u.pszVal, (len+1)*sizeof(CHAR));
2707         break;
2708     case VT_LPWSTR:
2709         len = lstrlenW(pvarSrc->u.pwszVal);
2710         pvarDest->u.pwszVal = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
2711         CopyMemory(pvarDest->u.pwszVal, pvarSrc->u.pwszVal, (len+1)*sizeof(WCHAR));
2712         break;
2713     case VT_BLOB:
2714     case VT_BLOB_OBJECT:
2715         if (pvarSrc->u.blob.pBlobData)
2716         {
2717             len = pvarSrc->u.blob.cbSize;
2718             pvarDest->u.blob.pBlobData = CoTaskMemAlloc(len);
2719             CopyMemory(pvarDest->u.blob.pBlobData, pvarSrc->u.blob.pBlobData, len);
2720         }
2721         break;
2722     case VT_BSTR:
2723         pvarDest->u.bstrVal = PropSysAllocString(pvarSrc->u.bstrVal);
2724         break;
2725     case VT_CF:
2726         if (pvarSrc->u.pclipdata)
2727         {
2728             len = pvarSrc->u.pclipdata->cbSize - sizeof(pvarSrc->u.pclipdata->ulClipFmt);
2729             pvarDest->u.pclipdata = CoTaskMemAlloc(sizeof (CLIPDATA));
2730             pvarDest->u.pclipdata->cbSize = pvarSrc->u.pclipdata->cbSize;
2731             pvarDest->u.pclipdata->ulClipFmt = pvarSrc->u.pclipdata->ulClipFmt;
2732             pvarDest->u.pclipdata->pClipData = CoTaskMemAlloc(len);
2733             CopyMemory(pvarDest->u.pclipdata->pClipData, pvarSrc->u.pclipdata->pClipData, len);
2734         }
2735         break;
2736     default:
2737         if (pvarSrc->vt & VT_VECTOR)
2738         {
2739             int elemSize;
2740             ULONG i;
2741
2742             switch(pvarSrc->vt & ~VT_VECTOR)
2743             {
2744             case VT_I1:       elemSize = sizeof(pvarSrc->u.cVal); break;
2745             case VT_UI1:      elemSize = sizeof(pvarSrc->u.bVal); break;
2746             case VT_I2:       elemSize = sizeof(pvarSrc->u.iVal); break;
2747             case VT_UI2:      elemSize = sizeof(pvarSrc->u.uiVal); break;
2748             case VT_BOOL:     elemSize = sizeof(pvarSrc->u.boolVal); break;
2749             case VT_I4:       elemSize = sizeof(pvarSrc->u.lVal); break;
2750             case VT_UI4:      elemSize = sizeof(pvarSrc->u.ulVal); break;
2751             case VT_R4:       elemSize = sizeof(pvarSrc->u.fltVal); break;
2752             case VT_R8:       elemSize = sizeof(pvarSrc->u.dblVal); break;
2753             case VT_ERROR:    elemSize = sizeof(pvarSrc->u.scode); break;
2754             case VT_I8:       elemSize = sizeof(pvarSrc->u.hVal); break;
2755             case VT_UI8:      elemSize = sizeof(pvarSrc->u.uhVal); break;
2756             case VT_CY:       elemSize = sizeof(pvarSrc->u.cyVal); break;
2757             case VT_DATE:     elemSize = sizeof(pvarSrc->u.date); break;
2758             case VT_FILETIME: elemSize = sizeof(pvarSrc->u.filetime); break;
2759             case VT_CLSID:    elemSize = sizeof(*pvarSrc->u.puuid); break;
2760             case VT_CF:       elemSize = sizeof(*pvarSrc->u.pclipdata); break;
2761             case VT_BSTR:     elemSize = sizeof(*pvarSrc->u.bstrVal); break;
2762             case VT_LPSTR:    elemSize = sizeof(*pvarSrc->u.pszVal); break;
2763             case VT_LPWSTR:   elemSize = sizeof(*pvarSrc->u.pwszVal); break;
2764
2765             case VT_VARIANT:
2766             default:
2767                 FIXME("Invalid element type: %ul\n", pvarSrc->vt & ~VT_VECTOR);
2768                 return E_INVALIDARG;
2769             }
2770             len = pvarSrc->u.capropvar.cElems;
2771             pvarDest->u.capropvar.pElems = CoTaskMemAlloc(len * elemSize);
2772             if (pvarSrc->vt == (VT_VECTOR | VT_VARIANT))
2773             {
2774                 for (i = 0; i < len; i++)
2775                     PropVariantCopy(&pvarDest->u.capropvar.pElems[i], &pvarSrc->u.capropvar.pElems[i]);
2776             }
2777             else if (pvarSrc->vt == (VT_VECTOR | VT_CF))
2778             {
2779                 FIXME("Copy clipformats\n");
2780             }
2781             else if (pvarSrc->vt == (VT_VECTOR | VT_BSTR))
2782             {
2783                 for (i = 0; i < len; i++)
2784                     pvarDest->u.cabstr.pElems[i] = PropSysAllocString(pvarSrc->u.cabstr.pElems[i]);
2785             }
2786             else if (pvarSrc->vt == (VT_VECTOR | VT_LPSTR))
2787             {
2788                 size_t strLen;
2789                 for (i = 0; i < len; i++)
2790                 {
2791                     strLen = lstrlenA(pvarSrc->u.calpstr.pElems[i]) + 1;
2792                     pvarDest->u.calpstr.pElems[i] = CoTaskMemAlloc(strLen);
2793                     memcpy(pvarDest->u.calpstr.pElems[i],
2794                      pvarSrc->u.calpstr.pElems[i], strLen);
2795                 }
2796             }
2797             else if (pvarSrc->vt == (VT_VECTOR | VT_LPWSTR))
2798             {
2799                 size_t strLen;
2800                 for (i = 0; i < len; i++)
2801                 {
2802                     strLen = (lstrlenW(pvarSrc->u.calpwstr.pElems[i]) + 1) *
2803                      sizeof(WCHAR);
2804                     pvarDest->u.calpstr.pElems[i] = CoTaskMemAlloc(strLen);
2805                     memcpy(pvarDest->u.calpstr.pElems[i],
2806                      pvarSrc->u.calpstr.pElems[i], strLen);
2807                 }
2808             }
2809             else
2810                 CopyMemory(pvarDest->u.capropvar.pElems, pvarSrc->u.capropvar.pElems, len * elemSize);
2811         }
2812         else
2813             WARN("Invalid/unsupported type %d\n", pvarSrc->vt);
2814     }
2815
2816     return S_OK;
2817 }
2818
2819 /***********************************************************************
2820  *           FreePropVariantArray                           [OLE32.@]
2821  */
2822 HRESULT WINAPI FreePropVariantArray(ULONG cVariants, /* [in] */
2823                                     PROPVARIANT *rgvars)    /* [in/out] */
2824 {
2825     ULONG i;
2826
2827     TRACE("(%lu, %p)\n", cVariants, rgvars);
2828
2829     for(i = 0; i < cVariants; i++)
2830         PropVariantClear(&rgvars[i]);
2831
2832     return S_OK;
2833 }
2834
2835 /******************************************************************************
2836  * DllDebugObjectRPCHook (OLE32.@)
2837  * turns on and off internal debugging,  pointer is only used on macintosh
2838  */
2839
2840 BOOL WINAPI DllDebugObjectRPCHook(BOOL b, void *dummy)
2841 {
2842   FIXME("stub\n");
2843   return TRUE;
2844 }