Added regedit unit test, a couple minor changes to regedit.
[wine] / dlls / ole32 / ole2.c
1
2 /*
3  *      OLE2 library
4  *
5  *      Copyright 1995  Martin von Loewis
6  *      Copyright 1999  Francis Beaudet
7  *      Copyright 1999  Noel Borthwick
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #include "config.h"
25
26 #include <assert.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30
31 #include "commctrl.h"
32 #include "ole2.h"
33 #include "ole2ver.h"
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winerror.h"
37 #include "winuser.h"
38 #include "winreg.h"
39
40 #include "wine/obj_clientserver.h"
41 #include "wine/winbase16.h"
42 #include "wine/wingdi16.h"
43 #include "wine/winuser16.h"
44 #include "ole32_main.h"
45
46 #include "wine/debug.h"
47
48 WINE_DEFAULT_DEBUG_CHANNEL(ole);
49 WINE_DECLARE_DEBUG_CHANNEL(accel);
50
51 /******************************************************************************
52  * These are static/global variables and internal data structures that the
53  * OLE module uses to maintain it's state.
54  */
55 typedef struct tagDropTargetNode
56 {
57   HWND          hwndTarget;
58   IDropTarget*    dropTarget;
59   struct tagDropTargetNode* prevDropTarget;
60   struct tagDropTargetNode* nextDropTarget;
61 } DropTargetNode;
62
63 typedef struct tagTrackerWindowInfo
64 {
65   IDataObject* dataObject;
66   IDropSource* dropSource;
67   DWORD        dwOKEffect;
68   DWORD*       pdwEffect;
69   BOOL       trackingDone;
70   HRESULT      returnValue;
71
72   BOOL       escPressed;
73   HWND       curDragTargetHWND;
74   IDropTarget* curDragTarget;
75 } TrackerWindowInfo;
76
77 typedef struct tagOleMenuDescriptor  /* OleMenuDescriptor */
78 {
79   HWND               hwndFrame;         /* The containers frame window */
80   HWND               hwndActiveObject;  /* The active objects window */
81   OLEMENUGROUPWIDTHS mgw;               /* OLE menu group widths for the shared menu */
82   HMENU              hmenuCombined;     /* The combined menu */
83   BOOL               bIsServerItem;     /* True if the currently open popup belongs to the server */
84 } OleMenuDescriptor;
85
86 typedef struct tagOleMenuHookItem   /* OleMenu hook item in per thread hook list */
87 {
88   DWORD tid;                /* Thread Id  */
89   HANDLE hHeap;             /* Heap this is allocated from */
90   HHOOK GetMsg_hHook;       /* message hook for WH_GETMESSAGE */
91   HHOOK CallWndProc_hHook;  /* message hook for WH_CALLWNDPROC */
92   struct tagOleMenuHookItem *next;
93 } OleMenuHookItem;
94
95 static OleMenuHookItem *hook_list;
96
97 /*
98  * This is the lock count on the OLE library. It is controlled by the
99  * OLEInitialize/OLEUninitialize methods.
100  */
101 static ULONG OLE_moduleLockCount = 0;
102
103 /*
104  * Name of our registered window class.
105  */
106 static const char OLEDD_DRAGTRACKERCLASS[] = "WineDragDropTracker32";
107
108 /*
109  * This is the head of the Drop target container.
110  */
111 static DropTargetNode* targetListHead = NULL;
112
113 /******************************************************************************
114  * These are the prototypes of miscelaneous utility methods
115  */
116 static void OLEUTL_ReadRegistryDWORDValue(HKEY regKey, DWORD* pdwValue);
117
118 /******************************************************************************
119  * These are the prototypes of the utility methods used to manage a shared menu
120  */
121 static void OLEMenu_Initialize();
122 static void OLEMenu_UnInitialize();
123 BOOL OLEMenu_InstallHooks( DWORD tid );
124 BOOL OLEMenu_UnInstallHooks( DWORD tid );
125 OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid );
126 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos );
127 BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor );
128 LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam);
129 LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam);
130
131 /******************************************************************************
132  * These are the prototypes of the OLE Clipboard initialization methods (in clipboard.c)
133  */
134 void OLEClipbrd_UnInitialize();
135 void OLEClipbrd_Initialize();
136
137 /******************************************************************************
138  * These are the prototypes of the utility methods used for OLE Drag n Drop
139  */
140 static void            OLEDD_Initialize();
141 static void            OLEDD_UnInitialize();
142 static void            OLEDD_InsertDropTarget(
143                          DropTargetNode* nodeToAdd);
144 static DropTargetNode* OLEDD_ExtractDropTarget(
145                          HWND hwndOfTarget);
146 static DropTargetNode* OLEDD_FindDropTarget(
147                          HWND hwndOfTarget);
148 static LRESULT WINAPI  OLEDD_DragTrackerWindowProc(
149                          HWND   hwnd,
150                          UINT   uMsg,
151                          WPARAM wParam,
152                          LPARAM   lParam);
153 static void OLEDD_TrackMouseMove(
154                          TrackerWindowInfo* trackerInfo,
155                          POINT            mousePos,
156                          DWORD              keyState);
157 static void OLEDD_TrackStateChange(
158                          TrackerWindowInfo* trackerInfo,
159                          POINT            mousePos,
160                          DWORD              keyState);
161 static DWORD OLEDD_GetButtonState();
162
163
164 /******************************************************************************
165  *              OleBuildVersion [OLE2.1]
166  *              OleBuildVersion [OLE32.84]
167  */
168 DWORD WINAPI OleBuildVersion(void)
169 {
170     TRACE("Returning version %d, build %d.\n", rmm, rup);
171     return (rmm<<16)+rup;
172 }
173
174 /***********************************************************************
175  *           OleInitialize       (OLE2.2)
176  *           OleInitialize       (OLE32.108)
177  */
178 HRESULT WINAPI OleInitialize(LPVOID reserved)
179 {
180   HRESULT hr;
181
182   TRACE("(%p)\n", reserved);
183
184   /*
185    * The first duty of the OleInitialize is to initialize the COM libraries.
186    */
187   hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
188
189   /*
190    * If the CoInitializeEx call failed, the OLE libraries can't be
191    * initialized.
192    */
193   if (FAILED(hr))
194     return hr;
195
196   /*
197    * Then, it has to initialize the OLE specific modules.
198    * This includes:
199    *     Clipboard
200    *     Drag and Drop
201    *     Object linking and Embedding
202    *     In-place activation
203    */
204   if (OLE_moduleLockCount==0)
205   {
206     /*
207      * Initialize the libraries.
208      */
209     TRACE("() - Initializing the OLE libraries\n");
210
211     /*
212      * OLE Clipboard
213      */
214     OLEClipbrd_Initialize();
215
216     /*
217      * Drag and Drop
218      */
219     OLEDD_Initialize();
220
221     /*
222      * OLE shared menu
223      */
224     OLEMenu_Initialize();
225   }
226
227   /*
228    * Then, we increase the lock count on the OLE module.
229    */
230   OLE_moduleLockCount++;
231
232   return hr;
233 }
234
235 /******************************************************************************
236  *              CoGetCurrentProcess     [COMPOBJ.34]
237  *              CoGetCurrentProcess     [OLE32.18]
238  *
239  * NOTES
240  *   Is DWORD really the correct return type for this function?
241  */
242 DWORD WINAPI CoGetCurrentProcess(void)
243 {
244         return GetCurrentProcessId();
245 }
246
247 /******************************************************************************
248  *              OleUninitialize [OLE2.3]
249  *              OleUninitialize [OLE32.131]
250  */
251 void WINAPI OleUninitialize(void)
252 {
253   TRACE("()\n");
254
255   /*
256    * Decrease the lock count on the OLE module.
257    */
258   OLE_moduleLockCount--;
259
260   /*
261    * If we hit the bottom of the lock stack, free the libraries.
262    */
263   if (OLE_moduleLockCount==0)
264   {
265     /*
266      * Actually free the libraries.
267      */
268     TRACE("() - Freeing the last reference count\n");
269
270     /*
271      * OLE Clipboard
272      */
273     OLEClipbrd_UnInitialize();
274
275     /*
276      * Drag and Drop
277      */
278     OLEDD_UnInitialize();
279
280     /*
281      * OLE shared menu
282      */
283     OLEMenu_UnInitialize();
284   }
285
286   /*
287    * Then, uninitialize the COM libraries.
288    */
289   CoUninitialize();
290 }
291
292 /******************************************************************************
293  *              CoRegisterMessageFilter [OLE32.38]
294  */
295 HRESULT WINAPI CoRegisterMessageFilter(
296     LPMESSAGEFILTER lpMessageFilter,    /* [in] Pointer to interface */
297     LPMESSAGEFILTER *lplpMessageFilter  /* [out] Indirect pointer to prior instance if non-NULL */
298 ) {
299     FIXME("stub\n");
300     if (lplpMessageFilter) {
301         *lplpMessageFilter = NULL;
302     }
303     return S_OK;
304 }
305
306 /******************************************************************************
307  *              OleInitializeWOW        [OLE32.109]
308  */
309 HRESULT WINAPI OleInitializeWOW(DWORD x) {
310         FIXME("(0x%08lx),stub!\n",x);
311         return 0;
312 }
313
314 /***********************************************************************
315  *           RegisterDragDrop (OLE2.35)
316  */
317 HRESULT WINAPI RegisterDragDrop16(
318         HWND16 hwnd,
319         LPDROPTARGET pDropTarget
320 ) {
321         FIXME("(0x%04x,%p),stub!\n",hwnd,pDropTarget);
322         return S_OK;
323 }
324
325 /***********************************************************************
326  *           RegisterDragDrop (OLE32.139)
327  */
328 HRESULT WINAPI RegisterDragDrop(
329         HWND hwnd,
330         LPDROPTARGET pDropTarget)
331 {
332   DropTargetNode* dropTargetInfo;
333
334   TRACE("(0x%x,%p)\n", hwnd, pDropTarget);
335
336   /*
337    * First, check if the window is already registered.
338    */
339   dropTargetInfo = OLEDD_FindDropTarget(hwnd);
340
341   if (dropTargetInfo!=NULL)
342     return DRAGDROP_E_ALREADYREGISTERED;
343
344   /*
345    * If it's not there, we can add it. We first create a node for it.
346    */
347   dropTargetInfo = HeapAlloc(GetProcessHeap(), 0, sizeof(DropTargetNode));
348
349   if (dropTargetInfo==NULL)
350     return E_OUTOFMEMORY;
351
352   dropTargetInfo->hwndTarget     = hwnd;
353   dropTargetInfo->prevDropTarget = NULL;
354   dropTargetInfo->nextDropTarget = NULL;
355
356   /*
357    * Don't forget that this is an interface pointer, need to nail it down since
358    * we keep a copy of it.
359    */
360   dropTargetInfo->dropTarget  = pDropTarget;
361   IDropTarget_AddRef(dropTargetInfo->dropTarget);
362
363   OLEDD_InsertDropTarget(dropTargetInfo);
364
365         return S_OK;
366 }
367
368 /***********************************************************************
369  *           RevokeDragDrop (OLE2.36)
370  */
371 HRESULT WINAPI RevokeDragDrop16(
372         HWND16 hwnd
373 ) {
374         FIXME("(0x%04x),stub!\n",hwnd);
375         return S_OK;
376 }
377
378 /***********************************************************************
379  *           RevokeDragDrop (OLE32.141)
380  */
381 HRESULT WINAPI RevokeDragDrop(
382         HWND hwnd)
383 {
384   DropTargetNode* dropTargetInfo;
385
386   TRACE("(0x%x)\n", hwnd);
387
388   /*
389    * First, check if the window is already registered.
390    */
391   dropTargetInfo = OLEDD_ExtractDropTarget(hwnd);
392
393   /*
394    * If it ain't in there, it's an error.
395    */
396   if (dropTargetInfo==NULL)
397     return DRAGDROP_E_NOTREGISTERED;
398
399   /*
400    * If it's in there, clean-up it's used memory and
401    * references
402    */
403   IDropTarget_Release(dropTargetInfo->dropTarget);
404   HeapFree(GetProcessHeap(), 0, dropTargetInfo);
405
406         return S_OK;
407 }
408
409 /***********************************************************************
410  *           OleRegGetUserType (OLE32.122)
411  *
412  * This implementation of OleRegGetUserType ignores the dwFormOfType
413  * parameter and always returns the full name of the object. This is
414  * not too bad since this is the case for many objects because of the
415  * way they are registered.
416  */
417 HRESULT WINAPI OleRegGetUserType(
418         REFCLSID clsid,
419         DWORD dwFormOfType,
420         LPOLESTR* pszUserType)
421 {
422   char    keyName[60];
423   DWORD   dwKeyType;
424   DWORD   cbData;
425   HKEY    clsidKey;
426   LONG    hres;
427   LPBYTE  buffer;
428   HRESULT retVal;
429   /*
430    * Initialize the out parameter.
431    */
432   *pszUserType = NULL;
433
434   /*
435    * Build the key name we're looking for
436    */
437   sprintf( keyName, "CLSID\\{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\",
438            clsid->Data1, clsid->Data2, clsid->Data3,
439            clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
440            clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );
441
442   TRACE("(%s, %ld, %p)\n", keyName, dwFormOfType, pszUserType);
443
444   /*
445    * Open the class id Key
446    */
447   hres = RegOpenKeyA(HKEY_CLASSES_ROOT,
448                      keyName,
449                      &clsidKey);
450
451   if (hres != ERROR_SUCCESS)
452     return REGDB_E_CLASSNOTREG;
453
454   /*
455    * Retrieve the size of the name string.
456    */
457   cbData = 0;
458
459   hres = RegQueryValueExA(clsidKey,
460                           "",
461                           NULL,
462                           &dwKeyType,
463                           NULL,
464                           &cbData);
465
466   if (hres!=ERROR_SUCCESS)
467   {
468     RegCloseKey(clsidKey);
469     return REGDB_E_READREGDB;
470   }
471
472   /*
473    * Allocate a buffer for the registry value.
474    */
475   *pszUserType = CoTaskMemAlloc(cbData*2);
476
477   if (*pszUserType==NULL)
478   {
479     RegCloseKey(clsidKey);
480     return E_OUTOFMEMORY;
481   }
482
483   buffer = HeapAlloc(GetProcessHeap(), 0, cbData);
484
485   if (buffer == NULL)
486   {
487     RegCloseKey(clsidKey);
488     CoTaskMemFree(*pszUserType);
489     *pszUserType=NULL;
490     return E_OUTOFMEMORY;
491   }
492
493   hres = RegQueryValueExA(clsidKey,
494                           "",
495                           NULL,
496                           &dwKeyType,
497                           buffer,
498                           &cbData);
499
500   RegCloseKey(clsidKey);
501
502
503   if (hres!=ERROR_SUCCESS)
504   {
505     CoTaskMemFree(*pszUserType);
506     *pszUserType=NULL;
507
508     retVal = REGDB_E_READREGDB;
509   }
510   else
511   {
512     MultiByteToWideChar( CP_ACP, 0, buffer, -1, *pszUserType, cbData /*FIXME*/ );
513     retVal = S_OK;
514   }
515   HeapFree(GetProcessHeap(), 0, buffer);
516
517   return retVal;
518 }
519
520 /***********************************************************************
521  * DoDragDrop [OLE32.65]
522  */
523 HRESULT WINAPI DoDragDrop (
524   IDataObject *pDataObject,  /* [in] ptr to the data obj           */
525   IDropSource* pDropSource,  /* [in] ptr to the source obj         */
526   DWORD       dwOKEffect,    /* [in] effects allowed by the source */
527   DWORD       *pdwEffect)    /* [out] ptr to effects of the source */
528 {
529   TrackerWindowInfo trackerInfo;
530   HWND            hwndTrackWindow;
531   MSG             msg;
532
533   TRACE("(DataObject %p, DropSource %p)\n", pDataObject, pDropSource);
534
535   /*
536    * Setup the drag n drop tracking window.
537    */
538   trackerInfo.dataObject        = pDataObject;
539   trackerInfo.dropSource        = pDropSource;
540   trackerInfo.dwOKEffect        = dwOKEffect;
541   trackerInfo.pdwEffect         = pdwEffect;
542   trackerInfo.trackingDone      = FALSE;
543   trackerInfo.escPressed        = FALSE;
544   trackerInfo.curDragTargetHWND = 0;
545   trackerInfo.curDragTarget     = 0;
546
547   hwndTrackWindow = CreateWindowA(OLEDD_DRAGTRACKERCLASS,
548                                     "TrackerWindow",
549                                     WS_POPUP,
550                                     CW_USEDEFAULT, CW_USEDEFAULT,
551                                     CW_USEDEFAULT, CW_USEDEFAULT,
552                                     0,
553                                     0,
554                                     0,
555                                     (LPVOID)&trackerInfo);
556
557   if (hwndTrackWindow!=0)
558   {
559     /*
560      * Capture the mouse input
561      */
562     SetCapture(hwndTrackWindow);
563
564     /*
565      * Pump messages. All mouse input should go the the capture window.
566      */
567     while (!trackerInfo.trackingDone && GetMessageA(&msg, 0, 0, 0) )
568     {
569       if ( (msg.message >= WM_KEYFIRST) &&
570            (msg.message <= WM_KEYLAST) )
571       {
572         /*
573          * When keyboard messages are sent to windows on this thread, we
574          * want to ignore notify the drop source that the state changed.
575          * in the case of the Escape key, we also notify the drop source
576          * we give it a special meaning.
577          */
578         if ( (msg.message==WM_KEYDOWN) &&
579              (msg.wParam==VK_ESCAPE) )
580         {
581           trackerInfo.escPressed = TRUE;
582         }
583
584         /*
585          * Notify the drop source.
586          */
587         OLEDD_TrackStateChange(&trackerInfo,
588                                msg.pt,
589                                OLEDD_GetButtonState());
590       }
591       else
592       {
593         /*
594          * Dispatch the messages only when it's not a keyboard message.
595          */
596         DispatchMessageA(&msg);
597       }
598     }
599
600     /*
601      * Destroy the temporary window.
602      */
603     DestroyWindow(hwndTrackWindow);
604
605     return trackerInfo.returnValue;
606   }
607
608   return E_FAIL;
609 }
610
611 /***********************************************************************
612  * OleQueryLinkFromData [OLE32.118]
613  */
614 HRESULT WINAPI OleQueryLinkFromData(
615   IDataObject* pSrcDataObject)
616 {
617   FIXME("(%p),stub!\n", pSrcDataObject);
618   return S_OK;
619 }
620
621 /***********************************************************************
622  * OleRegGetMiscStatus [OLE32.121]
623  */
624 HRESULT WINAPI OleRegGetMiscStatus(
625   REFCLSID clsid,
626   DWORD    dwAspect,
627   DWORD*   pdwStatus)
628 {
629   char    keyName[60];
630   HKEY    clsidKey;
631   HKEY    miscStatusKey;
632   HKEY    aspectKey;
633   LONG    result;
634
635   /*
636    * Initialize the out parameter.
637    */
638   *pdwStatus = 0;
639
640   /*
641    * Build the key name we're looking for
642    */
643   sprintf( keyName, "CLSID\\{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\",
644            clsid->Data1, clsid->Data2, clsid->Data3,
645            clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
646            clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );
647
648   TRACE("(%s, %ld, %p)\n", keyName, dwAspect, pdwStatus);
649
650   /*
651    * Open the class id Key
652    */
653   result = RegOpenKeyA(HKEY_CLASSES_ROOT,
654                        keyName,
655                        &clsidKey);
656
657   if (result != ERROR_SUCCESS)
658     return REGDB_E_CLASSNOTREG;
659
660   /*
661    * Get the MiscStatus
662    */
663   result = RegOpenKeyA(clsidKey,
664                        "MiscStatus",
665                        &miscStatusKey);
666
667
668   if (result != ERROR_SUCCESS)
669   {
670     RegCloseKey(clsidKey);
671     return REGDB_E_READREGDB;
672   }
673
674   /*
675    * Read the default value
676    */
677   OLEUTL_ReadRegistryDWORDValue(miscStatusKey, pdwStatus);
678
679   /*
680    * Open the key specific to the requested aspect.
681    */
682   sprintf(keyName, "%ld", dwAspect);
683
684   result = RegOpenKeyA(miscStatusKey,
685                        keyName,
686                        &aspectKey);
687
688   if (result == ERROR_SUCCESS)
689   {
690     OLEUTL_ReadRegistryDWORDValue(aspectKey, pdwStatus);
691     RegCloseKey(aspectKey);
692   }
693
694   /*
695    * Cleanup
696    */
697   RegCloseKey(miscStatusKey);
698   RegCloseKey(clsidKey);
699
700   return S_OK;
701 }
702
703 /******************************************************************************
704  *              OleSetContainedObject        [OLE32.128]
705  */
706 HRESULT WINAPI OleSetContainedObject(
707   LPUNKNOWN pUnknown,
708   BOOL      fContained)
709 {
710   IRunnableObject* runnable = NULL;
711   HRESULT          hres;
712
713   TRACE("(%p,%x), stub!\n", pUnknown, fContained);
714
715   hres = IUnknown_QueryInterface(pUnknown,
716                                  &IID_IRunnableObject,
717                                  (void**)&runnable);
718
719   if (SUCCEEDED(hres))
720   {
721     hres = IRunnableObject_SetContainedObject(runnable, fContained);
722
723     IRunnableObject_Release(runnable);
724
725     return hres;
726   }
727
728   return S_OK;
729 }
730
731 /******************************************************************************
732  *              OleLoad        [OLE32.112]
733  */
734 HRESULT WINAPI OleLoad(
735   LPSTORAGE       pStg,
736   REFIID          riid,
737   LPOLECLIENTSITE pClientSite,
738   LPVOID*         ppvObj)
739 {
740   IPersistStorage* persistStorage = NULL;
741   IOleObject*      oleObject      = NULL;
742   STATSTG          storageInfo;
743   HRESULT          hres;
744
745   TRACE("(%p,%p,%p,%p)\n", pStg, riid, pClientSite, ppvObj);
746
747   /*
748    * TODO, Conversion ... OleDoAutoConvert
749    */
750
751   /*
752    * Get the class ID for the object.
753    */
754   hres = IStorage_Stat(pStg, &storageInfo, STATFLAG_NONAME);
755
756   /*
757    * Now, try and create the handler for the object
758    */
759   hres = CoCreateInstance(&storageInfo.clsid,
760                           NULL,
761                           CLSCTX_INPROC_HANDLER,
762                           &IID_IOleObject,
763                           (void**)&oleObject);
764
765   /*
766    * If that fails, as it will most times, load the default
767    * OLE handler.
768    */
769   if (FAILED(hres))
770   {
771     hres = OleCreateDefaultHandler(&storageInfo.clsid,
772                                    NULL,
773                                    &IID_IOleObject,
774                                    (void**)&oleObject);
775   }
776
777   /*
778    * If we couldn't find a handler... this is bad. Abort the whole thing.
779    */
780   if (FAILED(hres))
781     return hres;
782
783   /*
784    * Inform the new object of it's client site.
785    */
786   hres = IOleObject_SetClientSite(oleObject, pClientSite);
787
788   /*
789    * Initialize the object with it's IPersistStorage interface.
790    */
791   hres = IOleObject_QueryInterface(oleObject,
792                                    &IID_IPersistStorage,
793                                    (void**)&persistStorage);
794
795   if (SUCCEEDED(hres))
796   {
797     IPersistStorage_Load(persistStorage, pStg);
798
799     IPersistStorage_Release(persistStorage);
800     persistStorage = NULL;
801   }
802
803   /*
804    * Return the requested interface to the caller.
805    */
806   hres = IOleObject_QueryInterface(oleObject, riid, ppvObj);
807
808   /*
809    * Cleanup interfaces used internally
810    */
811   IOleObject_Release(oleObject);
812
813   return hres;
814 }
815
816 /***********************************************************************
817  *           OleSave     [OLE32.124]
818  */
819 HRESULT WINAPI OleSave(
820   LPPERSISTSTORAGE pPS,
821   LPSTORAGE        pStg,
822   BOOL             fSameAsLoad)
823 {
824   HRESULT hres;
825   CLSID   objectClass;
826
827   TRACE("(%p,%p,%x)\n", pPS, pStg, fSameAsLoad);
828
829   /*
830    * First, we transfer the class ID (if available)
831    */
832   hres = IPersistStorage_GetClassID(pPS, &objectClass);
833
834   if (SUCCEEDED(hres))
835   {
836     WriteClassStg(pStg, &objectClass);
837   }
838
839   /*
840    * Then, we ask the object to save itself to the
841    * storage. If it is successful, we commit the storage.
842    */
843   hres = IPersistStorage_Save(pPS, pStg, fSameAsLoad);
844
845   if (SUCCEEDED(hres))
846   {
847     IStorage_Commit(pStg,
848                     STGC_DEFAULT);
849   }
850
851   return hres;
852 }
853
854
855 /******************************************************************************
856  *              OleLockRunning        [OLE32.114]
857  */
858 HRESULT WINAPI OleLockRunning(LPUNKNOWN pUnknown, BOOL fLock, BOOL fLastUnlockCloses)
859 {
860   IRunnableObject* runnable = NULL;
861   HRESULT          hres;
862
863   TRACE("(%p,%x,%x)\n", pUnknown, fLock, fLastUnlockCloses);
864
865   hres = IUnknown_QueryInterface(pUnknown,
866                                  &IID_IRunnableObject,
867                                  (void**)&runnable);
868
869   if (SUCCEEDED(hres))
870   {
871     hres = IRunnableObject_LockRunning(runnable, fLock, fLastUnlockCloses);
872
873     IRunnableObject_Release(runnable);
874
875     return hres;
876   }
877   else
878     return E_INVALIDARG;
879 }
880
881
882 /**************************************************************************
883  * Internal methods to manage the shared OLE menu in response to the
884  * OLE***MenuDescriptor API
885  */
886
887 /***
888  * OLEMenu_Initialize()
889  *
890  * Initializes the OLEMENU data structures.
891  */
892 static void OLEMenu_Initialize()
893 {
894 }
895
896 /***
897  * OLEMenu_UnInitialize()
898  *
899  * Releases the OLEMENU data structures.
900  */
901 static void OLEMenu_UnInitialize()
902 {
903 }
904
905 /*************************************************************************
906  * OLEMenu_InstallHooks
907  * Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
908  *
909  * RETURNS: TRUE if message hooks were succesfully installed
910  *          FALSE on failure
911  */
912 BOOL OLEMenu_InstallHooks( DWORD tid )
913 {
914   OleMenuHookItem *pHookItem = NULL;
915
916   /* Create an entry for the hook table */
917   if ( !(pHookItem = HeapAlloc(GetProcessHeap(), 0,
918                                sizeof(OleMenuHookItem)) ) )
919     return FALSE;
920
921   pHookItem->tid = tid;
922   pHookItem->hHeap = GetProcessHeap();
923
924   /* Install a thread scope message hook for WH_GETMESSAGE */
925   pHookItem->GetMsg_hHook = SetWindowsHookExA( WH_GETMESSAGE, OLEMenu_GetMsgProc,
926                                                0, GetCurrentThreadId() );
927   if ( !pHookItem->GetMsg_hHook )
928     goto CLEANUP;
929
930   /* Install a thread scope message hook for WH_CALLWNDPROC */
931   pHookItem->CallWndProc_hHook = SetWindowsHookExA( WH_CALLWNDPROC, OLEMenu_CallWndProc,
932                                                     0, GetCurrentThreadId() );
933   if ( !pHookItem->CallWndProc_hHook )
934     goto CLEANUP;
935
936   /* Insert the hook table entry */
937   pHookItem->next = hook_list;
938   hook_list = pHookItem;
939
940   return TRUE;
941
942 CLEANUP:
943   /* Unhook any hooks */
944   if ( pHookItem->GetMsg_hHook )
945     UnhookWindowsHookEx( pHookItem->GetMsg_hHook );
946   if ( pHookItem->CallWndProc_hHook )
947     UnhookWindowsHookEx( pHookItem->CallWndProc_hHook );
948   /* Release the hook table entry */
949   HeapFree(pHookItem->hHeap, 0, pHookItem );
950
951   return FALSE;
952 }
953
954 /*************************************************************************
955  * OLEMenu_UnInstallHooks
956  * UnInstall thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
957  *
958  * RETURNS: TRUE if message hooks were succesfully installed
959  *          FALSE on failure
960  */
961 BOOL OLEMenu_UnInstallHooks( DWORD tid )
962 {
963   OleMenuHookItem *pHookItem = NULL;
964   OleMenuHookItem **ppHook = &hook_list;
965
966   while (*ppHook)
967   {
968       if ((*ppHook)->tid == tid)
969       {
970           pHookItem = *ppHook;
971           *ppHook = pHookItem->next;
972           break;
973       }
974       ppHook = &(*ppHook)->next;
975   }
976   if (!pHookItem) return FALSE;
977
978   /* Uninstall the hooks installed for this thread */
979   if ( !UnhookWindowsHookEx( pHookItem->GetMsg_hHook ) )
980     goto CLEANUP;
981   if ( !UnhookWindowsHookEx( pHookItem->CallWndProc_hHook ) )
982     goto CLEANUP;
983
984   /* Release the hook table entry */
985   HeapFree(pHookItem->hHeap, 0, pHookItem );
986
987   return TRUE;
988
989 CLEANUP:
990   /* Release the hook table entry */
991   if (pHookItem)
992     HeapFree(pHookItem->hHeap, 0, pHookItem );
993
994   return FALSE;
995 }
996
997 /*************************************************************************
998  * OLEMenu_IsHookInstalled
999  * Tests if OLEMenu hooks have been installed for a thread
1000  *
1001  * RETURNS: The pointer and index of the hook table entry for the tid
1002  *          NULL and -1 for the index if no hooks were installed for this thread
1003  */
1004 OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid )
1005 {
1006   OleMenuHookItem *pHookItem = NULL;
1007
1008   /* Do a simple linear search for an entry whose tid matches ours.
1009    * We really need a map but efficiency is not a concern here. */
1010   for (pHookItem = hook_list; pHookItem; pHookItem = pHookItem->next)
1011   {
1012     if ( tid == pHookItem->tid )
1013       return pHookItem;
1014   }
1015
1016   return NULL;
1017 }
1018
1019 /***********************************************************************
1020  *           OLEMenu_FindMainMenuIndex
1021  *
1022  * Used by OLEMenu API to find the top level group a menu item belongs to.
1023  * On success pnPos contains the index of the item in the top level menu group
1024  *
1025  * RETURNS: TRUE if the ID was found, FALSE on failure
1026  */
1027 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos )
1028 {
1029   UINT i, nItems;
1030
1031   nItems = GetMenuItemCount( hMainMenu );
1032
1033   for (i = 0; i < nItems; i++)
1034   {
1035     HMENU hsubmenu;
1036
1037     /*  Is the current item a submenu? */
1038     if ( (hsubmenu = GetSubMenu(hMainMenu, i)) )
1039     {
1040       /* If the handle is the same we're done */
1041       if ( hsubmenu == hPopupMenu )
1042       {
1043         if (pnPos)
1044           *pnPos = i;
1045         return TRUE;
1046       }
1047       /* Recursively search without updating pnPos */
1048       else if ( OLEMenu_FindMainMenuIndex( hsubmenu, hPopupMenu, NULL ) )
1049       {
1050         if (pnPos)
1051           *pnPos = i;
1052         return TRUE;
1053       }
1054     }
1055   }
1056
1057   return FALSE;
1058 }
1059
1060 /***********************************************************************
1061  *           OLEMenu_SetIsServerMenu
1062  *
1063  * Checks whether a popup menu belongs to a shared menu group which is
1064  * owned by the server, and sets the menu descriptor state accordingly.
1065  * All menu messages from these groups should be routed to the server.
1066  *
1067  * RETURNS: TRUE if the popup menu is part of a server owned group
1068  *          FASE if the popup menu is part of a container owned group
1069  */
1070 BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor )
1071 {
1072   UINT nPos = 0, nWidth, i;
1073
1074   pOleMenuDescriptor->bIsServerItem = FALSE;
1075
1076   /* Don't bother searching if the popup is the combined menu itself */
1077   if ( hmenu == pOleMenuDescriptor->hmenuCombined )
1078     return FALSE;
1079
1080   /* Find the menu item index in the shared OLE menu that this item belongs to */
1081   if ( !OLEMenu_FindMainMenuIndex( pOleMenuDescriptor->hmenuCombined, hmenu,  &nPos ) )
1082     return FALSE;
1083
1084   /* The group widths array has counts for the number of elements
1085    * in the groups File, Edit, Container, Object, Window, Help.
1086    * The Edit, Object & Help groups belong to the server object
1087    * and the other three belong to the container.
1088    * Loop through the group widths and locate the group we are a member of.
1089    */
1090   for ( i = 0, nWidth = 0; i < 6; i++ )
1091   {
1092     nWidth += pOleMenuDescriptor->mgw.width[i];
1093     if ( nPos < nWidth )
1094     {
1095       /* Odd elements are server menu widths */
1096       pOleMenuDescriptor->bIsServerItem = (i%2) ? TRUE : FALSE;
1097       break;
1098     }
1099   }
1100
1101   return pOleMenuDescriptor->bIsServerItem;
1102 }
1103
1104 /*************************************************************************
1105  * OLEMenu_CallWndProc
1106  * Thread scope WH_CALLWNDPROC hook proc filter function (callback)
1107  * This is invoked from a message hook installed in OleSetMenuDescriptor.
1108  */
1109 LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam)
1110 {
1111   LPCWPSTRUCT pMsg = NULL;
1112   HOLEMENU hOleMenu = 0;
1113   OleMenuDescriptor *pOleMenuDescriptor = NULL;
1114   OleMenuHookItem *pHookItem = NULL;
1115   WORD fuFlags;
1116
1117   TRACE("%i, %04x, %08x\n", code, wParam, (unsigned)lParam );
1118
1119   /* Check if we're being asked to process the message */
1120   if ( HC_ACTION != code )
1121     goto NEXTHOOK;
1122
1123   /* Retrieve the current message being dispatched from lParam */
1124   pMsg = (LPCWPSTRUCT)lParam;
1125
1126   /* Check if the message is destined for a window we are interested in:
1127    * If the window has an OLEMenu property we may need to dispatch
1128    * the menu message to its active objects window instead. */
1129
1130   hOleMenu = (HOLEMENU)GetPropA( pMsg->hwnd, "PROP_OLEMenuDescriptor" );
1131   if ( !hOleMenu )
1132     goto NEXTHOOK;
1133
1134   /* Get the menu descriptor */
1135   pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1136   if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1137     goto NEXTHOOK;
1138
1139   /* Process menu messages */
1140   switch( pMsg->message )
1141   {
1142     case WM_INITMENU:
1143     {
1144       /* Reset the menu descriptor state */
1145       pOleMenuDescriptor->bIsServerItem = FALSE;
1146
1147       /* Send this message to the server as well */
1148       SendMessageA( pOleMenuDescriptor->hwndActiveObject,
1149                   pMsg->message, pMsg->wParam, pMsg->lParam );
1150       goto NEXTHOOK;
1151     }
1152
1153     case WM_INITMENUPOPUP:
1154     {
1155       /* Save the state for whether this is a server owned menu */
1156       OLEMenu_SetIsServerMenu( (HMENU)pMsg->wParam, pOleMenuDescriptor );
1157       break;
1158     }
1159
1160     case WM_MENUSELECT:
1161     {
1162       fuFlags = HIWORD(pMsg->wParam);  /* Get flags */
1163       if ( fuFlags & MF_SYSMENU )
1164          goto NEXTHOOK;
1165
1166       /* Save the state for whether this is a server owned popup menu */
1167       else if ( fuFlags & MF_POPUP )
1168         OLEMenu_SetIsServerMenu( (HMENU)pMsg->lParam, pOleMenuDescriptor );
1169
1170       break;
1171     }
1172
1173     case WM_DRAWITEM:
1174     {
1175       LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT) pMsg->lParam;
1176       if ( pMsg->wParam != 0 || lpdis->CtlType != ODT_MENU )
1177         goto NEXTHOOK;  /* Not a menu message */
1178
1179       break;
1180     }
1181
1182     default:
1183       goto NEXTHOOK;
1184   }
1185
1186   /* If the message was for the server dispatch it accordingly */
1187   if ( pOleMenuDescriptor->bIsServerItem )
1188   {
1189     SendMessageA( pOleMenuDescriptor->hwndActiveObject,
1190                   pMsg->message, pMsg->wParam, pMsg->lParam );
1191   }
1192
1193 NEXTHOOK:
1194   if ( pOleMenuDescriptor )
1195     GlobalUnlock( hOleMenu );
1196
1197   /* Lookup the hook item for the current thread */
1198   if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1199   {
1200     /* This should never fail!! */
1201     WARN("could not retrieve hHook for current thread!\n" );
1202     return 0;
1203   }
1204
1205   /* Pass on the message to the next hooker */
1206   return CallNextHookEx( pHookItem->CallWndProc_hHook, code, wParam, lParam );
1207 }
1208
1209 /*************************************************************************
1210  * OLEMenu_GetMsgProc
1211  * Thread scope WH_GETMESSAGE hook proc filter function (callback)
1212  * This is invoked from a message hook installed in OleSetMenuDescriptor.
1213  */
1214 LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam)
1215 {
1216   LPMSG pMsg = NULL;
1217   HOLEMENU hOleMenu = 0;
1218   OleMenuDescriptor *pOleMenuDescriptor = NULL;
1219   OleMenuHookItem *pHookItem = NULL;
1220   WORD wCode;
1221
1222   TRACE("%i, %04x, %08x\n", code, wParam, (unsigned)lParam );
1223
1224   /* Check if we're being asked to process a  messages */
1225   if ( HC_ACTION != code )
1226     goto NEXTHOOK;
1227
1228   /* Retrieve the current message being dispatched from lParam */
1229   pMsg = (LPMSG)lParam;
1230
1231   /* Check if the message is destined for a window we are interested in:
1232    * If the window has an OLEMenu property we may need to dispatch
1233    * the menu message to its active objects window instead. */
1234
1235   hOleMenu = (HOLEMENU)GetPropA( pMsg->hwnd, "PROP_OLEMenuDescriptor" );
1236   if ( !hOleMenu )
1237     goto NEXTHOOK;
1238
1239   /* Process menu messages */
1240   switch( pMsg->message )
1241   {
1242     case WM_COMMAND:
1243     {
1244       wCode = HIWORD(pMsg->wParam);  /* Get notification code */
1245       if ( wCode )
1246         goto NEXTHOOK;  /* Not a menu message */
1247       break;
1248     }
1249     default:
1250       goto NEXTHOOK;
1251   }
1252
1253   /* Get the menu descriptor */
1254   pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1255   if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1256     goto NEXTHOOK;
1257
1258   /* If the message was for the server dispatch it accordingly */
1259   if ( pOleMenuDescriptor->bIsServerItem )
1260   {
1261     /* Change the hWnd in the message to the active objects hWnd.
1262      * The message loop which reads this message will automatically
1263      * dispatch it to the embedded objects window. */
1264     pMsg->hwnd = pOleMenuDescriptor->hwndActiveObject;
1265   }
1266
1267 NEXTHOOK:
1268   if ( pOleMenuDescriptor )
1269     GlobalUnlock( hOleMenu );
1270
1271   /* Lookup the hook item for the current thread */
1272   if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1273   {
1274     /* This should never fail!! */
1275     WARN("could not retrieve hHook for current thread!\n" );
1276     return FALSE;
1277   }
1278
1279   /* Pass on the message to the next hooker */
1280   return CallNextHookEx( pHookItem->GetMsg_hHook, code, wParam, lParam );
1281 }
1282
1283 /***********************************************************************
1284  * OleCreateMenuDescriptor [OLE32.97]
1285  * Creates an OLE menu descriptor for OLE to use when dispatching
1286  * menu messages and commands.
1287  *
1288  * PARAMS:
1289  *    hmenuCombined  -  Handle to the objects combined menu
1290  *    lpMenuWidths   -  Pointer to array of 6 LONG's indicating menus per group
1291  *
1292  */
1293 HOLEMENU WINAPI OleCreateMenuDescriptor(
1294   HMENU                hmenuCombined,
1295   LPOLEMENUGROUPWIDTHS lpMenuWidths)
1296 {
1297   HOLEMENU hOleMenu;
1298   OleMenuDescriptor *pOleMenuDescriptor;
1299   int i;
1300
1301   if ( !hmenuCombined || !lpMenuWidths )
1302     return 0;
1303
1304   /* Create an OLE menu descriptor */
1305   if ( !(hOleMenu = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT,
1306                                 sizeof(OleMenuDescriptor) ) ) )
1307   return 0;
1308
1309   pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1310   if ( !pOleMenuDescriptor )
1311     return 0;
1312
1313   /* Initialize menu group widths and hmenu */
1314   for ( i = 0; i < 6; i++ )
1315     pOleMenuDescriptor->mgw.width[i] = lpMenuWidths->width[i];
1316
1317   pOleMenuDescriptor->hmenuCombined = hmenuCombined;
1318   pOleMenuDescriptor->bIsServerItem = FALSE;
1319   GlobalUnlock( hOleMenu );
1320
1321   return hOleMenu;
1322 }
1323
1324 /***********************************************************************
1325  * OleDestroyMenuDescriptor [OLE32.99]
1326  * Destroy the shared menu descriptor
1327  */
1328 HRESULT WINAPI OleDestroyMenuDescriptor(
1329   HOLEMENU hmenuDescriptor)
1330 {
1331   if ( hmenuDescriptor )
1332     GlobalFree( hmenuDescriptor );
1333         return S_OK;
1334 }
1335
1336 /***********************************************************************
1337  * OleSetMenuDescriptor [OLE32.129]
1338  * Installs or removes OLE dispatching code for the containers frame window
1339  * FIXME: The lpFrame and lpActiveObject parameters are currently ignored
1340  * OLE should install context sensitive help F1 filtering for the app when
1341  * these are non null.
1342  *
1343  * PARAMS:
1344  *     hOleMenu         Handle to composite menu descriptor
1345  *     hwndFrame        Handle to containers frame window
1346  *     hwndActiveObject Handle to objects in-place activation window
1347  *     lpFrame          Pointer to IOleInPlaceFrame on containers window
1348  *     lpActiveObject   Pointer to IOleInPlaceActiveObject on active in-place object
1349  *
1350  * RETURNS:
1351  *      S_OK                               - menu installed correctly
1352  *      E_FAIL, E_INVALIDARG, E_UNEXPECTED - failure
1353  */
1354 HRESULT WINAPI OleSetMenuDescriptor(
1355   HOLEMENU               hOleMenu,
1356   HWND                   hwndFrame,
1357   HWND                   hwndActiveObject,
1358   LPOLEINPLACEFRAME        lpFrame,
1359   LPOLEINPLACEACTIVEOBJECT lpActiveObject)
1360 {
1361   OleMenuDescriptor *pOleMenuDescriptor = NULL;
1362
1363   /* Check args */
1364   if ( !hwndFrame || (hOleMenu && !hwndActiveObject) )
1365     return E_INVALIDARG;
1366
1367   if ( lpFrame || lpActiveObject )
1368   {
1369      FIXME("(%x, %x, %x, %p, %p), Context sensitive help filtering not implemented!\n",
1370         (unsigned int)hOleMenu,
1371         hwndFrame,
1372         hwndActiveObject,
1373         lpFrame,
1374         lpActiveObject);
1375   }
1376
1377   /* Set up a message hook to intercept the containers frame window messages.
1378    * The message filter is responsible for dispatching menu messages from the
1379    * shared menu which are intended for the object.
1380    */
1381
1382   if ( hOleMenu )  /* Want to install dispatching code */
1383   {
1384     /* If OLEMenu hooks are already installed for this thread, fail
1385      * Note: This effectively means that OleSetMenuDescriptor cannot
1386      * be called twice in succession on the same frame window
1387      * without first calling it with a null hOleMenu to uninstall */
1388     if ( OLEMenu_IsHookInstalled( GetCurrentThreadId() ) )
1389   return E_FAIL;
1390
1391     /* Get the menu descriptor */
1392     pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1393     if ( !pOleMenuDescriptor )
1394       return E_UNEXPECTED;
1395
1396     /* Update the menu descriptor */
1397     pOleMenuDescriptor->hwndFrame = hwndFrame;
1398     pOleMenuDescriptor->hwndActiveObject = hwndActiveObject;
1399
1400     GlobalUnlock( hOleMenu );
1401     pOleMenuDescriptor = NULL;
1402
1403     /* Add a menu descriptor windows property to the frame window */
1404     SetPropA( hwndFrame, "PROP_OLEMenuDescriptor", hOleMenu );
1405
1406     /* Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC */
1407     if ( !OLEMenu_InstallHooks( GetCurrentThreadId() ) )
1408       return E_FAIL;
1409   }
1410   else  /* Want to uninstall dispatching code */
1411   {
1412     /* Uninstall the hooks */
1413     if ( !OLEMenu_UnInstallHooks( GetCurrentThreadId() ) )
1414       return E_FAIL;
1415
1416     /* Remove the menu descriptor property from the frame window */
1417     RemovePropA( hwndFrame, "PROP_OLEMenuDescriptor" );
1418   }
1419
1420   return S_OK;
1421 }
1422
1423 /******************************************************************************
1424  *              IsAccelerator        [OLE32.75]
1425  * Mostly copied from controls/menu.c TranslateAccelerator implementation
1426  */
1427 BOOL WINAPI IsAccelerator(HACCEL hAccel, int cAccelEntries, LPMSG lpMsg, WORD* lpwCmd)
1428 {
1429     /* YES, Accel16! */
1430     LPACCEL16 lpAccelTbl;
1431     int i;
1432
1433     if(!lpMsg) return FALSE;
1434     if (!hAccel || !(lpAccelTbl = (LPACCEL16)LockResource16(hAccel)))
1435     {
1436         WARN_(accel)("invalid accel handle=%04x\n", hAccel);
1437         return FALSE;
1438     }
1439     if((lpMsg->message != WM_KEYDOWN &&
1440         lpMsg->message != WM_KEYUP &&
1441         lpMsg->message != WM_SYSKEYDOWN &&
1442         lpMsg->message != WM_SYSKEYUP &&
1443         lpMsg->message != WM_CHAR)) return FALSE;
1444
1445     TRACE_(accel)("hAccel=%04x, cAccelEntries=%d,"
1446                 "msg->hwnd=%04x, msg->message=%04x, wParam=%08x, lParam=%08lx\n",
1447                 hAccel, cAccelEntries,
1448                 lpMsg->hwnd, lpMsg->message, lpMsg->wParam, lpMsg->lParam);
1449     for(i = 0; i < cAccelEntries; i++)
1450     {
1451         if(lpAccelTbl[i].key != lpMsg->wParam)
1452             continue;
1453
1454         if(lpMsg->message == WM_CHAR)
1455         {
1456             if(!(lpAccelTbl[i].fVirt & FALT) && !(lpAccelTbl[i].fVirt & FVIRTKEY))
1457             {
1458                 TRACE_(accel)("found accel for WM_CHAR: ('%c')\n", lpMsg->wParam & 0xff);
1459                 goto found;
1460             }
1461         }
1462         else
1463         {
1464             if(lpAccelTbl[i].fVirt & FVIRTKEY)
1465             {
1466                 INT mask = 0;
1467                 TRACE_(accel)("found accel for virt_key %04x (scan %04x)\n",
1468                                 lpMsg->wParam, HIWORD(lpMsg->lParam) & 0xff);
1469                 if(GetKeyState(VK_SHIFT) & 0x8000) mask |= FSHIFT;
1470                 if(GetKeyState(VK_CONTROL) & 0x8000) mask |= FCONTROL;
1471                 if(GetKeyState(VK_MENU) & 0x8000) mask |= FALT;
1472                 if(mask == (lpAccelTbl[i].fVirt & (FSHIFT | FCONTROL | FALT))) goto found;
1473                 TRACE_(accel)("incorrect SHIFT/CTRL/ALT-state\n");
1474             }
1475             else
1476             {
1477                 if(!(lpMsg->lParam & 0x01000000))  /* no special_key */
1478                 {
1479                     if((lpAccelTbl[i].fVirt & FALT) && (lpMsg->lParam & 0x20000000))
1480                     {                                                  /* ^^ ALT pressed */
1481                         TRACE_(accel)("found accel for Alt-%c\n", lpMsg->wParam & 0xff);
1482                         goto found;
1483                     }
1484                 }
1485             }
1486         }
1487     }
1488
1489     WARN_(accel)("couldn't translate accelerator key\n");
1490     return FALSE;
1491
1492 found:
1493     if(lpwCmd) *lpwCmd = lpAccelTbl[i].cmd;
1494     return TRUE;
1495 }
1496
1497 /***********************************************************************
1498  * ReleaseStgMedium [OLE32.140]
1499  */
1500 void WINAPI ReleaseStgMedium(
1501   STGMEDIUM* pmedium)
1502 {
1503   switch (pmedium->tymed)
1504   {
1505     case TYMED_HGLOBAL:
1506     {
1507       if ( (pmedium->pUnkForRelease==0) &&
1508            (pmedium->u.hGlobal!=0) )
1509         GlobalFree(pmedium->u.hGlobal);
1510
1511       pmedium->u.hGlobal = 0;
1512       break;
1513     }
1514     case TYMED_FILE:
1515     {
1516       if (pmedium->u.lpszFileName!=0)
1517       {
1518         if (pmedium->pUnkForRelease==0)
1519         {
1520           DeleteFileW(pmedium->u.lpszFileName);
1521         }
1522
1523         CoTaskMemFree(pmedium->u.lpszFileName);
1524       }
1525
1526       pmedium->u.lpszFileName = 0;
1527       break;
1528     }
1529     case TYMED_ISTREAM:
1530     {
1531       if (pmedium->u.pstm!=0)
1532       {
1533         IStream_Release(pmedium->u.pstm);
1534       }
1535
1536       pmedium->u.pstm = 0;
1537       break;
1538     }
1539     case TYMED_ISTORAGE:
1540     {
1541       if (pmedium->u.pstg!=0)
1542       {
1543         IStorage_Release(pmedium->u.pstg);
1544       }
1545
1546       pmedium->u.pstg = 0;
1547       break;
1548     }
1549     case TYMED_GDI:
1550     {
1551       if ( (pmedium->pUnkForRelease==0) &&
1552            (pmedium->u.hGlobal!=0) )
1553         DeleteObject(pmedium->u.hGlobal);
1554
1555       pmedium->u.hGlobal = 0;
1556       break;
1557     }
1558     case TYMED_MFPICT:
1559     {
1560       if ( (pmedium->pUnkForRelease==0) &&
1561            (pmedium->u.hMetaFilePict!=0) )
1562       {
1563         LPMETAFILEPICT pMP = GlobalLock(pmedium->u.hGlobal);
1564         DeleteMetaFile(pMP->hMF);
1565         GlobalUnlock(pmedium->u.hGlobal);
1566         GlobalFree(pmedium->u.hGlobal);
1567       }
1568
1569       pmedium->u.hMetaFilePict = 0;
1570       break;
1571     }
1572     case TYMED_ENHMF:
1573     {
1574       if ( (pmedium->pUnkForRelease==0) &&
1575            (pmedium->u.hEnhMetaFile!=0) )
1576       {
1577         DeleteEnhMetaFile(pmedium->u.hEnhMetaFile);
1578       }
1579
1580       pmedium->u.hEnhMetaFile = 0;
1581       break;
1582     }
1583     case TYMED_NULL:
1584     default:
1585       break;
1586   }
1587
1588   /*
1589    * After cleaning up, the unknown is released
1590    */
1591   if (pmedium->pUnkForRelease!=0)
1592   {
1593     IUnknown_Release(pmedium->pUnkForRelease);
1594     pmedium->pUnkForRelease = 0;
1595   }
1596 }
1597
1598 /***
1599  * OLEDD_Initialize()
1600  *
1601  * Initializes the OLE drag and drop data structures.
1602  */
1603 static void OLEDD_Initialize()
1604 {
1605     WNDCLASSA wndClass;
1606
1607     ZeroMemory (&wndClass, sizeof(WNDCLASSA));
1608     wndClass.style         = CS_GLOBALCLASS;
1609     wndClass.lpfnWndProc   = (WNDPROC)OLEDD_DragTrackerWindowProc;
1610     wndClass.cbClsExtra    = 0;
1611     wndClass.cbWndExtra    = sizeof(TrackerWindowInfo*);
1612     wndClass.hCursor       = 0;
1613     wndClass.hbrBackground = 0;
1614     wndClass.lpszClassName = OLEDD_DRAGTRACKERCLASS;
1615
1616     RegisterClassA (&wndClass);
1617 }
1618
1619 /***
1620  * OLEDD_UnInitialize()
1621  *
1622  * Releases the OLE drag and drop data structures.
1623  */
1624 static void OLEDD_UnInitialize()
1625 {
1626   /*
1627    * Simply empty the list.
1628    */
1629   while (targetListHead!=NULL)
1630   {
1631     RevokeDragDrop(targetListHead->hwndTarget);
1632   }
1633 }
1634
1635 /***
1636  * OLEDD_InsertDropTarget()
1637  *
1638  * Insert the target node in the tree.
1639  */
1640 static void OLEDD_InsertDropTarget(DropTargetNode* nodeToAdd)
1641 {
1642   DropTargetNode*  curNode;
1643   DropTargetNode** parentNodeLink;
1644
1645   /*
1646    * Iterate the tree to find the insertion point.
1647    */
1648   curNode        = targetListHead;
1649   parentNodeLink = &targetListHead;
1650
1651   while (curNode!=NULL)
1652   {
1653     if (nodeToAdd->hwndTarget<curNode->hwndTarget)
1654     {
1655       /*
1656        * If the node we want to add has a smaller HWND, go left
1657        */
1658       parentNodeLink = &curNode->prevDropTarget;
1659       curNode        =  curNode->prevDropTarget;
1660     }
1661     else if (nodeToAdd->hwndTarget>curNode->hwndTarget)
1662     {
1663       /*
1664        * If the node we want to add has a larger HWND, go right
1665        */
1666       parentNodeLink = &curNode->nextDropTarget;
1667       curNode        =  curNode->nextDropTarget;
1668     }
1669     else
1670     {
1671       /*
1672        * The item was found in the list. It shouldn't have been there
1673        */
1674       assert(FALSE);
1675       return;
1676     }
1677   }
1678
1679   /*
1680    * If we get here, we have found a spot for our item. The parentNodeLink
1681    * pointer points to the pointer that we have to modify.
1682    * The curNode should be NULL. We just have to establish the link and Voila!
1683    */
1684   assert(curNode==NULL);
1685   assert(parentNodeLink!=NULL);
1686   assert(*parentNodeLink==NULL);
1687
1688   *parentNodeLink=nodeToAdd;
1689 }
1690
1691 /***
1692  * OLEDD_ExtractDropTarget()
1693  *
1694  * Removes the target node from the tree.
1695  */
1696 static DropTargetNode* OLEDD_ExtractDropTarget(HWND hwndOfTarget)
1697 {
1698   DropTargetNode*  curNode;
1699   DropTargetNode** parentNodeLink;
1700
1701   /*
1702    * Iterate the tree to find the insertion point.
1703    */
1704   curNode        = targetListHead;
1705   parentNodeLink = &targetListHead;
1706
1707   while (curNode!=NULL)
1708   {
1709     if (hwndOfTarget<curNode->hwndTarget)
1710     {
1711       /*
1712        * If the node we want to add has a smaller HWND, go left
1713        */
1714       parentNodeLink = &curNode->prevDropTarget;
1715       curNode        =  curNode->prevDropTarget;
1716     }
1717     else if (hwndOfTarget>curNode->hwndTarget)
1718     {
1719       /*
1720        * If the node we want to add has a larger HWND, go right
1721        */
1722       parentNodeLink = &curNode->nextDropTarget;
1723       curNode        =  curNode->nextDropTarget;
1724     }
1725     else
1726     {
1727       /*
1728        * The item was found in the list. Detach it from it's parent and
1729        * re-insert it's kids in the tree.
1730        */
1731       assert(parentNodeLink!=NULL);
1732       assert(*parentNodeLink==curNode);
1733
1734       /*
1735        * We arbitrately re-attach the left sub-tree to the parent.
1736        */
1737       *parentNodeLink = curNode->prevDropTarget;
1738
1739       /*
1740        * And we re-insert the right subtree
1741        */
1742       if (curNode->nextDropTarget!=NULL)
1743       {
1744         OLEDD_InsertDropTarget(curNode->nextDropTarget);
1745       }
1746
1747       /*
1748        * The node we found is still a valid node once we complete
1749        * the unlinking of the kids.
1750        */
1751       curNode->nextDropTarget=NULL;
1752       curNode->prevDropTarget=NULL;
1753
1754       return curNode;
1755     }
1756   }
1757
1758   /*
1759    * If we get here, the node is not in the tree
1760    */
1761   return NULL;
1762 }
1763
1764 /***
1765  * OLEDD_FindDropTarget()
1766  *
1767  * Finds information about the drop target.
1768  */
1769 static DropTargetNode* OLEDD_FindDropTarget(HWND hwndOfTarget)
1770 {
1771   DropTargetNode*  curNode;
1772
1773   /*
1774    * Iterate the tree to find the HWND value.
1775    */
1776   curNode        = targetListHead;
1777
1778   while (curNode!=NULL)
1779   {
1780     if (hwndOfTarget<curNode->hwndTarget)
1781     {
1782       /*
1783        * If the node we want to add has a smaller HWND, go left
1784        */
1785       curNode =  curNode->prevDropTarget;
1786     }
1787     else if (hwndOfTarget>curNode->hwndTarget)
1788     {
1789       /*
1790        * If the node we want to add has a larger HWND, go right
1791        */
1792       curNode =  curNode->nextDropTarget;
1793     }
1794     else
1795     {
1796       /*
1797        * The item was found in the list.
1798        */
1799       return curNode;
1800     }
1801   }
1802
1803   /*
1804    * If we get here, the item is not in the list
1805    */
1806   return NULL;
1807 }
1808
1809 /***
1810  * OLEDD_DragTrackerWindowProc()
1811  *
1812  * This method is the WindowProcedure of the drag n drop tracking
1813  * window. During a drag n Drop operation, an invisible window is created
1814  * to receive the user input and act upon it. This procedure is in charge
1815  * of this behavior.
1816  */
1817 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(
1818                          HWND   hwnd,
1819                          UINT   uMsg,
1820                          WPARAM wParam,
1821                          LPARAM   lParam)
1822 {
1823   switch (uMsg)
1824   {
1825     case WM_CREATE:
1826     {
1827       LPCREATESTRUCTA createStruct = (LPCREATESTRUCTA)lParam;
1828
1829       SetWindowLongA(hwnd, 0, (LONG)createStruct->lpCreateParams);
1830
1831
1832       break;
1833     }
1834     case WM_MOUSEMOVE:
1835     {
1836       TrackerWindowInfo* trackerInfo = (TrackerWindowInfo*)GetWindowLongA(hwnd, 0);
1837       POINT            mousePos;
1838
1839       /*
1840        * Get the current mouse position in screen coordinates.
1841        */
1842       mousePos.x = LOWORD(lParam);
1843       mousePos.y = HIWORD(lParam);
1844       ClientToScreen(hwnd, &mousePos);
1845
1846       /*
1847        * Track the movement of the mouse.
1848        */
1849       OLEDD_TrackMouseMove(trackerInfo, mousePos, wParam);
1850
1851       break;
1852     }
1853     case WM_LBUTTONUP:
1854     case WM_MBUTTONUP:
1855     case WM_RBUTTONUP:
1856     case WM_LBUTTONDOWN:
1857     case WM_MBUTTONDOWN:
1858     case WM_RBUTTONDOWN:
1859     {
1860       TrackerWindowInfo* trackerInfo = (TrackerWindowInfo*)GetWindowLongA(hwnd, 0);
1861       POINT            mousePos;
1862
1863       /*
1864        * Get the current mouse position in screen coordinates.
1865        */
1866       mousePos.x = LOWORD(lParam);
1867       mousePos.y = HIWORD(lParam);
1868       ClientToScreen(hwnd, &mousePos);
1869
1870       /*
1871        * Notify everyone that the button state changed
1872        * TODO: Check if the "escape" key was pressed.
1873        */
1874       OLEDD_TrackStateChange(trackerInfo, mousePos, wParam);
1875
1876       break;
1877     }
1878   }
1879
1880   /*
1881    * This is a window proc after all. Let's call the default.
1882    */
1883   return DefWindowProcA (hwnd, uMsg, wParam, lParam);
1884 }
1885
1886 /***
1887  * OLEDD_TrackMouseMove()
1888  *
1889  * This method is invoked while a drag and drop operation is in effect.
1890  * it will generate the appropriate callbacks in the drop source
1891  * and drop target. It will also provide the expected feedback to
1892  * the user.
1893  *
1894  * params:
1895  *    trackerInfo - Pointer to the structure identifying the
1896  *                  drag & drop operation that is currently
1897  *                  active.
1898  *    mousePos    - Current position of the mouse in screen
1899  *                  coordinates.
1900  *    keyState    - Contains the state of the shift keys and the
1901  *                  mouse buttons (MK_LBUTTON and the like)
1902  */
1903 static void OLEDD_TrackMouseMove(
1904   TrackerWindowInfo* trackerInfo,
1905   POINT            mousePos,
1906   DWORD              keyState)
1907 {
1908   HWND   hwndNewTarget = 0;
1909   HRESULT  hr = S_OK;
1910
1911   /*
1912    * Get the handle of the window under the mouse
1913    */
1914   hwndNewTarget = WindowFromPoint(mousePos);
1915
1916   /*
1917    * Every time, we re-initialize the effects passed to the
1918    * IDropTarget to the effects allowed by the source.
1919    */
1920   *trackerInfo->pdwEffect = trackerInfo->dwOKEffect;
1921
1922   /*
1923    * If we are hovering over the same target as before, send the
1924    * DragOver notification
1925    */
1926   if ( (trackerInfo->curDragTarget != 0) &&
1927        (trackerInfo->curDragTargetHWND==hwndNewTarget) )
1928   {
1929     POINTL  mousePosParam;
1930
1931     /*
1932      * The documentation tells me that the coordinate should be in the target
1933      * window's coordinate space. However, the tests I made tell me the
1934      * coordinates should be in screen coordinates.
1935      */
1936     mousePosParam.x = mousePos.x;
1937     mousePosParam.y = mousePos.y;
1938
1939     IDropTarget_DragOver(trackerInfo->curDragTarget,
1940                          keyState,
1941                          mousePosParam,
1942                          trackerInfo->pdwEffect);
1943   }
1944   else
1945   {
1946     DropTargetNode* newDropTargetNode = 0;
1947
1948     /*
1949      * If we changed window, we have to notify our old target and check for
1950      * the new one.
1951      */
1952     if (trackerInfo->curDragTarget!=0)
1953     {
1954       IDropTarget_DragLeave(trackerInfo->curDragTarget);
1955     }
1956
1957     /*
1958      * Make sure we're hovering over a window.
1959      */
1960     if (hwndNewTarget!=0)
1961     {
1962       /*
1963        * Find-out if there is a drag target under the mouse
1964        */
1965       HWND nexttar = hwndNewTarget;
1966       do {
1967         newDropTargetNode = OLEDD_FindDropTarget(nexttar);
1968       } while (!newDropTargetNode && (nexttar = GetParent(nexttar)) != 0);
1969       if(nexttar) hwndNewTarget = nexttar;
1970
1971       trackerInfo->curDragTargetHWND = hwndNewTarget;
1972       trackerInfo->curDragTarget     = newDropTargetNode ? newDropTargetNode->dropTarget : 0;
1973
1974       /*
1975        * If there is, notify it that we just dragged-in
1976        */
1977       if (trackerInfo->curDragTarget!=0)
1978       {
1979         POINTL  mousePosParam;
1980
1981         /*
1982          * The documentation tells me that the coordinate should be in the target
1983          * window's coordinate space. However, the tests I made tell me the
1984          * coordinates should be in screen coordinates.
1985          */
1986         mousePosParam.x = mousePos.x;
1987         mousePosParam.y = mousePos.y;
1988
1989         IDropTarget_DragEnter(trackerInfo->curDragTarget,
1990                               trackerInfo->dataObject,
1991                               keyState,
1992                               mousePosParam,
1993                               trackerInfo->pdwEffect);
1994       }
1995     }
1996     else
1997     {
1998       /*
1999        * The mouse is not over a window so we don't track anything.
2000        */
2001       trackerInfo->curDragTargetHWND = 0;
2002       trackerInfo->curDragTarget     = 0;
2003     }
2004   }
2005
2006   /*
2007    * Now that we have done that, we have to tell the source to give
2008    * us feedback on the work being done by the target.  If we don't
2009    * have a target, simulate no effect.
2010    */
2011   if (trackerInfo->curDragTarget==0)
2012   {
2013     *trackerInfo->pdwEffect = DROPEFFECT_NONE;
2014   }
2015
2016   hr = IDropSource_GiveFeedback(trackerInfo->dropSource,
2017                                 *trackerInfo->pdwEffect);
2018
2019   /*
2020    * When we ask for feedback from the drop source, sometimes it will
2021    * do all the necessary work and sometimes it will not handle it
2022    * when that's the case, we must display the standard drag and drop
2023    * cursors.
2024    */
2025   if (hr==DRAGDROP_S_USEDEFAULTCURSORS)
2026   {
2027     if (*trackerInfo->pdwEffect & DROPEFFECT_MOVE)
2028     {
2029       SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(1)));
2030     }
2031     else if (*trackerInfo->pdwEffect & DROPEFFECT_COPY)
2032     {
2033       SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(2)));
2034     }
2035     else if (*trackerInfo->pdwEffect & DROPEFFECT_LINK)
2036     {
2037       SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(3)));
2038     }
2039     else
2040     {
2041       SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(0)));
2042     }
2043   }
2044 }
2045
2046 /***
2047  * OLEDD_TrackStateChange()
2048  *
2049  * This method is invoked while a drag and drop operation is in effect.
2050  * It is used to notify the drop target/drop source callbacks when
2051  * the state of the keyboard or mouse button change.
2052  *
2053  * params:
2054  *    trackerInfo - Pointer to the structure identifying the
2055  *                  drag & drop operation that is currently
2056  *                  active.
2057  *    mousePos    - Current position of the mouse in screen
2058  *                  coordinates.
2059  *    keyState    - Contains the state of the shift keys and the
2060  *                  mouse buttons (MK_LBUTTON and the like)
2061  */
2062 static void OLEDD_TrackStateChange(
2063   TrackerWindowInfo* trackerInfo,
2064   POINT            mousePos,
2065   DWORD              keyState)
2066 {
2067   /*
2068    * Ask the drop source what to do with the operation.
2069    */
2070   trackerInfo->returnValue = IDropSource_QueryContinueDrag(
2071                                trackerInfo->dropSource,
2072                                trackerInfo->escPressed,
2073                                keyState);
2074
2075   /*
2076    * All the return valued will stop the operation except the S_OK
2077    * return value.
2078    */
2079   if (trackerInfo->returnValue!=S_OK)
2080   {
2081     /*
2082      * Make sure the message loop in DoDragDrop stops
2083      */
2084     trackerInfo->trackingDone = TRUE;
2085
2086     /*
2087      * Release the mouse in case the drop target decides to show a popup
2088      * or a menu or something.
2089      */
2090     ReleaseCapture();
2091
2092     /*
2093      * If we end-up over a target, drop the object in the target or
2094      * inform the target that the operation was cancelled.
2095      */
2096     if (trackerInfo->curDragTarget!=0)
2097     {
2098       switch (trackerInfo->returnValue)
2099       {
2100         /*
2101          * If the source wants us to complete the operation, we tell
2102          * the drop target that we just dropped the object in it.
2103          */
2104         case DRAGDROP_S_DROP:
2105         {
2106           POINTL  mousePosParam;
2107
2108           /*
2109            * The documentation tells me that the coordinate should be
2110            * in the target window's coordinate space. However, the tests
2111            * I made tell me the coordinates should be in screen coordinates.
2112            */
2113           mousePosParam.x = mousePos.x;
2114           mousePosParam.y = mousePos.y;
2115
2116           IDropTarget_Drop(trackerInfo->curDragTarget,
2117                            trackerInfo->dataObject,
2118                            keyState,
2119                            mousePosParam,
2120                            trackerInfo->pdwEffect);
2121           break;
2122         }
2123         /*
2124          * If the source told us that we should cancel, fool the drop
2125          * target by telling it that the mouse left it's window.
2126          * Also set the drop effect to "NONE" in case the application
2127          * ignores the result of DoDragDrop.
2128          */
2129         case DRAGDROP_S_CANCEL:
2130           IDropTarget_DragLeave(trackerInfo->curDragTarget);
2131           *trackerInfo->pdwEffect = DROPEFFECT_NONE;
2132           break;
2133       }
2134     }
2135   }
2136 }
2137
2138 /***
2139  * OLEDD_GetButtonState()
2140  *
2141  * This method will use the current state of the keyboard to build
2142  * a button state mask equivalent to the one passed in the
2143  * WM_MOUSEMOVE wParam.
2144  */
2145 static DWORD OLEDD_GetButtonState()
2146 {
2147   BYTE  keyboardState[256];
2148   DWORD keyMask = 0;
2149
2150   GetKeyboardState(keyboardState);
2151
2152   if ( (keyboardState[VK_SHIFT] & 0x80) !=0)
2153     keyMask |= MK_SHIFT;
2154
2155   if ( (keyboardState[VK_CONTROL] & 0x80) !=0)
2156     keyMask |= MK_CONTROL;
2157
2158   if ( (keyboardState[VK_LBUTTON] & 0x80) !=0)
2159     keyMask |= MK_LBUTTON;
2160
2161   if ( (keyboardState[VK_RBUTTON] & 0x80) !=0)
2162     keyMask |= MK_RBUTTON;
2163
2164   if ( (keyboardState[VK_MBUTTON] & 0x80) !=0)
2165     keyMask |= MK_MBUTTON;
2166
2167   return keyMask;
2168 }
2169
2170 /***
2171  * OLEDD_GetButtonState()
2172  *
2173  * This method will read the default value of the registry key in
2174  * parameter and extract a DWORD value from it. The registry key value
2175  * can be in a string key or a DWORD key.
2176  *
2177  * params:
2178  *     regKey   - Key to read the default value from
2179  *     pdwValue - Pointer to the location where the DWORD
2180  *                value is returned. This value is not modified
2181  *                if the value is not found.
2182  */
2183
2184 static void OLEUTL_ReadRegistryDWORDValue(
2185   HKEY   regKey,
2186   DWORD* pdwValue)
2187 {
2188   char  buffer[20];
2189   DWORD dwKeyType;
2190   DWORD cbData = 20;
2191   LONG  lres;
2192
2193   lres = RegQueryValueExA(regKey,
2194                           "",
2195                           NULL,
2196                           &dwKeyType,
2197                           (LPBYTE)buffer,
2198                           &cbData);
2199
2200   if (lres==ERROR_SUCCESS)
2201   {
2202     switch (dwKeyType)
2203     {
2204       case REG_DWORD:
2205         *pdwValue = *(DWORD*)buffer;
2206         break;
2207       case REG_EXPAND_SZ:
2208       case REG_MULTI_SZ:
2209       case REG_SZ:
2210         *pdwValue = (DWORD)strtoul(buffer, NULL, 10);
2211         break;
2212     }
2213   }
2214 }
2215
2216 /******************************************************************************
2217  * OleMetaFilePictFromIconAndLabel (OLE2.56)
2218  *
2219  * Returns a global memory handle to a metafile which contains the icon and
2220  * label given.
2221  * I guess the result of that should look somehow like desktop icons.
2222  * If no hIcon is given, we load the icon via lpszSourceFile and iIconIndex.
2223  * This code might be wrong at some places.
2224  */
2225 HGLOBAL16 WINAPI OleMetaFilePictFromIconAndLabel16(
2226         HICON16 hIcon,
2227         LPCOLESTR16 lpszLabel,
2228         LPCOLESTR16 lpszSourceFile,
2229         UINT16 iIconIndex
2230 ) {
2231     METAFILEPICT16 *mf;
2232     HGLOBAL16 hmf;
2233     HDC16 hdc;
2234
2235     FIXME("(%04x, '%s', '%s', %d): incorrect metrics, please try to correct them !\n\n\n", hIcon, lpszLabel, lpszSourceFile, iIconIndex);
2236
2237     if (!hIcon) {
2238         if (lpszSourceFile) {
2239             HINSTANCE16 hInstance = LoadLibrary16(lpszSourceFile);
2240
2241             /* load the icon at index from lpszSourceFile */
2242             hIcon = (HICON16)LoadIconA(hInstance, (LPCSTR)(DWORD)iIconIndex);
2243             FreeLibrary16(hInstance);
2244         } else
2245             return (HGLOBAL)NULL;
2246     }
2247
2248     hdc = CreateMetaFile16(NULL);
2249     DrawIcon(hdc, 0, 0, hIcon); /* FIXME */
2250     TextOutA(hdc, 0, 0, lpszLabel, 1); /* FIXME */
2251     hmf = GlobalAlloc16(0, sizeof(METAFILEPICT16));
2252     mf = (METAFILEPICT16 *)GlobalLock16(hmf);
2253     mf->mm = MM_ANISOTROPIC;
2254     mf->xExt = 20; /* FIXME: bogus */
2255     mf->yExt = 20; /* dito */
2256     mf->hMF = CloseMetaFile16(hdc);
2257     return hmf;
2258 }
2259
2260 /******************************************************************************
2261  * DllDebugObjectRPCHook (OLE32.62)
2262  * turns on and off internal debugging,  pointer is only used on macintosh
2263  */
2264
2265 BOOL WINAPI DllDebugObjectRPCHook(BOOL b, void *dummy)
2266 {
2267   FIXME("stub\n");
2268   return TRUE;
2269 }
2270