4 * Copyright 1995 Martin von Loewis
5 * Copyright 1999 Francis Beaudet
6 * Copyright 1999 Noel Borthwick
7 * Copyright 1999, 2000 Marcus Meissner
8 * Copyright 2005 Juan Lang
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
34 #define NONAMELESSUNION
35 #define NONAMELESSSTRUCT
47 #include "wine/unicode.h"
48 #include "compobj_private.h"
49 #include "wine/list.h"
51 #include "wine/debug.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(ole);
54 WINE_DECLARE_DEBUG_CHANNEL(accel);
56 /******************************************************************************
57 * These are static/global variables and internal data structures that the
58 * OLE module uses to maintain it's state.
60 typedef struct tagDropTargetNode
63 IDropTarget* dropTarget;
67 typedef struct tagTrackerWindowInfo
69 IDataObject* dataObject;
70 IDropSource* dropSource;
77 HWND curTargetHWND; /* window the mouse is hovering over */
78 HWND curDragTargetHWND; /* might be a ancestor of curTargetHWND */
79 IDropTarget* curDragTarget;
80 POINTL curMousePos; /* current position of the mouse in screen coordinates */
81 DWORD dwKeyState; /* current state of the shift and ctrl keys and the mouse buttons */
84 typedef struct tagOleMenuDescriptor /* OleMenuDescriptor */
86 HWND hwndFrame; /* The containers frame window */
87 HWND hwndActiveObject; /* The active objects window */
88 OLEMENUGROUPWIDTHS mgw; /* OLE menu group widths for the shared menu */
89 HMENU hmenuCombined; /* The combined menu */
90 BOOL bIsServerItem; /* True if the currently open popup belongs to the server */
93 typedef struct tagOleMenuHookItem /* OleMenu hook item in per thread hook list */
95 DWORD tid; /* Thread Id */
96 HANDLE hHeap; /* Heap this is allocated from */
97 HHOOK GetMsg_hHook; /* message hook for WH_GETMESSAGE */
98 HHOOK CallWndProc_hHook; /* message hook for WH_CALLWNDPROC */
99 struct tagOleMenuHookItem *next;
102 static OleMenuHookItem *hook_list;
105 * This is the lock count on the OLE library. It is controlled by the
106 * OLEInitialize/OLEUninitialize methods.
108 static LONG OLE_moduleLockCount = 0;
111 * Name of our registered window class.
113 static const WCHAR OLEDD_DRAGTRACKERCLASS[] =
114 {'W','i','n','e','D','r','a','g','D','r','o','p','T','r','a','c','k','e','r','3','2',0};
117 * Name of menu descriptor property.
119 static const WCHAR prop_olemenuW[] =
120 {'P','R','O','P','_','O','L','E','M','e','n','u','D','e','s','c','r','i','p','t','o','r',0};
123 * This is the head of the Drop target container.
125 static struct list targetListHead = LIST_INIT(targetListHead);
127 /******************************************************************************
128 * These are the prototypes of miscellaneous utility methods
130 static void OLEUTL_ReadRegistryDWORDValue(HKEY regKey, DWORD* pdwValue);
132 /******************************************************************************
133 * These are the prototypes of the utility methods used to manage a shared menu
135 static void OLEMenu_Initialize(void);
136 static void OLEMenu_UnInitialize(void);
137 static BOOL OLEMenu_InstallHooks( DWORD tid );
138 static BOOL OLEMenu_UnInstallHooks( DWORD tid );
139 static OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid );
140 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos );
141 static BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor );
142 static LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam);
143 static LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam);
145 /******************************************************************************
146 * These are the prototypes of the OLE Clipboard initialization methods (in clipboard.c)
148 extern void OLEClipbrd_UnInitialize(void);
149 extern void OLEClipbrd_Initialize(void);
151 /******************************************************************************
152 * These are the prototypes of the utility methods used for OLE Drag n Drop
154 static void OLEDD_Initialize(void);
155 static DropTargetNode* OLEDD_FindDropTarget(
157 static void OLEDD_FreeDropTarget(DropTargetNode*, BOOL);
158 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(
163 static void OLEDD_TrackMouseMove(
164 TrackerWindowInfo* trackerInfo);
165 static void OLEDD_TrackStateChange(
166 TrackerWindowInfo* trackerInfo);
167 static DWORD OLEDD_GetButtonState(void);
170 /******************************************************************************
171 * OleBuildVersion [OLE32.@]
173 DWORD WINAPI OleBuildVersion(void)
175 TRACE("Returning version %d, build %d.\n", rmm, rup);
176 return (rmm<<16)+rup;
179 /***********************************************************************
180 * OleInitialize (OLE32.@)
182 HRESULT WINAPI OleInitialize(LPVOID reserved)
186 TRACE("(%p)\n", reserved);
189 * The first duty of the OleInitialize is to initialize the COM libraries.
191 hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
194 * If the CoInitializeEx call failed, the OLE libraries can't be
200 if (!COM_CurrentInfo()->ole_inits)
204 * Then, it has to initialize the OLE specific modules.
208 * Object linking and Embedding
209 * In-place activation
211 if (!COM_CurrentInfo()->ole_inits++ &&
212 InterlockedIncrement(&OLE_moduleLockCount) == 1)
215 * Initialize the libraries.
217 TRACE("() - Initializing the OLE libraries\n");
222 OLEClipbrd_Initialize();
232 OLEMenu_Initialize();
238 /******************************************************************************
239 * OleUninitialize [OLE32.@]
241 void WINAPI OleUninitialize(void)
246 * If we hit the bottom of the lock stack, free the libraries.
248 if (!--COM_CurrentInfo()->ole_inits && !InterlockedDecrement(&OLE_moduleLockCount))
251 * Actually free the libraries.
253 TRACE("() - Freeing the last reference count\n");
258 OLEClipbrd_UnInitialize();
263 OLEMenu_UnInitialize();
267 * Then, uninitialize the COM libraries.
272 /******************************************************************************
273 * OleInitializeWOW [OLE32.@]
275 HRESULT WINAPI OleInitializeWOW(DWORD x, DWORD y) {
276 FIXME("(0x%08x, 0x%08x),stub!\n",x, y);
280 /***********************************************************************
281 * RegisterDragDrop (OLE32.@)
283 HRESULT WINAPI RegisterDragDrop(
285 LPDROPTARGET pDropTarget)
287 DropTargetNode* dropTargetInfo;
289 TRACE("(%p,%p)\n", hwnd, pDropTarget);
291 if (!COM_CurrentApt())
293 ERR("COM not initialized\n");
294 return E_OUTOFMEMORY;
302 ERR("invalid hwnd %p\n", hwnd);
303 return DRAGDROP_E_INVALIDHWND;
307 * First, check if the window is already registered.
309 dropTargetInfo = OLEDD_FindDropTarget(hwnd);
311 if (dropTargetInfo!=NULL)
312 return DRAGDROP_E_ALREADYREGISTERED;
315 * If it's not there, we can add it. We first create a node for it.
317 dropTargetInfo = HeapAlloc(GetProcessHeap(), 0, sizeof(DropTargetNode));
319 if (dropTargetInfo==NULL)
320 return E_OUTOFMEMORY;
322 dropTargetInfo->hwndTarget = hwnd;
325 * Don't forget that this is an interface pointer, need to nail it down since
326 * we keep a copy of it.
328 IDropTarget_AddRef(pDropTarget);
329 dropTargetInfo->dropTarget = pDropTarget;
331 list_add_tail(&targetListHead, &dropTargetInfo->entry);
336 /***********************************************************************
337 * RevokeDragDrop (OLE32.@)
339 HRESULT WINAPI RevokeDragDrop(
342 DropTargetNode* dropTargetInfo;
344 TRACE("(%p)\n", hwnd);
348 ERR("invalid hwnd %p\n", hwnd);
349 return DRAGDROP_E_INVALIDHWND;
353 * First, check if the window is already registered.
355 dropTargetInfo = OLEDD_FindDropTarget(hwnd);
358 * If it ain't in there, it's an error.
360 if (dropTargetInfo==NULL)
361 return DRAGDROP_E_NOTREGISTERED;
363 OLEDD_FreeDropTarget(dropTargetInfo, TRUE);
368 /***********************************************************************
369 * OleRegGetUserType (OLE32.@)
371 * This implementation of OleRegGetUserType ignores the dwFormOfType
372 * parameter and always returns the full name of the object. This is
373 * not too bad since this is the case for many objects because of the
374 * way they are registered.
376 HRESULT WINAPI OleRegGetUserType(
379 LPOLESTR* pszUserType)
389 * Initialize the out parameter.
394 * Build the key name we're looking for
396 sprintf( keyName, "CLSID\\{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\",
397 clsid->Data1, clsid->Data2, clsid->Data3,
398 clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
399 clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );
401 TRACE("(%s, %d, %p)\n", keyName, dwFormOfType, pszUserType);
404 * Open the class id Key
406 hres = RegOpenKeyA(HKEY_CLASSES_ROOT,
410 if (hres != ERROR_SUCCESS)
411 return REGDB_E_CLASSNOTREG;
414 * Retrieve the size of the name string.
418 hres = RegQueryValueExA(clsidKey,
425 if (hres!=ERROR_SUCCESS)
427 RegCloseKey(clsidKey);
428 return REGDB_E_READREGDB;
432 * Allocate a buffer for the registry value.
434 *pszUserType = CoTaskMemAlloc(cbData*2);
436 if (*pszUserType==NULL)
438 RegCloseKey(clsidKey);
439 return E_OUTOFMEMORY;
442 buffer = HeapAlloc(GetProcessHeap(), 0, cbData);
446 RegCloseKey(clsidKey);
447 CoTaskMemFree(*pszUserType);
449 return E_OUTOFMEMORY;
452 hres = RegQueryValueExA(clsidKey,
459 RegCloseKey(clsidKey);
462 if (hres!=ERROR_SUCCESS)
464 CoTaskMemFree(*pszUserType);
467 retVal = REGDB_E_READREGDB;
471 MultiByteToWideChar( CP_ACP, 0, buffer, -1, *pszUserType, cbData /*FIXME*/ );
474 HeapFree(GetProcessHeap(), 0, buffer);
479 /***********************************************************************
480 * DoDragDrop [OLE32.@]
482 HRESULT WINAPI DoDragDrop (
483 IDataObject *pDataObject, /* [in] ptr to the data obj */
484 IDropSource* pDropSource, /* [in] ptr to the source obj */
485 DWORD dwOKEffect, /* [in] effects allowed by the source */
486 DWORD *pdwEffect) /* [out] ptr to effects of the source */
488 static const WCHAR trackerW[] = {'T','r','a','c','k','e','r','W','i','n','d','o','w',0};
489 TrackerWindowInfo trackerInfo;
490 HWND hwndTrackWindow;
493 TRACE("(%p, %p, %d, %p)\n", pDataObject, pDropSource, dwOKEffect, pdwEffect);
495 if (!pDataObject || !pDropSource || !pdwEffect)
499 * Setup the drag n drop tracking window.
502 trackerInfo.dataObject = pDataObject;
503 trackerInfo.dropSource = pDropSource;
504 trackerInfo.dwOKEffect = dwOKEffect;
505 trackerInfo.pdwEffect = pdwEffect;
506 trackerInfo.trackingDone = FALSE;
507 trackerInfo.escPressed = FALSE;
508 trackerInfo.curDragTargetHWND = 0;
509 trackerInfo.curTargetHWND = 0;
510 trackerInfo.curDragTarget = 0;
512 hwndTrackWindow = CreateWindowW(OLEDD_DRAGTRACKERCLASS, trackerW,
513 WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT,
514 CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, 0,
520 * Capture the mouse input
522 SetCapture(hwndTrackWindow);
527 * Pump messages. All mouse input should go to the capture window.
529 while (!trackerInfo.trackingDone && GetMessageW(&msg, 0, 0, 0) )
531 trackerInfo.curMousePos.x = msg.pt.x;
532 trackerInfo.curMousePos.y = msg.pt.y;
533 trackerInfo.dwKeyState = OLEDD_GetButtonState();
535 if ( (msg.message >= WM_KEYFIRST) &&
536 (msg.message <= WM_KEYLAST) )
539 * When keyboard messages are sent to windows on this thread, we
540 * want to ignore notify the drop source that the state changed.
541 * in the case of the Escape key, we also notify the drop source
542 * we give it a special meaning.
544 if ( (msg.message==WM_KEYDOWN) &&
545 (msg.wParam==VK_ESCAPE) )
547 trackerInfo.escPressed = TRUE;
551 * Notify the drop source.
553 OLEDD_TrackStateChange(&trackerInfo);
558 * Dispatch the messages only when it's not a keyboard message.
560 DispatchMessageW(&msg);
564 /* re-post the quit message to outer message loop */
565 if (msg.message == WM_QUIT)
566 PostQuitMessage(msg.wParam);
568 * Destroy the temporary window.
570 DestroyWindow(hwndTrackWindow);
572 return trackerInfo.returnValue;
578 /***********************************************************************
579 * OleQueryLinkFromData [OLE32.@]
581 HRESULT WINAPI OleQueryLinkFromData(
582 IDataObject* pSrcDataObject)
584 FIXME("(%p),stub!\n", pSrcDataObject);
588 /***********************************************************************
589 * OleRegGetMiscStatus [OLE32.@]
591 HRESULT WINAPI OleRegGetMiscStatus(
603 * Initialize the out parameter.
608 * Build the key name we're looking for
610 sprintf( keyName, "CLSID\\{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\",
611 clsid->Data1, clsid->Data2, clsid->Data3,
612 clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
613 clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );
615 TRACE("(%s, %d, %p)\n", keyName, dwAspect, pdwStatus);
618 * Open the class id Key
620 result = RegOpenKeyA(HKEY_CLASSES_ROOT,
624 if (result != ERROR_SUCCESS)
625 return REGDB_E_CLASSNOTREG;
630 result = RegOpenKeyA(clsidKey,
635 if (result != ERROR_SUCCESS)
637 RegCloseKey(clsidKey);
638 return REGDB_E_READREGDB;
642 * Read the default value
644 OLEUTL_ReadRegistryDWORDValue(miscStatusKey, pdwStatus);
647 * Open the key specific to the requested aspect.
649 sprintf(keyName, "%d", dwAspect);
651 result = RegOpenKeyA(miscStatusKey,
655 if (result == ERROR_SUCCESS)
657 OLEUTL_ReadRegistryDWORDValue(aspectKey, pdwStatus);
658 RegCloseKey(aspectKey);
664 RegCloseKey(miscStatusKey);
665 RegCloseKey(clsidKey);
670 static HRESULT EnumOLEVERB_Construct(HKEY hkeyVerb, ULONG index, IEnumOLEVERB **ppenum);
674 const IEnumOLEVERBVtbl *lpvtbl;
681 static HRESULT WINAPI EnumOLEVERB_QueryInterface(
682 IEnumOLEVERB *iface, REFIID riid, void **ppv)
684 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
685 if (IsEqualIID(riid, &IID_IUnknown) ||
686 IsEqualIID(riid, &IID_IEnumOLEVERB))
688 IUnknown_AddRef(iface);
692 return E_NOINTERFACE;
695 static ULONG WINAPI EnumOLEVERB_AddRef(
698 EnumOLEVERB *This = (EnumOLEVERB *)iface;
700 return InterlockedIncrement(&This->ref);
703 static ULONG WINAPI EnumOLEVERB_Release(
706 EnumOLEVERB *This = (EnumOLEVERB *)iface;
707 LONG refs = InterlockedDecrement(&This->ref);
711 RegCloseKey(This->hkeyVerb);
712 HeapFree(GetProcessHeap(), 0, This);
717 static HRESULT WINAPI EnumOLEVERB_Next(
718 IEnumOLEVERB *iface, ULONG celt, LPOLEVERB rgelt,
721 EnumOLEVERB *This = (EnumOLEVERB *)iface;
724 TRACE("(%d, %p, %p)\n", celt, rgelt, pceltFetched);
729 for (; celt; celt--, rgelt++)
734 LPWSTR pwszMenuFlags;
736 LONG res = RegEnumKeyW(This->hkeyVerb, This->index, wszSubKey, sizeof(wszSubKey)/sizeof(wszSubKey[0]));
737 if (res == ERROR_NO_MORE_ITEMS)
742 else if (res != ERROR_SUCCESS)
744 ERR("RegEnumKeyW failed with error %d\n", res);
745 hr = REGDB_E_READREGDB;
748 res = RegQueryValueW(This->hkeyVerb, wszSubKey, NULL, &cbData);
749 if (res != ERROR_SUCCESS)
751 ERR("RegQueryValueW failed with error %d\n", res);
752 hr = REGDB_E_READREGDB;
755 pwszOLEVERB = CoTaskMemAlloc(cbData);
761 res = RegQueryValueW(This->hkeyVerb, wszSubKey, pwszOLEVERB, &cbData);
762 if (res != ERROR_SUCCESS)
764 ERR("RegQueryValueW failed with error %d\n", res);
765 hr = REGDB_E_READREGDB;
766 CoTaskMemFree(pwszOLEVERB);
770 TRACE("verb string: %s\n", debugstr_w(pwszOLEVERB));
771 pwszMenuFlags = strchrW(pwszOLEVERB, ',');
774 hr = OLEOBJ_E_INVALIDVERB;
775 CoTaskMemFree(pwszOLEVERB);
778 /* nul terminate the name string and advance to first character */
779 *pwszMenuFlags = '\0';
781 pwszAttribs = strchrW(pwszMenuFlags, ',');
784 hr = OLEOBJ_E_INVALIDVERB;
785 CoTaskMemFree(pwszOLEVERB);
788 /* nul terminate the menu string and advance to first character */
792 /* fill out structure for this verb */
793 rgelt->lVerb = atolW(wszSubKey);
794 rgelt->lpszVerbName = pwszOLEVERB; /* user should free */
795 rgelt->fuFlags = atolW(pwszMenuFlags);
796 rgelt->grfAttribs = atolW(pwszAttribs);
805 static HRESULT WINAPI EnumOLEVERB_Skip(
806 IEnumOLEVERB *iface, ULONG celt)
808 EnumOLEVERB *This = (EnumOLEVERB *)iface;
810 TRACE("(%d)\n", celt);
816 static HRESULT WINAPI EnumOLEVERB_Reset(
819 EnumOLEVERB *This = (EnumOLEVERB *)iface;
827 static HRESULT WINAPI EnumOLEVERB_Clone(
829 IEnumOLEVERB **ppenum)
831 EnumOLEVERB *This = (EnumOLEVERB *)iface;
833 TRACE("(%p)\n", ppenum);
834 if (!DuplicateHandle(GetCurrentProcess(), This->hkeyVerb, GetCurrentProcess(), (HANDLE *)&hkeyVerb, 0, FALSE, DUPLICATE_SAME_ACCESS))
835 return HRESULT_FROM_WIN32(GetLastError());
836 return EnumOLEVERB_Construct(hkeyVerb, This->index, ppenum);
839 static const IEnumOLEVERBVtbl EnumOLEVERB_VTable =
841 EnumOLEVERB_QueryInterface,
850 static HRESULT EnumOLEVERB_Construct(HKEY hkeyVerb, ULONG index, IEnumOLEVERB **ppenum)
852 EnumOLEVERB *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
855 RegCloseKey(hkeyVerb);
856 return E_OUTOFMEMORY;
858 This->lpvtbl = &EnumOLEVERB_VTable;
861 This->hkeyVerb = hkeyVerb;
862 *ppenum = (IEnumOLEVERB *)&This->lpvtbl;
866 /***********************************************************************
867 * OleRegEnumVerbs [OLE32.@]
869 * Enumerates verbs associated with a class stored in the registry.
872 * clsid [I] Class ID to enumerate the verbs for.
873 * ppenum [O] Enumerator.
877 * REGDB_E_CLASSNOTREG: The specified class does not have a key in the registry.
878 * REGDB_E_READREGDB: The class key could not be opened for some other reason.
879 * OLE_E_REGDB_KEY: The Verb subkey for the class is not present.
880 * OLEOBJ_E_NOVERBS: The Verb subkey for the class is empty.
882 HRESULT WINAPI OleRegEnumVerbs (REFCLSID clsid, LPENUMOLEVERB* ppenum)
887 static const WCHAR wszVerb[] = {'V','e','r','b',0};
889 TRACE("(%s, %p)\n", debugstr_guid(clsid), ppenum);
891 res = COM_OpenKeyForCLSID(clsid, wszVerb, KEY_READ, &hkeyVerb);
894 if (res == REGDB_E_CLASSNOTREG)
895 ERR("CLSID %s not registered\n", debugstr_guid(clsid));
896 else if (res == REGDB_E_KEYMISSING)
897 ERR("no Verbs key for class %s\n", debugstr_guid(clsid));
899 ERR("failed to open Verbs key for CLSID %s with error %d\n",
900 debugstr_guid(clsid), res);
904 res = RegQueryInfoKeyW(hkeyVerb, NULL, NULL, NULL, &dwSubKeys, NULL,
905 NULL, NULL, NULL, NULL, NULL, NULL);
906 if (res != ERROR_SUCCESS)
908 ERR("failed to get subkey count with error %d\n", GetLastError());
909 return REGDB_E_READREGDB;
914 WARN("class %s has no verbs\n", debugstr_guid(clsid));
915 RegCloseKey(hkeyVerb);
916 return OLEOBJ_E_NOVERBS;
919 return EnumOLEVERB_Construct(hkeyVerb, 0, ppenum);
922 /******************************************************************************
923 * OleSetContainedObject [OLE32.@]
925 HRESULT WINAPI OleSetContainedObject(
929 IRunnableObject* runnable = NULL;
932 TRACE("(%p,%x)\n", pUnknown, fContained);
934 hres = IUnknown_QueryInterface(pUnknown,
935 &IID_IRunnableObject,
940 hres = IRunnableObject_SetContainedObject(runnable, fContained);
942 IRunnableObject_Release(runnable);
950 /******************************************************************************
953 * Set the OLE object to the running state.
956 * pUnknown [I] OLE object to run.
960 * Failure: Any HRESULT code.
962 HRESULT WINAPI OleRun(LPUNKNOWN pUnknown)
964 IRunnableObject *runable;
967 TRACE("(%p)\n", pUnknown);
969 hres = IUnknown_QueryInterface(pUnknown, &IID_IRunnableObject, (void**)&runable);
971 return S_OK; /* Appears to return no error. */
973 hres = IRunnableObject_Run(runable, NULL);
974 IRunnableObject_Release(runable);
978 /******************************************************************************
981 HRESULT WINAPI OleLoad(
984 LPOLECLIENTSITE pClientSite,
987 IPersistStorage* persistStorage = NULL;
989 IOleObject* pOleObject = NULL;
993 TRACE("(%p, %s, %p, %p)\n", pStg, debugstr_guid(riid), pClientSite, ppvObj);
998 * TODO, Conversion ... OleDoAutoConvert
1002 * Get the class ID for the object.
1004 hres = IStorage_Stat(pStg, &storageInfo, STATFLAG_NONAME);
1007 * Now, try and create the handler for the object
1009 hres = CoCreateInstance(&storageInfo.clsid,
1011 CLSCTX_INPROC_HANDLER|CLSCTX_INPROC_SERVER,
1016 * If that fails, as it will most times, load the default
1021 hres = OleCreateDefaultHandler(&storageInfo.clsid,
1028 * If we couldn't find a handler... this is bad. Abort the whole thing.
1035 hres = IUnknown_QueryInterface(pUnk, &IID_IOleObject, (void **)&pOleObject);
1036 if (SUCCEEDED(hres))
1039 hres = IOleObject_GetMiscStatus(pOleObject, DVASPECT_CONTENT, &dwStatus);
1043 if (SUCCEEDED(hres))
1045 * Initialize the object with it's IPersistStorage interface.
1047 hres = IOleObject_QueryInterface(pUnk,
1048 &IID_IPersistStorage,
1049 (void**)&persistStorage);
1051 if (SUCCEEDED(hres))
1053 hres = IPersistStorage_Load(persistStorage, pStg);
1055 IPersistStorage_Release(persistStorage);
1056 persistStorage = NULL;
1059 if (SUCCEEDED(hres) && pClientSite)
1061 * Inform the new object of it's client site.
1063 hres = IOleObject_SetClientSite(pOleObject, pClientSite);
1066 * Cleanup interfaces used internally
1069 IOleObject_Release(pOleObject);
1071 if (SUCCEEDED(hres))
1075 hres1 = IUnknown_QueryInterface(pUnk, &IID_IOleLink, (void **)&pOleLink);
1076 if (SUCCEEDED(hres1))
1078 FIXME("handle OLE link\n");
1079 IOleLink_Release(pOleLink);
1085 IUnknown_Release(pUnk);
1094 /***********************************************************************
1097 HRESULT WINAPI OleSave(
1098 LPPERSISTSTORAGE pPS,
1105 TRACE("(%p,%p,%x)\n", pPS, pStg, fSameAsLoad);
1108 * First, we transfer the class ID (if available)
1110 hres = IPersistStorage_GetClassID(pPS, &objectClass);
1112 if (SUCCEEDED(hres))
1114 WriteClassStg(pStg, &objectClass);
1118 * Then, we ask the object to save itself to the
1119 * storage. If it is successful, we commit the storage.
1121 hres = IPersistStorage_Save(pPS, pStg, fSameAsLoad);
1123 if (SUCCEEDED(hres))
1125 IStorage_Commit(pStg,
1133 /******************************************************************************
1134 * OleLockRunning [OLE32.@]
1136 HRESULT WINAPI OleLockRunning(LPUNKNOWN pUnknown, BOOL fLock, BOOL fLastUnlockCloses)
1138 IRunnableObject* runnable = NULL;
1141 TRACE("(%p,%x,%x)\n", pUnknown, fLock, fLastUnlockCloses);
1143 hres = IUnknown_QueryInterface(pUnknown,
1144 &IID_IRunnableObject,
1147 if (SUCCEEDED(hres))
1149 hres = IRunnableObject_LockRunning(runnable, fLock, fLastUnlockCloses);
1151 IRunnableObject_Release(runnable);
1160 /**************************************************************************
1161 * Internal methods to manage the shared OLE menu in response to the
1162 * OLE***MenuDescriptor API
1166 * OLEMenu_Initialize()
1168 * Initializes the OLEMENU data structures.
1170 static void OLEMenu_Initialize(void)
1175 * OLEMenu_UnInitialize()
1177 * Releases the OLEMENU data structures.
1179 static void OLEMenu_UnInitialize(void)
1183 /*************************************************************************
1184 * OLEMenu_InstallHooks
1185 * Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
1187 * RETURNS: TRUE if message hooks were successfully installed
1190 static BOOL OLEMenu_InstallHooks( DWORD tid )
1192 OleMenuHookItem *pHookItem;
1194 /* Create an entry for the hook table */
1195 if ( !(pHookItem = HeapAlloc(GetProcessHeap(), 0,
1196 sizeof(OleMenuHookItem)) ) )
1199 pHookItem->tid = tid;
1200 pHookItem->hHeap = GetProcessHeap();
1201 pHookItem->CallWndProc_hHook = NULL;
1203 /* Install a thread scope message hook for WH_GETMESSAGE */
1204 pHookItem->GetMsg_hHook = SetWindowsHookExW( WH_GETMESSAGE, OLEMenu_GetMsgProc,
1205 0, GetCurrentThreadId() );
1206 if ( !pHookItem->GetMsg_hHook )
1209 /* Install a thread scope message hook for WH_CALLWNDPROC */
1210 pHookItem->CallWndProc_hHook = SetWindowsHookExW( WH_CALLWNDPROC, OLEMenu_CallWndProc,
1211 0, GetCurrentThreadId() );
1212 if ( !pHookItem->CallWndProc_hHook )
1215 /* Insert the hook table entry */
1216 pHookItem->next = hook_list;
1217 hook_list = pHookItem;
1222 /* Unhook any hooks */
1223 if ( pHookItem->GetMsg_hHook )
1224 UnhookWindowsHookEx( pHookItem->GetMsg_hHook );
1225 if ( pHookItem->CallWndProc_hHook )
1226 UnhookWindowsHookEx( pHookItem->CallWndProc_hHook );
1227 /* Release the hook table entry */
1228 HeapFree(pHookItem->hHeap, 0, pHookItem );
1233 /*************************************************************************
1234 * OLEMenu_UnInstallHooks
1235 * UnInstall thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
1237 * RETURNS: TRUE if message hooks were successfully installed
1240 static BOOL OLEMenu_UnInstallHooks( DWORD tid )
1242 OleMenuHookItem *pHookItem = NULL;
1243 OleMenuHookItem **ppHook = &hook_list;
1247 if ((*ppHook)->tid == tid)
1249 pHookItem = *ppHook;
1250 *ppHook = pHookItem->next;
1253 ppHook = &(*ppHook)->next;
1255 if (!pHookItem) return FALSE;
1257 /* Uninstall the hooks installed for this thread */
1258 if ( !UnhookWindowsHookEx( pHookItem->GetMsg_hHook ) )
1260 if ( !UnhookWindowsHookEx( pHookItem->CallWndProc_hHook ) )
1263 /* Release the hook table entry */
1264 HeapFree(pHookItem->hHeap, 0, pHookItem );
1269 /* Release the hook table entry */
1270 HeapFree(pHookItem->hHeap, 0, pHookItem );
1275 /*************************************************************************
1276 * OLEMenu_IsHookInstalled
1277 * Tests if OLEMenu hooks have been installed for a thread
1279 * RETURNS: The pointer and index of the hook table entry for the tid
1280 * NULL and -1 for the index if no hooks were installed for this thread
1282 static OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid )
1284 OleMenuHookItem *pHookItem;
1286 /* Do a simple linear search for an entry whose tid matches ours.
1287 * We really need a map but efficiency is not a concern here. */
1288 for (pHookItem = hook_list; pHookItem; pHookItem = pHookItem->next)
1290 if ( tid == pHookItem->tid )
1297 /***********************************************************************
1298 * OLEMenu_FindMainMenuIndex
1300 * Used by OLEMenu API to find the top level group a menu item belongs to.
1301 * On success pnPos contains the index of the item in the top level menu group
1303 * RETURNS: TRUE if the ID was found, FALSE on failure
1305 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos )
1309 nItems = GetMenuItemCount( hMainMenu );
1311 for (i = 0; i < nItems; i++)
1315 /* Is the current item a submenu? */
1316 if ( (hsubmenu = GetSubMenu(hMainMenu, i)) )
1318 /* If the handle is the same we're done */
1319 if ( hsubmenu == hPopupMenu )
1325 /* Recursively search without updating pnPos */
1326 else if ( OLEMenu_FindMainMenuIndex( hsubmenu, hPopupMenu, NULL ) )
1338 /***********************************************************************
1339 * OLEMenu_SetIsServerMenu
1341 * Checks whether a popup menu belongs to a shared menu group which is
1342 * owned by the server, and sets the menu descriptor state accordingly.
1343 * All menu messages from these groups should be routed to the server.
1345 * RETURNS: TRUE if the popup menu is part of a server owned group
1346 * FALSE if the popup menu is part of a container owned group
1348 static BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor )
1350 UINT nPos = 0, nWidth, i;
1352 pOleMenuDescriptor->bIsServerItem = FALSE;
1354 /* Don't bother searching if the popup is the combined menu itself */
1355 if ( hmenu == pOleMenuDescriptor->hmenuCombined )
1358 /* Find the menu item index in the shared OLE menu that this item belongs to */
1359 if ( !OLEMenu_FindMainMenuIndex( pOleMenuDescriptor->hmenuCombined, hmenu, &nPos ) )
1362 /* The group widths array has counts for the number of elements
1363 * in the groups File, Edit, Container, Object, Window, Help.
1364 * The Edit, Object & Help groups belong to the server object
1365 * and the other three belong to the container.
1366 * Loop through the group widths and locate the group we are a member of.
1368 for ( i = 0, nWidth = 0; i < 6; i++ )
1370 nWidth += pOleMenuDescriptor->mgw.width[i];
1371 if ( nPos < nWidth )
1373 /* Odd elements are server menu widths */
1374 pOleMenuDescriptor->bIsServerItem = (i%2) ? TRUE : FALSE;
1379 return pOleMenuDescriptor->bIsServerItem;
1382 /*************************************************************************
1383 * OLEMenu_CallWndProc
1384 * Thread scope WH_CALLWNDPROC hook proc filter function (callback)
1385 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1387 static LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam)
1390 HOLEMENU hOleMenu = 0;
1391 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1392 OleMenuHookItem *pHookItem = NULL;
1395 TRACE("%i, %04lx, %08lx\n", code, wParam, lParam );
1397 /* Check if we're being asked to process the message */
1398 if ( HC_ACTION != code )
1401 /* Retrieve the current message being dispatched from lParam */
1402 pMsg = (LPCWPSTRUCT)lParam;
1404 /* Check if the message is destined for a window we are interested in:
1405 * If the window has an OLEMenu property we may need to dispatch
1406 * the menu message to its active objects window instead. */
1408 hOleMenu = GetPropW( pMsg->hwnd, prop_olemenuW );
1412 /* Get the menu descriptor */
1413 pOleMenuDescriptor = GlobalLock( hOleMenu );
1414 if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1417 /* Process menu messages */
1418 switch( pMsg->message )
1422 /* Reset the menu descriptor state */
1423 pOleMenuDescriptor->bIsServerItem = FALSE;
1425 /* Send this message to the server as well */
1426 SendMessageW( pOleMenuDescriptor->hwndActiveObject,
1427 pMsg->message, pMsg->wParam, pMsg->lParam );
1431 case WM_INITMENUPOPUP:
1433 /* Save the state for whether this is a server owned menu */
1434 OLEMenu_SetIsServerMenu( (HMENU)pMsg->wParam, pOleMenuDescriptor );
1440 fuFlags = HIWORD(pMsg->wParam); /* Get flags */
1441 if ( fuFlags & MF_SYSMENU )
1444 /* Save the state for whether this is a server owned popup menu */
1445 else if ( fuFlags & MF_POPUP )
1446 OLEMenu_SetIsServerMenu( (HMENU)pMsg->lParam, pOleMenuDescriptor );
1453 LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT) pMsg->lParam;
1454 if ( pMsg->wParam != 0 || lpdis->CtlType != ODT_MENU )
1455 goto NEXTHOOK; /* Not a menu message */
1464 /* If the message was for the server dispatch it accordingly */
1465 if ( pOleMenuDescriptor->bIsServerItem )
1467 SendMessageW( pOleMenuDescriptor->hwndActiveObject,
1468 pMsg->message, pMsg->wParam, pMsg->lParam );
1472 if ( pOleMenuDescriptor )
1473 GlobalUnlock( hOleMenu );
1475 /* Lookup the hook item for the current thread */
1476 if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1478 /* This should never fail!! */
1479 WARN("could not retrieve hHook for current thread!\n" );
1483 /* Pass on the message to the next hooker */
1484 return CallNextHookEx( pHookItem->CallWndProc_hHook, code, wParam, lParam );
1487 /*************************************************************************
1488 * OLEMenu_GetMsgProc
1489 * Thread scope WH_GETMESSAGE hook proc filter function (callback)
1490 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1492 static LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam)
1495 HOLEMENU hOleMenu = 0;
1496 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1497 OleMenuHookItem *pHookItem = NULL;
1500 TRACE("%i, %04lx, %08lx\n", code, wParam, lParam );
1502 /* Check if we're being asked to process a messages */
1503 if ( HC_ACTION != code )
1506 /* Retrieve the current message being dispatched from lParam */
1507 pMsg = (LPMSG)lParam;
1509 /* Check if the message is destined for a window we are interested in:
1510 * If the window has an OLEMenu property we may need to dispatch
1511 * the menu message to its active objects window instead. */
1513 hOleMenu = GetPropW( pMsg->hwnd, prop_olemenuW );
1517 /* Process menu messages */
1518 switch( pMsg->message )
1522 wCode = HIWORD(pMsg->wParam); /* Get notification code */
1524 goto NEXTHOOK; /* Not a menu message */
1531 /* Get the menu descriptor */
1532 pOleMenuDescriptor = GlobalLock( hOleMenu );
1533 if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1536 /* If the message was for the server dispatch it accordingly */
1537 if ( pOleMenuDescriptor->bIsServerItem )
1539 /* Change the hWnd in the message to the active objects hWnd.
1540 * The message loop which reads this message will automatically
1541 * dispatch it to the embedded objects window. */
1542 pMsg->hwnd = pOleMenuDescriptor->hwndActiveObject;
1546 if ( pOleMenuDescriptor )
1547 GlobalUnlock( hOleMenu );
1549 /* Lookup the hook item for the current thread */
1550 if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1552 /* This should never fail!! */
1553 WARN("could not retrieve hHook for current thread!\n" );
1557 /* Pass on the message to the next hooker */
1558 return CallNextHookEx( pHookItem->GetMsg_hHook, code, wParam, lParam );
1561 /***********************************************************************
1562 * OleCreateMenuDescriptor [OLE32.@]
1563 * Creates an OLE menu descriptor for OLE to use when dispatching
1564 * menu messages and commands.
1567 * hmenuCombined - Handle to the objects combined menu
1568 * lpMenuWidths - Pointer to array of 6 LONG's indicating menus per group
1571 HOLEMENU WINAPI OleCreateMenuDescriptor(
1572 HMENU hmenuCombined,
1573 LPOLEMENUGROUPWIDTHS lpMenuWidths)
1576 OleMenuDescriptor *pOleMenuDescriptor;
1579 if ( !hmenuCombined || !lpMenuWidths )
1582 /* Create an OLE menu descriptor */
1583 if ( !(hOleMenu = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT,
1584 sizeof(OleMenuDescriptor) ) ) )
1587 pOleMenuDescriptor = GlobalLock( hOleMenu );
1588 if ( !pOleMenuDescriptor )
1591 /* Initialize menu group widths and hmenu */
1592 for ( i = 0; i < 6; i++ )
1593 pOleMenuDescriptor->mgw.width[i] = lpMenuWidths->width[i];
1595 pOleMenuDescriptor->hmenuCombined = hmenuCombined;
1596 pOleMenuDescriptor->bIsServerItem = FALSE;
1597 GlobalUnlock( hOleMenu );
1602 /***********************************************************************
1603 * OleDestroyMenuDescriptor [OLE32.@]
1604 * Destroy the shared menu descriptor
1606 HRESULT WINAPI OleDestroyMenuDescriptor(
1607 HOLEMENU hmenuDescriptor)
1609 if ( hmenuDescriptor )
1610 GlobalFree( hmenuDescriptor );
1614 /***********************************************************************
1615 * OleSetMenuDescriptor [OLE32.@]
1616 * Installs or removes OLE dispatching code for the containers frame window.
1619 * hOleMenu Handle to composite menu descriptor
1620 * hwndFrame Handle to containers frame window
1621 * hwndActiveObject Handle to objects in-place activation window
1622 * lpFrame Pointer to IOleInPlaceFrame on containers window
1623 * lpActiveObject Pointer to IOleInPlaceActiveObject on active in-place object
1626 * S_OK - menu installed correctly
1627 * E_FAIL, E_INVALIDARG, E_UNEXPECTED - failure
1630 * The lpFrame and lpActiveObject parameters are currently ignored
1631 * OLE should install context sensitive help F1 filtering for the app when
1632 * these are non null.
1634 HRESULT WINAPI OleSetMenuDescriptor(
1637 HWND hwndActiveObject,
1638 LPOLEINPLACEFRAME lpFrame,
1639 LPOLEINPLACEACTIVEOBJECT lpActiveObject)
1641 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1644 if ( !hwndFrame || (hOleMenu && !hwndActiveObject) )
1645 return E_INVALIDARG;
1647 if ( lpFrame || lpActiveObject )
1649 FIXME("(%p, %p, %p, %p, %p), Context sensitive help filtering not implemented!\n",
1657 /* Set up a message hook to intercept the containers frame window messages.
1658 * The message filter is responsible for dispatching menu messages from the
1659 * shared menu which are intended for the object.
1662 if ( hOleMenu ) /* Want to install dispatching code */
1664 /* If OLEMenu hooks are already installed for this thread, fail
1665 * Note: This effectively means that OleSetMenuDescriptor cannot
1666 * be called twice in succession on the same frame window
1667 * without first calling it with a null hOleMenu to uninstall */
1668 if ( OLEMenu_IsHookInstalled( GetCurrentThreadId() ) )
1671 /* Get the menu descriptor */
1672 pOleMenuDescriptor = GlobalLock( hOleMenu );
1673 if ( !pOleMenuDescriptor )
1674 return E_UNEXPECTED;
1676 /* Update the menu descriptor */
1677 pOleMenuDescriptor->hwndFrame = hwndFrame;
1678 pOleMenuDescriptor->hwndActiveObject = hwndActiveObject;
1680 GlobalUnlock( hOleMenu );
1681 pOleMenuDescriptor = NULL;
1683 /* Add a menu descriptor windows property to the frame window */
1684 SetPropW( hwndFrame, prop_olemenuW, hOleMenu );
1686 /* Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC */
1687 if ( !OLEMenu_InstallHooks( GetCurrentThreadId() ) )
1690 else /* Want to uninstall dispatching code */
1692 /* Uninstall the hooks */
1693 if ( !OLEMenu_UnInstallHooks( GetCurrentThreadId() ) )
1696 /* Remove the menu descriptor property from the frame window */
1697 RemovePropW( hwndFrame, prop_olemenuW );
1703 /******************************************************************************
1704 * IsAccelerator [OLE32.@]
1705 * Mostly copied from controls/menu.c TranslateAccelerator implementation
1707 BOOL WINAPI IsAccelerator(HACCEL hAccel, int cAccelEntries, LPMSG lpMsg, WORD* lpwCmd)
1712 if(!lpMsg) return FALSE;
1715 WARN_(accel)("NULL accel handle\n");
1718 if((lpMsg->message != WM_KEYDOWN &&
1719 lpMsg->message != WM_SYSKEYDOWN &&
1720 lpMsg->message != WM_SYSCHAR &&
1721 lpMsg->message != WM_CHAR)) return FALSE;
1722 lpAccelTbl = HeapAlloc(GetProcessHeap(), 0, cAccelEntries * sizeof(ACCEL));
1723 if (NULL == lpAccelTbl)
1727 if (CopyAcceleratorTableW(hAccel, lpAccelTbl, cAccelEntries) != cAccelEntries)
1729 WARN_(accel)("CopyAcceleratorTableW failed\n");
1730 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
1734 TRACE_(accel)("hAccel=%p, cAccelEntries=%d,"
1735 "msg->hwnd=%p, msg->message=%04x, wParam=%08lx, lParam=%08lx\n",
1736 hAccel, cAccelEntries,
1737 lpMsg->hwnd, lpMsg->message, lpMsg->wParam, lpMsg->lParam);
1738 for(i = 0; i < cAccelEntries; i++)
1740 if(lpAccelTbl[i].key != lpMsg->wParam)
1743 if(lpMsg->message == WM_CHAR)
1745 if(!(lpAccelTbl[i].fVirt & FALT) && !(lpAccelTbl[i].fVirt & FVIRTKEY))
1747 TRACE_(accel)("found accel for WM_CHAR: ('%c')\n", LOWORD(lpMsg->wParam) & 0xff);
1753 if(lpAccelTbl[i].fVirt & FVIRTKEY)
1756 TRACE_(accel)("found accel for virt_key %04lx (scan %04x)\n",
1757 lpMsg->wParam, HIWORD(lpMsg->lParam) & 0xff);
1758 if(GetKeyState(VK_SHIFT) & 0x8000) mask |= FSHIFT;
1759 if(GetKeyState(VK_CONTROL) & 0x8000) mask |= FCONTROL;
1760 if(GetKeyState(VK_MENU) & 0x8000) mask |= FALT;
1761 if(mask == (lpAccelTbl[i].fVirt & (FSHIFT | FCONTROL | FALT))) goto found;
1762 TRACE_(accel)("incorrect SHIFT/CTRL/ALT-state\n");
1766 if(!(lpMsg->lParam & 0x01000000)) /* no special_key */
1768 if((lpAccelTbl[i].fVirt & FALT) && (lpMsg->lParam & 0x20000000))
1769 { /* ^^ ALT pressed */
1770 TRACE_(accel)("found accel for Alt-%c\n", LOWORD(lpMsg->wParam) & 0xff);
1778 WARN_(accel)("couldn't translate accelerator key\n");
1779 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
1783 if(lpwCmd) *lpwCmd = lpAccelTbl[i].cmd;
1784 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
1788 /***********************************************************************
1789 * ReleaseStgMedium [OLE32.@]
1791 void WINAPI ReleaseStgMedium(
1794 switch (pmedium->tymed)
1798 if ( (pmedium->pUnkForRelease==0) &&
1799 (pmedium->u.hGlobal!=0) )
1800 GlobalFree(pmedium->u.hGlobal);
1805 if (pmedium->u.lpszFileName!=0)
1807 if (pmedium->pUnkForRelease==0)
1809 DeleteFileW(pmedium->u.lpszFileName);
1812 CoTaskMemFree(pmedium->u.lpszFileName);
1818 if (pmedium->u.pstm!=0)
1820 IStream_Release(pmedium->u.pstm);
1824 case TYMED_ISTORAGE:
1826 if (pmedium->u.pstg!=0)
1828 IStorage_Release(pmedium->u.pstg);
1834 if ( (pmedium->pUnkForRelease==0) &&
1835 (pmedium->u.hBitmap!=0) )
1836 DeleteObject(pmedium->u.hBitmap);
1841 if ( (pmedium->pUnkForRelease==0) &&
1842 (pmedium->u.hMetaFilePict!=0) )
1844 LPMETAFILEPICT pMP = GlobalLock(pmedium->u.hMetaFilePict);
1845 DeleteMetaFile(pMP->hMF);
1846 GlobalUnlock(pmedium->u.hMetaFilePict);
1847 GlobalFree(pmedium->u.hMetaFilePict);
1853 if ( (pmedium->pUnkForRelease==0) &&
1854 (pmedium->u.hEnhMetaFile!=0) )
1856 DeleteEnhMetaFile(pmedium->u.hEnhMetaFile);
1864 pmedium->tymed=TYMED_NULL;
1867 * After cleaning up, the unknown is released
1869 if (pmedium->pUnkForRelease!=0)
1871 IUnknown_Release(pmedium->pUnkForRelease);
1872 pmedium->pUnkForRelease = 0;
1877 * OLEDD_Initialize()
1879 * Initializes the OLE drag and drop data structures.
1881 static void OLEDD_Initialize(void)
1885 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
1886 wndClass.style = CS_GLOBALCLASS;
1887 wndClass.lpfnWndProc = OLEDD_DragTrackerWindowProc;
1888 wndClass.cbClsExtra = 0;
1889 wndClass.cbWndExtra = sizeof(TrackerWindowInfo*);
1890 wndClass.hCursor = 0;
1891 wndClass.hbrBackground = 0;
1892 wndClass.lpszClassName = OLEDD_DRAGTRACKERCLASS;
1894 RegisterClassW (&wndClass);
1898 * OLEDD_FreeDropTarget()
1900 * Frees the drag and drop data structure
1902 static void OLEDD_FreeDropTarget(DropTargetNode *dropTargetInfo, BOOL release_drop_target)
1904 list_remove(&dropTargetInfo->entry);
1905 if (release_drop_target) IDropTarget_Release(dropTargetInfo->dropTarget);
1906 HeapFree(GetProcessHeap(), 0, dropTargetInfo);
1910 * OLEDD_UnInitialize()
1912 * Releases the OLE drag and drop data structures.
1914 void OLEDD_UnInitialize(void)
1917 * Simply empty the list.
1919 while (!list_empty(&targetListHead))
1921 DropTargetNode* curNode = LIST_ENTRY(list_head(&targetListHead), DropTargetNode, entry);
1922 OLEDD_FreeDropTarget(curNode, FALSE);
1927 * OLEDD_FindDropTarget()
1929 * Finds information about the drop target.
1931 static DropTargetNode* OLEDD_FindDropTarget(HWND hwndOfTarget)
1933 DropTargetNode* curNode;
1936 * Iterate the list to find the HWND value.
1938 LIST_FOR_EACH_ENTRY(curNode, &targetListHead, DropTargetNode, entry)
1939 if (hwndOfTarget==curNode->hwndTarget)
1943 * If we get here, the item is not in the list
1949 * OLEDD_DragTrackerWindowProc()
1951 * This method is the WindowProcedure of the drag n drop tracking
1952 * window. During a drag n Drop operation, an invisible window is created
1953 * to receive the user input and act upon it. This procedure is in charge
1957 #define DRAG_TIMER_ID 1
1959 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(
1969 LPCREATESTRUCTA createStruct = (LPCREATESTRUCTA)lParam;
1971 SetWindowLongPtrW(hwnd, 0, (LONG_PTR)createStruct->lpCreateParams);
1972 SetTimer(hwnd, DRAG_TIMER_ID, 50, NULL);
1979 OLEDD_TrackMouseMove((TrackerWindowInfo*)GetWindowLongPtrA(hwnd, 0));
1985 case WM_LBUTTONDOWN:
1986 case WM_MBUTTONDOWN:
1987 case WM_RBUTTONDOWN:
1989 OLEDD_TrackStateChange((TrackerWindowInfo*)GetWindowLongPtrA(hwnd, 0));
1994 KillTimer(hwnd, DRAG_TIMER_ID);
2000 * This is a window proc after all. Let's call the default.
2002 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
2006 * OLEDD_TrackMouseMove()
2008 * This method is invoked while a drag and drop operation is in effect.
2009 * it will generate the appropriate callbacks in the drop source
2010 * and drop target. It will also provide the expected feedback to
2014 * trackerInfo - Pointer to the structure identifying the
2015 * drag & drop operation that is currently
2018 static void OLEDD_TrackMouseMove(TrackerWindowInfo* trackerInfo)
2020 HWND hwndNewTarget = 0;
2025 * Get the handle of the window under the mouse
2027 pt.x = trackerInfo->curMousePos.x;
2028 pt.y = trackerInfo->curMousePos.y;
2029 hwndNewTarget = WindowFromPoint(pt);
2032 * Every time, we re-initialize the effects passed to the
2033 * IDropTarget to the effects allowed by the source.
2035 *trackerInfo->pdwEffect = trackerInfo->dwOKEffect;
2038 * If we are hovering over the same target as before, send the
2039 * DragOver notification
2041 if ( (trackerInfo->curDragTarget != 0) &&
2042 (trackerInfo->curTargetHWND == hwndNewTarget) )
2044 IDropTarget_DragOver(trackerInfo->curDragTarget,
2045 trackerInfo->dwKeyState,
2046 trackerInfo->curMousePos,
2047 trackerInfo->pdwEffect);
2051 DropTargetNode* newDropTargetNode = 0;
2054 * If we changed window, we have to notify our old target and check for
2057 if (trackerInfo->curDragTarget!=0)
2059 IDropTarget_DragLeave(trackerInfo->curDragTarget);
2063 * Make sure we're hovering over a window.
2065 if (hwndNewTarget!=0)
2068 * Find-out if there is a drag target under the mouse
2070 HWND nexttar = hwndNewTarget;
2071 trackerInfo->curTargetHWND = hwndNewTarget;
2074 newDropTargetNode = OLEDD_FindDropTarget(nexttar);
2075 } while (!newDropTargetNode && (nexttar = GetParent(nexttar)) != 0);
2076 if(nexttar) hwndNewTarget = nexttar;
2078 trackerInfo->curDragTargetHWND = hwndNewTarget;
2079 trackerInfo->curDragTarget = newDropTargetNode ? newDropTargetNode->dropTarget : 0;
2082 * If there is, notify it that we just dragged-in
2084 if (trackerInfo->curDragTarget)
2086 hr = IDropTarget_DragEnter(trackerInfo->curDragTarget,
2087 trackerInfo->dataObject,
2088 trackerInfo->dwKeyState,
2089 trackerInfo->curMousePos,
2090 trackerInfo->pdwEffect);
2092 /* failed DragEnter() means invalid target */
2095 trackerInfo->curDragTargetHWND = 0;
2096 trackerInfo->curTargetHWND = 0;
2097 trackerInfo->curDragTarget = 0;
2104 * The mouse is not over a window so we don't track anything.
2106 trackerInfo->curDragTargetHWND = 0;
2107 trackerInfo->curTargetHWND = 0;
2108 trackerInfo->curDragTarget = 0;
2113 * Now that we have done that, we have to tell the source to give
2114 * us feedback on the work being done by the target. If we don't
2115 * have a target, simulate no effect.
2117 if (trackerInfo->curDragTarget==0)
2119 *trackerInfo->pdwEffect = DROPEFFECT_NONE;
2122 hr = IDropSource_GiveFeedback(trackerInfo->dropSource,
2123 *trackerInfo->pdwEffect);
2126 * When we ask for feedback from the drop source, sometimes it will
2127 * do all the necessary work and sometimes it will not handle it
2128 * when that's the case, we must display the standard drag and drop
2131 if (hr == DRAGDROP_S_USEDEFAULTCURSORS)
2135 if (*trackerInfo->pdwEffect & DROPEFFECT_MOVE)
2137 hCur = LoadCursorW(hProxyDll, MAKEINTRESOURCEW(1));
2139 else if (*trackerInfo->pdwEffect & DROPEFFECT_COPY)
2141 hCur = LoadCursorW(hProxyDll, MAKEINTRESOURCEW(2));
2143 else if (*trackerInfo->pdwEffect & DROPEFFECT_LINK)
2145 hCur = LoadCursorW(hProxyDll, MAKEINTRESOURCEW(3));
2149 hCur = LoadCursorW(hProxyDll, MAKEINTRESOURCEW(0));
2157 * OLEDD_TrackStateChange()
2159 * This method is invoked while a drag and drop operation is in effect.
2160 * It is used to notify the drop target/drop source callbacks when
2161 * the state of the keyboard or mouse button change.
2164 * trackerInfo - Pointer to the structure identifying the
2165 * drag & drop operation that is currently
2168 static void OLEDD_TrackStateChange(TrackerWindowInfo* trackerInfo)
2171 * Ask the drop source what to do with the operation.
2173 trackerInfo->returnValue = IDropSource_QueryContinueDrag(
2174 trackerInfo->dropSource,
2175 trackerInfo->escPressed,
2176 trackerInfo->dwKeyState);
2179 * All the return valued will stop the operation except the S_OK
2182 if (trackerInfo->returnValue!=S_OK)
2185 * Make sure the message loop in DoDragDrop stops
2187 trackerInfo->trackingDone = TRUE;
2190 * Release the mouse in case the drop target decides to show a popup
2191 * or a menu or something.
2196 * If we end-up over a target, drop the object in the target or
2197 * inform the target that the operation was cancelled.
2199 if (trackerInfo->curDragTarget)
2201 switch (trackerInfo->returnValue)
2204 * If the source wants us to complete the operation, we tell
2205 * the drop target that we just dropped the object in it.
2207 case DRAGDROP_S_DROP:
2208 if (*trackerInfo->pdwEffect != DROPEFFECT_NONE)
2209 IDropTarget_Drop(trackerInfo->curDragTarget,
2210 trackerInfo->dataObject,
2211 trackerInfo->dwKeyState,
2212 trackerInfo->curMousePos,
2213 trackerInfo->pdwEffect);
2215 IDropTarget_DragLeave(trackerInfo->curDragTarget);
2219 * If the source told us that we should cancel, fool the drop
2220 * target by telling it that the mouse left it's window.
2221 * Also set the drop effect to "NONE" in case the application
2222 * ignores the result of DoDragDrop.
2224 case DRAGDROP_S_CANCEL:
2225 IDropTarget_DragLeave(trackerInfo->curDragTarget);
2226 *trackerInfo->pdwEffect = DROPEFFECT_NONE;
2234 * OLEDD_GetButtonState()
2236 * This method will use the current state of the keyboard to build
2237 * a button state mask equivalent to the one passed in the
2238 * WM_MOUSEMOVE wParam.
2240 static DWORD OLEDD_GetButtonState(void)
2242 BYTE keyboardState[256];
2245 GetKeyboardState(keyboardState);
2247 if ( (keyboardState[VK_SHIFT] & 0x80) !=0)
2248 keyMask |= MK_SHIFT;
2250 if ( (keyboardState[VK_CONTROL] & 0x80) !=0)
2251 keyMask |= MK_CONTROL;
2253 if ( (keyboardState[VK_LBUTTON] & 0x80) !=0)
2254 keyMask |= MK_LBUTTON;
2256 if ( (keyboardState[VK_RBUTTON] & 0x80) !=0)
2257 keyMask |= MK_RBUTTON;
2259 if ( (keyboardState[VK_MBUTTON] & 0x80) !=0)
2260 keyMask |= MK_MBUTTON;
2266 * OLEDD_GetButtonState()
2268 * This method will read the default value of the registry key in
2269 * parameter and extract a DWORD value from it. The registry key value
2270 * can be in a string key or a DWORD key.
2273 * regKey - Key to read the default value from
2274 * pdwValue - Pointer to the location where the DWORD
2275 * value is returned. This value is not modified
2276 * if the value is not found.
2279 static void OLEUTL_ReadRegistryDWORDValue(
2284 DWORD cbData = sizeof(buffer);
2288 lres = RegQueryValueExA(regKey,
2295 if (lres==ERROR_SUCCESS)
2300 *pdwValue = *(DWORD*)buffer;
2305 *pdwValue = (DWORD)strtoul(buffer, NULL, 10);
2311 /******************************************************************************
2314 * The operation of this function is documented literally in the WinAPI
2315 * documentation to involve a QueryInterface for the IViewObject interface,
2316 * followed by a call to IViewObject::Draw.
2318 HRESULT WINAPI OleDraw(
2325 IViewObject *viewobject;
2327 hres = IUnknown_QueryInterface(pUnk,
2329 (void**)&viewobject);
2331 if (SUCCEEDED(hres))
2335 rectl.left = lprcBounds->left;
2336 rectl.right = lprcBounds->right;
2337 rectl.top = lprcBounds->top;
2338 rectl.bottom = lprcBounds->bottom;
2339 hres = IViewObject_Draw(viewobject, dwAspect, -1, 0, 0, 0, hdcDraw, &rectl, 0, 0, 0);
2341 IViewObject_Release(viewobject);
2346 return DV_E_NOIVIEWOBJECT;
2350 /***********************************************************************
2351 * OleTranslateAccelerator [OLE32.@]
2353 HRESULT WINAPI OleTranslateAccelerator (LPOLEINPLACEFRAME lpFrame,
2354 LPOLEINPLACEFRAMEINFO lpFrameInfo, LPMSG lpmsg)
2358 TRACE("(%p,%p,%p)\n", lpFrame, lpFrameInfo, lpmsg);
2360 if (IsAccelerator(lpFrameInfo->haccel,lpFrameInfo->cAccelEntries,lpmsg,&wID))
2361 return IOleInPlaceFrame_TranslateAccelerator(lpFrame,lpmsg,wID);
2366 /******************************************************************************
2367 * OleCreate [OLE32.@]
2370 HRESULT WINAPI OleCreate(
2374 LPFORMATETC pFormatEtc,
2375 LPOLECLIENTSITE pClientSite,
2380 IUnknown * pUnk = NULL;
2381 IOleObject *pOleObject = NULL;
2383 TRACE("(%s, %s, %d, %p, %p, %p, %p)\n", debugstr_guid(rclsid),
2384 debugstr_guid(riid), renderopt, pFormatEtc, pClientSite, pStg, ppvObj);
2386 hres = CoCreateInstance(rclsid, 0, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER, riid, (LPVOID*)&pUnk);
2388 if (SUCCEEDED(hres))
2389 hres = IStorage_SetClass(pStg, rclsid);
2391 if (pClientSite && SUCCEEDED(hres))
2393 hres = IUnknown_QueryInterface(pUnk, &IID_IOleObject, (LPVOID*)&pOleObject);
2394 if (SUCCEEDED(hres))
2397 hres = IOleObject_GetMiscStatus(pOleObject, DVASPECT_CONTENT, &dwStatus);
2401 if (SUCCEEDED(hres))
2403 IPersistStorage * pPS;
2404 if (SUCCEEDED((hres = IUnknown_QueryInterface(pUnk, &IID_IPersistStorage, (LPVOID*)&pPS))))
2406 TRACE("trying to set stg %p\n", pStg);
2407 hres = IPersistStorage_InitNew(pPS, pStg);
2408 TRACE("-- result 0x%08x\n", hres);
2409 IPersistStorage_Release(pPS);
2413 if (pClientSite && SUCCEEDED(hres))
2415 TRACE("trying to set clientsite %p\n", pClientSite);
2416 hres = IOleObject_SetClientSite(pOleObject, pClientSite);
2417 TRACE("-- result 0x%08x\n", hres);
2421 IOleObject_Release(pOleObject);
2423 if (((renderopt == OLERENDER_DRAW) || (renderopt == OLERENDER_FORMAT)) &&
2426 IRunnableObject *pRunnable;
2427 IOleCache *pOleCache;
2430 hres2 = IUnknown_QueryInterface(pUnk, &IID_IRunnableObject, (void **)&pRunnable);
2431 if (SUCCEEDED(hres2))
2433 hres = IRunnableObject_Run(pRunnable, NULL);
2434 IRunnableObject_Release(pRunnable);
2437 if (SUCCEEDED(hres))
2439 hres2 = IUnknown_QueryInterface(pUnk, &IID_IOleCache, (void **)&pOleCache);
2440 if (SUCCEEDED(hres2))
2443 hres = IOleCache_Cache(pOleCache, pFormatEtc, ADVF_PRIMEFIRST, &dwConnection);
2444 IOleCache_Release(pOleCache);
2449 if (FAILED(hres) && pUnk)
2451 IUnknown_Release(pUnk);
2457 TRACE("-- %p\n", pUnk);
2461 /******************************************************************************
2462 * OleGetAutoConvert [OLE32.@]
2464 HRESULT WINAPI OleGetAutoConvert(REFCLSID clsidOld, LPCLSID pClsidNew)
2466 static const WCHAR wszAutoConvertTo[] = {'A','u','t','o','C','o','n','v','e','r','t','T','o',0};
2468 WCHAR buf[CHARS_IN_GUID];
2472 res = COM_OpenKeyForCLSID(clsidOld, wszAutoConvertTo, KEY_READ, &hkey);
2477 if (RegQueryValueW(hkey, NULL, buf, &len))
2479 res = REGDB_E_KEYMISSING;
2482 res = CLSIDFromString(buf, pClsidNew);
2484 if (hkey) RegCloseKey(hkey);
2488 /******************************************************************************
2489 * OleSetAutoConvert [OLE32.@]
2491 HRESULT WINAPI OleSetAutoConvert(REFCLSID clsidOld, REFCLSID clsidNew)
2493 static const WCHAR wszAutoConvertTo[] = {'A','u','t','o','C','o','n','v','e','r','t','T','o',0};
2495 WCHAR szClsidNew[CHARS_IN_GUID];
2498 TRACE("(%s,%s)\n", debugstr_guid(clsidOld), debugstr_guid(clsidNew));
2500 res = COM_OpenKeyForCLSID(clsidOld, NULL, KEY_READ | KEY_WRITE, &hkey);
2503 StringFromGUID2(clsidNew, szClsidNew, CHARS_IN_GUID);
2504 if (RegSetValueW(hkey, wszAutoConvertTo, REG_SZ, szClsidNew, (strlenW(szClsidNew)+1) * sizeof(WCHAR)))
2506 res = REGDB_E_WRITEREGDB;
2511 if (hkey) RegCloseKey(hkey);
2515 /******************************************************************************
2516 * OleDoAutoConvert [OLE32.@]
2518 HRESULT WINAPI OleDoAutoConvert(LPSTORAGE pStg, LPCLSID pClsidNew)
2520 FIXME("(%p,%p) : stub\n",pStg,pClsidNew);
2524 /******************************************************************************
2525 * OleIsRunning [OLE32.@]
2527 BOOL WINAPI OleIsRunning(LPOLEOBJECT pObject)
2529 IRunnableObject *pRunnable;
2533 TRACE("(%p)\n", pObject);
2535 hr = IOleObject_QueryInterface(pObject, &IID_IRunnableObject, (void **)&pRunnable);
2538 running = IRunnableObject_IsRunning(pRunnable);
2539 IRunnableObject_Release(pRunnable);
2543 /***********************************************************************
2544 * OleNoteObjectVisible [OLE32.@]
2546 HRESULT WINAPI OleNoteObjectVisible(LPUNKNOWN pUnknown, BOOL bVisible)
2548 TRACE("(%p, %s)\n", pUnknown, bVisible ? "TRUE" : "FALSE");
2549 return CoLockObjectExternal(pUnknown, bVisible, TRUE);
2553 /***********************************************************************
2554 * OLE_FreeClipDataArray [internal]
2557 * frees the data associated with an array of CLIPDATAs
2559 static void OLE_FreeClipDataArray(ULONG count, CLIPDATA * pClipDataArray)
2562 for (i = 0; i < count; i++)
2563 if (pClipDataArray[i].pClipData)
2564 CoTaskMemFree(pClipDataArray[i].pClipData);
2567 /***********************************************************************
2568 * PropSysAllocString [OLE32.@]
2570 * Basically a copy of SysAllocStringLen.
2572 BSTR WINAPI PropSysAllocString(LPCOLESTR str)
2576 WCHAR* stringBuffer;
2581 len = lstrlenW(str);
2583 * Find the length of the buffer passed-in, in bytes.
2585 bufferSize = len * sizeof (WCHAR);
2588 * Allocate a new buffer to hold the string.
2589 * Don't forget to keep an empty spot at the beginning of the
2590 * buffer for the character count and an extra character at the
2593 newBuffer = HeapAlloc(GetProcessHeap(), 0,
2594 bufferSize + sizeof(WCHAR) + sizeof(DWORD));
2597 * If the memory allocation failed, return a null pointer.
2603 * Copy the length of the string in the placeholder.
2605 *newBuffer = bufferSize;
2608 * Skip the byte count.
2612 memcpy(newBuffer, str, bufferSize);
2615 * Make sure that there is a nul character at the end of the
2618 stringBuffer = (WCHAR*)newBuffer;
2619 stringBuffer[len] = '\0';
2621 return stringBuffer;
2624 /***********************************************************************
2625 * PropSysFreeString [OLE32.@]
2627 * Copy of SysFreeString.
2629 void WINAPI PropSysFreeString(LPOLESTR str)
2631 DWORD* bufferPointer;
2633 /* NULL is a valid parameter */
2637 * We have to be careful when we free a BSTR pointer, it points to
2638 * the beginning of the string but it skips the byte count contained
2639 * before the string.
2641 bufferPointer = (DWORD*)str;
2646 * Free the memory from its "real" origin.
2648 HeapFree(GetProcessHeap(), 0, bufferPointer);
2651 /******************************************************************************
2652 * Check if a PROPVARIANT's type is valid.
2654 static inline HRESULT PROPVARIANT_ValidateType(VARTYPE vt)
2681 case VT_STREAMED_OBJECT:
2682 case VT_STORED_OBJECT:
2683 case VT_BLOB_OBJECT:
2686 case VT_I2|VT_VECTOR:
2687 case VT_I4|VT_VECTOR:
2688 case VT_R4|VT_VECTOR:
2689 case VT_R8|VT_VECTOR:
2690 case VT_CY|VT_VECTOR:
2691 case VT_DATE|VT_VECTOR:
2692 case VT_BSTR|VT_VECTOR:
2693 case VT_ERROR|VT_VECTOR:
2694 case VT_BOOL|VT_VECTOR:
2695 case VT_VARIANT|VT_VECTOR:
2696 case VT_UI1|VT_VECTOR:
2697 case VT_UI2|VT_VECTOR:
2698 case VT_UI4|VT_VECTOR:
2699 case VT_I8|VT_VECTOR:
2700 case VT_UI8|VT_VECTOR:
2701 case VT_LPSTR|VT_VECTOR:
2702 case VT_LPWSTR|VT_VECTOR:
2703 case VT_FILETIME|VT_VECTOR:
2704 case VT_CF|VT_VECTOR:
2705 case VT_CLSID|VT_VECTOR:
2708 WARN("Bad type %d\n", vt);
2709 return STG_E_INVALIDPARAMETER;
2712 /***********************************************************************
2713 * PropVariantClear [OLE32.@]
2715 HRESULT WINAPI PropVariantClear(PROPVARIANT * pvar) /* [in/out] */
2719 TRACE("(%p)\n", pvar);
2724 hr = PROPVARIANT_ValidateType(pvar->vt);
2749 case VT_STREAMED_OBJECT:
2751 case VT_STORED_OBJECT:
2752 if (pvar->u.pStream)
2753 IUnknown_Release(pvar->u.pStream);
2758 /* pick an arbitrary typed pointer - we don't care about the type
2759 * as we are just freeing it */
2760 CoTaskMemFree(pvar->u.puuid);
2763 case VT_BLOB_OBJECT:
2764 CoTaskMemFree(pvar->u.blob.pBlobData);
2767 if (pvar->u.bstrVal)
2768 PropSysFreeString(pvar->u.bstrVal);
2771 if (pvar->u.pclipdata)
2773 OLE_FreeClipDataArray(1, pvar->u.pclipdata);
2774 CoTaskMemFree(pvar->u.pclipdata);
2778 if (pvar->vt & VT_VECTOR)
2782 switch (pvar->vt & ~VT_VECTOR)
2785 FreePropVariantArray(pvar->u.capropvar.cElems, pvar->u.capropvar.pElems);
2788 OLE_FreeClipDataArray(pvar->u.caclipdata.cElems, pvar->u.caclipdata.pElems);
2791 for (i = 0; i < pvar->u.cabstr.cElems; i++)
2792 PropSysFreeString(pvar->u.cabstr.pElems[i]);
2795 for (i = 0; i < pvar->u.calpstr.cElems; i++)
2796 CoTaskMemFree(pvar->u.calpstr.pElems[i]);
2799 for (i = 0; i < pvar->u.calpwstr.cElems; i++)
2800 CoTaskMemFree(pvar->u.calpwstr.pElems[i]);
2803 if (pvar->vt & ~VT_VECTOR)
2805 /* pick an arbitrary VT_VECTOR structure - they all have the same
2807 CoTaskMemFree(pvar->u.capropvar.pElems);
2811 WARN("Invalid/unsupported type %d\n", pvar->vt);
2814 ZeroMemory(pvar, sizeof(*pvar));
2819 /***********************************************************************
2820 * PropVariantCopy [OLE32.@]
2822 HRESULT WINAPI PropVariantCopy(PROPVARIANT *pvarDest, /* [out] */
2823 const PROPVARIANT *pvarSrc) /* [in] */
2828 TRACE("(%p, %p vt %04x)\n", pvarDest, pvarSrc, pvarSrc->vt);
2830 hr = PROPVARIANT_ValidateType(pvarSrc->vt);
2834 /* this will deal with most cases */
2835 *pvarDest = *pvarSrc;
2859 case VT_STREAMED_OBJECT:
2861 case VT_STORED_OBJECT:
2862 IUnknown_AddRef((LPUNKNOWN)pvarDest->u.pStream);
2865 pvarDest->u.puuid = CoTaskMemAlloc(sizeof(CLSID));
2866 *pvarDest->u.puuid = *pvarSrc->u.puuid;
2869 len = strlen(pvarSrc->u.pszVal);
2870 pvarDest->u.pszVal = CoTaskMemAlloc((len+1)*sizeof(CHAR));
2871 CopyMemory(pvarDest->u.pszVal, pvarSrc->u.pszVal, (len+1)*sizeof(CHAR));
2874 len = lstrlenW(pvarSrc->u.pwszVal);
2875 pvarDest->u.pwszVal = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
2876 CopyMemory(pvarDest->u.pwszVal, pvarSrc->u.pwszVal, (len+1)*sizeof(WCHAR));
2879 case VT_BLOB_OBJECT:
2880 if (pvarSrc->u.blob.pBlobData)
2882 len = pvarSrc->u.blob.cbSize;
2883 pvarDest->u.blob.pBlobData = CoTaskMemAlloc(len);
2884 CopyMemory(pvarDest->u.blob.pBlobData, pvarSrc->u.blob.pBlobData, len);
2888 pvarDest->u.bstrVal = PropSysAllocString(pvarSrc->u.bstrVal);
2891 if (pvarSrc->u.pclipdata)
2893 len = pvarSrc->u.pclipdata->cbSize - sizeof(pvarSrc->u.pclipdata->ulClipFmt);
2894 pvarDest->u.pclipdata = CoTaskMemAlloc(sizeof (CLIPDATA));
2895 pvarDest->u.pclipdata->cbSize = pvarSrc->u.pclipdata->cbSize;
2896 pvarDest->u.pclipdata->ulClipFmt = pvarSrc->u.pclipdata->ulClipFmt;
2897 pvarDest->u.pclipdata->pClipData = CoTaskMemAlloc(len);
2898 CopyMemory(pvarDest->u.pclipdata->pClipData, pvarSrc->u.pclipdata->pClipData, len);
2902 if (pvarSrc->vt & VT_VECTOR)
2907 switch(pvarSrc->vt & ~VT_VECTOR)
2909 case VT_I1: elemSize = sizeof(pvarSrc->u.cVal); break;
2910 case VT_UI1: elemSize = sizeof(pvarSrc->u.bVal); break;
2911 case VT_I2: elemSize = sizeof(pvarSrc->u.iVal); break;
2912 case VT_UI2: elemSize = sizeof(pvarSrc->u.uiVal); break;
2913 case VT_BOOL: elemSize = sizeof(pvarSrc->u.boolVal); break;
2914 case VT_I4: elemSize = sizeof(pvarSrc->u.lVal); break;
2915 case VT_UI4: elemSize = sizeof(pvarSrc->u.ulVal); break;
2916 case VT_R4: elemSize = sizeof(pvarSrc->u.fltVal); break;
2917 case VT_R8: elemSize = sizeof(pvarSrc->u.dblVal); break;
2918 case VT_ERROR: elemSize = sizeof(pvarSrc->u.scode); break;
2919 case VT_I8: elemSize = sizeof(pvarSrc->u.hVal); break;
2920 case VT_UI8: elemSize = sizeof(pvarSrc->u.uhVal); break;
2921 case VT_CY: elemSize = sizeof(pvarSrc->u.cyVal); break;
2922 case VT_DATE: elemSize = sizeof(pvarSrc->u.date); break;
2923 case VT_FILETIME: elemSize = sizeof(pvarSrc->u.filetime); break;
2924 case VT_CLSID: elemSize = sizeof(*pvarSrc->u.puuid); break;
2925 case VT_CF: elemSize = sizeof(*pvarSrc->u.pclipdata); break;
2926 case VT_BSTR: elemSize = sizeof(pvarSrc->u.bstrVal); break;
2927 case VT_LPSTR: elemSize = sizeof(pvarSrc->u.pszVal); break;
2928 case VT_LPWSTR: elemSize = sizeof(pvarSrc->u.pwszVal); break;
2929 case VT_VARIANT: elemSize = sizeof(*pvarSrc->u.pvarVal); break;
2932 FIXME("Invalid element type: %ul\n", pvarSrc->vt & ~VT_VECTOR);
2933 return E_INVALIDARG;
2935 len = pvarSrc->u.capropvar.cElems;
2936 pvarDest->u.capropvar.pElems = CoTaskMemAlloc(len * elemSize);
2937 if (pvarSrc->vt == (VT_VECTOR | VT_VARIANT))
2939 for (i = 0; i < len; i++)
2940 PropVariantCopy(&pvarDest->u.capropvar.pElems[i], &pvarSrc->u.capropvar.pElems[i]);
2942 else if (pvarSrc->vt == (VT_VECTOR | VT_CF))
2944 FIXME("Copy clipformats\n");
2946 else if (pvarSrc->vt == (VT_VECTOR | VT_BSTR))
2948 for (i = 0; i < len; i++)
2949 pvarDest->u.cabstr.pElems[i] = PropSysAllocString(pvarSrc->u.cabstr.pElems[i]);
2951 else if (pvarSrc->vt == (VT_VECTOR | VT_LPSTR))
2954 for (i = 0; i < len; i++)
2956 strLen = lstrlenA(pvarSrc->u.calpstr.pElems[i]) + 1;
2957 pvarDest->u.calpstr.pElems[i] = CoTaskMemAlloc(strLen);
2958 memcpy(pvarDest->u.calpstr.pElems[i],
2959 pvarSrc->u.calpstr.pElems[i], strLen);
2962 else if (pvarSrc->vt == (VT_VECTOR | VT_LPWSTR))
2965 for (i = 0; i < len; i++)
2967 strLen = (lstrlenW(pvarSrc->u.calpwstr.pElems[i]) + 1) *
2969 pvarDest->u.calpstr.pElems[i] = CoTaskMemAlloc(strLen);
2970 memcpy(pvarDest->u.calpstr.pElems[i],
2971 pvarSrc->u.calpstr.pElems[i], strLen);
2975 CopyMemory(pvarDest->u.capropvar.pElems, pvarSrc->u.capropvar.pElems, len * elemSize);
2978 WARN("Invalid/unsupported type %d\n", pvarSrc->vt);
2984 /***********************************************************************
2985 * FreePropVariantArray [OLE32.@]
2987 HRESULT WINAPI FreePropVariantArray(ULONG cVariants, /* [in] */
2988 PROPVARIANT *rgvars) /* [in/out] */
2992 TRACE("(%u, %p)\n", cVariants, rgvars);
2995 return E_INVALIDARG;
2997 for(i = 0; i < cVariants; i++)
2998 PropVariantClear(&rgvars[i]);
3003 /******************************************************************************
3004 * DllDebugObjectRPCHook (OLE32.@)
3005 * turns on and off internal debugging, pointer is only used on macintosh
3008 BOOL WINAPI DllDebugObjectRPCHook(BOOL b, void *dummy)