4 * Copyright 1995 Martin von Loewis
5 * Copyright 1999 Francis Beaudet
6 * Copyright 1999 Noel Borthwick
7 * Copyright 1999, 2000 Marcus Meissner
8 * Copyright 2005 Juan Lang
9 * Copyright 2011 Adam Martinson for CodeWeavers
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
35 #define NONAMELESSUNION
36 #define NONAMELESSSTRUCT
48 #include "wine/unicode.h"
49 #include "compobj_private.h"
50 #include "wine/list.h"
52 #include "wine/debug.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(ole);
55 WINE_DECLARE_DEBUG_CHANNEL(accel);
57 /******************************************************************************
58 * These are static/global variables and internal data structures that the
59 * OLE module uses to maintain it's state.
61 typedef struct tagTrackerWindowInfo
63 IDataObject* dataObject;
64 IDropSource* dropSource;
71 HWND curTargetHWND; /* window the mouse is hovering over */
72 HWND curDragTargetHWND; /* might be a ancestor of curTargetHWND */
73 IDropTarget* curDragTarget;
74 POINTL curMousePos; /* current position of the mouse in screen coordinates */
75 DWORD dwKeyState; /* current state of the shift and ctrl keys and the mouse buttons */
78 typedef struct tagOleMenuDescriptor /* OleMenuDescriptor */
80 HWND hwndFrame; /* The containers frame window */
81 HWND hwndActiveObject; /* The active objects window */
82 OLEMENUGROUPWIDTHS mgw; /* OLE menu group widths for the shared menu */
83 HMENU hmenuCombined; /* The combined menu */
84 BOOL bIsServerItem; /* True if the currently open popup belongs to the server */
87 typedef struct tagOleMenuHookItem /* OleMenu hook item in per thread hook list */
89 DWORD tid; /* Thread Id */
90 HANDLE hHeap; /* Heap this is allocated from */
91 HHOOK GetMsg_hHook; /* message hook for WH_GETMESSAGE */
92 HHOOK CallWndProc_hHook; /* message hook for WH_CALLWNDPROC */
93 struct tagOleMenuHookItem *next;
96 static OleMenuHookItem *hook_list;
99 * This is the lock count on the OLE library. It is controlled by the
100 * OLEInitialize/OLEUninitialize methods.
102 static LONG OLE_moduleLockCount = 0;
105 * Name of our registered window class.
107 static const WCHAR OLEDD_DRAGTRACKERCLASS[] =
108 {'W','i','n','e','D','r','a','g','D','r','o','p','T','r','a','c','k','e','r','3','2',0};
111 * Name of menu descriptor property.
113 static const WCHAR prop_olemenuW[] =
114 {'P','R','O','P','_','O','L','E','M','e','n','u','D','e','s','c','r','i','p','t','o','r',0};
116 /* property to store IDropTarget pointer */
117 static const WCHAR prop_oledroptarget[] =
118 {'O','l','e','D','r','o','p','T','a','r','g','e','t','I','n','t','e','r','f','a','c','e',0};
120 /* property to store Marshalled IDropTarget pointer */
121 static const WCHAR prop_marshalleddroptarget[] =
122 {'W','i','n','e','M','a','r','s','h','a','l','l','e','d','D','r','o','p','T','a','r','g','e','t',0};
124 static const WCHAR clsidfmtW[] =
125 {'C','L','S','I','D','\\','{','%','0','8','x','-','%','0','4','x','-','%','0','4','x','-',
126 '%','0','2','x','%','0','2','x','-','%','0','2','x','%','0','2','x','%','0','2','x','%','0','2','x',
127 '%','0','2','x','%','0','2','x','}','\\',0};
129 static const WCHAR emptyW[] = { 0 };
131 /******************************************************************************
132 * These are the prototypes of miscellaneous utility methods
134 static void OLEUTL_ReadRegistryDWORDValue(HKEY regKey, DWORD* pdwValue);
136 /******************************************************************************
137 * These are the prototypes of the utility methods used to manage a shared menu
139 static void OLEMenu_Initialize(void);
140 static void OLEMenu_UnInitialize(void);
141 static BOOL OLEMenu_InstallHooks( DWORD tid );
142 static BOOL OLEMenu_UnInstallHooks( DWORD tid );
143 static OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid );
144 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos );
145 static BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor );
146 static LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam);
147 static LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam);
149 /******************************************************************************
150 * These are the prototypes of the OLE Clipboard initialization methods (in clipboard.c)
152 extern void OLEClipbrd_UnInitialize(void);
153 extern void OLEClipbrd_Initialize(void);
155 /******************************************************************************
156 * These are the prototypes of the utility methods used for OLE Drag n Drop
158 static void OLEDD_Initialize(void);
159 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
160 static void OLEDD_TrackMouseMove(TrackerWindowInfo* trackerInfo);
161 static void OLEDD_TrackStateChange(TrackerWindowInfo* trackerInfo);
162 static DWORD OLEDD_GetButtonState(void);
164 /******************************************************************************
165 * OleBuildVersion [OLE32.@]
167 DWORD WINAPI OleBuildVersion(void)
169 TRACE("Returning version %d, build %d.\n", rmm, rup);
170 return (rmm<<16)+rup;
173 /***********************************************************************
174 * OleInitialize (OLE32.@)
176 HRESULT WINAPI OleInitialize(LPVOID reserved)
180 TRACE("(%p)\n", reserved);
183 * The first duty of the OleInitialize is to initialize the COM libraries.
185 hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
188 * If the CoInitializeEx call failed, the OLE libraries can't be
194 if (!COM_CurrentInfo()->ole_inits)
198 * Then, it has to initialize the OLE specific modules.
202 * Object linking and Embedding
203 * In-place activation
205 if (!COM_CurrentInfo()->ole_inits++ &&
206 InterlockedIncrement(&OLE_moduleLockCount) == 1)
209 * Initialize the libraries.
211 TRACE("() - Initializing the OLE libraries\n");
216 OLEClipbrd_Initialize();
226 OLEMenu_Initialize();
232 /******************************************************************************
233 * OleUninitialize [OLE32.@]
235 void WINAPI OleUninitialize(void)
240 * If we hit the bottom of the lock stack, free the libraries.
242 if (!--COM_CurrentInfo()->ole_inits && !InterlockedDecrement(&OLE_moduleLockCount))
245 * Actually free the libraries.
247 TRACE("() - Freeing the last reference count\n");
252 OLEClipbrd_UnInitialize();
257 OLEMenu_UnInitialize();
261 * Then, uninitialize the COM libraries.
266 /******************************************************************************
267 * OleInitializeWOW [OLE32.@]
269 HRESULT WINAPI OleInitializeWOW(DWORD x, DWORD y) {
270 FIXME("(0x%08x, 0x%08x),stub!\n",x, y);
274 /*************************************************************
275 * get_droptarget_handle
277 * Retrieve a handle to the map containing the marshalled IDropTarget.
278 * This handle belongs to the process that called RegisterDragDrop.
279 * See get_droptarget_local_handle().
281 static inline HANDLE get_droptarget_handle(HWND hwnd)
283 return GetPropW(hwnd, prop_marshalleddroptarget);
286 /*************************************************************
289 * Is the window a droptarget.
291 static inline BOOL is_droptarget(HWND hwnd)
293 return get_droptarget_handle(hwnd) != 0;
296 /*************************************************************
297 * get_droptarget_local_handle
299 * Retrieve a handle to the map containing the marshalled IDropTarget.
300 * The handle should be closed when finished with.
302 static HANDLE get_droptarget_local_handle(HWND hwnd)
304 HANDLE handle, local_handle = 0;
306 handle = get_droptarget_handle(hwnd);
313 GetWindowThreadProcessId(hwnd, &pid);
314 process = OpenProcess(PROCESS_DUP_HANDLE, FALSE, pid);
317 DuplicateHandle(process, handle, GetCurrentProcess(), &local_handle, 0, FALSE, DUPLICATE_SAME_ACCESS);
318 CloseHandle(process);
324 /***********************************************************************
325 * create_map_from_stream
327 * Helper for RegisterDragDrop. Creates a file mapping object
328 * with the contents of the provided stream. The stream must
329 * be a global memory backed stream.
331 static HRESULT create_map_from_stream(IStream *stream, HANDLE *map)
338 hr = GetHGlobalFromStream(stream, &hmem);
339 if(FAILED(hr)) return hr;
341 size = GlobalSize(hmem);
342 *map = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, size, NULL);
343 if(!*map) return E_OUTOFMEMORY;
345 data = MapViewOfFile(*map, FILE_MAP_WRITE, 0, 0, size);
346 memcpy(data, GlobalLock(hmem), size);
348 UnmapViewOfFile(data);
352 /***********************************************************************
353 * create_stream_from_map
355 * Creates a stream from the provided map.
357 static HRESULT create_stream_from_map(HANDLE map, IStream **stream)
359 HRESULT hr = E_OUTOFMEMORY;
362 MEMORY_BASIC_INFORMATION info;
364 data = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
367 VirtualQuery(data, &info, sizeof(info));
368 TRACE("size %d\n", (int)info.RegionSize);
370 hmem = GlobalAlloc(GMEM_MOVEABLE, info.RegionSize);
373 memcpy(GlobalLock(hmem), data, info.RegionSize);
375 hr = CreateStreamOnHGlobal(hmem, TRUE, stream);
377 UnmapViewOfFile(data);
381 /* This is to work around apps which break COM rules by not implementing
382 * IDropTarget::QueryInterface(). Windows doesn't expose this because it
383 * doesn't call CoMarshallInterface() in RegisterDragDrop().
384 * The wrapper is only used internally, and only exists for the life of
385 * the marshal. We don't want to hold a ref on the app provided target
386 * as some apps destroy this prior to CoUninitialize without calling
387 * RevokeDragDrop. The only (long-term) ref is held by the window prop. */
389 IDropTarget IDropTarget_iface;
394 static inline DropTargetWrapper* impl_from_IDropTarget(IDropTarget* iface)
396 return CONTAINING_RECORD(iface, DropTargetWrapper, IDropTarget_iface);
399 static HRESULT WINAPI DropTargetWrapper_QueryInterface(IDropTarget* iface,
403 DropTargetWrapper* This = impl_from_IDropTarget(iface);
404 if (IsEqualIID(riid, &IID_IUnknown) ||
405 IsEqualIID(riid, &IID_IDropTarget))
407 IDropTarget_AddRef(&This->IDropTarget_iface);
408 *ppvObject = &This->IDropTarget_iface;
412 return E_NOINTERFACE;
415 static ULONG WINAPI DropTargetWrapper_AddRef(IDropTarget* iface)
417 DropTargetWrapper* This = impl_from_IDropTarget(iface);
418 return InterlockedIncrement(&This->refs);
421 static ULONG WINAPI DropTargetWrapper_Release(IDropTarget* iface)
423 DropTargetWrapper* This = impl_from_IDropTarget(iface);
424 ULONG refs = InterlockedDecrement(&This->refs);
425 if (!refs) HeapFree(GetProcessHeap(), 0, This);
429 static inline HRESULT get_target_from_wrapper( IDropTarget *wrapper, IDropTarget **target )
431 DropTargetWrapper* This = impl_from_IDropTarget( wrapper );
432 *target = GetPropW( This->hwnd, prop_oledroptarget );
433 if (!*target) return DRAGDROP_E_NOTREGISTERED;
434 IDropTarget_AddRef( *target );
438 static HRESULT WINAPI DropTargetWrapper_DragEnter(IDropTarget* iface,
439 IDataObject* pDataObj,
445 HRESULT r = get_target_from_wrapper( iface, &target );
449 r = IDropTarget_DragEnter( target, pDataObj, grfKeyState, pt, pdwEffect );
450 IDropTarget_Release( target );
455 static HRESULT WINAPI DropTargetWrapper_DragOver(IDropTarget* iface,
461 HRESULT r = get_target_from_wrapper( iface, &target );
465 r = IDropTarget_DragOver( target, grfKeyState, pt, pdwEffect );
466 IDropTarget_Release( target );
471 static HRESULT WINAPI DropTargetWrapper_DragLeave(IDropTarget* iface)
474 HRESULT r = get_target_from_wrapper( iface, &target );
478 r = IDropTarget_DragLeave( target );
479 IDropTarget_Release( target );
484 static HRESULT WINAPI DropTargetWrapper_Drop(IDropTarget* iface,
485 IDataObject* pDataObj,
491 HRESULT r = get_target_from_wrapper( iface, &target );
495 r = IDropTarget_Drop( target, pDataObj, grfKeyState, pt, pdwEffect );
496 IDropTarget_Release( target );
501 static const IDropTargetVtbl DropTargetWrapperVTbl =
503 DropTargetWrapper_QueryInterface,
504 DropTargetWrapper_AddRef,
505 DropTargetWrapper_Release,
506 DropTargetWrapper_DragEnter,
507 DropTargetWrapper_DragOver,
508 DropTargetWrapper_DragLeave,
509 DropTargetWrapper_Drop
512 static IDropTarget* WrapDropTarget( HWND hwnd )
514 DropTargetWrapper* This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
518 This->IDropTarget_iface.lpVtbl = &DropTargetWrapperVTbl;
522 return &This->IDropTarget_iface;
525 /***********************************************************************
526 * get_droptarget_pointer
528 * Retrieves the marshalled IDropTarget from the window.
530 static IDropTarget* get_droptarget_pointer(HWND hwnd)
532 IDropTarget *droptarget = NULL;
536 map = get_droptarget_local_handle(hwnd);
537 if(!map) return NULL;
539 if(SUCCEEDED(create_stream_from_map(map, &stream)))
541 CoUnmarshalInterface(stream, &IID_IDropTarget, (void**)&droptarget);
542 IStream_Release(stream);
548 /***********************************************************************
549 * RegisterDragDrop (OLE32.@)
551 HRESULT WINAPI RegisterDragDrop(HWND hwnd, LPDROPTARGET pDropTarget)
557 IDropTarget *wrapper;
559 TRACE("(%p,%p)\n", hwnd, pDropTarget);
561 if (!COM_CurrentApt())
563 ERR("COM not initialized\n");
564 return E_OUTOFMEMORY;
572 ERR("invalid hwnd %p\n", hwnd);
573 return DRAGDROP_E_INVALIDHWND;
576 /* block register for other processes windows */
577 GetWindowThreadProcessId(hwnd, &pid);
578 if (pid != GetCurrentProcessId())
580 FIXME("register for another process windows is disabled\n");
581 return DRAGDROP_E_INVALIDHWND;
584 /* check if the window is already registered */
585 if (is_droptarget(hwnd))
586 return DRAGDROP_E_ALREADYREGISTERED;
589 * Marshal the drop target pointer into a shared memory map and
590 * store the map's handle in a Wine specific window prop. We also
591 * store the drop target pointer itself in the
592 * "OleDropTargetInterface" prop for compatibility with Windows.
595 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
596 if(FAILED(hr)) return hr;
598 /* IDropTarget::QueryInterface() shouldn't be called, some (broken) apps depend on this. */
599 wrapper = WrapDropTarget( hwnd );
602 IStream_Release(stream);
603 return E_OUTOFMEMORY;
605 hr = CoMarshalInterface(stream, &IID_IDropTarget, (IUnknown*)wrapper, MSHCTX_LOCAL, NULL, MSHLFLAGS_TABLESTRONG);
606 IDropTarget_Release(wrapper);
610 hr = create_map_from_stream(stream, &map);
613 IDropTarget_AddRef(pDropTarget);
614 SetPropW(hwnd, prop_oledroptarget, pDropTarget);
615 SetPropW(hwnd, prop_marshalleddroptarget, map);
621 IStream_Seek(stream, zero, STREAM_SEEK_SET, NULL);
622 CoReleaseMarshalData(stream);
625 IStream_Release(stream);
630 /***********************************************************************
631 * RevokeDragDrop (OLE32.@)
633 HRESULT WINAPI RevokeDragDrop(HWND hwnd)
637 IDropTarget *drop_target;
640 TRACE("(%p)\n", hwnd);
644 ERR("invalid hwnd %p\n", hwnd);
645 return DRAGDROP_E_INVALIDHWND;
648 /* no registration data */
649 if (!(map = get_droptarget_handle(hwnd)))
650 return DRAGDROP_E_NOTREGISTERED;
652 drop_target = GetPropW(hwnd, prop_oledroptarget);
653 if(drop_target) IDropTarget_Release(drop_target);
655 RemovePropW(hwnd, prop_oledroptarget);
656 RemovePropW(hwnd, prop_marshalleddroptarget);
658 hr = create_stream_from_map(map, &stream);
661 CoReleaseMarshalData(stream);
662 IStream_Release(stream);
669 /***********************************************************************
670 * OleRegGetUserType (OLE32.@)
672 * This implementation of OleRegGetUserType ignores the dwFormOfType
673 * parameter and always returns the full name of the object. This is
674 * not too bad since this is the case for many objects because of the
675 * way they are registered.
677 HRESULT WINAPI OleRegGetUserType(
680 LPOLESTR* pszUserType)
689 * Initialize the out parameter.
694 * Build the key name we're looking for
696 sprintfW( keyName, clsidfmtW,
697 clsid->Data1, clsid->Data2, clsid->Data3,
698 clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
699 clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );
701 TRACE("(%s, %d, %p)\n", debugstr_w(keyName), dwFormOfType, pszUserType);
704 * Open the class id Key
706 hres = open_classes_key(HKEY_CLASSES_ROOT, keyName, MAXIMUM_ALLOWED, &clsidKey);
707 if (hres != ERROR_SUCCESS)
708 return REGDB_E_CLASSNOTREG;
711 * Retrieve the size of the name string.
715 hres = RegQueryValueExW(clsidKey,
722 if (hres!=ERROR_SUCCESS)
724 RegCloseKey(clsidKey);
725 return REGDB_E_READREGDB;
729 * Allocate a buffer for the registry value.
731 *pszUserType = CoTaskMemAlloc(cbData);
733 if (*pszUserType==NULL)
735 RegCloseKey(clsidKey);
736 return E_OUTOFMEMORY;
739 hres = RegQueryValueExW(clsidKey,
743 (LPBYTE) *pszUserType,
746 RegCloseKey(clsidKey);
748 if (hres != ERROR_SUCCESS)
750 CoTaskMemFree(*pszUserType);
753 return REGDB_E_READREGDB;
759 /***********************************************************************
760 * DoDragDrop [OLE32.@]
762 HRESULT WINAPI DoDragDrop (
763 IDataObject *pDataObject, /* [in] ptr to the data obj */
764 IDropSource* pDropSource, /* [in] ptr to the source obj */
765 DWORD dwOKEffect, /* [in] effects allowed by the source */
766 DWORD *pdwEffect) /* [out] ptr to effects of the source */
768 static const WCHAR trackerW[] = {'T','r','a','c','k','e','r','W','i','n','d','o','w',0};
769 TrackerWindowInfo trackerInfo;
770 HWND hwndTrackWindow;
773 TRACE("(%p, %p, %08x, %p)\n", pDataObject, pDropSource, dwOKEffect, pdwEffect);
775 if (!pDataObject || !pDropSource || !pdwEffect)
779 * Setup the drag n drop tracking window.
782 trackerInfo.dataObject = pDataObject;
783 trackerInfo.dropSource = pDropSource;
784 trackerInfo.dwOKEffect = dwOKEffect;
785 trackerInfo.pdwEffect = pdwEffect;
786 trackerInfo.trackingDone = FALSE;
787 trackerInfo.escPressed = FALSE;
788 trackerInfo.curDragTargetHWND = 0;
789 trackerInfo.curTargetHWND = 0;
790 trackerInfo.curDragTarget = 0;
792 hwndTrackWindow = CreateWindowW(OLEDD_DRAGTRACKERCLASS, trackerW,
793 WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT,
794 CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, 0,
800 * Capture the mouse input
802 SetCapture(hwndTrackWindow);
807 * Pump messages. All mouse input should go to the capture window.
809 while (!trackerInfo.trackingDone && GetMessageW(&msg, 0, 0, 0) )
811 trackerInfo.curMousePos.x = msg.pt.x;
812 trackerInfo.curMousePos.y = msg.pt.y;
813 trackerInfo.dwKeyState = OLEDD_GetButtonState();
815 if ( (msg.message >= WM_KEYFIRST) &&
816 (msg.message <= WM_KEYLAST) )
819 * When keyboard messages are sent to windows on this thread, we
820 * want to ignore notify the drop source that the state changed.
821 * in the case of the Escape key, we also notify the drop source
822 * we give it a special meaning.
824 if ( (msg.message==WM_KEYDOWN) &&
825 (msg.wParam==VK_ESCAPE) )
827 trackerInfo.escPressed = TRUE;
831 * Notify the drop source.
833 OLEDD_TrackStateChange(&trackerInfo);
838 * Dispatch the messages only when it's not a keyboard message.
840 DispatchMessageW(&msg);
844 /* re-post the quit message to outer message loop */
845 if (msg.message == WM_QUIT)
846 PostQuitMessage(msg.wParam);
848 * Destroy the temporary window.
850 DestroyWindow(hwndTrackWindow);
852 return trackerInfo.returnValue;
858 /***********************************************************************
859 * OleQueryLinkFromData [OLE32.@]
861 HRESULT WINAPI OleQueryLinkFromData(
862 IDataObject* pSrcDataObject)
864 FIXME("(%p),stub!\n", pSrcDataObject);
868 /***********************************************************************
869 * OleRegGetMiscStatus [OLE32.@]
871 HRESULT WINAPI OleRegGetMiscStatus(
876 static const WCHAR miscstatusW[] = {'M','i','s','c','S','t','a','t','u','s',0};
877 static const WCHAR dfmtW[] = {'%','d',0};
885 * Initialize the out parameter.
890 * Build the key name we're looking for
892 sprintfW( keyName, clsidfmtW,
893 clsid->Data1, clsid->Data2, clsid->Data3,
894 clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
895 clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );
897 TRACE("(%s, %d, %p)\n", debugstr_w(keyName), dwAspect, pdwStatus);
900 * Open the class id Key
902 result = open_classes_key(HKEY_CLASSES_ROOT, keyName, MAXIMUM_ALLOWED, &clsidKey);
903 if (result != ERROR_SUCCESS)
904 return REGDB_E_CLASSNOTREG;
909 result = open_classes_key(clsidKey, miscstatusW, MAXIMUM_ALLOWED, &miscStatusKey);
910 if (result != ERROR_SUCCESS)
912 RegCloseKey(clsidKey);
913 return REGDB_E_READREGDB;
917 * Read the default value
919 OLEUTL_ReadRegistryDWORDValue(miscStatusKey, pdwStatus);
922 * Open the key specific to the requested aspect.
924 sprintfW(keyName, dfmtW, dwAspect);
926 result = open_classes_key(miscStatusKey, keyName, MAXIMUM_ALLOWED, &aspectKey);
927 if (result == ERROR_SUCCESS)
929 OLEUTL_ReadRegistryDWORDValue(aspectKey, pdwStatus);
930 RegCloseKey(aspectKey);
936 RegCloseKey(miscStatusKey);
937 RegCloseKey(clsidKey);
942 static HRESULT EnumOLEVERB_Construct(HKEY hkeyVerb, ULONG index, IEnumOLEVERB **ppenum);
946 IEnumOLEVERB IEnumOLEVERB_iface;
953 static inline EnumOLEVERB *impl_from_IEnumOLEVERB(IEnumOLEVERB *iface)
955 return CONTAINING_RECORD(iface, EnumOLEVERB, IEnumOLEVERB_iface);
958 static HRESULT WINAPI EnumOLEVERB_QueryInterface(
959 IEnumOLEVERB *iface, REFIID riid, void **ppv)
961 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
962 if (IsEqualIID(riid, &IID_IUnknown) ||
963 IsEqualIID(riid, &IID_IEnumOLEVERB))
965 IEnumOLEVERB_AddRef(iface);
969 return E_NOINTERFACE;
972 static ULONG WINAPI EnumOLEVERB_AddRef(
975 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
977 return InterlockedIncrement(&This->ref);
980 static ULONG WINAPI EnumOLEVERB_Release(
983 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
984 LONG refs = InterlockedDecrement(&This->ref);
988 RegCloseKey(This->hkeyVerb);
989 HeapFree(GetProcessHeap(), 0, This);
994 static HRESULT WINAPI EnumOLEVERB_Next(
995 IEnumOLEVERB *iface, ULONG celt, LPOLEVERB rgelt,
998 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
1001 TRACE("(%d, %p, %p)\n", celt, rgelt, pceltFetched);
1006 for (; celt; celt--, rgelt++)
1008 WCHAR wszSubKey[20];
1011 LPWSTR pwszMenuFlags;
1013 LONG res = RegEnumKeyW(This->hkeyVerb, This->index, wszSubKey, sizeof(wszSubKey)/sizeof(wszSubKey[0]));
1014 if (res == ERROR_NO_MORE_ITEMS)
1019 else if (res != ERROR_SUCCESS)
1021 ERR("RegEnumKeyW failed with error %d\n", res);
1022 hr = REGDB_E_READREGDB;
1025 res = RegQueryValueW(This->hkeyVerb, wszSubKey, NULL, &cbData);
1026 if (res != ERROR_SUCCESS)
1028 ERR("RegQueryValueW failed with error %d\n", res);
1029 hr = REGDB_E_READREGDB;
1032 pwszOLEVERB = CoTaskMemAlloc(cbData);
1038 res = RegQueryValueW(This->hkeyVerb, wszSubKey, pwszOLEVERB, &cbData);
1039 if (res != ERROR_SUCCESS)
1041 ERR("RegQueryValueW failed with error %d\n", res);
1042 hr = REGDB_E_READREGDB;
1043 CoTaskMemFree(pwszOLEVERB);
1047 TRACE("verb string: %s\n", debugstr_w(pwszOLEVERB));
1048 pwszMenuFlags = strchrW(pwszOLEVERB, ',');
1051 hr = OLEOBJ_E_INVALIDVERB;
1052 CoTaskMemFree(pwszOLEVERB);
1055 /* nul terminate the name string and advance to first character */
1056 *pwszMenuFlags = '\0';
1058 pwszAttribs = strchrW(pwszMenuFlags, ',');
1061 hr = OLEOBJ_E_INVALIDVERB;
1062 CoTaskMemFree(pwszOLEVERB);
1065 /* nul terminate the menu string and advance to first character */
1066 *pwszAttribs = '\0';
1069 /* fill out structure for this verb */
1070 rgelt->lVerb = atolW(wszSubKey);
1071 rgelt->lpszVerbName = pwszOLEVERB; /* user should free */
1072 rgelt->fuFlags = atolW(pwszMenuFlags);
1073 rgelt->grfAttribs = atolW(pwszAttribs);
1082 static HRESULT WINAPI EnumOLEVERB_Skip(
1083 IEnumOLEVERB *iface, ULONG celt)
1085 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
1087 TRACE("(%d)\n", celt);
1089 This->index += celt;
1093 static HRESULT WINAPI EnumOLEVERB_Reset(
1094 IEnumOLEVERB *iface)
1096 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
1104 static HRESULT WINAPI EnumOLEVERB_Clone(
1105 IEnumOLEVERB *iface,
1106 IEnumOLEVERB **ppenum)
1108 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
1110 TRACE("(%p)\n", ppenum);
1111 if (!DuplicateHandle(GetCurrentProcess(), This->hkeyVerb, GetCurrentProcess(), (HANDLE *)&hkeyVerb, 0, FALSE, DUPLICATE_SAME_ACCESS))
1112 return HRESULT_FROM_WIN32(GetLastError());
1113 return EnumOLEVERB_Construct(hkeyVerb, This->index, ppenum);
1116 static const IEnumOLEVERBVtbl EnumOLEVERB_VTable =
1118 EnumOLEVERB_QueryInterface,
1120 EnumOLEVERB_Release,
1127 static HRESULT EnumOLEVERB_Construct(HKEY hkeyVerb, ULONG index, IEnumOLEVERB **ppenum)
1129 EnumOLEVERB *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1132 RegCloseKey(hkeyVerb);
1133 return E_OUTOFMEMORY;
1135 This->IEnumOLEVERB_iface.lpVtbl = &EnumOLEVERB_VTable;
1137 This->index = index;
1138 This->hkeyVerb = hkeyVerb;
1139 *ppenum = &This->IEnumOLEVERB_iface;
1143 /***********************************************************************
1144 * OleRegEnumVerbs [OLE32.@]
1146 * Enumerates verbs associated with a class stored in the registry.
1149 * clsid [I] Class ID to enumerate the verbs for.
1150 * ppenum [O] Enumerator.
1154 * REGDB_E_CLASSNOTREG: The specified class does not have a key in the registry.
1155 * REGDB_E_READREGDB: The class key could not be opened for some other reason.
1156 * OLE_E_REGDB_KEY: The Verb subkey for the class is not present.
1157 * OLEOBJ_E_NOVERBS: The Verb subkey for the class is empty.
1159 HRESULT WINAPI OleRegEnumVerbs (REFCLSID clsid, LPENUMOLEVERB* ppenum)
1164 static const WCHAR wszVerb[] = {'V','e','r','b',0};
1166 TRACE("(%s, %p)\n", debugstr_guid(clsid), ppenum);
1168 res = COM_OpenKeyForCLSID(clsid, wszVerb, KEY_READ, &hkeyVerb);
1171 if (res == REGDB_E_CLASSNOTREG)
1172 ERR("CLSID %s not registered\n", debugstr_guid(clsid));
1173 else if (res == REGDB_E_KEYMISSING)
1174 ERR("no Verbs key for class %s\n", debugstr_guid(clsid));
1176 ERR("failed to open Verbs key for CLSID %s with error %d\n",
1177 debugstr_guid(clsid), res);
1181 res = RegQueryInfoKeyW(hkeyVerb, NULL, NULL, NULL, &dwSubKeys, NULL,
1182 NULL, NULL, NULL, NULL, NULL, NULL);
1183 if (res != ERROR_SUCCESS)
1185 ERR("failed to get subkey count with error %d\n", GetLastError());
1186 return REGDB_E_READREGDB;
1191 WARN("class %s has no verbs\n", debugstr_guid(clsid));
1192 RegCloseKey(hkeyVerb);
1193 return OLEOBJ_E_NOVERBS;
1196 return EnumOLEVERB_Construct(hkeyVerb, 0, ppenum);
1199 /******************************************************************************
1200 * OleSetContainedObject [OLE32.@]
1202 HRESULT WINAPI OleSetContainedObject(
1206 IRunnableObject* runnable = NULL;
1209 TRACE("(%p,%x)\n", pUnknown, fContained);
1211 hres = IUnknown_QueryInterface(pUnknown,
1212 &IID_IRunnableObject,
1215 if (SUCCEEDED(hres))
1217 hres = IRunnableObject_SetContainedObject(runnable, fContained);
1219 IRunnableObject_Release(runnable);
1227 /******************************************************************************
1230 * Set the OLE object to the running state.
1233 * pUnknown [I] OLE object to run.
1237 * Failure: Any HRESULT code.
1239 HRESULT WINAPI OleRun(LPUNKNOWN pUnknown)
1241 IRunnableObject *runable;
1244 TRACE("(%p)\n", pUnknown);
1246 hres = IUnknown_QueryInterface(pUnknown, &IID_IRunnableObject, (void**)&runable);
1248 return S_OK; /* Appears to return no error. */
1250 hres = IRunnableObject_Run(runable, NULL);
1251 IRunnableObject_Release(runable);
1255 /******************************************************************************
1258 HRESULT WINAPI OleLoad(
1261 LPOLECLIENTSITE pClientSite,
1264 IPersistStorage* persistStorage = NULL;
1266 IOleObject* pOleObject = NULL;
1267 STATSTG storageInfo;
1270 TRACE("(%p, %s, %p, %p)\n", pStg, debugstr_guid(riid), pClientSite, ppvObj);
1275 * TODO, Conversion ... OleDoAutoConvert
1279 * Get the class ID for the object.
1281 hres = IStorage_Stat(pStg, &storageInfo, STATFLAG_NONAME);
1284 * Now, try and create the handler for the object
1286 hres = CoCreateInstance(&storageInfo.clsid,
1288 CLSCTX_INPROC_HANDLER|CLSCTX_INPROC_SERVER,
1293 * If that fails, as it will most times, load the default
1298 hres = OleCreateDefaultHandler(&storageInfo.clsid,
1305 * If we couldn't find a handler... this is bad. Abort the whole thing.
1312 hres = IUnknown_QueryInterface(pUnk, &IID_IOleObject, (void **)&pOleObject);
1313 if (SUCCEEDED(hres))
1316 hres = IOleObject_GetMiscStatus(pOleObject, DVASPECT_CONTENT, &dwStatus);
1321 * Initialize the object with its IPersistStorage interface.
1323 hres = IUnknown_QueryInterface(pUnk, &IID_IPersistStorage, (void**)&persistStorage);
1324 if (SUCCEEDED(hres))
1326 hres = IPersistStorage_Load(persistStorage, pStg);
1328 IPersistStorage_Release(persistStorage);
1329 persistStorage = NULL;
1332 if (SUCCEEDED(hres) && pClientSite)
1334 * Inform the new object of it's client site.
1336 hres = IOleObject_SetClientSite(pOleObject, pClientSite);
1339 * Cleanup interfaces used internally
1342 IOleObject_Release(pOleObject);
1344 if (SUCCEEDED(hres))
1348 hres1 = IUnknown_QueryInterface(pUnk, &IID_IOleLink, (void **)&pOleLink);
1349 if (SUCCEEDED(hres1))
1351 FIXME("handle OLE link\n");
1352 IOleLink_Release(pOleLink);
1358 IUnknown_Release(pUnk);
1367 /***********************************************************************
1370 HRESULT WINAPI OleSave(
1371 LPPERSISTSTORAGE pPS,
1378 TRACE("(%p,%p,%x)\n", pPS, pStg, fSameAsLoad);
1381 * First, we transfer the class ID (if available)
1383 hres = IPersistStorage_GetClassID(pPS, &objectClass);
1385 if (SUCCEEDED(hres))
1387 WriteClassStg(pStg, &objectClass);
1391 * Then, we ask the object to save itself to the
1392 * storage. If it is successful, we commit the storage.
1394 hres = IPersistStorage_Save(pPS, pStg, fSameAsLoad);
1396 if (SUCCEEDED(hres))
1398 IStorage_Commit(pStg,
1406 /******************************************************************************
1407 * OleLockRunning [OLE32.@]
1409 HRESULT WINAPI OleLockRunning(LPUNKNOWN pUnknown, BOOL fLock, BOOL fLastUnlockCloses)
1411 IRunnableObject* runnable = NULL;
1414 TRACE("(%p,%x,%x)\n", pUnknown, fLock, fLastUnlockCloses);
1416 hres = IUnknown_QueryInterface(pUnknown,
1417 &IID_IRunnableObject,
1420 if (SUCCEEDED(hres))
1422 hres = IRunnableObject_LockRunning(runnable, fLock, fLastUnlockCloses);
1424 IRunnableObject_Release(runnable);
1433 /**************************************************************************
1434 * Internal methods to manage the shared OLE menu in response to the
1435 * OLE***MenuDescriptor API
1439 * OLEMenu_Initialize()
1441 * Initializes the OLEMENU data structures.
1443 static void OLEMenu_Initialize(void)
1448 * OLEMenu_UnInitialize()
1450 * Releases the OLEMENU data structures.
1452 static void OLEMenu_UnInitialize(void)
1456 /*************************************************************************
1457 * OLEMenu_InstallHooks
1458 * Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
1460 * RETURNS: TRUE if message hooks were successfully installed
1463 static BOOL OLEMenu_InstallHooks( DWORD tid )
1465 OleMenuHookItem *pHookItem;
1467 /* Create an entry for the hook table */
1468 if ( !(pHookItem = HeapAlloc(GetProcessHeap(), 0,
1469 sizeof(OleMenuHookItem)) ) )
1472 pHookItem->tid = tid;
1473 pHookItem->hHeap = GetProcessHeap();
1474 pHookItem->CallWndProc_hHook = NULL;
1476 /* Install a thread scope message hook for WH_GETMESSAGE */
1477 pHookItem->GetMsg_hHook = SetWindowsHookExW( WH_GETMESSAGE, OLEMenu_GetMsgProc,
1478 0, GetCurrentThreadId() );
1479 if ( !pHookItem->GetMsg_hHook )
1482 /* Install a thread scope message hook for WH_CALLWNDPROC */
1483 pHookItem->CallWndProc_hHook = SetWindowsHookExW( WH_CALLWNDPROC, OLEMenu_CallWndProc,
1484 0, GetCurrentThreadId() );
1485 if ( !pHookItem->CallWndProc_hHook )
1488 /* Insert the hook table entry */
1489 pHookItem->next = hook_list;
1490 hook_list = pHookItem;
1495 /* Unhook any hooks */
1496 if ( pHookItem->GetMsg_hHook )
1497 UnhookWindowsHookEx( pHookItem->GetMsg_hHook );
1498 if ( pHookItem->CallWndProc_hHook )
1499 UnhookWindowsHookEx( pHookItem->CallWndProc_hHook );
1500 /* Release the hook table entry */
1501 HeapFree(pHookItem->hHeap, 0, pHookItem );
1506 /*************************************************************************
1507 * OLEMenu_UnInstallHooks
1508 * UnInstall thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
1510 * RETURNS: TRUE if message hooks were successfully installed
1513 static BOOL OLEMenu_UnInstallHooks( DWORD tid )
1515 OleMenuHookItem *pHookItem = NULL;
1516 OleMenuHookItem **ppHook = &hook_list;
1520 if ((*ppHook)->tid == tid)
1522 pHookItem = *ppHook;
1523 *ppHook = pHookItem->next;
1526 ppHook = &(*ppHook)->next;
1528 if (!pHookItem) return FALSE;
1530 /* Uninstall the hooks installed for this thread */
1531 if ( !UnhookWindowsHookEx( pHookItem->GetMsg_hHook ) )
1533 if ( !UnhookWindowsHookEx( pHookItem->CallWndProc_hHook ) )
1536 /* Release the hook table entry */
1537 HeapFree(pHookItem->hHeap, 0, pHookItem );
1542 /* Release the hook table entry */
1543 HeapFree(pHookItem->hHeap, 0, pHookItem );
1548 /*************************************************************************
1549 * OLEMenu_IsHookInstalled
1550 * Tests if OLEMenu hooks have been installed for a thread
1552 * RETURNS: The pointer and index of the hook table entry for the tid
1553 * NULL and -1 for the index if no hooks were installed for this thread
1555 static OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid )
1557 OleMenuHookItem *pHookItem;
1559 /* Do a simple linear search for an entry whose tid matches ours.
1560 * We really need a map but efficiency is not a concern here. */
1561 for (pHookItem = hook_list; pHookItem; pHookItem = pHookItem->next)
1563 if ( tid == pHookItem->tid )
1570 /***********************************************************************
1571 * OLEMenu_FindMainMenuIndex
1573 * Used by OLEMenu API to find the top level group a menu item belongs to.
1574 * On success pnPos contains the index of the item in the top level menu group
1576 * RETURNS: TRUE if the ID was found, FALSE on failure
1578 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos )
1582 nItems = GetMenuItemCount( hMainMenu );
1584 for (i = 0; i < nItems; i++)
1588 /* Is the current item a submenu? */
1589 if ( (hsubmenu = GetSubMenu(hMainMenu, i)) )
1591 /* If the handle is the same we're done */
1592 if ( hsubmenu == hPopupMenu )
1598 /* Recursively search without updating pnPos */
1599 else if ( OLEMenu_FindMainMenuIndex( hsubmenu, hPopupMenu, NULL ) )
1611 /***********************************************************************
1612 * OLEMenu_SetIsServerMenu
1614 * Checks whether a popup menu belongs to a shared menu group which is
1615 * owned by the server, and sets the menu descriptor state accordingly.
1616 * All menu messages from these groups should be routed to the server.
1618 * RETURNS: TRUE if the popup menu is part of a server owned group
1619 * FALSE if the popup menu is part of a container owned group
1621 static BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor )
1623 UINT nPos = 0, nWidth, i;
1625 pOleMenuDescriptor->bIsServerItem = FALSE;
1627 /* Don't bother searching if the popup is the combined menu itself */
1628 if ( hmenu == pOleMenuDescriptor->hmenuCombined )
1631 /* Find the menu item index in the shared OLE menu that this item belongs to */
1632 if ( !OLEMenu_FindMainMenuIndex( pOleMenuDescriptor->hmenuCombined, hmenu, &nPos ) )
1635 /* The group widths array has counts for the number of elements
1636 * in the groups File, Edit, Container, Object, Window, Help.
1637 * The Edit, Object & Help groups belong to the server object
1638 * and the other three belong to the container.
1639 * Loop through the group widths and locate the group we are a member of.
1641 for ( i = 0, nWidth = 0; i < 6; i++ )
1643 nWidth += pOleMenuDescriptor->mgw.width[i];
1644 if ( nPos < nWidth )
1646 /* Odd elements are server menu widths */
1647 pOleMenuDescriptor->bIsServerItem = i%2;
1652 return pOleMenuDescriptor->bIsServerItem;
1655 /*************************************************************************
1656 * OLEMenu_CallWndProc
1657 * Thread scope WH_CALLWNDPROC hook proc filter function (callback)
1658 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1660 static LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam)
1663 HOLEMENU hOleMenu = 0;
1664 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1665 OleMenuHookItem *pHookItem = NULL;
1668 TRACE("%i, %04lx, %08lx\n", code, wParam, lParam );
1670 /* Check if we're being asked to process the message */
1671 if ( HC_ACTION != code )
1674 /* Retrieve the current message being dispatched from lParam */
1675 pMsg = (LPCWPSTRUCT)lParam;
1677 /* Check if the message is destined for a window we are interested in:
1678 * If the window has an OLEMenu property we may need to dispatch
1679 * the menu message to its active objects window instead. */
1681 hOleMenu = GetPropW( pMsg->hwnd, prop_olemenuW );
1685 /* Get the menu descriptor */
1686 pOleMenuDescriptor = GlobalLock( hOleMenu );
1687 if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1690 /* Process menu messages */
1691 switch( pMsg->message )
1695 /* Reset the menu descriptor state */
1696 pOleMenuDescriptor->bIsServerItem = FALSE;
1698 /* Send this message to the server as well */
1699 SendMessageW( pOleMenuDescriptor->hwndActiveObject,
1700 pMsg->message, pMsg->wParam, pMsg->lParam );
1704 case WM_INITMENUPOPUP:
1706 /* Save the state for whether this is a server owned menu */
1707 OLEMenu_SetIsServerMenu( (HMENU)pMsg->wParam, pOleMenuDescriptor );
1713 fuFlags = HIWORD(pMsg->wParam); /* Get flags */
1714 if ( fuFlags & MF_SYSMENU )
1717 /* Save the state for whether this is a server owned popup menu */
1718 else if ( fuFlags & MF_POPUP )
1719 OLEMenu_SetIsServerMenu( (HMENU)pMsg->lParam, pOleMenuDescriptor );
1726 LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT) pMsg->lParam;
1727 if ( pMsg->wParam != 0 || lpdis->CtlType != ODT_MENU )
1728 goto NEXTHOOK; /* Not a menu message */
1737 /* If the message was for the server dispatch it accordingly */
1738 if ( pOleMenuDescriptor->bIsServerItem )
1740 SendMessageW( pOleMenuDescriptor->hwndActiveObject,
1741 pMsg->message, pMsg->wParam, pMsg->lParam );
1745 if ( pOleMenuDescriptor )
1746 GlobalUnlock( hOleMenu );
1748 /* Lookup the hook item for the current thread */
1749 if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1751 /* This should never fail!! */
1752 WARN("could not retrieve hHook for current thread!\n" );
1756 /* Pass on the message to the next hooker */
1757 return CallNextHookEx( pHookItem->CallWndProc_hHook, code, wParam, lParam );
1760 /*************************************************************************
1761 * OLEMenu_GetMsgProc
1762 * Thread scope WH_GETMESSAGE hook proc filter function (callback)
1763 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1765 static LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam)
1768 HOLEMENU hOleMenu = 0;
1769 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1770 OleMenuHookItem *pHookItem = NULL;
1773 TRACE("%i, %04lx, %08lx\n", code, wParam, lParam );
1775 /* Check if we're being asked to process a messages */
1776 if ( HC_ACTION != code )
1779 /* Retrieve the current message being dispatched from lParam */
1780 pMsg = (LPMSG)lParam;
1782 /* Check if the message is destined for a window we are interested in:
1783 * If the window has an OLEMenu property we may need to dispatch
1784 * the menu message to its active objects window instead. */
1786 hOleMenu = GetPropW( pMsg->hwnd, prop_olemenuW );
1790 /* Process menu messages */
1791 switch( pMsg->message )
1795 wCode = HIWORD(pMsg->wParam); /* Get notification code */
1797 goto NEXTHOOK; /* Not a menu message */
1804 /* Get the menu descriptor */
1805 pOleMenuDescriptor = GlobalLock( hOleMenu );
1806 if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1809 /* If the message was for the server dispatch it accordingly */
1810 if ( pOleMenuDescriptor->bIsServerItem )
1812 /* Change the hWnd in the message to the active objects hWnd.
1813 * The message loop which reads this message will automatically
1814 * dispatch it to the embedded objects window. */
1815 pMsg->hwnd = pOleMenuDescriptor->hwndActiveObject;
1819 if ( pOleMenuDescriptor )
1820 GlobalUnlock( hOleMenu );
1822 /* Lookup the hook item for the current thread */
1823 if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1825 /* This should never fail!! */
1826 WARN("could not retrieve hHook for current thread!\n" );
1830 /* Pass on the message to the next hooker */
1831 return CallNextHookEx( pHookItem->GetMsg_hHook, code, wParam, lParam );
1834 /***********************************************************************
1835 * OleCreateMenuDescriptor [OLE32.@]
1836 * Creates an OLE menu descriptor for OLE to use when dispatching
1837 * menu messages and commands.
1840 * hmenuCombined - Handle to the objects combined menu
1841 * lpMenuWidths - Pointer to array of 6 LONG's indicating menus per group
1844 HOLEMENU WINAPI OleCreateMenuDescriptor(
1845 HMENU hmenuCombined,
1846 LPOLEMENUGROUPWIDTHS lpMenuWidths)
1849 OleMenuDescriptor *pOleMenuDescriptor;
1852 if ( !hmenuCombined || !lpMenuWidths )
1855 /* Create an OLE menu descriptor */
1856 if ( !(hOleMenu = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT,
1857 sizeof(OleMenuDescriptor) ) ) )
1860 pOleMenuDescriptor = GlobalLock( hOleMenu );
1861 if ( !pOleMenuDescriptor )
1864 /* Initialize menu group widths and hmenu */
1865 for ( i = 0; i < 6; i++ )
1866 pOleMenuDescriptor->mgw.width[i] = lpMenuWidths->width[i];
1868 pOleMenuDescriptor->hmenuCombined = hmenuCombined;
1869 pOleMenuDescriptor->bIsServerItem = FALSE;
1870 GlobalUnlock( hOleMenu );
1875 /***********************************************************************
1876 * OleDestroyMenuDescriptor [OLE32.@]
1877 * Destroy the shared menu descriptor
1879 HRESULT WINAPI OleDestroyMenuDescriptor(
1880 HOLEMENU hmenuDescriptor)
1882 if ( hmenuDescriptor )
1883 GlobalFree( hmenuDescriptor );
1887 /***********************************************************************
1888 * OleSetMenuDescriptor [OLE32.@]
1889 * Installs or removes OLE dispatching code for the containers frame window.
1892 * hOleMenu Handle to composite menu descriptor
1893 * hwndFrame Handle to containers frame window
1894 * hwndActiveObject Handle to objects in-place activation window
1895 * lpFrame Pointer to IOleInPlaceFrame on containers window
1896 * lpActiveObject Pointer to IOleInPlaceActiveObject on active in-place object
1899 * S_OK - menu installed correctly
1900 * E_FAIL, E_INVALIDARG, E_UNEXPECTED - failure
1903 * The lpFrame and lpActiveObject parameters are currently ignored
1904 * OLE should install context sensitive help F1 filtering for the app when
1905 * these are non null.
1907 HRESULT WINAPI OleSetMenuDescriptor(
1910 HWND hwndActiveObject,
1911 LPOLEINPLACEFRAME lpFrame,
1912 LPOLEINPLACEACTIVEOBJECT lpActiveObject)
1914 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1917 if ( !hwndFrame || (hOleMenu && !hwndActiveObject) )
1918 return E_INVALIDARG;
1920 if ( lpFrame || lpActiveObject )
1922 FIXME("(%p, %p, %p, %p, %p), Context sensitive help filtering not implemented!\n",
1930 /* Set up a message hook to intercept the containers frame window messages.
1931 * The message filter is responsible for dispatching menu messages from the
1932 * shared menu which are intended for the object.
1935 if ( hOleMenu ) /* Want to install dispatching code */
1937 /* If OLEMenu hooks are already installed for this thread, fail
1938 * Note: This effectively means that OleSetMenuDescriptor cannot
1939 * be called twice in succession on the same frame window
1940 * without first calling it with a null hOleMenu to uninstall */
1941 if ( OLEMenu_IsHookInstalled( GetCurrentThreadId() ) )
1944 /* Get the menu descriptor */
1945 pOleMenuDescriptor = GlobalLock( hOleMenu );
1946 if ( !pOleMenuDescriptor )
1947 return E_UNEXPECTED;
1949 /* Update the menu descriptor */
1950 pOleMenuDescriptor->hwndFrame = hwndFrame;
1951 pOleMenuDescriptor->hwndActiveObject = hwndActiveObject;
1953 GlobalUnlock( hOleMenu );
1954 pOleMenuDescriptor = NULL;
1956 /* Add a menu descriptor windows property to the frame window */
1957 SetPropW( hwndFrame, prop_olemenuW, hOleMenu );
1959 /* Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC */
1960 if ( !OLEMenu_InstallHooks( GetCurrentThreadId() ) )
1963 else /* Want to uninstall dispatching code */
1965 /* Uninstall the hooks */
1966 if ( !OLEMenu_UnInstallHooks( GetCurrentThreadId() ) )
1969 /* Remove the menu descriptor property from the frame window */
1970 RemovePropW( hwndFrame, prop_olemenuW );
1976 /******************************************************************************
1977 * IsAccelerator [OLE32.@]
1978 * Mostly copied from controls/menu.c TranslateAccelerator implementation
1980 BOOL WINAPI IsAccelerator(HACCEL hAccel, int cAccelEntries, LPMSG lpMsg, WORD* lpwCmd)
1985 if(!lpMsg) return FALSE;
1988 WARN_(accel)("NULL accel handle\n");
1991 if((lpMsg->message != WM_KEYDOWN &&
1992 lpMsg->message != WM_SYSKEYDOWN &&
1993 lpMsg->message != WM_SYSCHAR &&
1994 lpMsg->message != WM_CHAR)) return FALSE;
1995 lpAccelTbl = HeapAlloc(GetProcessHeap(), 0, cAccelEntries * sizeof(ACCEL));
1996 if (NULL == lpAccelTbl)
2000 if (CopyAcceleratorTableW(hAccel, lpAccelTbl, cAccelEntries) != cAccelEntries)
2002 WARN_(accel)("CopyAcceleratorTableW failed\n");
2003 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
2007 TRACE_(accel)("hAccel=%p, cAccelEntries=%d,"
2008 "msg->hwnd=%p, msg->message=%04x, wParam=%08lx, lParam=%08lx\n",
2009 hAccel, cAccelEntries,
2010 lpMsg->hwnd, lpMsg->message, lpMsg->wParam, lpMsg->lParam);
2011 for(i = 0; i < cAccelEntries; i++)
2013 if(lpAccelTbl[i].key != lpMsg->wParam)
2016 if(lpMsg->message == WM_CHAR)
2018 if(!(lpAccelTbl[i].fVirt & FALT) && !(lpAccelTbl[i].fVirt & FVIRTKEY))
2020 TRACE_(accel)("found accel for WM_CHAR: ('%c')\n", LOWORD(lpMsg->wParam) & 0xff);
2026 if(lpAccelTbl[i].fVirt & FVIRTKEY)
2029 TRACE_(accel)("found accel for virt_key %04lx (scan %04x)\n",
2030 lpMsg->wParam, HIWORD(lpMsg->lParam) & 0xff);
2031 if(GetKeyState(VK_SHIFT) & 0x8000) mask |= FSHIFT;
2032 if(GetKeyState(VK_CONTROL) & 0x8000) mask |= FCONTROL;
2033 if(GetKeyState(VK_MENU) & 0x8000) mask |= FALT;
2034 if(mask == (lpAccelTbl[i].fVirt & (FSHIFT | FCONTROL | FALT))) goto found;
2035 TRACE_(accel)("incorrect SHIFT/CTRL/ALT-state\n");
2039 if(!(lpMsg->lParam & 0x01000000)) /* no special_key */
2041 if((lpAccelTbl[i].fVirt & FALT) && (lpMsg->lParam & 0x20000000))
2042 { /* ^^ ALT pressed */
2043 TRACE_(accel)("found accel for Alt-%c\n", LOWORD(lpMsg->wParam) & 0xff);
2051 WARN_(accel)("couldn't translate accelerator key\n");
2052 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
2056 if(lpwCmd) *lpwCmd = lpAccelTbl[i].cmd;
2057 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
2061 /***********************************************************************
2062 * ReleaseStgMedium [OLE32.@]
2064 void WINAPI ReleaseStgMedium(
2067 switch (pmedium->tymed)
2071 if ( (pmedium->pUnkForRelease==0) &&
2072 (pmedium->u.hGlobal!=0) )
2073 GlobalFree(pmedium->u.hGlobal);
2078 if (pmedium->u.lpszFileName!=0)
2080 if (pmedium->pUnkForRelease==0)
2082 DeleteFileW(pmedium->u.lpszFileName);
2085 CoTaskMemFree(pmedium->u.lpszFileName);
2091 if (pmedium->u.pstm!=0)
2093 IStream_Release(pmedium->u.pstm);
2097 case TYMED_ISTORAGE:
2099 if (pmedium->u.pstg!=0)
2101 IStorage_Release(pmedium->u.pstg);
2107 if ( (pmedium->pUnkForRelease==0) &&
2108 (pmedium->u.hBitmap!=0) )
2109 DeleteObject(pmedium->u.hBitmap);
2114 if ( (pmedium->pUnkForRelease==0) &&
2115 (pmedium->u.hMetaFilePict!=0) )
2117 LPMETAFILEPICT pMP = GlobalLock(pmedium->u.hMetaFilePict);
2118 DeleteMetaFile(pMP->hMF);
2119 GlobalUnlock(pmedium->u.hMetaFilePict);
2120 GlobalFree(pmedium->u.hMetaFilePict);
2126 if ( (pmedium->pUnkForRelease==0) &&
2127 (pmedium->u.hEnhMetaFile!=0) )
2129 DeleteEnhMetaFile(pmedium->u.hEnhMetaFile);
2137 pmedium->tymed=TYMED_NULL;
2140 * After cleaning up, the unknown is released
2142 if (pmedium->pUnkForRelease!=0)
2144 IUnknown_Release(pmedium->pUnkForRelease);
2145 pmedium->pUnkForRelease = 0;
2150 * OLEDD_Initialize()
2152 * Initializes the OLE drag and drop data structures.
2154 static void OLEDD_Initialize(void)
2158 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
2159 wndClass.style = CS_GLOBALCLASS;
2160 wndClass.lpfnWndProc = OLEDD_DragTrackerWindowProc;
2161 wndClass.cbClsExtra = 0;
2162 wndClass.cbWndExtra = sizeof(TrackerWindowInfo*);
2163 wndClass.hCursor = 0;
2164 wndClass.hbrBackground = 0;
2165 wndClass.lpszClassName = OLEDD_DRAGTRACKERCLASS;
2167 RegisterClassW (&wndClass);
2171 * OLEDD_DragTrackerWindowProc()
2173 * This method is the WindowProcedure of the drag n drop tracking
2174 * window. During a drag n Drop operation, an invisible window is created
2175 * to receive the user input and act upon it. This procedure is in charge
2179 #define DRAG_TIMER_ID 1
2181 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(
2191 LPCREATESTRUCTA createStruct = (LPCREATESTRUCTA)lParam;
2193 SetWindowLongPtrW(hwnd, 0, (LONG_PTR)createStruct->lpCreateParams);
2194 SetTimer(hwnd, DRAG_TIMER_ID, 50, NULL);
2201 OLEDD_TrackMouseMove((TrackerWindowInfo*)GetWindowLongPtrA(hwnd, 0));
2207 case WM_LBUTTONDOWN:
2208 case WM_MBUTTONDOWN:
2209 case WM_RBUTTONDOWN:
2211 OLEDD_TrackStateChange((TrackerWindowInfo*)GetWindowLongPtrA(hwnd, 0));
2216 KillTimer(hwnd, DRAG_TIMER_ID);
2222 * This is a window proc after all. Let's call the default.
2224 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
2228 * OLEDD_TrackMouseMove()
2230 * This method is invoked while a drag and drop operation is in effect.
2231 * it will generate the appropriate callbacks in the drop source
2232 * and drop target. It will also provide the expected feedback to
2236 * trackerInfo - Pointer to the structure identifying the
2237 * drag & drop operation that is currently
2240 static void OLEDD_TrackMouseMove(TrackerWindowInfo* trackerInfo)
2242 HWND hwndNewTarget = 0;
2247 * Get the handle of the window under the mouse
2249 pt.x = trackerInfo->curMousePos.x;
2250 pt.y = trackerInfo->curMousePos.y;
2251 hwndNewTarget = WindowFromPoint(pt);
2254 * Every time, we re-initialize the effects passed to the
2255 * IDropTarget to the effects allowed by the source.
2257 *trackerInfo->pdwEffect = trackerInfo->dwOKEffect;
2260 * If we are hovering over the same target as before, send the
2261 * DragOver notification
2263 if ( (trackerInfo->curDragTarget != 0) &&
2264 (trackerInfo->curTargetHWND == hwndNewTarget) )
2266 IDropTarget_DragOver(trackerInfo->curDragTarget,
2267 trackerInfo->dwKeyState,
2268 trackerInfo->curMousePos,
2269 trackerInfo->pdwEffect);
2274 * If we changed window, we have to notify our old target and check for
2277 if (trackerInfo->curDragTarget)
2278 IDropTarget_DragLeave(trackerInfo->curDragTarget);
2281 * Make sure we're hovering over a window.
2286 * Find-out if there is a drag target under the mouse
2288 HWND next_target_wnd = hwndNewTarget;
2290 trackerInfo->curTargetHWND = hwndNewTarget;
2292 while (next_target_wnd && !is_droptarget(next_target_wnd))
2293 next_target_wnd = GetParent(next_target_wnd);
2295 if (next_target_wnd) hwndNewTarget = next_target_wnd;
2297 trackerInfo->curDragTargetHWND = hwndNewTarget;
2298 if(trackerInfo->curDragTarget) IDropTarget_Release(trackerInfo->curDragTarget);
2299 trackerInfo->curDragTarget = get_droptarget_pointer(hwndNewTarget);
2302 * If there is, notify it that we just dragged-in
2304 if (trackerInfo->curDragTarget)
2306 hr = IDropTarget_DragEnter(trackerInfo->curDragTarget,
2307 trackerInfo->dataObject,
2308 trackerInfo->dwKeyState,
2309 trackerInfo->curMousePos,
2310 trackerInfo->pdwEffect);
2312 /* failed DragEnter() means invalid target */
2315 trackerInfo->curDragTargetHWND = 0;
2316 trackerInfo->curTargetHWND = 0;
2317 IDropTarget_Release(trackerInfo->curDragTarget);
2318 trackerInfo->curDragTarget = 0;
2325 * The mouse is not over a window so we don't track anything.
2327 trackerInfo->curDragTargetHWND = 0;
2328 trackerInfo->curTargetHWND = 0;
2329 if(trackerInfo->curDragTarget) IDropTarget_Release(trackerInfo->curDragTarget);
2330 trackerInfo->curDragTarget = 0;
2335 * Now that we have done that, we have to tell the source to give
2336 * us feedback on the work being done by the target. If we don't
2337 * have a target, simulate no effect.
2339 if (trackerInfo->curDragTarget==0)
2341 *trackerInfo->pdwEffect = DROPEFFECT_NONE;
2344 hr = IDropSource_GiveFeedback(trackerInfo->dropSource,
2345 *trackerInfo->pdwEffect);
2348 * When we ask for feedback from the drop source, sometimes it will
2349 * do all the necessary work and sometimes it will not handle it
2350 * when that's the case, we must display the standard drag and drop
2353 if (hr == DRAGDROP_S_USEDEFAULTCURSORS)
2357 if (*trackerInfo->pdwEffect & DROPEFFECT_MOVE)
2359 hCur = LoadCursorW(hProxyDll, MAKEINTRESOURCEW(2));
2361 else if (*trackerInfo->pdwEffect & DROPEFFECT_COPY)
2363 hCur = LoadCursorW(hProxyDll, MAKEINTRESOURCEW(3));
2365 else if (*trackerInfo->pdwEffect & DROPEFFECT_LINK)
2367 hCur = LoadCursorW(hProxyDll, MAKEINTRESOURCEW(4));
2371 hCur = LoadCursorW(hProxyDll, MAKEINTRESOURCEW(1));
2379 * OLEDD_TrackStateChange()
2381 * This method is invoked while a drag and drop operation is in effect.
2382 * It is used to notify the drop target/drop source callbacks when
2383 * the state of the keyboard or mouse button change.
2386 * trackerInfo - Pointer to the structure identifying the
2387 * drag & drop operation that is currently
2390 static void OLEDD_TrackStateChange(TrackerWindowInfo* trackerInfo)
2393 * Ask the drop source what to do with the operation.
2395 trackerInfo->returnValue = IDropSource_QueryContinueDrag(
2396 trackerInfo->dropSource,
2397 trackerInfo->escPressed,
2398 trackerInfo->dwKeyState);
2401 * All the return valued will stop the operation except the S_OK
2404 if (trackerInfo->returnValue!=S_OK)
2407 * Make sure the message loop in DoDragDrop stops
2409 trackerInfo->trackingDone = TRUE;
2412 * Release the mouse in case the drop target decides to show a popup
2413 * or a menu or something.
2418 * If we end-up over a target, drop the object in the target or
2419 * inform the target that the operation was cancelled.
2421 if (trackerInfo->curDragTarget)
2423 switch (trackerInfo->returnValue)
2426 * If the source wants us to complete the operation, we tell
2427 * the drop target that we just dropped the object in it.
2429 case DRAGDROP_S_DROP:
2430 if (*trackerInfo->pdwEffect != DROPEFFECT_NONE)
2431 IDropTarget_Drop(trackerInfo->curDragTarget,
2432 trackerInfo->dataObject,
2433 trackerInfo->dwKeyState,
2434 trackerInfo->curMousePos,
2435 trackerInfo->pdwEffect);
2437 IDropTarget_DragLeave(trackerInfo->curDragTarget);
2441 * If the source told us that we should cancel, fool the drop
2442 * target by telling it that the mouse left it's window.
2443 * Also set the drop effect to "NONE" in case the application
2444 * ignores the result of DoDragDrop.
2446 case DRAGDROP_S_CANCEL:
2447 IDropTarget_DragLeave(trackerInfo->curDragTarget);
2448 *trackerInfo->pdwEffect = DROPEFFECT_NONE;
2456 * OLEDD_GetButtonState()
2458 * This method will use the current state of the keyboard to build
2459 * a button state mask equivalent to the one passed in the
2460 * WM_MOUSEMOVE wParam.
2462 static DWORD OLEDD_GetButtonState(void)
2464 BYTE keyboardState[256];
2467 GetKeyboardState(keyboardState);
2469 if ( (keyboardState[VK_SHIFT] & 0x80) !=0)
2470 keyMask |= MK_SHIFT;
2472 if ( (keyboardState[VK_CONTROL] & 0x80) !=0)
2473 keyMask |= MK_CONTROL;
2475 if ( (keyboardState[VK_LBUTTON] & 0x80) !=0)
2476 keyMask |= MK_LBUTTON;
2478 if ( (keyboardState[VK_RBUTTON] & 0x80) !=0)
2479 keyMask |= MK_RBUTTON;
2481 if ( (keyboardState[VK_MBUTTON] & 0x80) !=0)
2482 keyMask |= MK_MBUTTON;
2488 * OLEDD_GetButtonState()
2490 * This method will read the default value of the registry key in
2491 * parameter and extract a DWORD value from it. The registry key value
2492 * can be in a string key or a DWORD key.
2495 * regKey - Key to read the default value from
2496 * pdwValue - Pointer to the location where the DWORD
2497 * value is returned. This value is not modified
2498 * if the value is not found.
2501 static void OLEUTL_ReadRegistryDWORDValue(
2506 DWORD cbData = sizeof(buffer);
2510 lres = RegQueryValueExW(regKey,
2517 if (lres==ERROR_SUCCESS)
2522 *pdwValue = *(DWORD*)buffer;
2527 *pdwValue = (DWORD)strtoulW(buffer, NULL, 10);
2533 /******************************************************************************
2536 * The operation of this function is documented literally in the WinAPI
2537 * documentation to involve a QueryInterface for the IViewObject interface,
2538 * followed by a call to IViewObject::Draw.
2540 HRESULT WINAPI OleDraw(
2547 IViewObject *viewobject;
2549 if (!pUnk) return E_INVALIDARG;
2551 hres = IUnknown_QueryInterface(pUnk,
2553 (void**)&viewobject);
2554 if (SUCCEEDED(hres))
2556 hres = IViewObject_Draw(viewobject, dwAspect, -1, 0, 0, 0, hdcDraw, (RECTL*)rect, 0, 0, 0);
2557 IViewObject_Release(viewobject);
2561 return DV_E_NOIVIEWOBJECT;
2564 /***********************************************************************
2565 * OleTranslateAccelerator [OLE32.@]
2567 HRESULT WINAPI OleTranslateAccelerator (LPOLEINPLACEFRAME lpFrame,
2568 LPOLEINPLACEFRAMEINFO lpFrameInfo, LPMSG lpmsg)
2572 TRACE("(%p,%p,%p)\n", lpFrame, lpFrameInfo, lpmsg);
2574 if (IsAccelerator(lpFrameInfo->haccel,lpFrameInfo->cAccelEntries,lpmsg,&wID))
2575 return IOleInPlaceFrame_TranslateAccelerator(lpFrame,lpmsg,wID);
2580 /******************************************************************************
2581 * OleCreate [OLE32.@]
2584 HRESULT WINAPI OleCreate(
2588 LPFORMATETC pFormatEtc,
2589 LPOLECLIENTSITE pClientSite,
2594 IUnknown * pUnk = NULL;
2595 IOleObject *pOleObject = NULL;
2597 TRACE("(%s, %s, %d, %p, %p, %p, %p)\n", debugstr_guid(rclsid),
2598 debugstr_guid(riid), renderopt, pFormatEtc, pClientSite, pStg, ppvObj);
2600 hres = CoCreateInstance(rclsid, 0, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER, riid, (LPVOID*)&pUnk);
2602 if (SUCCEEDED(hres))
2603 hres = IStorage_SetClass(pStg, rclsid);
2605 if (pClientSite && SUCCEEDED(hres))
2607 hres = IUnknown_QueryInterface(pUnk, &IID_IOleObject, (LPVOID*)&pOleObject);
2608 if (SUCCEEDED(hres))
2611 hres = IOleObject_GetMiscStatus(pOleObject, DVASPECT_CONTENT, &dwStatus);
2615 if (SUCCEEDED(hres))
2617 IPersistStorage * pPS;
2618 if (SUCCEEDED((hres = IUnknown_QueryInterface(pUnk, &IID_IPersistStorage, (LPVOID*)&pPS))))
2620 TRACE("trying to set stg %p\n", pStg);
2621 hres = IPersistStorage_InitNew(pPS, pStg);
2622 TRACE("-- result 0x%08x\n", hres);
2623 IPersistStorage_Release(pPS);
2627 if (pClientSite && SUCCEEDED(hres))
2629 TRACE("trying to set clientsite %p\n", pClientSite);
2630 hres = IOleObject_SetClientSite(pOleObject, pClientSite);
2631 TRACE("-- result 0x%08x\n", hres);
2635 IOleObject_Release(pOleObject);
2637 if (((renderopt == OLERENDER_DRAW) || (renderopt == OLERENDER_FORMAT)) &&
2640 IRunnableObject *pRunnable;
2641 IOleCache *pOleCache;
2644 hres2 = IUnknown_QueryInterface(pUnk, &IID_IRunnableObject, (void **)&pRunnable);
2645 if (SUCCEEDED(hres2))
2647 hres = IRunnableObject_Run(pRunnable, NULL);
2648 IRunnableObject_Release(pRunnable);
2651 if (SUCCEEDED(hres))
2653 hres2 = IUnknown_QueryInterface(pUnk, &IID_IOleCache, (void **)&pOleCache);
2654 if (SUCCEEDED(hres2))
2657 if (renderopt == OLERENDER_DRAW && !pFormatEtc) {
2661 pfe.dwAspect = DVASPECT_CONTENT;
2663 pfe.tymed = TYMED_NULL;
2664 hres = IOleCache_Cache(pOleCache, &pfe, ADVF_PRIMEFIRST, &dwConnection);
2667 hres = IOleCache_Cache(pOleCache, pFormatEtc, ADVF_PRIMEFIRST, &dwConnection);
2668 IOleCache_Release(pOleCache);
2673 if (FAILED(hres) && pUnk)
2675 IUnknown_Release(pUnk);
2681 TRACE("-- %p\n", pUnk);
2685 /******************************************************************************
2686 * OleGetAutoConvert [OLE32.@]
2688 HRESULT WINAPI OleGetAutoConvert(REFCLSID clsidOld, LPCLSID pClsidNew)
2690 static const WCHAR wszAutoConvertTo[] = {'A','u','t','o','C','o','n','v','e','r','t','T','o',0};
2692 WCHAR buf[CHARS_IN_GUID];
2696 res = COM_OpenKeyForCLSID(clsidOld, wszAutoConvertTo, KEY_READ, &hkey);
2701 if (RegQueryValueW(hkey, NULL, buf, &len))
2703 res = REGDB_E_KEYMISSING;
2706 res = CLSIDFromString(buf, pClsidNew);
2708 if (hkey) RegCloseKey(hkey);
2712 /******************************************************************************
2713 * OleSetAutoConvert [OLE32.@]
2715 HRESULT WINAPI OleSetAutoConvert(REFCLSID clsidOld, REFCLSID clsidNew)
2717 static const WCHAR wszAutoConvertTo[] = {'A','u','t','o','C','o','n','v','e','r','t','T','o',0};
2719 WCHAR szClsidNew[CHARS_IN_GUID];
2722 TRACE("(%s,%s)\n", debugstr_guid(clsidOld), debugstr_guid(clsidNew));
2724 res = COM_OpenKeyForCLSID(clsidOld, NULL, KEY_READ | KEY_WRITE, &hkey);
2727 StringFromGUID2(clsidNew, szClsidNew, CHARS_IN_GUID);
2728 if (RegSetValueW(hkey, wszAutoConvertTo, REG_SZ, szClsidNew, (strlenW(szClsidNew)+1) * sizeof(WCHAR)))
2730 res = REGDB_E_WRITEREGDB;
2735 if (hkey) RegCloseKey(hkey);
2739 /******************************************************************************
2740 * OleDoAutoConvert [OLE32.@]
2742 HRESULT WINAPI OleDoAutoConvert(LPSTORAGE pStg, LPCLSID pClsidNew)
2744 FIXME("(%p,%p) : stub\n",pStg,pClsidNew);
2748 /******************************************************************************
2749 * OleIsRunning [OLE32.@]
2751 BOOL WINAPI OleIsRunning(LPOLEOBJECT object)
2753 IRunnableObject *pRunnable;
2757 TRACE("(%p)\n", object);
2759 if (!object) return FALSE;
2761 hr = IOleObject_QueryInterface(object, &IID_IRunnableObject, (void **)&pRunnable);
2764 running = IRunnableObject_IsRunning(pRunnable);
2765 IRunnableObject_Release(pRunnable);
2769 /***********************************************************************
2770 * OleNoteObjectVisible [OLE32.@]
2772 HRESULT WINAPI OleNoteObjectVisible(LPUNKNOWN pUnknown, BOOL bVisible)
2774 TRACE("(%p, %s)\n", pUnknown, bVisible ? "TRUE" : "FALSE");
2775 return CoLockObjectExternal(pUnknown, bVisible, TRUE);
2779 /***********************************************************************
2780 * OLE_FreeClipDataArray [internal]
2783 * frees the data associated with an array of CLIPDATAs
2785 static void OLE_FreeClipDataArray(ULONG count, CLIPDATA * pClipDataArray)
2788 for (i = 0; i < count; i++)
2789 if (pClipDataArray[i].pClipData)
2790 CoTaskMemFree(pClipDataArray[i].pClipData);
2793 /***********************************************************************
2794 * PropSysAllocString [OLE32.@]
2796 * Forward to oleaut32.
2798 BSTR WINAPI PropSysAllocString(LPCOLESTR str)
2800 return SysAllocString(str);
2803 /***********************************************************************
2804 * PropSysFreeString [OLE32.@]
2806 * Forward to oleaut32.
2808 void WINAPI PropSysFreeString(LPOLESTR str)
2813 /******************************************************************************
2814 * Check if a PROPVARIANT's type is valid.
2816 static inline HRESULT PROPVARIANT_ValidateType(VARTYPE vt)
2844 case VT_STREAMED_OBJECT:
2845 case VT_STORED_OBJECT:
2846 case VT_BLOB_OBJECT:
2849 case VT_I1|VT_VECTOR:
2850 case VT_I2|VT_VECTOR:
2851 case VT_I4|VT_VECTOR:
2852 case VT_I8|VT_VECTOR:
2853 case VT_R4|VT_VECTOR:
2854 case VT_R8|VT_VECTOR:
2855 case VT_CY|VT_VECTOR:
2856 case VT_DATE|VT_VECTOR:
2857 case VT_BSTR|VT_VECTOR:
2858 case VT_ERROR|VT_VECTOR:
2859 case VT_BOOL|VT_VECTOR:
2860 case VT_VARIANT|VT_VECTOR:
2861 case VT_UI1|VT_VECTOR:
2862 case VT_UI2|VT_VECTOR:
2863 case VT_UI4|VT_VECTOR:
2864 case VT_UI8|VT_VECTOR:
2865 case VT_LPSTR|VT_VECTOR:
2866 case VT_LPWSTR|VT_VECTOR:
2867 case VT_FILETIME|VT_VECTOR:
2868 case VT_CF|VT_VECTOR:
2869 case VT_CLSID|VT_VECTOR:
2872 WARN("Bad type %d\n", vt);
2873 return STG_E_INVALIDPARAMETER;
2876 /***********************************************************************
2877 * PropVariantClear [OLE32.@]
2879 HRESULT WINAPI PropVariantClear(PROPVARIANT * pvar) /* [in/out] */
2883 TRACE("(%p)\n", pvar);
2888 hr = PROPVARIANT_ValidateType(pvar->vt);
2914 case VT_STREAMED_OBJECT:
2916 case VT_STORED_OBJECT:
2917 if (pvar->u.pStream)
2918 IStream_Release(pvar->u.pStream);
2923 /* pick an arbitrary typed pointer - we don't care about the type
2924 * as we are just freeing it */
2925 CoTaskMemFree(pvar->u.puuid);
2928 case VT_BLOB_OBJECT:
2929 CoTaskMemFree(pvar->u.blob.pBlobData);
2932 PropSysFreeString(pvar->u.bstrVal);
2935 if (pvar->u.pclipdata)
2937 OLE_FreeClipDataArray(1, pvar->u.pclipdata);
2938 CoTaskMemFree(pvar->u.pclipdata);
2942 if (pvar->vt & VT_VECTOR)
2946 switch (pvar->vt & ~VT_VECTOR)
2949 FreePropVariantArray(pvar->u.capropvar.cElems, pvar->u.capropvar.pElems);
2952 OLE_FreeClipDataArray(pvar->u.caclipdata.cElems, pvar->u.caclipdata.pElems);
2955 for (i = 0; i < pvar->u.cabstr.cElems; i++)
2956 PropSysFreeString(pvar->u.cabstr.pElems[i]);
2959 for (i = 0; i < pvar->u.calpstr.cElems; i++)
2960 CoTaskMemFree(pvar->u.calpstr.pElems[i]);
2963 for (i = 0; i < pvar->u.calpwstr.cElems; i++)
2964 CoTaskMemFree(pvar->u.calpwstr.pElems[i]);
2967 if (pvar->vt & ~VT_VECTOR)
2969 /* pick an arbitrary VT_VECTOR structure - they all have the same
2971 CoTaskMemFree(pvar->u.capropvar.pElems);
2975 WARN("Invalid/unsupported type %d\n", pvar->vt);
2978 ZeroMemory(pvar, sizeof(*pvar));
2983 /***********************************************************************
2984 * PropVariantCopy [OLE32.@]
2986 HRESULT WINAPI PropVariantCopy(PROPVARIANT *pvarDest, /* [out] */
2987 const PROPVARIANT *pvarSrc) /* [in] */
2992 TRACE("(%p, %p vt %04x)\n", pvarDest, pvarSrc, pvarSrc->vt);
2994 hr = PROPVARIANT_ValidateType(pvarSrc->vt);
2998 /* this will deal with most cases */
2999 *pvarDest = *pvarSrc;
3023 case VT_STREAMED_OBJECT:
3025 case VT_STORED_OBJECT:
3026 IUnknown_AddRef((LPUNKNOWN)pvarDest->u.pStream);
3029 pvarDest->u.puuid = CoTaskMemAlloc(sizeof(CLSID));
3030 *pvarDest->u.puuid = *pvarSrc->u.puuid;
3033 len = strlen(pvarSrc->u.pszVal);
3034 pvarDest->u.pszVal = CoTaskMemAlloc((len+1)*sizeof(CHAR));
3035 CopyMemory(pvarDest->u.pszVal, pvarSrc->u.pszVal, (len+1)*sizeof(CHAR));
3038 len = lstrlenW(pvarSrc->u.pwszVal);
3039 pvarDest->u.pwszVal = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
3040 CopyMemory(pvarDest->u.pwszVal, pvarSrc->u.pwszVal, (len+1)*sizeof(WCHAR));
3043 case VT_BLOB_OBJECT:
3044 if (pvarSrc->u.blob.pBlobData)
3046 len = pvarSrc->u.blob.cbSize;
3047 pvarDest->u.blob.pBlobData = CoTaskMemAlloc(len);
3048 CopyMemory(pvarDest->u.blob.pBlobData, pvarSrc->u.blob.pBlobData, len);
3052 pvarDest->u.bstrVal = PropSysAllocString(pvarSrc->u.bstrVal);
3055 if (pvarSrc->u.pclipdata)
3057 len = pvarSrc->u.pclipdata->cbSize - sizeof(pvarSrc->u.pclipdata->ulClipFmt);
3058 pvarDest->u.pclipdata = CoTaskMemAlloc(sizeof (CLIPDATA));
3059 pvarDest->u.pclipdata->cbSize = pvarSrc->u.pclipdata->cbSize;
3060 pvarDest->u.pclipdata->ulClipFmt = pvarSrc->u.pclipdata->ulClipFmt;
3061 pvarDest->u.pclipdata->pClipData = CoTaskMemAlloc(len);
3062 CopyMemory(pvarDest->u.pclipdata->pClipData, pvarSrc->u.pclipdata->pClipData, len);
3066 if (pvarSrc->vt & VT_VECTOR)
3071 switch(pvarSrc->vt & ~VT_VECTOR)
3073 case VT_I1: elemSize = sizeof(pvarSrc->u.cVal); break;
3074 case VT_UI1: elemSize = sizeof(pvarSrc->u.bVal); break;
3075 case VT_I2: elemSize = sizeof(pvarSrc->u.iVal); break;
3076 case VT_UI2: elemSize = sizeof(pvarSrc->u.uiVal); break;
3077 case VT_BOOL: elemSize = sizeof(pvarSrc->u.boolVal); break;
3078 case VT_I4: elemSize = sizeof(pvarSrc->u.lVal); break;
3079 case VT_UI4: elemSize = sizeof(pvarSrc->u.ulVal); break;
3080 case VT_R4: elemSize = sizeof(pvarSrc->u.fltVal); break;
3081 case VT_R8: elemSize = sizeof(pvarSrc->u.dblVal); break;
3082 case VT_ERROR: elemSize = sizeof(pvarSrc->u.scode); break;
3083 case VT_I8: elemSize = sizeof(pvarSrc->u.hVal); break;
3084 case VT_UI8: elemSize = sizeof(pvarSrc->u.uhVal); break;
3085 case VT_CY: elemSize = sizeof(pvarSrc->u.cyVal); break;
3086 case VT_DATE: elemSize = sizeof(pvarSrc->u.date); break;
3087 case VT_FILETIME: elemSize = sizeof(pvarSrc->u.filetime); break;
3088 case VT_CLSID: elemSize = sizeof(*pvarSrc->u.puuid); break;
3089 case VT_CF: elemSize = sizeof(*pvarSrc->u.pclipdata); break;
3090 case VT_BSTR: elemSize = sizeof(pvarSrc->u.bstrVal); break;
3091 case VT_LPSTR: elemSize = sizeof(pvarSrc->u.pszVal); break;
3092 case VT_LPWSTR: elemSize = sizeof(pvarSrc->u.pwszVal); break;
3093 case VT_VARIANT: elemSize = sizeof(*pvarSrc->u.pvarVal); break;
3096 FIXME("Invalid element type: %ul\n", pvarSrc->vt & ~VT_VECTOR);
3097 return E_INVALIDARG;
3099 len = pvarSrc->u.capropvar.cElems;
3100 pvarDest->u.capropvar.pElems = CoTaskMemAlloc(len * elemSize);
3101 if (pvarSrc->vt == (VT_VECTOR | VT_VARIANT))
3103 for (i = 0; i < len; i++)
3104 PropVariantCopy(&pvarDest->u.capropvar.pElems[i], &pvarSrc->u.capropvar.pElems[i]);
3106 else if (pvarSrc->vt == (VT_VECTOR | VT_CF))
3108 FIXME("Copy clipformats\n");
3110 else if (pvarSrc->vt == (VT_VECTOR | VT_BSTR))
3112 for (i = 0; i < len; i++)
3113 pvarDest->u.cabstr.pElems[i] = PropSysAllocString(pvarSrc->u.cabstr.pElems[i]);
3115 else if (pvarSrc->vt == (VT_VECTOR | VT_LPSTR))
3118 for (i = 0; i < len; i++)
3120 strLen = lstrlenA(pvarSrc->u.calpstr.pElems[i]) + 1;
3121 pvarDest->u.calpstr.pElems[i] = CoTaskMemAlloc(strLen);
3122 memcpy(pvarDest->u.calpstr.pElems[i],
3123 pvarSrc->u.calpstr.pElems[i], strLen);
3126 else if (pvarSrc->vt == (VT_VECTOR | VT_LPWSTR))
3129 for (i = 0; i < len; i++)
3131 strLen = (lstrlenW(pvarSrc->u.calpwstr.pElems[i]) + 1) *
3133 pvarDest->u.calpstr.pElems[i] = CoTaskMemAlloc(strLen);
3134 memcpy(pvarDest->u.calpstr.pElems[i],
3135 pvarSrc->u.calpstr.pElems[i], strLen);
3139 CopyMemory(pvarDest->u.capropvar.pElems, pvarSrc->u.capropvar.pElems, len * elemSize);
3142 WARN("Invalid/unsupported type %d\n", pvarSrc->vt);
3148 /***********************************************************************
3149 * FreePropVariantArray [OLE32.@]
3151 HRESULT WINAPI FreePropVariantArray(ULONG cVariants, /* [in] */
3152 PROPVARIANT *rgvars) /* [in/out] */
3156 TRACE("(%u, %p)\n", cVariants, rgvars);
3159 return E_INVALIDARG;
3161 for(i = 0; i < cVariants; i++)
3162 PropVariantClear(&rgvars[i]);
3167 /******************************************************************************
3168 * DllDebugObjectRPCHook (OLE32.@)
3169 * turns on and off internal debugging, pointer is only used on macintosh
3172 BOOL WINAPI DllDebugObjectRPCHook(BOOL b, void *dummy)