Do not check for non NULL pointer before HeapFree'ing it. It's
[wine.git] / dlls / ole32 / ole2.c
blobb1dbce3330b4167af3fd39ba6f0440d8454b2718
1 /*
2 * OLE2 library
4 * Copyright 1995 Martin von Loewis
5 * Copyright 1999 Francis Beaudet
6 * Copyright 1999 Noel Borthwick
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "config.h"
25 #include <assert.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <string.h>
31 #define COBJMACROS
32 #define NONAMELESSUNION
33 #define NONAMELESSSTRUCT
35 #include "windef.h"
36 #include "winbase.h"
37 #include "winerror.h"
38 #include "wingdi.h"
39 #include "winuser.h"
40 #include "winnls.h"
41 #include "winreg.h"
42 #include "commctrl.h"
43 #include "ole2.h"
44 #include "ole2ver.h"
45 #include "wownt32.h"
47 #include "wine/winbase16.h"
48 #include "wine/wingdi16.h"
49 #include "wine/winuser16.h"
50 #include "ole32_main.h"
52 #include "wine/debug.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(ole);
55 WINE_DECLARE_DEBUG_CHANNEL(accel);
57 #define HICON_16(h32) (LOWORD(h32))
58 #define HICON_32(h16) ((HICON)(ULONG_PTR)(h16))
59 #define HINSTANCE_32(h16) ((HINSTANCE)(ULONG_PTR)(h16))
61 /******************************************************************************
62 * These are static/global variables and internal data structures that the
63 * OLE module uses to maintain it's state.
65 typedef struct tagDropTargetNode
67 HWND hwndTarget;
68 IDropTarget* dropTarget;
69 struct tagDropTargetNode* prevDropTarget;
70 struct tagDropTargetNode* nextDropTarget;
71 } DropTargetNode;
73 typedef struct tagTrackerWindowInfo
75 IDataObject* dataObject;
76 IDropSource* dropSource;
77 DWORD dwOKEffect;
78 DWORD* pdwEffect;
79 BOOL trackingDone;
80 HRESULT returnValue;
82 BOOL escPressed;
83 HWND curTargetHWND; /* window the mouse is hovering over */
84 HWND curDragTargetHWND; /* might be a ancestor of curTargetHWND */
85 IDropTarget* curDragTarget;
86 } TrackerWindowInfo;
88 typedef struct tagOleMenuDescriptor /* OleMenuDescriptor */
90 HWND hwndFrame; /* The containers frame window */
91 HWND hwndActiveObject; /* The active objects window */
92 OLEMENUGROUPWIDTHS mgw; /* OLE menu group widths for the shared menu */
93 HMENU hmenuCombined; /* The combined menu */
94 BOOL bIsServerItem; /* True if the currently open popup belongs to the server */
95 } OleMenuDescriptor;
97 typedef struct tagOleMenuHookItem /* OleMenu hook item in per thread hook list */
99 DWORD tid; /* Thread Id */
100 HANDLE hHeap; /* Heap this is allocated from */
101 HHOOK GetMsg_hHook; /* message hook for WH_GETMESSAGE */
102 HHOOK CallWndProc_hHook; /* message hook for WH_CALLWNDPROC */
103 struct tagOleMenuHookItem *next;
104 } OleMenuHookItem;
106 static OleMenuHookItem *hook_list;
109 * This is the lock count on the OLE library. It is controlled by the
110 * OLEInitialize/OLEUninitialize methods.
112 static ULONG OLE_moduleLockCount = 0;
115 * Name of our registered window class.
117 static const char OLEDD_DRAGTRACKERCLASS[] = "WineDragDropTracker32";
120 * This is the head of the Drop target container.
122 static DropTargetNode* targetListHead = NULL;
124 /******************************************************************************
125 * These are the prototypes of miscelaneous utility methods
127 static void OLEUTL_ReadRegistryDWORDValue(HKEY regKey, DWORD* pdwValue);
129 /******************************************************************************
130 * These are the prototypes of the utility methods used to manage a shared menu
132 static void OLEMenu_Initialize(void);
133 static void OLEMenu_UnInitialize(void);
134 BOOL OLEMenu_InstallHooks( DWORD tid );
135 BOOL OLEMenu_UnInstallHooks( DWORD tid );
136 OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid );
137 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos );
138 BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor );
139 LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam);
140 LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam);
142 /******************************************************************************
143 * These are the prototypes of the OLE Clipboard initialization methods (in clipboard.c)
145 void OLEClipbrd_UnInitialize(void);
146 void OLEClipbrd_Initialize(void);
148 /******************************************************************************
149 * These are the prototypes of the utility methods used for OLE Drag n Drop
151 static void OLEDD_Initialize(void);
152 static void OLEDD_UnInitialize(void);
153 static void OLEDD_InsertDropTarget(
154 DropTargetNode* nodeToAdd);
155 static DropTargetNode* OLEDD_ExtractDropTarget(
156 HWND hwndOfTarget);
157 static DropTargetNode* OLEDD_FindDropTarget(
158 HWND hwndOfTarget);
159 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(
160 HWND hwnd,
161 UINT uMsg,
162 WPARAM wParam,
163 LPARAM lParam);
164 static void OLEDD_TrackMouseMove(
165 TrackerWindowInfo* trackerInfo,
166 POINT mousePos,
167 DWORD keyState);
168 static void OLEDD_TrackStateChange(
169 TrackerWindowInfo* trackerInfo,
170 POINT mousePos,
171 DWORD keyState);
172 static DWORD OLEDD_GetButtonState(void);
175 /******************************************************************************
176 * OleBuildVersion [OLE2.1]
177 * OleBuildVersion [OLE32.@]
179 DWORD WINAPI OleBuildVersion(void)
181 TRACE("Returning version %d, build %d.\n", rmm, rup);
182 return (rmm<<16)+rup;
185 /***********************************************************************
186 * OleInitialize (OLE2.2)
187 * OleInitialize (OLE32.@)
189 HRESULT WINAPI OleInitialize(LPVOID reserved)
191 HRESULT hr;
193 TRACE("(%p)\n", reserved);
196 * The first duty of the OleInitialize is to initialize the COM libraries.
198 hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
201 * If the CoInitializeEx call failed, the OLE libraries can't be
202 * initialized.
204 if (FAILED(hr))
205 return hr;
208 * Then, it has to initialize the OLE specific modules.
209 * This includes:
210 * Clipboard
211 * Drag and Drop
212 * Object linking and Embedding
213 * In-place activation
215 if (OLE_moduleLockCount==0)
218 * Initialize the libraries.
220 TRACE("() - Initializing the OLE libraries\n");
223 * OLE Clipboard
225 OLEClipbrd_Initialize();
228 * Drag and Drop
230 OLEDD_Initialize();
233 * OLE shared menu
235 OLEMenu_Initialize();
239 * Then, we increase the lock count on the OLE module.
241 OLE_moduleLockCount++;
243 return hr;
246 /******************************************************************************
247 * CoGetCurrentProcess [COMPOBJ.34]
248 * CoGetCurrentProcess [OLE32.@]
250 * NOTES
251 * Is DWORD really the correct return type for this function?
253 DWORD WINAPI CoGetCurrentProcess(void)
255 return GetCurrentProcessId();
258 /******************************************************************************
259 * OleUninitialize [OLE2.3]
260 * OleUninitialize [OLE32.@]
262 void WINAPI OleUninitialize(void)
264 TRACE("()\n");
267 * Decrease the lock count on the OLE module.
269 OLE_moduleLockCount--;
272 * If we hit the bottom of the lock stack, free the libraries.
274 if (OLE_moduleLockCount==0)
277 * Actually free the libraries.
279 TRACE("() - Freeing the last reference count\n");
282 * OLE Clipboard
284 OLEClipbrd_UnInitialize();
287 * Drag and Drop
289 OLEDD_UnInitialize();
292 * OLE shared menu
294 OLEMenu_UnInitialize();
298 * Then, uninitialize the COM libraries.
300 CoUninitialize();
303 /******************************************************************************
304 * CoRegisterMessageFilter [OLE32.@]
306 HRESULT WINAPI CoRegisterMessageFilter(
307 LPMESSAGEFILTER lpMessageFilter, /* [in] Pointer to interface */
308 LPMESSAGEFILTER *lplpMessageFilter /* [out] Indirect pointer to prior instance if non-NULL */
310 FIXME("stub\n");
311 if (lplpMessageFilter) {
312 *lplpMessageFilter = NULL;
314 return S_OK;
317 /******************************************************************************
318 * OleInitializeWOW [OLE32.@]
320 HRESULT WINAPI OleInitializeWOW(DWORD x) {
321 FIXME("(0x%08lx),stub!\n",x);
322 return 0;
325 /***********************************************************************
326 * RegisterDragDrop (OLE32.@)
328 HRESULT WINAPI RegisterDragDrop(
329 HWND hwnd,
330 LPDROPTARGET pDropTarget)
332 DropTargetNode* dropTargetInfo;
334 TRACE("(%p,%p)\n", hwnd, pDropTarget);
336 if (!pDropTarget)
337 return E_INVALIDARG;
340 * First, check if the window is already registered.
342 dropTargetInfo = OLEDD_FindDropTarget(hwnd);
344 if (dropTargetInfo!=NULL)
345 return DRAGDROP_E_ALREADYREGISTERED;
348 * If it's not there, we can add it. We first create a node for it.
350 dropTargetInfo = HeapAlloc(GetProcessHeap(), 0, sizeof(DropTargetNode));
352 if (dropTargetInfo==NULL)
353 return E_OUTOFMEMORY;
355 dropTargetInfo->hwndTarget = hwnd;
356 dropTargetInfo->prevDropTarget = NULL;
357 dropTargetInfo->nextDropTarget = NULL;
360 * Don't forget that this is an interface pointer, need to nail it down since
361 * we keep a copy of it.
363 dropTargetInfo->dropTarget = pDropTarget;
364 IDropTarget_AddRef(dropTargetInfo->dropTarget);
366 OLEDD_InsertDropTarget(dropTargetInfo);
368 return S_OK;
371 /***********************************************************************
372 * RevokeDragDrop (OLE32.@)
374 HRESULT WINAPI RevokeDragDrop(
375 HWND hwnd)
377 DropTargetNode* dropTargetInfo;
379 TRACE("(%p)\n", hwnd);
382 * First, check if the window is already registered.
384 dropTargetInfo = OLEDD_ExtractDropTarget(hwnd);
387 * If it ain't in there, it's an error.
389 if (dropTargetInfo==NULL)
390 return DRAGDROP_E_NOTREGISTERED;
393 * If it's in there, clean-up it's used memory and
394 * references
396 IDropTarget_Release(dropTargetInfo->dropTarget);
397 HeapFree(GetProcessHeap(), 0, dropTargetInfo);
399 return S_OK;
402 /***********************************************************************
403 * OleRegGetUserType (OLE32.@)
405 * This implementation of OleRegGetUserType ignores the dwFormOfType
406 * parameter and always returns the full name of the object. This is
407 * not too bad since this is the case for many objects because of the
408 * way they are registered.
410 HRESULT WINAPI OleRegGetUserType(
411 REFCLSID clsid,
412 DWORD dwFormOfType,
413 LPOLESTR* pszUserType)
415 char keyName[60];
416 DWORD dwKeyType;
417 DWORD cbData;
418 HKEY clsidKey;
419 LONG hres;
420 LPBYTE buffer;
421 HRESULT retVal;
423 * Initialize the out parameter.
425 *pszUserType = NULL;
428 * Build the key name we're looking for
430 sprintf( keyName, "CLSID\\{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\",
431 clsid->Data1, clsid->Data2, clsid->Data3,
432 clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
433 clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );
435 TRACE("(%s, %ld, %p)\n", keyName, dwFormOfType, pszUserType);
438 * Open the class id Key
440 hres = RegOpenKeyA(HKEY_CLASSES_ROOT,
441 keyName,
442 &clsidKey);
444 if (hres != ERROR_SUCCESS)
445 return REGDB_E_CLASSNOTREG;
448 * Retrieve the size of the name string.
450 cbData = 0;
452 hres = RegQueryValueExA(clsidKey,
454 NULL,
455 &dwKeyType,
456 NULL,
457 &cbData);
459 if (hres!=ERROR_SUCCESS)
461 RegCloseKey(clsidKey);
462 return REGDB_E_READREGDB;
466 * Allocate a buffer for the registry value.
468 *pszUserType = CoTaskMemAlloc(cbData*2);
470 if (*pszUserType==NULL)
472 RegCloseKey(clsidKey);
473 return E_OUTOFMEMORY;
476 buffer = HeapAlloc(GetProcessHeap(), 0, cbData);
478 if (buffer == NULL)
480 RegCloseKey(clsidKey);
481 CoTaskMemFree(*pszUserType);
482 *pszUserType=NULL;
483 return E_OUTOFMEMORY;
486 hres = RegQueryValueExA(clsidKey,
488 NULL,
489 &dwKeyType,
490 buffer,
491 &cbData);
493 RegCloseKey(clsidKey);
496 if (hres!=ERROR_SUCCESS)
498 CoTaskMemFree(*pszUserType);
499 *pszUserType=NULL;
501 retVal = REGDB_E_READREGDB;
503 else
505 MultiByteToWideChar( CP_ACP, 0, buffer, -1, *pszUserType, cbData /*FIXME*/ );
506 retVal = S_OK;
508 HeapFree(GetProcessHeap(), 0, buffer);
510 return retVal;
513 /***********************************************************************
514 * DoDragDrop [OLE32.@]
516 HRESULT WINAPI DoDragDrop (
517 IDataObject *pDataObject, /* [in] ptr to the data obj */
518 IDropSource* pDropSource, /* [in] ptr to the source obj */
519 DWORD dwOKEffect, /* [in] effects allowed by the source */
520 DWORD *pdwEffect) /* [out] ptr to effects of the source */
522 TrackerWindowInfo trackerInfo;
523 HWND hwndTrackWindow;
524 MSG msg;
526 TRACE("(DataObject %p, DropSource %p)\n", pDataObject, pDropSource);
529 * Setup the drag n drop tracking window.
531 if (!IsValidInterface((LPUNKNOWN)pDropSource))
532 return E_INVALIDARG;
534 trackerInfo.dataObject = pDataObject;
535 trackerInfo.dropSource = pDropSource;
536 trackerInfo.dwOKEffect = dwOKEffect;
537 trackerInfo.pdwEffect = pdwEffect;
538 trackerInfo.trackingDone = FALSE;
539 trackerInfo.escPressed = FALSE;
540 trackerInfo.curDragTargetHWND = 0;
541 trackerInfo.curTargetHWND = 0;
542 trackerInfo.curDragTarget = 0;
544 hwndTrackWindow = CreateWindowA(OLEDD_DRAGTRACKERCLASS,
545 "TrackerWindow",
546 WS_POPUP,
547 CW_USEDEFAULT, CW_USEDEFAULT,
548 CW_USEDEFAULT, CW_USEDEFAULT,
552 (LPVOID)&trackerInfo);
554 if (hwndTrackWindow!=0)
557 * Capture the mouse input
559 SetCapture(hwndTrackWindow);
562 * Pump messages. All mouse input should go the the capture window.
564 while (!trackerInfo.trackingDone && GetMessageA(&msg, 0, 0, 0) )
566 if ( (msg.message >= WM_KEYFIRST) &&
567 (msg.message <= WM_KEYLAST) )
570 * When keyboard messages are sent to windows on this thread, we
571 * want to ignore notify the drop source that the state changed.
572 * in the case of the Escape key, we also notify the drop source
573 * we give it a special meaning.
575 if ( (msg.message==WM_KEYDOWN) &&
576 (msg.wParam==VK_ESCAPE) )
578 trackerInfo.escPressed = TRUE;
582 * Notify the drop source.
584 OLEDD_TrackStateChange(&trackerInfo,
585 msg.pt,
586 OLEDD_GetButtonState());
588 else
591 * Dispatch the messages only when it's not a keyboard message.
593 DispatchMessageA(&msg);
598 * Destroy the temporary window.
600 DestroyWindow(hwndTrackWindow);
602 return trackerInfo.returnValue;
605 return E_FAIL;
608 /***********************************************************************
609 * OleQueryLinkFromData [OLE32.@]
611 HRESULT WINAPI OleQueryLinkFromData(
612 IDataObject* pSrcDataObject)
614 FIXME("(%p),stub!\n", pSrcDataObject);
615 return S_OK;
618 /***********************************************************************
619 * OleRegGetMiscStatus [OLE32.@]
621 HRESULT WINAPI OleRegGetMiscStatus(
622 REFCLSID clsid,
623 DWORD dwAspect,
624 DWORD* pdwStatus)
626 char keyName[60];
627 HKEY clsidKey;
628 HKEY miscStatusKey;
629 HKEY aspectKey;
630 LONG result;
633 * Initialize the out parameter.
635 *pdwStatus = 0;
638 * Build the key name we're looking for
640 sprintf( keyName, "CLSID\\{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\",
641 clsid->Data1, clsid->Data2, clsid->Data3,
642 clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
643 clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );
645 TRACE("(%s, %ld, %p)\n", keyName, dwAspect, pdwStatus);
648 * Open the class id Key
650 result = RegOpenKeyA(HKEY_CLASSES_ROOT,
651 keyName,
652 &clsidKey);
654 if (result != ERROR_SUCCESS)
655 return REGDB_E_CLASSNOTREG;
658 * Get the MiscStatus
660 result = RegOpenKeyA(clsidKey,
661 "MiscStatus",
662 &miscStatusKey);
665 if (result != ERROR_SUCCESS)
667 RegCloseKey(clsidKey);
668 return REGDB_E_READREGDB;
672 * Read the default value
674 OLEUTL_ReadRegistryDWORDValue(miscStatusKey, pdwStatus);
677 * Open the key specific to the requested aspect.
679 sprintf(keyName, "%ld", dwAspect);
681 result = RegOpenKeyA(miscStatusKey,
682 keyName,
683 &aspectKey);
685 if (result == ERROR_SUCCESS)
687 OLEUTL_ReadRegistryDWORDValue(aspectKey, pdwStatus);
688 RegCloseKey(aspectKey);
692 * Cleanup
694 RegCloseKey(miscStatusKey);
695 RegCloseKey(clsidKey);
697 return S_OK;
700 /******************************************************************************
701 * OleSetContainedObject [OLE32.@]
703 HRESULT WINAPI OleSetContainedObject(
704 LPUNKNOWN pUnknown,
705 BOOL fContained)
707 IRunnableObject* runnable = NULL;
708 HRESULT hres;
710 TRACE("(%p,%x), stub!\n", pUnknown, fContained);
712 hres = IUnknown_QueryInterface(pUnknown,
713 &IID_IRunnableObject,
714 (void**)&runnable);
716 if (SUCCEEDED(hres))
718 hres = IRunnableObject_SetContainedObject(runnable, fContained);
720 IRunnableObject_Release(runnable);
722 return hres;
725 return S_OK;
728 /******************************************************************************
729 * OleLoad [OLE32.@]
731 HRESULT WINAPI OleLoad(
732 LPSTORAGE pStg,
733 REFIID riid,
734 LPOLECLIENTSITE pClientSite,
735 LPVOID* ppvObj)
737 IPersistStorage* persistStorage = NULL;
738 IOleObject* oleObject = NULL;
739 STATSTG storageInfo;
740 HRESULT hres;
742 TRACE("(%p,%p,%p,%p)\n", pStg, riid, pClientSite, ppvObj);
745 * TODO, Conversion ... OleDoAutoConvert
749 * Get the class ID for the object.
751 hres = IStorage_Stat(pStg, &storageInfo, STATFLAG_NONAME);
754 * Now, try and create the handler for the object
756 hres = CoCreateInstance(&storageInfo.clsid,
757 NULL,
758 CLSCTX_INPROC_HANDLER,
759 &IID_IOleObject,
760 (void**)&oleObject);
763 * If that fails, as it will most times, load the default
764 * OLE handler.
766 if (FAILED(hres))
768 hres = OleCreateDefaultHandler(&storageInfo.clsid,
769 NULL,
770 &IID_IOleObject,
771 (void**)&oleObject);
775 * If we couldn't find a handler... this is bad. Abort the whole thing.
777 if (FAILED(hres))
778 return hres;
781 * Inform the new object of it's client site.
783 hres = IOleObject_SetClientSite(oleObject, pClientSite);
786 * Initialize the object with it's IPersistStorage interface.
788 hres = IOleObject_QueryInterface(oleObject,
789 &IID_IPersistStorage,
790 (void**)&persistStorage);
792 if (SUCCEEDED(hres))
794 IPersistStorage_Load(persistStorage, pStg);
796 IPersistStorage_Release(persistStorage);
797 persistStorage = NULL;
801 * Return the requested interface to the caller.
803 hres = IOleObject_QueryInterface(oleObject, riid, ppvObj);
806 * Cleanup interfaces used internally
808 IOleObject_Release(oleObject);
810 return hres;
813 /***********************************************************************
814 * OleSave [OLE32.@]
816 HRESULT WINAPI OleSave(
817 LPPERSISTSTORAGE pPS,
818 LPSTORAGE pStg,
819 BOOL fSameAsLoad)
821 HRESULT hres;
822 CLSID objectClass;
824 TRACE("(%p,%p,%x)\n", pPS, pStg, fSameAsLoad);
827 * First, we transfer the class ID (if available)
829 hres = IPersistStorage_GetClassID(pPS, &objectClass);
831 if (SUCCEEDED(hres))
833 WriteClassStg(pStg, &objectClass);
837 * Then, we ask the object to save itself to the
838 * storage. If it is successful, we commit the storage.
840 hres = IPersistStorage_Save(pPS, pStg, fSameAsLoad);
842 if (SUCCEEDED(hres))
844 IStorage_Commit(pStg,
845 STGC_DEFAULT);
848 return hres;
852 /******************************************************************************
853 * OleLockRunning [OLE32.@]
855 HRESULT WINAPI OleLockRunning(LPUNKNOWN pUnknown, BOOL fLock, BOOL fLastUnlockCloses)
857 IRunnableObject* runnable = NULL;
858 HRESULT hres;
860 TRACE("(%p,%x,%x)\n", pUnknown, fLock, fLastUnlockCloses);
862 hres = IUnknown_QueryInterface(pUnknown,
863 &IID_IRunnableObject,
864 (void**)&runnable);
866 if (SUCCEEDED(hres))
868 hres = IRunnableObject_LockRunning(runnable, fLock, fLastUnlockCloses);
870 IRunnableObject_Release(runnable);
872 return hres;
874 else
875 return E_INVALIDARG;
879 /**************************************************************************
880 * Internal methods to manage the shared OLE menu in response to the
881 * OLE***MenuDescriptor API
884 /***
885 * OLEMenu_Initialize()
887 * Initializes the OLEMENU data structures.
889 static void OLEMenu_Initialize()
893 /***
894 * OLEMenu_UnInitialize()
896 * Releases the OLEMENU data structures.
898 static void OLEMenu_UnInitialize()
902 /*************************************************************************
903 * OLEMenu_InstallHooks
904 * Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
906 * RETURNS: TRUE if message hooks were successfully installed
907 * FALSE on failure
909 BOOL OLEMenu_InstallHooks( DWORD tid )
911 OleMenuHookItem *pHookItem = NULL;
913 /* Create an entry for the hook table */
914 if ( !(pHookItem = HeapAlloc(GetProcessHeap(), 0,
915 sizeof(OleMenuHookItem)) ) )
916 return FALSE;
918 pHookItem->tid = tid;
919 pHookItem->hHeap = GetProcessHeap();
921 /* Install a thread scope message hook for WH_GETMESSAGE */
922 pHookItem->GetMsg_hHook = SetWindowsHookExA( WH_GETMESSAGE, OLEMenu_GetMsgProc,
923 0, GetCurrentThreadId() );
924 if ( !pHookItem->GetMsg_hHook )
925 goto CLEANUP;
927 /* Install a thread scope message hook for WH_CALLWNDPROC */
928 pHookItem->CallWndProc_hHook = SetWindowsHookExA( WH_CALLWNDPROC, OLEMenu_CallWndProc,
929 0, GetCurrentThreadId() );
930 if ( !pHookItem->CallWndProc_hHook )
931 goto CLEANUP;
933 /* Insert the hook table entry */
934 pHookItem->next = hook_list;
935 hook_list = pHookItem;
937 return TRUE;
939 CLEANUP:
940 /* Unhook any hooks */
941 if ( pHookItem->GetMsg_hHook )
942 UnhookWindowsHookEx( pHookItem->GetMsg_hHook );
943 if ( pHookItem->CallWndProc_hHook )
944 UnhookWindowsHookEx( pHookItem->CallWndProc_hHook );
945 /* Release the hook table entry */
946 HeapFree(pHookItem->hHeap, 0, pHookItem );
948 return FALSE;
951 /*************************************************************************
952 * OLEMenu_UnInstallHooks
953 * UnInstall thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
955 * RETURNS: TRUE if message hooks were successfully installed
956 * FALSE on failure
958 BOOL OLEMenu_UnInstallHooks( DWORD tid )
960 OleMenuHookItem *pHookItem = NULL;
961 OleMenuHookItem **ppHook = &hook_list;
963 while (*ppHook)
965 if ((*ppHook)->tid == tid)
967 pHookItem = *ppHook;
968 *ppHook = pHookItem->next;
969 break;
971 ppHook = &(*ppHook)->next;
973 if (!pHookItem) return FALSE;
975 /* Uninstall the hooks installed for this thread */
976 if ( !UnhookWindowsHookEx( pHookItem->GetMsg_hHook ) )
977 goto CLEANUP;
978 if ( !UnhookWindowsHookEx( pHookItem->CallWndProc_hHook ) )
979 goto CLEANUP;
981 /* Release the hook table entry */
982 HeapFree(pHookItem->hHeap, 0, pHookItem );
984 return TRUE;
986 CLEANUP:
987 /* Release the hook table entry */
988 HeapFree(pHookItem->hHeap, 0, pHookItem );
990 return FALSE;
993 /*************************************************************************
994 * OLEMenu_IsHookInstalled
995 * Tests if OLEMenu hooks have been installed for a thread
997 * RETURNS: The pointer and index of the hook table entry for the tid
998 * NULL and -1 for the index if no hooks were installed for this thread
1000 OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid )
1002 OleMenuHookItem *pHookItem = NULL;
1004 /* Do a simple linear search for an entry whose tid matches ours.
1005 * We really need a map but efficiency is not a concern here. */
1006 for (pHookItem = hook_list; pHookItem; pHookItem = pHookItem->next)
1008 if ( tid == pHookItem->tid )
1009 return pHookItem;
1012 return NULL;
1015 /***********************************************************************
1016 * OLEMenu_FindMainMenuIndex
1018 * Used by OLEMenu API to find the top level group a menu item belongs to.
1019 * On success pnPos contains the index of the item in the top level menu group
1021 * RETURNS: TRUE if the ID was found, FALSE on failure
1023 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos )
1025 UINT i, nItems;
1027 nItems = GetMenuItemCount( hMainMenu );
1029 for (i = 0; i < nItems; i++)
1031 HMENU hsubmenu;
1033 /* Is the current item a submenu? */
1034 if ( (hsubmenu = GetSubMenu(hMainMenu, i)) )
1036 /* If the handle is the same we're done */
1037 if ( hsubmenu == hPopupMenu )
1039 if (pnPos)
1040 *pnPos = i;
1041 return TRUE;
1043 /* Recursively search without updating pnPos */
1044 else if ( OLEMenu_FindMainMenuIndex( hsubmenu, hPopupMenu, NULL ) )
1046 if (pnPos)
1047 *pnPos = i;
1048 return TRUE;
1053 return FALSE;
1056 /***********************************************************************
1057 * OLEMenu_SetIsServerMenu
1059 * Checks whether a popup menu belongs to a shared menu group which is
1060 * owned by the server, and sets the menu descriptor state accordingly.
1061 * All menu messages from these groups should be routed to the server.
1063 * RETURNS: TRUE if the popup menu is part of a server owned group
1064 * FASE if the popup menu is part of a container owned group
1066 BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor )
1068 UINT nPos = 0, nWidth, i;
1070 pOleMenuDescriptor->bIsServerItem = FALSE;
1072 /* Don't bother searching if the popup is the combined menu itself */
1073 if ( hmenu == pOleMenuDescriptor->hmenuCombined )
1074 return FALSE;
1076 /* Find the menu item index in the shared OLE menu that this item belongs to */
1077 if ( !OLEMenu_FindMainMenuIndex( pOleMenuDescriptor->hmenuCombined, hmenu, &nPos ) )
1078 return FALSE;
1080 /* The group widths array has counts for the number of elements
1081 * in the groups File, Edit, Container, Object, Window, Help.
1082 * The Edit, Object & Help groups belong to the server object
1083 * and the other three belong to the container.
1084 * Loop through the group widths and locate the group we are a member of.
1086 for ( i = 0, nWidth = 0; i < 6; i++ )
1088 nWidth += pOleMenuDescriptor->mgw.width[i];
1089 if ( nPos < nWidth )
1091 /* Odd elements are server menu widths */
1092 pOleMenuDescriptor->bIsServerItem = (i%2) ? TRUE : FALSE;
1093 break;
1097 return pOleMenuDescriptor->bIsServerItem;
1100 /*************************************************************************
1101 * OLEMenu_CallWndProc
1102 * Thread scope WH_CALLWNDPROC hook proc filter function (callback)
1103 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1105 LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam)
1107 LPCWPSTRUCT pMsg = NULL;
1108 HOLEMENU hOleMenu = 0;
1109 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1110 OleMenuHookItem *pHookItem = NULL;
1111 WORD fuFlags;
1113 TRACE("%i, %04x, %08x\n", code, wParam, (unsigned)lParam );
1115 /* Check if we're being asked to process the message */
1116 if ( HC_ACTION != code )
1117 goto NEXTHOOK;
1119 /* Retrieve the current message being dispatched from lParam */
1120 pMsg = (LPCWPSTRUCT)lParam;
1122 /* Check if the message is destined for a window we are interested in:
1123 * If the window has an OLEMenu property we may need to dispatch
1124 * the menu message to its active objects window instead. */
1126 hOleMenu = (HOLEMENU)GetPropA( pMsg->hwnd, "PROP_OLEMenuDescriptor" );
1127 if ( !hOleMenu )
1128 goto NEXTHOOK;
1130 /* Get the menu descriptor */
1131 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1132 if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1133 goto NEXTHOOK;
1135 /* Process menu messages */
1136 switch( pMsg->message )
1138 case WM_INITMENU:
1140 /* Reset the menu descriptor state */
1141 pOleMenuDescriptor->bIsServerItem = FALSE;
1143 /* Send this message to the server as well */
1144 SendMessageA( pOleMenuDescriptor->hwndActiveObject,
1145 pMsg->message, pMsg->wParam, pMsg->lParam );
1146 goto NEXTHOOK;
1149 case WM_INITMENUPOPUP:
1151 /* Save the state for whether this is a server owned menu */
1152 OLEMenu_SetIsServerMenu( (HMENU)pMsg->wParam, pOleMenuDescriptor );
1153 break;
1156 case WM_MENUSELECT:
1158 fuFlags = HIWORD(pMsg->wParam); /* Get flags */
1159 if ( fuFlags & MF_SYSMENU )
1160 goto NEXTHOOK;
1162 /* Save the state for whether this is a server owned popup menu */
1163 else if ( fuFlags & MF_POPUP )
1164 OLEMenu_SetIsServerMenu( (HMENU)pMsg->lParam, pOleMenuDescriptor );
1166 break;
1169 case WM_DRAWITEM:
1171 LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT) pMsg->lParam;
1172 if ( pMsg->wParam != 0 || lpdis->CtlType != ODT_MENU )
1173 goto NEXTHOOK; /* Not a menu message */
1175 break;
1178 default:
1179 goto NEXTHOOK;
1182 /* If the message was for the server dispatch it accordingly */
1183 if ( pOleMenuDescriptor->bIsServerItem )
1185 SendMessageA( pOleMenuDescriptor->hwndActiveObject,
1186 pMsg->message, pMsg->wParam, pMsg->lParam );
1189 NEXTHOOK:
1190 if ( pOleMenuDescriptor )
1191 GlobalUnlock( hOleMenu );
1193 /* Lookup the hook item for the current thread */
1194 if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1196 /* This should never fail!! */
1197 WARN("could not retrieve hHook for current thread!\n" );
1198 return 0;
1201 /* Pass on the message to the next hooker */
1202 return CallNextHookEx( pHookItem->CallWndProc_hHook, code, wParam, lParam );
1205 /*************************************************************************
1206 * OLEMenu_GetMsgProc
1207 * Thread scope WH_GETMESSAGE hook proc filter function (callback)
1208 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1210 LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam)
1212 LPMSG pMsg = NULL;
1213 HOLEMENU hOleMenu = 0;
1214 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1215 OleMenuHookItem *pHookItem = NULL;
1216 WORD wCode;
1218 TRACE("%i, %04x, %08x\n", code, wParam, (unsigned)lParam );
1220 /* Check if we're being asked to process a messages */
1221 if ( HC_ACTION != code )
1222 goto NEXTHOOK;
1224 /* Retrieve the current message being dispatched from lParam */
1225 pMsg = (LPMSG)lParam;
1227 /* Check if the message is destined for a window we are interested in:
1228 * If the window has an OLEMenu property we may need to dispatch
1229 * the menu message to its active objects window instead. */
1231 hOleMenu = (HOLEMENU)GetPropA( pMsg->hwnd, "PROP_OLEMenuDescriptor" );
1232 if ( !hOleMenu )
1233 goto NEXTHOOK;
1235 /* Process menu messages */
1236 switch( pMsg->message )
1238 case WM_COMMAND:
1240 wCode = HIWORD(pMsg->wParam); /* Get notification code */
1241 if ( wCode )
1242 goto NEXTHOOK; /* Not a menu message */
1243 break;
1245 default:
1246 goto NEXTHOOK;
1249 /* Get the menu descriptor */
1250 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1251 if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1252 goto NEXTHOOK;
1254 /* If the message was for the server dispatch it accordingly */
1255 if ( pOleMenuDescriptor->bIsServerItem )
1257 /* Change the hWnd in the message to the active objects hWnd.
1258 * The message loop which reads this message will automatically
1259 * dispatch it to the embedded objects window. */
1260 pMsg->hwnd = pOleMenuDescriptor->hwndActiveObject;
1263 NEXTHOOK:
1264 if ( pOleMenuDescriptor )
1265 GlobalUnlock( hOleMenu );
1267 /* Lookup the hook item for the current thread */
1268 if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1270 /* This should never fail!! */
1271 WARN("could not retrieve hHook for current thread!\n" );
1272 return FALSE;
1275 /* Pass on the message to the next hooker */
1276 return CallNextHookEx( pHookItem->GetMsg_hHook, code, wParam, lParam );
1279 /***********************************************************************
1280 * OleCreateMenuDescriptor [OLE32.@]
1281 * Creates an OLE menu descriptor for OLE to use when dispatching
1282 * menu messages and commands.
1284 * PARAMS:
1285 * hmenuCombined - Handle to the objects combined menu
1286 * lpMenuWidths - Pointer to array of 6 LONG's indicating menus per group
1289 HOLEMENU WINAPI OleCreateMenuDescriptor(
1290 HMENU hmenuCombined,
1291 LPOLEMENUGROUPWIDTHS lpMenuWidths)
1293 HOLEMENU hOleMenu;
1294 OleMenuDescriptor *pOleMenuDescriptor;
1295 int i;
1297 if ( !hmenuCombined || !lpMenuWidths )
1298 return 0;
1300 /* Create an OLE menu descriptor */
1301 if ( !(hOleMenu = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT,
1302 sizeof(OleMenuDescriptor) ) ) )
1303 return 0;
1305 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1306 if ( !pOleMenuDescriptor )
1307 return 0;
1309 /* Initialize menu group widths and hmenu */
1310 for ( i = 0; i < 6; i++ )
1311 pOleMenuDescriptor->mgw.width[i] = lpMenuWidths->width[i];
1313 pOleMenuDescriptor->hmenuCombined = hmenuCombined;
1314 pOleMenuDescriptor->bIsServerItem = FALSE;
1315 GlobalUnlock( hOleMenu );
1317 return hOleMenu;
1320 /***********************************************************************
1321 * OleDestroyMenuDescriptor [OLE32.@]
1322 * Destroy the shared menu descriptor
1324 HRESULT WINAPI OleDestroyMenuDescriptor(
1325 HOLEMENU hmenuDescriptor)
1327 if ( hmenuDescriptor )
1328 GlobalFree( hmenuDescriptor );
1329 return S_OK;
1332 /***********************************************************************
1333 * OleSetMenuDescriptor [OLE32.@]
1334 * Installs or removes OLE dispatching code for the containers frame window
1335 * FIXME: The lpFrame and lpActiveObject parameters are currently ignored
1336 * OLE should install context sensitive help F1 filtering for the app when
1337 * these are non null.
1339 * PARAMS:
1340 * hOleMenu Handle to composite menu descriptor
1341 * hwndFrame Handle to containers frame window
1342 * hwndActiveObject Handle to objects in-place activation window
1343 * lpFrame Pointer to IOleInPlaceFrame on containers window
1344 * lpActiveObject Pointer to IOleInPlaceActiveObject on active in-place object
1346 * RETURNS:
1347 * S_OK - menu installed correctly
1348 * E_FAIL, E_INVALIDARG, E_UNEXPECTED - failure
1350 HRESULT WINAPI OleSetMenuDescriptor(
1351 HOLEMENU hOleMenu,
1352 HWND hwndFrame,
1353 HWND hwndActiveObject,
1354 LPOLEINPLACEFRAME lpFrame,
1355 LPOLEINPLACEACTIVEOBJECT lpActiveObject)
1357 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1359 /* Check args */
1360 if ( !hwndFrame || (hOleMenu && !hwndActiveObject) )
1361 return E_INVALIDARG;
1363 if ( lpFrame || lpActiveObject )
1365 FIXME("(%x, %p, %p, %p, %p), Context sensitive help filtering not implemented!\n",
1366 (unsigned int)hOleMenu,
1367 hwndFrame,
1368 hwndActiveObject,
1369 lpFrame,
1370 lpActiveObject);
1373 /* Set up a message hook to intercept the containers frame window messages.
1374 * The message filter is responsible for dispatching menu messages from the
1375 * shared menu which are intended for the object.
1378 if ( hOleMenu ) /* Want to install dispatching code */
1380 /* If OLEMenu hooks are already installed for this thread, fail
1381 * Note: This effectively means that OleSetMenuDescriptor cannot
1382 * be called twice in succession on the same frame window
1383 * without first calling it with a null hOleMenu to uninstall */
1384 if ( OLEMenu_IsHookInstalled( GetCurrentThreadId() ) )
1385 return E_FAIL;
1387 /* Get the menu descriptor */
1388 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1389 if ( !pOleMenuDescriptor )
1390 return E_UNEXPECTED;
1392 /* Update the menu descriptor */
1393 pOleMenuDescriptor->hwndFrame = hwndFrame;
1394 pOleMenuDescriptor->hwndActiveObject = hwndActiveObject;
1396 GlobalUnlock( hOleMenu );
1397 pOleMenuDescriptor = NULL;
1399 /* Add a menu descriptor windows property to the frame window */
1400 SetPropA( hwndFrame, "PROP_OLEMenuDescriptor", hOleMenu );
1402 /* Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC */
1403 if ( !OLEMenu_InstallHooks( GetCurrentThreadId() ) )
1404 return E_FAIL;
1406 else /* Want to uninstall dispatching code */
1408 /* Uninstall the hooks */
1409 if ( !OLEMenu_UnInstallHooks( GetCurrentThreadId() ) )
1410 return E_FAIL;
1412 /* Remove the menu descriptor property from the frame window */
1413 RemovePropA( hwndFrame, "PROP_OLEMenuDescriptor" );
1416 return S_OK;
1419 /******************************************************************************
1420 * IsAccelerator [OLE32.@]
1421 * Mostly copied from controls/menu.c TranslateAccelerator implementation
1423 BOOL WINAPI IsAccelerator(HACCEL hAccel, int cAccelEntries, LPMSG lpMsg, WORD* lpwCmd)
1425 LPACCEL lpAccelTbl;
1426 int i;
1428 if(!lpMsg) return FALSE;
1429 if (!hAccel)
1431 WARN_(accel)("NULL accel handle\n");
1432 return FALSE;
1434 if((lpMsg->message != WM_KEYDOWN &&
1435 lpMsg->message != WM_KEYUP &&
1436 lpMsg->message != WM_SYSKEYDOWN &&
1437 lpMsg->message != WM_SYSKEYUP &&
1438 lpMsg->message != WM_CHAR)) return FALSE;
1439 lpAccelTbl = HeapAlloc(GetProcessHeap(), 0, cAccelEntries * sizeof(ACCEL));
1440 if (NULL == lpAccelTbl)
1442 return FALSE;
1444 if (CopyAcceleratorTableW(hAccel, lpAccelTbl, cAccelEntries) != cAccelEntries)
1446 WARN_(accel)("CopyAcceleratorTableW failed\n");
1447 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
1448 return FALSE;
1451 TRACE_(accel)("hAccel=%p, cAccelEntries=%d,"
1452 "msg->hwnd=%p, msg->message=%04x, wParam=%08x, lParam=%08lx\n",
1453 hAccel, cAccelEntries,
1454 lpMsg->hwnd, lpMsg->message, lpMsg->wParam, lpMsg->lParam);
1455 for(i = 0; i < cAccelEntries; i++)
1457 if(lpAccelTbl[i].key != lpMsg->wParam)
1458 continue;
1460 if(lpMsg->message == WM_CHAR)
1462 if(!(lpAccelTbl[i].fVirt & FALT) && !(lpAccelTbl[i].fVirt & FVIRTKEY))
1464 TRACE_(accel)("found accel for WM_CHAR: ('%c')\n", lpMsg->wParam & 0xff);
1465 goto found;
1468 else
1470 if(lpAccelTbl[i].fVirt & FVIRTKEY)
1472 INT mask = 0;
1473 TRACE_(accel)("found accel for virt_key %04x (scan %04x)\n",
1474 lpMsg->wParam, HIWORD(lpMsg->lParam) & 0xff);
1475 if(GetKeyState(VK_SHIFT) & 0x8000) mask |= FSHIFT;
1476 if(GetKeyState(VK_CONTROL) & 0x8000) mask |= FCONTROL;
1477 if(GetKeyState(VK_MENU) & 0x8000) mask |= FALT;
1478 if(mask == (lpAccelTbl[i].fVirt & (FSHIFT | FCONTROL | FALT))) goto found;
1479 TRACE_(accel)("incorrect SHIFT/CTRL/ALT-state\n");
1481 else
1483 if(!(lpMsg->lParam & 0x01000000)) /* no special_key */
1485 if((lpAccelTbl[i].fVirt & FALT) && (lpMsg->lParam & 0x20000000))
1486 { /* ^^ ALT pressed */
1487 TRACE_(accel)("found accel for Alt-%c\n", lpMsg->wParam & 0xff);
1488 goto found;
1495 WARN_(accel)("couldn't translate accelerator key\n");
1496 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
1497 return FALSE;
1499 found:
1500 if(lpwCmd) *lpwCmd = lpAccelTbl[i].cmd;
1501 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
1502 return TRUE;
1505 /***********************************************************************
1506 * ReleaseStgMedium [OLE32.@]
1508 void WINAPI ReleaseStgMedium(
1509 STGMEDIUM* pmedium)
1511 switch (pmedium->tymed)
1513 case TYMED_HGLOBAL:
1515 if ( (pmedium->pUnkForRelease==0) &&
1516 (pmedium->u.hGlobal!=0) )
1517 GlobalFree(pmedium->u.hGlobal);
1518 break;
1520 case TYMED_FILE:
1522 if (pmedium->u.lpszFileName!=0)
1524 if (pmedium->pUnkForRelease==0)
1526 DeleteFileW(pmedium->u.lpszFileName);
1529 CoTaskMemFree(pmedium->u.lpszFileName);
1531 break;
1533 case TYMED_ISTREAM:
1535 if (pmedium->u.pstm!=0)
1537 IStream_Release(pmedium->u.pstm);
1539 break;
1541 case TYMED_ISTORAGE:
1543 if (pmedium->u.pstg!=0)
1545 IStorage_Release(pmedium->u.pstg);
1547 break;
1549 case TYMED_GDI:
1551 if ( (pmedium->pUnkForRelease==0) &&
1552 (pmedium->u.hBitmap!=0) )
1553 DeleteObject(pmedium->u.hBitmap);
1554 break;
1556 case TYMED_MFPICT:
1558 if ( (pmedium->pUnkForRelease==0) &&
1559 (pmedium->u.hMetaFilePict!=0) )
1561 LPMETAFILEPICT pMP = GlobalLock(pmedium->u.hMetaFilePict);
1562 DeleteMetaFile(pMP->hMF);
1563 GlobalUnlock(pmedium->u.hMetaFilePict);
1564 GlobalFree(pmedium->u.hMetaFilePict);
1566 break;
1568 case TYMED_ENHMF:
1570 if ( (pmedium->pUnkForRelease==0) &&
1571 (pmedium->u.hEnhMetaFile!=0) )
1573 DeleteEnhMetaFile(pmedium->u.hEnhMetaFile);
1575 break;
1577 case TYMED_NULL:
1578 default:
1579 break;
1581 pmedium->tymed=TYMED_NULL;
1584 * After cleaning up, the unknown is released
1586 if (pmedium->pUnkForRelease!=0)
1588 IUnknown_Release(pmedium->pUnkForRelease);
1589 pmedium->pUnkForRelease = 0;
1593 /***
1594 * OLEDD_Initialize()
1596 * Initializes the OLE drag and drop data structures.
1598 static void OLEDD_Initialize()
1600 WNDCLASSA wndClass;
1602 ZeroMemory (&wndClass, sizeof(WNDCLASSA));
1603 wndClass.style = CS_GLOBALCLASS;
1604 wndClass.lpfnWndProc = OLEDD_DragTrackerWindowProc;
1605 wndClass.cbClsExtra = 0;
1606 wndClass.cbWndExtra = sizeof(TrackerWindowInfo*);
1607 wndClass.hCursor = 0;
1608 wndClass.hbrBackground = 0;
1609 wndClass.lpszClassName = OLEDD_DRAGTRACKERCLASS;
1611 RegisterClassA (&wndClass);
1614 /***
1615 * OLEDD_UnInitialize()
1617 * Releases the OLE drag and drop data structures.
1619 static void OLEDD_UnInitialize()
1622 * Simply empty the list.
1624 while (targetListHead!=NULL)
1626 RevokeDragDrop(targetListHead->hwndTarget);
1630 /***
1631 * OLEDD_InsertDropTarget()
1633 * Insert the target node in the tree.
1635 static void OLEDD_InsertDropTarget(DropTargetNode* nodeToAdd)
1637 DropTargetNode* curNode;
1638 DropTargetNode** parentNodeLink;
1641 * Iterate the tree to find the insertion point.
1643 curNode = targetListHead;
1644 parentNodeLink = &targetListHead;
1646 while (curNode!=NULL)
1648 if (nodeToAdd->hwndTarget<curNode->hwndTarget)
1651 * If the node we want to add has a smaller HWND, go left
1653 parentNodeLink = &curNode->prevDropTarget;
1654 curNode = curNode->prevDropTarget;
1656 else if (nodeToAdd->hwndTarget>curNode->hwndTarget)
1659 * If the node we want to add has a larger HWND, go right
1661 parentNodeLink = &curNode->nextDropTarget;
1662 curNode = curNode->nextDropTarget;
1664 else
1667 * The item was found in the list. It shouldn't have been there
1669 assert(FALSE);
1670 return;
1675 * If we get here, we have found a spot for our item. The parentNodeLink
1676 * pointer points to the pointer that we have to modify.
1677 * The curNode should be NULL. We just have to establish the link and Voila!
1679 assert(curNode==NULL);
1680 assert(parentNodeLink!=NULL);
1681 assert(*parentNodeLink==NULL);
1683 *parentNodeLink=nodeToAdd;
1686 /***
1687 * OLEDD_ExtractDropTarget()
1689 * Removes the target node from the tree.
1691 static DropTargetNode* OLEDD_ExtractDropTarget(HWND hwndOfTarget)
1693 DropTargetNode* curNode;
1694 DropTargetNode** parentNodeLink;
1697 * Iterate the tree to find the insertion point.
1699 curNode = targetListHead;
1700 parentNodeLink = &targetListHead;
1702 while (curNode!=NULL)
1704 if (hwndOfTarget<curNode->hwndTarget)
1707 * If the node we want to add has a smaller HWND, go left
1709 parentNodeLink = &curNode->prevDropTarget;
1710 curNode = curNode->prevDropTarget;
1712 else if (hwndOfTarget>curNode->hwndTarget)
1715 * If the node we want to add has a larger HWND, go right
1717 parentNodeLink = &curNode->nextDropTarget;
1718 curNode = curNode->nextDropTarget;
1720 else
1723 * The item was found in the list. Detach it from it's parent and
1724 * re-insert it's kids in the tree.
1726 assert(parentNodeLink!=NULL);
1727 assert(*parentNodeLink==curNode);
1730 * We arbitrately re-attach the left sub-tree to the parent.
1732 *parentNodeLink = curNode->prevDropTarget;
1735 * And we re-insert the right subtree
1737 if (curNode->nextDropTarget!=NULL)
1739 OLEDD_InsertDropTarget(curNode->nextDropTarget);
1743 * The node we found is still a valid node once we complete
1744 * the unlinking of the kids.
1746 curNode->nextDropTarget=NULL;
1747 curNode->prevDropTarget=NULL;
1749 return curNode;
1754 * If we get here, the node is not in the tree
1756 return NULL;
1759 /***
1760 * OLEDD_FindDropTarget()
1762 * Finds information about the drop target.
1764 static DropTargetNode* OLEDD_FindDropTarget(HWND hwndOfTarget)
1766 DropTargetNode* curNode;
1769 * Iterate the tree to find the HWND value.
1771 curNode = targetListHead;
1773 while (curNode!=NULL)
1775 if (hwndOfTarget<curNode->hwndTarget)
1778 * If the node we want to add has a smaller HWND, go left
1780 curNode = curNode->prevDropTarget;
1782 else if (hwndOfTarget>curNode->hwndTarget)
1785 * If the node we want to add has a larger HWND, go right
1787 curNode = curNode->nextDropTarget;
1789 else
1792 * The item was found in the list.
1794 return curNode;
1799 * If we get here, the item is not in the list
1801 return NULL;
1804 /***
1805 * OLEDD_DragTrackerWindowProc()
1807 * This method is the WindowProcedure of the drag n drop tracking
1808 * window. During a drag n Drop operation, an invisible window is created
1809 * to receive the user input and act upon it. This procedure is in charge
1810 * of this behavior.
1812 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(
1813 HWND hwnd,
1814 UINT uMsg,
1815 WPARAM wParam,
1816 LPARAM lParam)
1818 switch (uMsg)
1820 case WM_CREATE:
1822 LPCREATESTRUCTA createStruct = (LPCREATESTRUCTA)lParam;
1824 SetWindowLongA(hwnd, 0, (LONG)createStruct->lpCreateParams);
1827 break;
1829 case WM_MOUSEMOVE:
1831 TrackerWindowInfo* trackerInfo = (TrackerWindowInfo*)GetWindowLongA(hwnd, 0);
1832 POINT mousePos;
1835 * Get the current mouse position in screen coordinates.
1837 mousePos.x = LOWORD(lParam);
1838 mousePos.y = HIWORD(lParam);
1839 ClientToScreen(hwnd, &mousePos);
1842 * Track the movement of the mouse.
1844 OLEDD_TrackMouseMove(trackerInfo, mousePos, wParam);
1846 break;
1848 case WM_LBUTTONUP:
1849 case WM_MBUTTONUP:
1850 case WM_RBUTTONUP:
1851 case WM_LBUTTONDOWN:
1852 case WM_MBUTTONDOWN:
1853 case WM_RBUTTONDOWN:
1855 TrackerWindowInfo* trackerInfo = (TrackerWindowInfo*)GetWindowLongA(hwnd, 0);
1856 POINT mousePos;
1859 * Get the current mouse position in screen coordinates.
1861 mousePos.x = LOWORD(lParam);
1862 mousePos.y = HIWORD(lParam);
1863 ClientToScreen(hwnd, &mousePos);
1866 * Notify everyone that the button state changed
1867 * TODO: Check if the "escape" key was pressed.
1869 OLEDD_TrackStateChange(trackerInfo, mousePos, wParam);
1871 break;
1876 * This is a window proc after all. Let's call the default.
1878 return DefWindowProcA (hwnd, uMsg, wParam, lParam);
1881 /***
1882 * OLEDD_TrackMouseMove()
1884 * This method is invoked while a drag and drop operation is in effect.
1885 * it will generate the appropriate callbacks in the drop source
1886 * and drop target. It will also provide the expected feedback to
1887 * the user.
1889 * params:
1890 * trackerInfo - Pointer to the structure identifying the
1891 * drag & drop operation that is currently
1892 * active.
1893 * mousePos - Current position of the mouse in screen
1894 * coordinates.
1895 * keyState - Contains the state of the shift keys and the
1896 * mouse buttons (MK_LBUTTON and the like)
1898 static void OLEDD_TrackMouseMove(
1899 TrackerWindowInfo* trackerInfo,
1900 POINT mousePos,
1901 DWORD keyState)
1903 HWND hwndNewTarget = 0;
1904 HRESULT hr = S_OK;
1907 * Get the handle of the window under the mouse
1909 hwndNewTarget = WindowFromPoint(mousePos);
1912 * Every time, we re-initialize the effects passed to the
1913 * IDropTarget to the effects allowed by the source.
1915 *trackerInfo->pdwEffect = trackerInfo->dwOKEffect;
1918 * If we are hovering over the same target as before, send the
1919 * DragOver notification
1921 if ( (trackerInfo->curDragTarget != 0) &&
1922 (trackerInfo->curTargetHWND == hwndNewTarget) )
1924 POINTL mousePosParam;
1927 * The documentation tells me that the coordinate should be in the target
1928 * window's coordinate space. However, the tests I made tell me the
1929 * coordinates should be in screen coordinates.
1931 mousePosParam.x = mousePos.x;
1932 mousePosParam.y = mousePos.y;
1934 IDropTarget_DragOver(trackerInfo->curDragTarget,
1935 keyState,
1936 mousePosParam,
1937 trackerInfo->pdwEffect);
1939 else
1941 DropTargetNode* newDropTargetNode = 0;
1944 * If we changed window, we have to notify our old target and check for
1945 * the new one.
1947 if (trackerInfo->curDragTarget!=0)
1949 IDropTarget_DragLeave(trackerInfo->curDragTarget);
1953 * Make sure we're hovering over a window.
1955 if (hwndNewTarget!=0)
1958 * Find-out if there is a drag target under the mouse
1960 HWND nexttar = hwndNewTarget;
1961 trackerInfo->curTargetHWND = hwndNewTarget;
1963 do {
1964 newDropTargetNode = OLEDD_FindDropTarget(nexttar);
1965 } while (!newDropTargetNode && (nexttar = GetParent(nexttar)) != 0);
1966 if(nexttar) hwndNewTarget = nexttar;
1968 trackerInfo->curDragTargetHWND = hwndNewTarget;
1969 trackerInfo->curDragTarget = newDropTargetNode ? newDropTargetNode->dropTarget : 0;
1972 * If there is, notify it that we just dragged-in
1974 if (trackerInfo->curDragTarget!=0)
1976 POINTL mousePosParam;
1979 * The documentation tells me that the coordinate should be in the target
1980 * window's coordinate space. However, the tests I made tell me the
1981 * coordinates should be in screen coordinates.
1983 mousePosParam.x = mousePos.x;
1984 mousePosParam.y = mousePos.y;
1986 IDropTarget_DragEnter(trackerInfo->curDragTarget,
1987 trackerInfo->dataObject,
1988 keyState,
1989 mousePosParam,
1990 trackerInfo->pdwEffect);
1993 else
1996 * The mouse is not over a window so we don't track anything.
1998 trackerInfo->curDragTargetHWND = 0;
1999 trackerInfo->curTargetHWND = 0;
2000 trackerInfo->curDragTarget = 0;
2005 * Now that we have done that, we have to tell the source to give
2006 * us feedback on the work being done by the target. If we don't
2007 * have a target, simulate no effect.
2009 if (trackerInfo->curDragTarget==0)
2011 *trackerInfo->pdwEffect = DROPEFFECT_NONE;
2014 hr = IDropSource_GiveFeedback(trackerInfo->dropSource,
2015 *trackerInfo->pdwEffect);
2018 * When we ask for feedback from the drop source, sometimes it will
2019 * do all the necessary work and sometimes it will not handle it
2020 * when that's the case, we must display the standard drag and drop
2021 * cursors.
2023 if (hr==DRAGDROP_S_USEDEFAULTCURSORS)
2025 if (*trackerInfo->pdwEffect & DROPEFFECT_MOVE)
2027 SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(1)));
2029 else if (*trackerInfo->pdwEffect & DROPEFFECT_COPY)
2031 SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(2)));
2033 else if (*trackerInfo->pdwEffect & DROPEFFECT_LINK)
2035 SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(3)));
2037 else
2039 SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(0)));
2044 /***
2045 * OLEDD_TrackStateChange()
2047 * This method is invoked while a drag and drop operation is in effect.
2048 * It is used to notify the drop target/drop source callbacks when
2049 * the state of the keyboard or mouse button change.
2051 * params:
2052 * trackerInfo - Pointer to the structure identifying the
2053 * drag & drop operation that is currently
2054 * active.
2055 * mousePos - Current position of the mouse in screen
2056 * coordinates.
2057 * keyState - Contains the state of the shift keys and the
2058 * mouse buttons (MK_LBUTTON and the like)
2060 static void OLEDD_TrackStateChange(
2061 TrackerWindowInfo* trackerInfo,
2062 POINT mousePos,
2063 DWORD keyState)
2066 * Ask the drop source what to do with the operation.
2068 trackerInfo->returnValue = IDropSource_QueryContinueDrag(
2069 trackerInfo->dropSource,
2070 trackerInfo->escPressed,
2071 keyState);
2074 * All the return valued will stop the operation except the S_OK
2075 * return value.
2077 if (trackerInfo->returnValue!=S_OK)
2080 * Make sure the message loop in DoDragDrop stops
2082 trackerInfo->trackingDone = TRUE;
2085 * Release the mouse in case the drop target decides to show a popup
2086 * or a menu or something.
2088 ReleaseCapture();
2091 * If we end-up over a target, drop the object in the target or
2092 * inform the target that the operation was cancelled.
2094 if (trackerInfo->curDragTarget!=0)
2096 switch (trackerInfo->returnValue)
2099 * If the source wants us to complete the operation, we tell
2100 * the drop target that we just dropped the object in it.
2102 case DRAGDROP_S_DROP:
2104 POINTL mousePosParam;
2107 * The documentation tells me that the coordinate should be
2108 * in the target window's coordinate space. However, the tests
2109 * I made tell me the coordinates should be in screen coordinates.
2111 mousePosParam.x = mousePos.x;
2112 mousePosParam.y = mousePos.y;
2114 IDropTarget_Drop(trackerInfo->curDragTarget,
2115 trackerInfo->dataObject,
2116 keyState,
2117 mousePosParam,
2118 trackerInfo->pdwEffect);
2119 break;
2122 * If the source told us that we should cancel, fool the drop
2123 * target by telling it that the mouse left it's window.
2124 * Also set the drop effect to "NONE" in case the application
2125 * ignores the result of DoDragDrop.
2127 case DRAGDROP_S_CANCEL:
2128 IDropTarget_DragLeave(trackerInfo->curDragTarget);
2129 *trackerInfo->pdwEffect = DROPEFFECT_NONE;
2130 break;
2136 /***
2137 * OLEDD_GetButtonState()
2139 * This method will use the current state of the keyboard to build
2140 * a button state mask equivalent to the one passed in the
2141 * WM_MOUSEMOVE wParam.
2143 static DWORD OLEDD_GetButtonState()
2145 BYTE keyboardState[256];
2146 DWORD keyMask = 0;
2148 GetKeyboardState(keyboardState);
2150 if ( (keyboardState[VK_SHIFT] & 0x80) !=0)
2151 keyMask |= MK_SHIFT;
2153 if ( (keyboardState[VK_CONTROL] & 0x80) !=0)
2154 keyMask |= MK_CONTROL;
2156 if ( (keyboardState[VK_LBUTTON] & 0x80) !=0)
2157 keyMask |= MK_LBUTTON;
2159 if ( (keyboardState[VK_RBUTTON] & 0x80) !=0)
2160 keyMask |= MK_RBUTTON;
2162 if ( (keyboardState[VK_MBUTTON] & 0x80) !=0)
2163 keyMask |= MK_MBUTTON;
2165 return keyMask;
2168 /***
2169 * OLEDD_GetButtonState()
2171 * This method will read the default value of the registry key in
2172 * parameter and extract a DWORD value from it. The registry key value
2173 * can be in a string key or a DWORD key.
2175 * params:
2176 * regKey - Key to read the default value from
2177 * pdwValue - Pointer to the location where the DWORD
2178 * value is returned. This value is not modified
2179 * if the value is not found.
2182 static void OLEUTL_ReadRegistryDWORDValue(
2183 HKEY regKey,
2184 DWORD* pdwValue)
2186 char buffer[20];
2187 DWORD dwKeyType;
2188 DWORD cbData = 20;
2189 LONG lres;
2191 lres = RegQueryValueExA(regKey,
2193 NULL,
2194 &dwKeyType,
2195 (LPBYTE)buffer,
2196 &cbData);
2198 if (lres==ERROR_SUCCESS)
2200 switch (dwKeyType)
2202 case REG_DWORD:
2203 *pdwValue = *(DWORD*)buffer;
2204 break;
2205 case REG_EXPAND_SZ:
2206 case REG_MULTI_SZ:
2207 case REG_SZ:
2208 *pdwValue = (DWORD)strtoul(buffer, NULL, 10);
2209 break;
2214 /******************************************************************************
2215 * OleDraw (OLE32.@)
2217 * The operation of this function is documented literally in the WinAPI
2218 * documentation to involve a QueryInterface for the IViewObject interface,
2219 * followed by a call to IViewObject::Draw.
2221 HRESULT WINAPI OleDraw(
2222 IUnknown *pUnk,
2223 DWORD dwAspect,
2224 HDC hdcDraw,
2225 LPCRECT lprcBounds)
2227 HRESULT hres;
2228 IViewObject *viewobject;
2230 hres = IUnknown_QueryInterface(pUnk,
2231 &IID_IViewObject,
2232 (void**)&viewobject);
2234 if (SUCCEEDED(hres))
2236 RECTL rectl;
2238 rectl.left = lprcBounds->left;
2239 rectl.right = lprcBounds->right;
2240 rectl.top = lprcBounds->top;
2241 rectl.bottom = lprcBounds->bottom;
2242 hres = IViewObject_Draw(viewobject, dwAspect, -1, 0, 0, 0, hdcDraw, &rectl, 0, 0, 0);
2244 IViewObject_Release(viewobject);
2245 return hres;
2247 else
2249 return DV_E_NOIVIEWOBJECT;
2253 /***********************************************************************
2254 * OleTranslateAccelerator [OLE32.@]
2256 HRESULT WINAPI OleTranslateAccelerator (LPOLEINPLACEFRAME lpFrame,
2257 LPOLEINPLACEFRAMEINFO lpFrameInfo, LPMSG lpmsg)
2259 WORD wID;
2261 TRACE("(%p,%p,%p)\n", lpFrame, lpFrameInfo, lpmsg);
2263 if (IsAccelerator(lpFrameInfo->haccel,lpFrameInfo->cAccelEntries,lpmsg,&wID))
2264 return IOleInPlaceFrame_TranslateAccelerator(lpFrame,lpmsg,wID);
2266 return S_FALSE;
2269 /******************************************************************************
2270 * OleCreate [OLE32.@]
2273 HRESULT WINAPI OleCreate(
2274 REFCLSID rclsid,
2275 REFIID riid,
2276 DWORD renderopt,
2277 LPFORMATETC pFormatEtc,
2278 LPOLECLIENTSITE pClientSite,
2279 LPSTORAGE pStg,
2280 LPVOID* ppvObj)
2282 HRESULT hres, hres1;
2283 IUnknown * pUnk = NULL;
2285 FIXME("\n\t%s\n\t%s semi-stub!\n", debugstr_guid(rclsid), debugstr_guid(riid));
2287 if (SUCCEEDED((hres = CoCreateInstance(rclsid, 0, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER|CLSCTX_LOCAL_SERVER , riid, (LPVOID*)&pUnk))))
2289 if (pClientSite)
2291 IOleObject * pOE;
2292 IPersistStorage * pPS;
2293 if (SUCCEEDED((hres = IUnknown_QueryInterface( pUnk, &IID_IOleObject, (LPVOID*)&pOE))))
2295 TRACE("trying to set clientsite %p\n", pClientSite);
2296 hres1 = IOleObject_SetClientSite(pOE, pClientSite);
2297 TRACE("-- result 0x%08lx\n", hres1);
2298 IOleObject_Release(pOE);
2300 if (SUCCEEDED((hres = IUnknown_QueryInterface( pUnk, &IID_IPersistStorage, (LPVOID*)&pPS))))
2302 TRACE("trying to set stg %p\n", pStg);
2303 hres1 = IPersistStorage_InitNew(pPS, pStg);
2304 TRACE("-- result 0x%08lx\n", hres1);
2305 IPersistStorage_Release(pPS);
2310 *ppvObj = pUnk;
2312 TRACE("-- %p \n", pUnk);
2313 return hres;
2316 /***********************************************************************
2317 * OLE_FreeClipDataArray [internal]
2319 * NOTES:
2320 * frees the data associated with an array of CLIPDATAs
2322 static void OLE_FreeClipDataArray(ULONG count, CLIPDATA * pClipDataArray)
2324 ULONG i;
2325 for (i = 0; i < count; i++)
2326 if (pClipDataArray[i].pClipData)
2327 CoTaskMemFree(pClipDataArray[i].pClipData);
2330 /******************************************************************************
2331 * Check if a PROPVARIANT's type is valid.
2333 static inline HRESULT PROPVARIANT_ValidateType(VARTYPE vt)
2335 switch (vt)
2337 case VT_EMPTY:
2338 case VT_NULL:
2339 case VT_I2:
2340 case VT_I4:
2341 case VT_R4:
2342 case VT_R8:
2343 case VT_CY:
2344 case VT_DATE:
2345 case VT_BSTR:
2346 case VT_ERROR:
2347 case VT_BOOL:
2348 case VT_UI1:
2349 case VT_UI2:
2350 case VT_UI4:
2351 case VT_I8:
2352 case VT_UI8:
2353 case VT_LPSTR:
2354 case VT_LPWSTR:
2355 case VT_FILETIME:
2356 case VT_BLOB:
2357 case VT_STREAM:
2358 case VT_STORAGE:
2359 case VT_STREAMED_OBJECT:
2360 case VT_STORED_OBJECT:
2361 case VT_BLOB_OBJECT:
2362 case VT_CF:
2363 case VT_CLSID:
2364 case VT_I2|VT_VECTOR:
2365 case VT_I4|VT_VECTOR:
2366 case VT_R4|VT_VECTOR:
2367 case VT_R8|VT_VECTOR:
2368 case VT_CY|VT_VECTOR:
2369 case VT_DATE|VT_VECTOR:
2370 case VT_BSTR|VT_VECTOR:
2371 case VT_ERROR|VT_VECTOR:
2372 case VT_BOOL|VT_VECTOR:
2373 case VT_VARIANT|VT_VECTOR:
2374 case VT_UI1|VT_VECTOR:
2375 case VT_UI2|VT_VECTOR:
2376 case VT_UI4|VT_VECTOR:
2377 case VT_I8|VT_VECTOR:
2378 case VT_UI8|VT_VECTOR:
2379 case VT_LPSTR|VT_VECTOR:
2380 case VT_LPWSTR|VT_VECTOR:
2381 case VT_FILETIME|VT_VECTOR:
2382 case VT_CF|VT_VECTOR:
2383 case VT_CLSID|VT_VECTOR:
2384 return S_OK;
2386 WARN("Bad type %d\n", vt);
2387 return STG_E_INVALIDPARAMETER;
2390 /***********************************************************************
2391 * PropVariantClear [OLE32.@]
2393 HRESULT WINAPI PropVariantClear(PROPVARIANT * pvar) /* [in/out] */
2395 HRESULT hr;
2397 TRACE("(%p)\n", pvar);
2399 if (!pvar)
2400 return S_OK;
2402 hr = PROPVARIANT_ValidateType(pvar->vt);
2403 if (FAILED(hr))
2404 return hr;
2406 switch(pvar->vt)
2408 case VT_STREAM:
2409 case VT_STREAMED_OBJECT:
2410 case VT_STORAGE:
2411 case VT_STORED_OBJECT:
2412 if (pvar->u.pStream)
2413 IUnknown_Release(pvar->u.pStream);
2414 break;
2415 case VT_CLSID:
2416 case VT_LPSTR:
2417 case VT_LPWSTR:
2418 /* pick an arbitary typed pointer - we don't care about the type
2419 * as we are just freeing it */
2420 CoTaskMemFree(pvar->u.puuid);
2421 break;
2422 case VT_BLOB:
2423 case VT_BLOB_OBJECT:
2424 CoTaskMemFree(pvar->u.blob.pBlobData);
2425 break;
2426 case VT_BSTR:
2427 if (pvar->u.bstrVal)
2429 FIXME("Need to load OLEAUT32 for SysFreeString\n");
2430 /* SysFreeString(pvar->u.bstrVal); */
2432 break;
2433 case VT_CF:
2434 if (pvar->u.pclipdata)
2436 OLE_FreeClipDataArray(1, pvar->u.pclipdata);
2437 CoTaskMemFree(pvar->u.pclipdata);
2439 break;
2440 default:
2441 switch (pvar->vt & ~VT_VECTOR)
2443 case VT_VARIANT:
2444 FreePropVariantArray(pvar->u.capropvar.cElems, pvar->u.capropvar.pElems);
2445 break;
2446 case VT_CF:
2447 OLE_FreeClipDataArray(pvar->u.caclipdata.cElems, pvar->u.caclipdata.pElems);
2448 break;
2449 case VT_BSTR:
2450 case VT_LPSTR:
2451 case VT_LPWSTR:
2452 case VT_CLSID:
2453 FIXME("Freeing of vector sub-type not supported yet\n");
2455 if (pvar->vt & ~VT_VECTOR)
2457 /* pick an arbitary VT_VECTOR structure - they all have the same
2458 * memory layout */
2459 CoTaskMemFree(pvar->u.capropvar.pElems);
2463 ZeroMemory(pvar, sizeof(*pvar));
2465 return S_OK;
2468 /***********************************************************************
2469 * PropVariantCopy [OLE32.@]
2471 HRESULT WINAPI PropVariantCopy(PROPVARIANT *pvarDest, /* [out] */
2472 const PROPVARIANT *pvarSrc) /* [in] */
2474 ULONG len;
2475 HRESULT hr;
2477 TRACE("(%p, %p)\n", pvarDest, pvarSrc);
2479 hr = PROPVARIANT_ValidateType(pvarSrc->vt);
2480 if (FAILED(hr))
2481 return hr;
2483 /* this will deal with most cases */
2484 CopyMemory(pvarDest, pvarSrc, sizeof(*pvarDest));
2486 switch(pvarSrc->vt)
2488 case VT_STREAM:
2489 case VT_STREAMED_OBJECT:
2490 case VT_STORAGE:
2491 case VT_STORED_OBJECT:
2492 IUnknown_AddRef((LPUNKNOWN)pvarDest->u.pStream);
2493 break;
2494 case VT_CLSID:
2495 pvarDest->u.puuid = CoTaskMemAlloc(sizeof(CLSID));
2496 CopyMemory(pvarDest->u.puuid, pvarSrc->u.puuid, sizeof(CLSID));
2497 break;
2498 case VT_LPSTR:
2499 len = strlen(pvarSrc->u.pszVal);
2500 pvarDest->u.pszVal = CoTaskMemAlloc((len+1)*sizeof(CHAR));
2501 CopyMemory(pvarDest->u.pszVal, pvarSrc->u.pszVal, (len+1)*sizeof(CHAR));
2502 break;
2503 case VT_LPWSTR:
2504 len = lstrlenW(pvarSrc->u.pwszVal);
2505 pvarDest->u.pwszVal = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
2506 CopyMemory(pvarDest->u.pwszVal, pvarSrc->u.pwszVal, (len+1)*sizeof(WCHAR));
2507 break;
2508 case VT_BLOB:
2509 case VT_BLOB_OBJECT:
2510 if (pvarSrc->u.blob.pBlobData)
2512 len = pvarSrc->u.blob.cbSize;
2513 pvarDest->u.blob.pBlobData = CoTaskMemAlloc(len);
2514 CopyMemory(pvarDest->u.blob.pBlobData, pvarSrc->u.blob.pBlobData, len);
2516 break;
2517 case VT_BSTR:
2518 FIXME("Need to copy BSTR\n");
2519 break;
2520 case VT_CF:
2521 if (pvarSrc->u.pclipdata)
2523 len = pvarSrc->u.pclipdata->cbSize - sizeof(pvarSrc->u.pclipdata->ulClipFmt);
2524 CoTaskMemAlloc(len);
2525 CopyMemory(pvarDest->u.pclipdata->pClipData, pvarSrc->u.pclipdata->pClipData, len);
2527 break;
2528 default:
2529 if (pvarSrc->vt & VT_VECTOR)
2531 int elemSize;
2532 switch(pvarSrc->vt & VT_VECTOR)
2534 case VT_I1: elemSize = sizeof(pvarSrc->u.cVal); break;
2535 case VT_UI1: elemSize = sizeof(pvarSrc->u.bVal); break;
2536 case VT_I2: elemSize = sizeof(pvarSrc->u.iVal); break;
2537 case VT_UI2: elemSize = sizeof(pvarSrc->u.uiVal); break;
2538 case VT_BOOL: elemSize = sizeof(pvarSrc->u.boolVal); break;
2539 case VT_I4: elemSize = sizeof(pvarSrc->u.lVal); break;
2540 case VT_UI4: elemSize = sizeof(pvarSrc->u.ulVal); break;
2541 case VT_R4: elemSize = sizeof(pvarSrc->u.fltVal); break;
2542 case VT_R8: elemSize = sizeof(pvarSrc->u.dblVal); break;
2543 case VT_ERROR: elemSize = sizeof(pvarSrc->u.scode); break;
2544 case VT_I8: elemSize = sizeof(pvarSrc->u.hVal); break;
2545 case VT_UI8: elemSize = sizeof(pvarSrc->u.uhVal); break;
2546 case VT_CY: elemSize = sizeof(pvarSrc->u.cyVal); break;
2547 case VT_DATE: elemSize = sizeof(pvarSrc->u.date); break;
2548 case VT_FILETIME: elemSize = sizeof(pvarSrc->u.filetime); break;
2549 case VT_CLSID: elemSize = sizeof(*pvarSrc->u.puuid); break;
2550 case VT_CF: elemSize = sizeof(*pvarSrc->u.pclipdata); break;
2552 case VT_BSTR:
2553 case VT_LPSTR:
2554 case VT_LPWSTR:
2555 case VT_VARIANT:
2556 default:
2557 FIXME("Invalid element type: %ul\n", pvarSrc->vt & VT_VECTOR);
2558 return E_INVALIDARG;
2560 len = pvarSrc->u.capropvar.cElems;
2561 pvarDest->u.capropvar.pElems = CoTaskMemAlloc(len * elemSize);
2562 if (pvarSrc->vt == (VT_VECTOR | VT_VARIANT))
2564 ULONG i;
2565 for (i = 0; i < len; i++)
2566 PropVariantCopy(&pvarDest->u.capropvar.pElems[i], &pvarSrc->u.capropvar.pElems[i]);
2568 else if (pvarSrc->vt == (VT_VECTOR | VT_CF))
2570 FIXME("Copy clipformats\n");
2572 else if (pvarSrc->vt == (VT_VECTOR | VT_BSTR))
2574 FIXME("Copy BSTRs\n");
2576 else if (pvarSrc->vt == (VT_VECTOR | VT_LPSTR))
2578 FIXME("Copy LPSTRs\n");
2580 else if (pvarSrc->vt == (VT_VECTOR | VT_LPSTR))
2582 FIXME("Copy LPWSTRs\n");
2584 else
2585 CopyMemory(pvarDest->u.capropvar.pElems, pvarSrc->u.capropvar.pElems, len * elemSize);
2589 return S_OK;
2592 /***********************************************************************
2593 * FreePropVariantArray [OLE32.@]
2595 HRESULT WINAPI FreePropVariantArray(ULONG cVariants, /* [in] */
2596 PROPVARIANT *rgvars) /* [in/out] */
2598 ULONG i;
2600 TRACE("(%lu, %p)\n", cVariants, rgvars);
2602 for(i = 0; i < cVariants; i++)
2603 PropVariantClear(&rgvars[i]);
2605 return S_OK;
2608 /******************************************************************************
2609 * DllDebugObjectRPCHook (OLE32.@)
2610 * turns on and off internal debugging, pointer is only used on macintosh
2613 BOOL WINAPI DllDebugObjectRPCHook(BOOL b, void *dummy)
2615 FIXME("stub\n");
2616 return TRUE;