4 * Copyright 1995 Martin von Loewis
5 * Copyright 1999 Francis Beaudet
6 * Copyright 1999 Noel Borthwick
7 * Copyright 1999, 2000 Marcus Meissner
8 * Copyright 1999-2000 Abey George
9 * Copyright 2005 Juan Lang
10 * Copyright 2011 Adam Martinson for CodeWeavers
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
44 #include "compobj_private.h"
46 #include "wine/list.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
51 WINE_DECLARE_DEBUG_CHANNEL(accel
);
53 /******************************************************************************
54 * These are static/global variables and internal data structures that the
55 * OLE module uses to maintain its state.
57 typedef struct tagTrackerWindowInfo
59 IDataObject
* dataObject
;
60 IDropSource
* dropSource
;
68 HWND curTargetHWND
; /* window the mouse is hovering over */
69 IDropTarget
* curDragTarget
;
70 POINTL curMousePos
; /* current position of the mouse in screen coordinates */
71 DWORD dwKeyState
; /* current state of the shift and ctrl keys and the mouse buttons */
74 typedef struct tagOleMenuDescriptor
/* OleMenuDescriptor */
76 HWND hwndFrame
; /* The containers frame window */
77 HWND hwndActiveObject
; /* The active objects window */
78 OLEMENUGROUPWIDTHS mgw
; /* OLE menu group widths for the shared menu */
79 HMENU hmenuCombined
; /* The combined menu */
80 BOOL bIsServerItem
; /* True if the currently open popup belongs to the server */
83 typedef struct tagOleMenuHookItem
/* OleMenu hook item in per thread hook list */
85 DWORD tid
; /* Thread Id */
86 HANDLE hHeap
; /* Heap this is allocated from */
87 HHOOK GetMsg_hHook
; /* message hook for WH_GETMESSAGE */
88 HHOOK CallWndProc_hHook
; /* message hook for WH_CALLWNDPROC */
89 struct tagOleMenuHookItem
*next
;
92 static OleMenuHookItem
*hook_list
;
95 * This is the lock count on the OLE library. It is controlled by the
96 * OLEInitialize/OLEUninitialize methods.
98 static LONG OLE_moduleLockCount
= 0;
101 * Name of our registered window class.
103 static const WCHAR OLEDD_DRAGTRACKERCLASS
[] = L
"WineDragDropTracker32";
106 * Name of menu descriptor property.
108 static const WCHAR prop_olemenuW
[] = L
"PROP_OLEMenuDescriptor";
110 /* property to store IDropTarget pointer */
111 static const WCHAR prop_oledroptarget
[] = L
"OleDropTargetInterface";
113 /* property to store Marshalled IDropTarget pointer */
114 static const WCHAR prop_marshalleddroptarget
[] = L
"WineMarshalledDropTarget";
116 /******************************************************************************
117 * These are the prototypes of miscellaneous utility methods
119 static void OLEUTL_ReadRegistryDWORDValue(HKEY regKey
, DWORD
* pdwValue
);
121 /******************************************************************************
122 * These are the prototypes of the utility methods used to manage a shared menu
124 static void OLEMenu_Initialize(void);
125 static void OLEMenu_UnInitialize(void);
126 static BOOL
OLEMenu_InstallHooks( DWORD tid
);
127 static BOOL
OLEMenu_UnInstallHooks( DWORD tid
);
128 static OleMenuHookItem
* OLEMenu_IsHookInstalled( DWORD tid
);
129 static BOOL
OLEMenu_FindMainMenuIndex( HMENU hMainMenu
, HMENU hPopupMenu
, UINT
*pnPos
);
130 static BOOL
OLEMenu_SetIsServerMenu( HMENU hmenu
, OleMenuDescriptor
*pOleMenuDescriptor
);
131 static LRESULT CALLBACK
OLEMenu_CallWndProc(INT code
, WPARAM wParam
, LPARAM lParam
);
132 static LRESULT CALLBACK
OLEMenu_GetMsgProc(INT code
, WPARAM wParam
, LPARAM lParam
);
134 /******************************************************************************
135 * This is a prototype of the OLE Clipboard uninitialization method (in clipboard.c)
137 extern void clipbrd_uninitialize(void);
139 /******************************************************************************
140 * These are the prototypes of the utility methods used for OLE Drag n Drop
142 static void OLEDD_Initialize(void);
143 static LRESULT WINAPI
OLEDD_DragTrackerWindowProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
144 static void OLEDD_TrackStateChange(TrackerWindowInfo
* trackerInfo
);
145 static DWORD
OLEDD_GetButtonState(void);
147 /******************************************************************************
148 * OleBuildVersion [OLE32.@]
150 DWORD WINAPI
OleBuildVersion(void)
152 TRACE("Returning version %d, build %d.\n", rmm
, rup
);
153 return (rmm
<<16)+rup
;
156 /***********************************************************************
157 * OleInitialize (OLE32.@)
159 HRESULT WINAPI DECLSPEC_HOTPATCH
OleInitialize(LPVOID reserved
)
163 TRACE("(%p)\n", reserved
);
166 * The first duty of the OleInitialize is to initialize the COM libraries.
168 hr
= CoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
171 * If the CoInitializeEx call failed, the OLE libraries can't be
177 if (!COM_CurrentInfo()->ole_inits
)
183 * Then, it has to initialize the OLE specific modules.
187 * Object linking and Embedding
188 * In-place activation
190 if (!COM_CurrentInfo()->ole_inits
++ &&
191 InterlockedIncrement(&OLE_moduleLockCount
) == 1)
194 * Initialize the libraries.
196 TRACE("() - Initializing the OLE libraries\n");
206 OLEMenu_Initialize();
212 /******************************************************************************
213 * OleUninitialize [OLE32.@]
215 void WINAPI DECLSPEC_HOTPATCH
OleUninitialize(void)
219 if (COM_CurrentInfo()->ole_inits
== 0)
221 WARN("ole_inits is already 0\n");
225 * If we hit the bottom of the lock stack, free the libraries.
227 if (!--COM_CurrentInfo()->ole_inits
&& !InterlockedDecrement(&OLE_moduleLockCount
))
230 * Actually free the libraries.
232 TRACE("() - Freeing the last reference count\n");
237 clipbrd_uninitialize();
242 OLEMenu_UnInitialize();
246 * Then, uninitialize the COM libraries.
251 /******************************************************************************
252 * OleInitializeWOW [OLE32.@]
254 HRESULT WINAPI
OleInitializeWOW(DWORD x
, DWORD y
)
256 FIXME("%#lx, %#lx stub!\n", x
, y
);
260 /*************************************************************
261 * get_droptarget_handle
263 * Retrieve a handle to the map containing the marshalled IDropTarget.
264 * This handle belongs to the process that called RegisterDragDrop.
265 * See get_droptarget_local_handle().
267 static inline HANDLE
get_droptarget_handle(HWND hwnd
)
269 return GetPropW(hwnd
, prop_marshalleddroptarget
);
272 /*************************************************************
275 * Is the window a droptarget.
277 static inline BOOL
is_droptarget(HWND hwnd
)
279 return get_droptarget_handle(hwnd
) != 0;
282 /*************************************************************
283 * get_droptarget_local_handle
285 * Retrieve a handle to the map containing the marshalled IDropTarget.
286 * The handle should be closed when finished with.
288 static HANDLE
get_droptarget_local_handle(HWND hwnd
)
290 HANDLE handle
, local_handle
= 0;
292 handle
= get_droptarget_handle(hwnd
);
299 GetWindowThreadProcessId(hwnd
, &pid
);
300 process
= OpenProcess(PROCESS_DUP_HANDLE
, FALSE
, pid
);
303 DuplicateHandle(process
, handle
, GetCurrentProcess(), &local_handle
, 0, FALSE
, DUPLICATE_SAME_ACCESS
);
304 CloseHandle(process
);
310 /***********************************************************************
311 * create_map_from_stream
313 * Helper for RegisterDragDrop. Creates a file mapping object
314 * with the contents of the provided stream. The stream must
315 * be a global memory backed stream.
317 static HRESULT
create_map_from_stream(IStream
*stream
, HANDLE
*map
)
324 hr
= GetHGlobalFromStream(stream
, &hmem
);
325 if(FAILED(hr
)) return hr
;
327 size
= GlobalSize(hmem
);
328 *map
= CreateFileMappingW(INVALID_HANDLE_VALUE
, NULL
, PAGE_READWRITE
, 0, size
, NULL
);
329 if(!*map
) return E_OUTOFMEMORY
;
331 data
= MapViewOfFile(*map
, FILE_MAP_WRITE
, 0, 0, size
);
332 memcpy(data
, GlobalLock(hmem
), size
);
334 UnmapViewOfFile(data
);
338 /***********************************************************************
339 * create_stream_from_map
341 * Creates a stream from the provided map.
343 static HRESULT
create_stream_from_map(HANDLE map
, IStream
**stream
)
345 HRESULT hr
= E_OUTOFMEMORY
;
348 MEMORY_BASIC_INFORMATION info
;
350 data
= MapViewOfFile(map
, FILE_MAP_READ
, 0, 0, 0);
353 VirtualQuery(data
, &info
, sizeof(info
));
354 TRACE("size %d\n", (int)info
.RegionSize
);
356 hmem
= GlobalAlloc(GMEM_MOVEABLE
, info
.RegionSize
);
359 memcpy(GlobalLock(hmem
), data
, info
.RegionSize
);
361 hr
= CreateStreamOnHGlobal(hmem
, TRUE
, stream
);
363 UnmapViewOfFile(data
);
367 /* This is to work around apps which break COM rules by not implementing
368 * IDropTarget::QueryInterface(). Windows doesn't expose this because it
369 * doesn't call CoMarshallInterface() in RegisterDragDrop().
370 * The wrapper is only used internally, and only exists for the life of
371 * the marshal. We don't want to hold a ref on the app provided target
372 * as some apps destroy this prior to CoUninitialize without calling
373 * RevokeDragDrop. The only (long-term) ref is held by the window prop. */
375 IDropTarget IDropTarget_iface
;
380 static inline DropTargetWrapper
* impl_from_IDropTarget(IDropTarget
* iface
)
382 return CONTAINING_RECORD(iface
, DropTargetWrapper
, IDropTarget_iface
);
385 static HRESULT WINAPI
DropTargetWrapper_QueryInterface(IDropTarget
* iface
,
389 DropTargetWrapper
* This
= impl_from_IDropTarget(iface
);
390 if (IsEqualIID(riid
, &IID_IUnknown
) ||
391 IsEqualIID(riid
, &IID_IDropTarget
))
393 IDropTarget_AddRef(&This
->IDropTarget_iface
);
394 *ppvObject
= &This
->IDropTarget_iface
;
398 return E_NOINTERFACE
;
401 static ULONG WINAPI
DropTargetWrapper_AddRef(IDropTarget
* iface
)
403 DropTargetWrapper
* This
= impl_from_IDropTarget(iface
);
404 return InterlockedIncrement(&This
->refs
);
407 static ULONG WINAPI
DropTargetWrapper_Release(IDropTarget
* iface
)
409 DropTargetWrapper
* This
= impl_from_IDropTarget(iface
);
410 ULONG refs
= InterlockedDecrement(&This
->refs
);
411 if (!refs
) HeapFree(GetProcessHeap(), 0, This
);
415 static inline HRESULT
get_target_from_wrapper( IDropTarget
*wrapper
, IDropTarget
**target
)
417 DropTargetWrapper
* This
= impl_from_IDropTarget( wrapper
);
418 *target
= GetPropW( This
->hwnd
, prop_oledroptarget
);
419 if (!*target
) return DRAGDROP_E_NOTREGISTERED
;
420 IDropTarget_AddRef( *target
);
424 static HRESULT WINAPI
DropTargetWrapper_DragEnter(IDropTarget
* iface
,
425 IDataObject
* pDataObj
,
431 HRESULT r
= get_target_from_wrapper( iface
, &target
);
435 r
= IDropTarget_DragEnter( target
, pDataObj
, grfKeyState
, pt
, pdwEffect
);
436 IDropTarget_Release( target
);
441 static HRESULT WINAPI
DropTargetWrapper_DragOver(IDropTarget
* iface
,
447 HRESULT r
= get_target_from_wrapper( iface
, &target
);
451 r
= IDropTarget_DragOver( target
, grfKeyState
, pt
, pdwEffect
);
452 IDropTarget_Release( target
);
457 static HRESULT WINAPI
DropTargetWrapper_DragLeave(IDropTarget
* iface
)
460 HRESULT r
= get_target_from_wrapper( iface
, &target
);
464 r
= IDropTarget_DragLeave( target
);
465 IDropTarget_Release( target
);
470 static HRESULT WINAPI
DropTargetWrapper_Drop(IDropTarget
* iface
,
471 IDataObject
* pDataObj
,
477 HRESULT r
= get_target_from_wrapper( iface
, &target
);
481 r
= IDropTarget_Drop( target
, pDataObj
, grfKeyState
, pt
, pdwEffect
);
482 IDropTarget_Release( target
);
487 static const IDropTargetVtbl DropTargetWrapperVTbl
=
489 DropTargetWrapper_QueryInterface
,
490 DropTargetWrapper_AddRef
,
491 DropTargetWrapper_Release
,
492 DropTargetWrapper_DragEnter
,
493 DropTargetWrapper_DragOver
,
494 DropTargetWrapper_DragLeave
,
495 DropTargetWrapper_Drop
498 static IDropTarget
* WrapDropTarget( HWND hwnd
)
500 DropTargetWrapper
* This
= HeapAlloc(GetProcessHeap(), 0, sizeof(*This
));
504 This
->IDropTarget_iface
.lpVtbl
= &DropTargetWrapperVTbl
;
508 return &This
->IDropTarget_iface
;
511 /***********************************************************************
512 * get_droptarget_pointer
514 * Retrieves the marshalled IDropTarget from the window.
516 static IDropTarget
* get_droptarget_pointer(HWND hwnd
)
518 IDropTarget
*droptarget
= NULL
;
522 map
= get_droptarget_local_handle(hwnd
);
523 if(!map
) return NULL
;
525 if(SUCCEEDED(create_stream_from_map(map
, &stream
)))
527 CoUnmarshalInterface(stream
, &IID_IDropTarget
, (void**)&droptarget
);
528 IStream_Release(stream
);
534 /***********************************************************************
535 * RegisterDragDrop (OLE32.@)
537 HRESULT WINAPI
RegisterDragDrop(HWND hwnd
, LPDROPTARGET pDropTarget
)
543 IDropTarget
*wrapper
;
545 TRACE("(%p,%p)\n", hwnd
, pDropTarget
);
547 if (!COM_CurrentApt())
549 ERR("COM not initialized\n");
550 return E_OUTOFMEMORY
;
558 ERR("invalid hwnd %p\n", hwnd
);
559 return DRAGDROP_E_INVALIDHWND
;
562 /* block register for other processes windows */
563 GetWindowThreadProcessId(hwnd
, &pid
);
564 if (pid
!= GetCurrentProcessId())
566 FIXME("register for another process windows is disabled\n");
567 return DRAGDROP_E_INVALIDHWND
;
570 /* check if the window is already registered */
571 if (is_droptarget(hwnd
))
572 return DRAGDROP_E_ALREADYREGISTERED
;
575 * Marshal the drop target pointer into a shared memory map and
576 * store the map's handle in a Wine specific window prop. We also
577 * store the drop target pointer itself in the
578 * "OleDropTargetInterface" prop for compatibility with Windows.
581 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &stream
);
582 if(FAILED(hr
)) return hr
;
584 /* IDropTarget::QueryInterface() shouldn't be called, some (broken) apps depend on this. */
585 wrapper
= WrapDropTarget( hwnd
);
588 IStream_Release(stream
);
589 return E_OUTOFMEMORY
;
591 hr
= CoMarshalInterface(stream
, &IID_IDropTarget
, (IUnknown
*)wrapper
, MSHCTX_LOCAL
, NULL
, MSHLFLAGS_TABLESTRONG
);
592 IDropTarget_Release(wrapper
);
596 hr
= create_map_from_stream(stream
, &map
);
599 IDropTarget_AddRef(pDropTarget
);
600 SetPropW(hwnd
, prop_oledroptarget
, pDropTarget
);
601 SetPropW(hwnd
, prop_marshalleddroptarget
, map
);
607 IStream_Seek(stream
, zero
, STREAM_SEEK_SET
, NULL
);
608 CoReleaseMarshalData(stream
);
611 IStream_Release(stream
);
616 /***********************************************************************
617 * RevokeDragDrop (OLE32.@)
619 HRESULT WINAPI
RevokeDragDrop(HWND hwnd
)
623 IDropTarget
*drop_target
;
626 TRACE("(%p)\n", hwnd
);
630 ERR("invalid hwnd %p\n", hwnd
);
631 return DRAGDROP_E_INVALIDHWND
;
634 /* no registration data */
635 if (!(map
= get_droptarget_handle(hwnd
)))
636 return DRAGDROP_E_NOTREGISTERED
;
638 drop_target
= GetPropW(hwnd
, prop_oledroptarget
);
639 if(drop_target
) IDropTarget_Release(drop_target
);
641 RemovePropW(hwnd
, prop_oledroptarget
);
642 RemovePropW(hwnd
, prop_marshalleddroptarget
);
644 hr
= create_stream_from_map(map
, &stream
);
647 CoReleaseMarshalData(stream
);
648 IStream_Release(stream
);
655 /***********************************************************************
656 * OleRegGetUserType (OLE32.@)
658 HRESULT WINAPI
OleRegGetUserType(REFCLSID clsid
, DWORD form
, LPOLESTR
*usertype
)
660 DWORD valuetype
, valuelen
;
661 WCHAR auxkeynameW
[16];
666 TRACE("%s, %lu, %p.\n", debugstr_guid(clsid
), form
, usertype
);
673 /* Return immediately if it's not registered. */
674 hres
= COM_OpenKeyForCLSID(clsid
, NULL
, KEY_READ
, &usertypekey
);
680 /* Try additional types if requested. If they don't exist fall back to USERCLASSTYPE_FULL. */
681 if (form
!= USERCLASSTYPE_FULL
)
685 swprintf(auxkeynameW
, ARRAY_SIZE(auxkeynameW
), L
"AuxUserType\\%d", form
);
686 if (COM_OpenKeyForCLSID(clsid
, auxkeynameW
, KEY_READ
, &auxkey
) == S_OK
)
688 if (!RegQueryValueExW(auxkey
, L
"", NULL
, &valuetype
, NULL
, &valuelen
) && valuelen
)
690 RegCloseKey(usertypekey
);
691 usertypekey
= auxkey
;
699 if (RegQueryValueExW(usertypekey
, L
"", NULL
, &valuetype
, NULL
, &valuelen
))
701 RegCloseKey(usertypekey
);
702 return REGDB_E_READREGDB
;
705 *usertype
= CoTaskMemAlloc(valuelen
);
708 RegCloseKey(usertypekey
);
709 return E_OUTOFMEMORY
;
712 ret
= RegQueryValueExW(usertypekey
, L
"", NULL
, &valuetype
, (BYTE
*)*usertype
, &valuelen
);
713 RegCloseKey(usertypekey
);
714 if (ret
!= ERROR_SUCCESS
)
716 CoTaskMemFree(*usertype
);
718 return REGDB_E_READREGDB
;
724 /***********************************************************************
725 * DoDragDrop [OLE32.@]
727 HRESULT WINAPI
DoDragDrop (
728 IDataObject
*pDataObject
, /* [in] ptr to the data obj */
729 IDropSource
* pDropSource
, /* [in] ptr to the source obj */
730 DWORD dwOKEffect
, /* [in] effects allowed by the source */
731 DWORD
*pdwEffect
) /* [out] ptr to effects of the source */
733 TrackerWindowInfo trackerInfo
;
734 HWND hwndTrackWindow
;
737 TRACE("%p, %p, %#lx, %p.\n", pDataObject
, pDropSource
, dwOKEffect
, pdwEffect
);
739 if (!pDataObject
|| !pDropSource
|| !pdwEffect
)
743 * Setup the drag n drop tracking window.
746 trackerInfo
.dataObject
= pDataObject
;
747 trackerInfo
.dropSource
= pDropSource
;
748 trackerInfo
.dwOKEffect
= dwOKEffect
;
749 trackerInfo
.pdwEffect
= pdwEffect
;
750 trackerInfo
.trackingDone
= FALSE
;
751 trackerInfo
.inTrackCall
= FALSE
;
752 trackerInfo
.escPressed
= FALSE
;
753 trackerInfo
.curTargetHWND
= 0;
754 trackerInfo
.curDragTarget
= 0;
756 hwndTrackWindow
= CreateWindowW(OLEDD_DRAGTRACKERCLASS
, L
"TrackerWindow",
757 WS_POPUP
, CW_USEDEFAULT
, CW_USEDEFAULT
,
758 CW_USEDEFAULT
, CW_USEDEFAULT
, 0, 0, 0,
764 * Capture the mouse input
766 SetCapture(hwndTrackWindow
);
771 * Pump messages. All mouse input should go to the capture window.
773 while (!trackerInfo
.trackingDone
&& GetMessageW(&msg
, 0, 0, 0) )
775 trackerInfo
.curMousePos
.x
= msg
.pt
.x
;
776 trackerInfo
.curMousePos
.y
= msg
.pt
.y
;
777 trackerInfo
.dwKeyState
= OLEDD_GetButtonState();
779 if ( (msg
.message
>= WM_KEYFIRST
) &&
780 (msg
.message
<= WM_KEYLAST
) )
783 * When keyboard messages are sent to windows on this thread, we
784 * want to ignore notify the drop source that the state changed.
785 * in the case of the Escape key, we also notify the drop source
786 * we give it a special meaning.
788 if ( (msg
.message
==WM_KEYDOWN
) &&
789 (msg
.wParam
==VK_ESCAPE
) )
791 trackerInfo
.escPressed
= TRUE
;
795 * Notify the drop source.
797 OLEDD_TrackStateChange(&trackerInfo
);
802 * Dispatch the messages only when it's not a keyboard message.
804 DispatchMessageW(&msg
);
808 /* re-post the quit message to outer message loop */
809 if (msg
.message
== WM_QUIT
)
810 PostQuitMessage(msg
.wParam
);
812 * Destroy the temporary window.
814 DestroyWindow(hwndTrackWindow
);
816 return trackerInfo
.returnValue
;
822 /***********************************************************************
823 * OleQueryLinkFromData [OLE32.@]
825 HRESULT WINAPI
OleQueryLinkFromData(
826 IDataObject
* pSrcDataObject
)
828 FIXME("(%p),stub!\n", pSrcDataObject
);
832 /***********************************************************************
833 * OleRegGetMiscStatus [OLE32.@]
835 HRESULT WINAPI
OleRegGetMiscStatus(
846 TRACE("%s, %ld, %p.\n", debugstr_guid(clsid
), dwAspect
, pdwStatus
);
848 if (!pdwStatus
) return E_INVALIDARG
;
852 if (actctx_get_miscstatus(clsid
, dwAspect
, pdwStatus
)) return S_OK
;
854 hr
= COM_OpenKeyForCLSID(clsid
, L
"MiscStatus", KEY_READ
, &miscStatusKey
);
856 /* missing key is not a failure */
857 return hr
== REGDB_E_KEYMISSING
? S_OK
: hr
;
859 OLEUTL_ReadRegistryDWORDValue(miscStatusKey
, pdwStatus
);
862 * Open the key specific to the requested aspect.
864 swprintf(keyName
, ARRAY_SIZE(keyName
), L
"%d", dwAspect
);
866 result
= open_classes_key(miscStatusKey
, keyName
, KEY_READ
, &aspectKey
);
867 if (result
== ERROR_SUCCESS
)
869 OLEUTL_ReadRegistryDWORDValue(aspectKey
, pdwStatus
);
870 RegCloseKey(aspectKey
);
873 RegCloseKey(miscStatusKey
);
877 static HRESULT
EnumOLEVERB_Construct(HKEY hkeyVerb
, ULONG index
, IEnumOLEVERB
**ppenum
);
881 IEnumOLEVERB IEnumOLEVERB_iface
;
888 static inline EnumOLEVERB
*impl_from_IEnumOLEVERB(IEnumOLEVERB
*iface
)
890 return CONTAINING_RECORD(iface
, EnumOLEVERB
, IEnumOLEVERB_iface
);
893 static HRESULT WINAPI
EnumOLEVERB_QueryInterface(
894 IEnumOLEVERB
*iface
, REFIID riid
, void **ppv
)
896 TRACE("(%s, %p)\n", debugstr_guid(riid
), ppv
);
897 if (IsEqualIID(riid
, &IID_IUnknown
) ||
898 IsEqualIID(riid
, &IID_IEnumOLEVERB
))
900 IEnumOLEVERB_AddRef(iface
);
904 return E_NOINTERFACE
;
907 static ULONG WINAPI
EnumOLEVERB_AddRef(
910 EnumOLEVERB
*This
= impl_from_IEnumOLEVERB(iface
);
912 return InterlockedIncrement(&This
->ref
);
915 static ULONG WINAPI
EnumOLEVERB_Release(
918 EnumOLEVERB
*This
= impl_from_IEnumOLEVERB(iface
);
919 LONG refs
= InterlockedDecrement(&This
->ref
);
923 RegCloseKey(This
->hkeyVerb
);
924 HeapFree(GetProcessHeap(), 0, This
);
929 static HRESULT WINAPI
EnumOLEVERB_Next(
930 IEnumOLEVERB
*iface
, ULONG celt
, LPOLEVERB rgelt
,
933 EnumOLEVERB
*This
= impl_from_IEnumOLEVERB(iface
);
936 TRACE("%p, %lu, %p, %p.\n", iface
, celt
, rgelt
, pceltFetched
);
941 for (; celt
; celt
--, rgelt
++)
946 LPWSTR pwszMenuFlags
;
948 LONG res
= RegEnumKeyW(This
->hkeyVerb
, This
->index
, wszSubKey
, ARRAY_SIZE(wszSubKey
));
949 if (res
== ERROR_NO_MORE_ITEMS
)
954 else if (res
!= ERROR_SUCCESS
)
956 ERR("RegEnumKeyW failed with error %ld\n", res
);
957 hr
= REGDB_E_READREGDB
;
960 res
= RegQueryValueW(This
->hkeyVerb
, wszSubKey
, NULL
, &cbData
);
961 if (res
!= ERROR_SUCCESS
)
963 ERR("RegQueryValueW failed with error %ld\n", res
);
964 hr
= REGDB_E_READREGDB
;
967 pwszOLEVERB
= CoTaskMemAlloc(cbData
);
973 res
= RegQueryValueW(This
->hkeyVerb
, wszSubKey
, pwszOLEVERB
, &cbData
);
974 if (res
!= ERROR_SUCCESS
)
976 ERR("RegQueryValueW failed with error %ld\n", res
);
977 hr
= REGDB_E_READREGDB
;
978 CoTaskMemFree(pwszOLEVERB
);
982 TRACE("verb string: %s\n", debugstr_w(pwszOLEVERB
));
983 pwszMenuFlags
= wcschr(pwszOLEVERB
, ',');
986 hr
= OLEOBJ_E_INVALIDVERB
;
987 CoTaskMemFree(pwszOLEVERB
);
990 /* nul terminate the name string and advance to first character */
991 *pwszMenuFlags
= '\0';
993 pwszAttribs
= wcschr(pwszMenuFlags
, ',');
996 hr
= OLEOBJ_E_INVALIDVERB
;
997 CoTaskMemFree(pwszOLEVERB
);
1000 /* nul terminate the menu string and advance to first character */
1001 *pwszAttribs
= '\0';
1004 /* fill out structure for this verb */
1005 rgelt
->lVerb
= wcstol(wszSubKey
, NULL
, 10);
1006 rgelt
->lpszVerbName
= pwszOLEVERB
; /* user should free */
1007 rgelt
->fuFlags
= wcstol(pwszMenuFlags
, NULL
, 10);
1008 rgelt
->grfAttribs
= wcstol(pwszAttribs
, NULL
, 10);
1017 static HRESULT WINAPI
EnumOLEVERB_Skip(
1018 IEnumOLEVERB
*iface
, ULONG celt
)
1020 EnumOLEVERB
*This
= impl_from_IEnumOLEVERB(iface
);
1022 TRACE("%p, %lu.\n", iface
, celt
);
1024 This
->index
+= celt
;
1028 static HRESULT WINAPI
EnumOLEVERB_Reset(
1029 IEnumOLEVERB
*iface
)
1031 EnumOLEVERB
*This
= impl_from_IEnumOLEVERB(iface
);
1039 static HRESULT WINAPI
EnumOLEVERB_Clone(
1040 IEnumOLEVERB
*iface
,
1041 IEnumOLEVERB
**ppenum
)
1043 EnumOLEVERB
*This
= impl_from_IEnumOLEVERB(iface
);
1045 TRACE("(%p)\n", ppenum
);
1046 if (!DuplicateHandle(GetCurrentProcess(), This
->hkeyVerb
, GetCurrentProcess(), (HANDLE
*)&hkeyVerb
, 0, FALSE
, DUPLICATE_SAME_ACCESS
))
1047 return HRESULT_FROM_WIN32(GetLastError());
1048 return EnumOLEVERB_Construct(hkeyVerb
, This
->index
, ppenum
);
1051 static const IEnumOLEVERBVtbl EnumOLEVERB_VTable
=
1053 EnumOLEVERB_QueryInterface
,
1055 EnumOLEVERB_Release
,
1062 static HRESULT
EnumOLEVERB_Construct(HKEY hkeyVerb
, ULONG index
, IEnumOLEVERB
**ppenum
)
1064 EnumOLEVERB
*This
= HeapAlloc(GetProcessHeap(), 0, sizeof(*This
));
1067 RegCloseKey(hkeyVerb
);
1068 return E_OUTOFMEMORY
;
1070 This
->IEnumOLEVERB_iface
.lpVtbl
= &EnumOLEVERB_VTable
;
1072 This
->index
= index
;
1073 This
->hkeyVerb
= hkeyVerb
;
1074 *ppenum
= &This
->IEnumOLEVERB_iface
;
1078 /***********************************************************************
1079 * OleRegEnumVerbs [OLE32.@]
1081 * Enumerates verbs associated with a class stored in the registry.
1084 * clsid [I] Class ID to enumerate the verbs for.
1085 * ppenum [O] Enumerator.
1089 * REGDB_E_CLASSNOTREG: The specified class does not have a key in the registry.
1090 * REGDB_E_READREGDB: The class key could not be opened for some other reason.
1091 * OLE_E_REGDB_KEY: The Verb subkey for the class is not present.
1092 * OLEOBJ_E_NOVERBS: The Verb subkey for the class is empty.
1094 HRESULT WINAPI
OleRegEnumVerbs (REFCLSID clsid
, LPENUMOLEVERB
* ppenum
)
1100 TRACE("(%s, %p)\n", debugstr_guid(clsid
), ppenum
);
1102 res
= COM_OpenKeyForCLSID(clsid
, L
"Verb", KEY_READ
, &hkeyVerb
);
1105 if (res
== REGDB_E_CLASSNOTREG
)
1106 ERR("CLSID %s not registered\n", debugstr_guid(clsid
));
1107 else if (res
== REGDB_E_KEYMISSING
)
1108 ERR("no Verbs key for class %s\n", debugstr_guid(clsid
));
1110 ERR("failed to open Verbs key for CLSID %s with error %ld\n",
1111 debugstr_guid(clsid
), res
);
1115 res
= RegQueryInfoKeyW(hkeyVerb
, NULL
, NULL
, NULL
, &dwSubKeys
, NULL
,
1116 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
1117 if (res
!= ERROR_SUCCESS
)
1119 ERR("failed to get subkey count with error %ld\n", GetLastError());
1120 return REGDB_E_READREGDB
;
1125 WARN("class %s has no verbs\n", debugstr_guid(clsid
));
1126 RegCloseKey(hkeyVerb
);
1127 return OLEOBJ_E_NOVERBS
;
1130 return EnumOLEVERB_Construct(hkeyVerb
, 0, ppenum
);
1133 /******************************************************************************
1134 * OleSetContainedObject [OLE32.@]
1136 HRESULT WINAPI
OleSetContainedObject(
1140 IRunnableObject
* runnable
= NULL
;
1143 TRACE("(%p,%x)\n", pUnknown
, fContained
);
1145 hres
= IUnknown_QueryInterface(pUnknown
,
1146 &IID_IRunnableObject
,
1149 if (SUCCEEDED(hres
))
1151 hres
= IRunnableObject_SetContainedObject(runnable
, fContained
);
1153 IRunnableObject_Release(runnable
);
1161 /******************************************************************************
1164 * Set the OLE object to the running state.
1167 * pUnknown [I] OLE object to run.
1171 * Failure: Any HRESULT code.
1173 HRESULT WINAPI DECLSPEC_HOTPATCH
OleRun(LPUNKNOWN pUnknown
)
1175 IRunnableObject
*runable
;
1178 TRACE("(%p)\n", pUnknown
);
1180 hres
= IUnknown_QueryInterface(pUnknown
, &IID_IRunnableObject
, (void**)&runable
);
1182 return S_OK
; /* Appears to return no error. */
1184 hres
= IRunnableObject_Run(runable
, NULL
);
1185 IRunnableObject_Release(runable
);
1189 /******************************************************************************
1192 HRESULT WINAPI
OleLoad(
1195 LPOLECLIENTSITE pClientSite
,
1198 IPersistStorage
* persistStorage
= NULL
;
1200 IOleObject
* pOleObject
= NULL
;
1201 STATSTG storageInfo
;
1204 TRACE("(%p, %s, %p, %p)\n", pStg
, debugstr_guid(riid
), pClientSite
, ppvObj
);
1209 * TODO, Conversion ... OleDoAutoConvert
1213 * Get the class ID for the object.
1215 hres
= IStorage_Stat(pStg
, &storageInfo
, STATFLAG_NONAME
);
1220 * Now, try and create the handler for the object
1222 hres
= CoCreateInstance(&storageInfo
.clsid
,
1224 CLSCTX_INPROC_HANDLER
|CLSCTX_INPROC_SERVER
,
1229 * If that fails, as it will most times, load the default
1234 hres
= OleCreateDefaultHandler(&storageInfo
.clsid
,
1241 * If we couldn't find a handler... this is bad. Abort the whole thing.
1248 hres
= IUnknown_QueryInterface(pUnk
, &IID_IOleObject
, (void **)&pOleObject
);
1249 if (SUCCEEDED(hres
))
1252 hres
= IOleObject_GetMiscStatus(pOleObject
, DVASPECT_CONTENT
, &dwStatus
);
1257 * Initialize the object with its IPersistStorage interface.
1259 hres
= IUnknown_QueryInterface(pUnk
, &IID_IPersistStorage
, (void**)&persistStorage
);
1260 if (SUCCEEDED(hres
))
1262 hres
= IPersistStorage_Load(persistStorage
, pStg
);
1264 IPersistStorage_Release(persistStorage
);
1265 persistStorage
= NULL
;
1268 if (SUCCEEDED(hres
) && pClientSite
)
1270 * Inform the new object of its client site.
1272 hres
= IOleObject_SetClientSite(pOleObject
, pClientSite
);
1275 * Cleanup interfaces used internally
1278 IOleObject_Release(pOleObject
);
1280 if (SUCCEEDED(hres
))
1284 hres1
= IUnknown_QueryInterface(pUnk
, &IID_IOleLink
, (void **)&pOleLink
);
1285 if (SUCCEEDED(hres1
))
1287 FIXME("handle OLE link\n");
1288 IOleLink_Release(pOleLink
);
1294 IUnknown_Release(pUnk
);
1303 /***********************************************************************
1306 HRESULT WINAPI
OleSave(
1307 LPPERSISTSTORAGE pPS
,
1314 TRACE("(%p,%p,%x)\n", pPS
, pStg
, fSameAsLoad
);
1317 * First, we transfer the class ID (if available)
1319 hres
= IPersistStorage_GetClassID(pPS
, &objectClass
);
1321 if (SUCCEEDED(hres
))
1323 WriteClassStg(pStg
, &objectClass
);
1327 * Then, we ask the object to save itself to the
1328 * storage. If it is successful, we commit the storage.
1330 hres
= IPersistStorage_Save(pPS
, pStg
, fSameAsLoad
);
1332 if (SUCCEEDED(hres
))
1334 IStorage_Commit(pStg
,
1342 /******************************************************************************
1343 * OleLockRunning [OLE32.@]
1345 HRESULT WINAPI
OleLockRunning(LPUNKNOWN pUnknown
, BOOL fLock
, BOOL fLastUnlockCloses
)
1347 IRunnableObject
* runnable
= NULL
;
1350 TRACE("(%p,%x,%x)\n", pUnknown
, fLock
, fLastUnlockCloses
);
1352 hres
= IUnknown_QueryInterface(pUnknown
,
1353 &IID_IRunnableObject
,
1356 if (SUCCEEDED(hres
))
1358 hres
= IRunnableObject_LockRunning(runnable
, fLock
, fLastUnlockCloses
);
1360 IRunnableObject_Release(runnable
);
1369 /**************************************************************************
1370 * Internal methods to manage the shared OLE menu in response to the
1371 * OLE***MenuDescriptor API
1375 * OLEMenu_Initialize()
1377 * Initializes the OLEMENU data structures.
1379 static void OLEMenu_Initialize(void)
1384 * OLEMenu_UnInitialize()
1386 * Releases the OLEMENU data structures.
1388 static void OLEMenu_UnInitialize(void)
1392 /*************************************************************************
1393 * OLEMenu_InstallHooks
1394 * Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
1396 * RETURNS: TRUE if message hooks were successfully installed
1399 static BOOL
OLEMenu_InstallHooks( DWORD tid
)
1401 OleMenuHookItem
*pHookItem
;
1403 /* Create an entry for the hook table */
1404 if ( !(pHookItem
= HeapAlloc(GetProcessHeap(), 0,
1405 sizeof(OleMenuHookItem
)) ) )
1408 pHookItem
->tid
= tid
;
1409 pHookItem
->hHeap
= GetProcessHeap();
1410 pHookItem
->CallWndProc_hHook
= NULL
;
1412 /* Install a thread scope message hook for WH_GETMESSAGE */
1413 pHookItem
->GetMsg_hHook
= SetWindowsHookExW( WH_GETMESSAGE
, OLEMenu_GetMsgProc
,
1414 0, GetCurrentThreadId() );
1415 if ( !pHookItem
->GetMsg_hHook
)
1418 /* Install a thread scope message hook for WH_CALLWNDPROC */
1419 pHookItem
->CallWndProc_hHook
= SetWindowsHookExW( WH_CALLWNDPROC
, OLEMenu_CallWndProc
,
1420 0, GetCurrentThreadId() );
1421 if ( !pHookItem
->CallWndProc_hHook
)
1424 /* Insert the hook table entry */
1425 pHookItem
->next
= hook_list
;
1426 hook_list
= pHookItem
;
1431 /* Unhook any hooks */
1432 if ( pHookItem
->GetMsg_hHook
)
1433 UnhookWindowsHookEx( pHookItem
->GetMsg_hHook
);
1434 if ( pHookItem
->CallWndProc_hHook
)
1435 UnhookWindowsHookEx( pHookItem
->CallWndProc_hHook
);
1436 /* Release the hook table entry */
1437 HeapFree(pHookItem
->hHeap
, 0, pHookItem
);
1442 /*************************************************************************
1443 * OLEMenu_UnInstallHooks
1444 * UnInstall thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
1446 * RETURNS: TRUE if message hooks were successfully installed
1449 static BOOL
OLEMenu_UnInstallHooks( DWORD tid
)
1451 OleMenuHookItem
*pHookItem
= NULL
;
1452 OleMenuHookItem
**ppHook
= &hook_list
;
1456 if ((*ppHook
)->tid
== tid
)
1458 pHookItem
= *ppHook
;
1459 *ppHook
= pHookItem
->next
;
1462 ppHook
= &(*ppHook
)->next
;
1464 if (!pHookItem
) return FALSE
;
1466 /* Uninstall the hooks installed for this thread */
1467 if ( !UnhookWindowsHookEx( pHookItem
->GetMsg_hHook
) )
1469 if ( !UnhookWindowsHookEx( pHookItem
->CallWndProc_hHook
) )
1472 /* Release the hook table entry */
1473 HeapFree(pHookItem
->hHeap
, 0, pHookItem
);
1478 /* Release the hook table entry */
1479 HeapFree(pHookItem
->hHeap
, 0, pHookItem
);
1484 /*************************************************************************
1485 * OLEMenu_IsHookInstalled
1486 * Tests if OLEMenu hooks have been installed for a thread
1488 * RETURNS: The pointer and index of the hook table entry for the tid
1489 * NULL and -1 for the index if no hooks were installed for this thread
1491 static OleMenuHookItem
* OLEMenu_IsHookInstalled( DWORD tid
)
1493 OleMenuHookItem
*pHookItem
;
1495 /* Do a simple linear search for an entry whose tid matches ours.
1496 * We really need a map but efficiency is not a concern here. */
1497 for (pHookItem
= hook_list
; pHookItem
; pHookItem
= pHookItem
->next
)
1499 if ( tid
== pHookItem
->tid
)
1506 /***********************************************************************
1507 * OLEMenu_FindMainMenuIndex
1509 * Used by OLEMenu API to find the top level group a menu item belongs to.
1510 * On success pnPos contains the index of the item in the top level menu group
1512 * RETURNS: TRUE if the ID was found, FALSE on failure
1514 static BOOL
OLEMenu_FindMainMenuIndex( HMENU hMainMenu
, HMENU hPopupMenu
, UINT
*pnPos
)
1518 nItems
= GetMenuItemCount( hMainMenu
);
1520 for (i
= 0; i
< nItems
; i
++)
1524 /* Is the current item a submenu? */
1525 if ( (hsubmenu
= GetSubMenu(hMainMenu
, i
)) )
1527 /* If the handle is the same we're done */
1528 if ( hsubmenu
== hPopupMenu
)
1534 /* Recursively search without updating pnPos */
1535 else if ( OLEMenu_FindMainMenuIndex( hsubmenu
, hPopupMenu
, NULL
) )
1547 /***********************************************************************
1548 * OLEMenu_SetIsServerMenu
1550 * Checks whether a popup menu belongs to a shared menu group which is
1551 * owned by the server, and sets the menu descriptor state accordingly.
1552 * All menu messages from these groups should be routed to the server.
1554 * RETURNS: TRUE if the popup menu is part of a server owned group
1555 * FALSE if the popup menu is part of a container owned group
1557 static BOOL
OLEMenu_SetIsServerMenu( HMENU hmenu
, OleMenuDescriptor
*pOleMenuDescriptor
)
1559 UINT nPos
= 0, nWidth
, i
;
1561 pOleMenuDescriptor
->bIsServerItem
= FALSE
;
1563 /* Don't bother searching if the popup is the combined menu itself */
1564 if ( hmenu
== pOleMenuDescriptor
->hmenuCombined
)
1567 /* Find the menu item index in the shared OLE menu that this item belongs to */
1568 if ( !OLEMenu_FindMainMenuIndex( pOleMenuDescriptor
->hmenuCombined
, hmenu
, &nPos
) )
1571 /* The group widths array has counts for the number of elements
1572 * in the groups File, Edit, Container, Object, Window, Help.
1573 * The Edit, Object & Help groups belong to the server object
1574 * and the other three belong to the container.
1575 * Loop through the group widths and locate the group we are a member of.
1577 for ( i
= 0, nWidth
= 0; i
< 6; i
++ )
1579 nWidth
+= pOleMenuDescriptor
->mgw
.width
[i
];
1580 if ( nPos
< nWidth
)
1582 /* Odd elements are server menu widths */
1583 pOleMenuDescriptor
->bIsServerItem
= i
%2;
1588 return pOleMenuDescriptor
->bIsServerItem
;
1591 /*************************************************************************
1592 * OLEMenu_CallWndProc
1593 * Thread scope WH_CALLWNDPROC hook proc filter function (callback)
1594 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1596 static LRESULT CALLBACK
OLEMenu_CallWndProc(INT code
, WPARAM wParam
, LPARAM lParam
)
1599 HOLEMENU hOleMenu
= 0;
1600 OleMenuDescriptor
*pOleMenuDescriptor
= NULL
;
1601 OleMenuHookItem
*pHookItem
= NULL
;
1604 TRACE("%i, %#Ix, %#Ix.\n", code
, wParam
, lParam
);
1606 /* Check if we're being asked to process the message */
1607 if ( HC_ACTION
!= code
)
1610 /* Retrieve the current message being dispatched from lParam */
1611 pMsg
= (LPCWPSTRUCT
)lParam
;
1613 /* Check if the message is destined for a window we are interested in:
1614 * If the window has an OLEMenu property we may need to dispatch
1615 * the menu message to its active objects window instead. */
1617 hOleMenu
= GetPropW( pMsg
->hwnd
, prop_olemenuW
);
1621 /* Get the menu descriptor */
1622 pOleMenuDescriptor
= GlobalLock( hOleMenu
);
1623 if ( !pOleMenuDescriptor
) /* Bad descriptor! */
1626 /* Process menu messages */
1627 switch( pMsg
->message
)
1631 /* Reset the menu descriptor state */
1632 pOleMenuDescriptor
->bIsServerItem
= FALSE
;
1634 /* Send this message to the server as well */
1635 SendMessageW( pOleMenuDescriptor
->hwndActiveObject
,
1636 pMsg
->message
, pMsg
->wParam
, pMsg
->lParam
);
1640 case WM_INITMENUPOPUP
:
1642 /* Save the state for whether this is a server owned menu */
1643 OLEMenu_SetIsServerMenu( (HMENU
)pMsg
->wParam
, pOleMenuDescriptor
);
1649 fuFlags
= HIWORD(pMsg
->wParam
); /* Get flags */
1650 if ( fuFlags
& MF_SYSMENU
)
1653 /* Save the state for whether this is a server owned popup menu */
1654 else if ( fuFlags
& MF_POPUP
)
1655 OLEMenu_SetIsServerMenu( (HMENU
)pMsg
->lParam
, pOleMenuDescriptor
);
1662 LPDRAWITEMSTRUCT lpdis
= (LPDRAWITEMSTRUCT
) pMsg
->lParam
;
1663 if ( pMsg
->wParam
!= 0 || lpdis
->CtlType
!= ODT_MENU
)
1664 goto NEXTHOOK
; /* Not a menu message */
1673 /* If the message was for the server dispatch it accordingly */
1674 if ( pOleMenuDescriptor
->bIsServerItem
)
1676 SendMessageW( pOleMenuDescriptor
->hwndActiveObject
,
1677 pMsg
->message
, pMsg
->wParam
, pMsg
->lParam
);
1681 if ( pOleMenuDescriptor
)
1682 GlobalUnlock( hOleMenu
);
1684 /* Lookup the hook item for the current thread */
1685 if ( !( pHookItem
= OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1687 /* This should never fail!! */
1688 WARN("could not retrieve hHook for current thread!\n" );
1692 /* Pass on the message to the next hooker */
1693 return CallNextHookEx( pHookItem
->CallWndProc_hHook
, code
, wParam
, lParam
);
1696 /*************************************************************************
1697 * OLEMenu_GetMsgProc
1698 * Thread scope WH_GETMESSAGE hook proc filter function (callback)
1699 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1701 static LRESULT CALLBACK
OLEMenu_GetMsgProc(INT code
, WPARAM wParam
, LPARAM lParam
)
1704 HOLEMENU hOleMenu
= 0;
1705 OleMenuDescriptor
*pOleMenuDescriptor
= NULL
;
1706 OleMenuHookItem
*pHookItem
= NULL
;
1709 TRACE("%i, %#Ix, %#Ix.\n", code
, wParam
, lParam
);
1711 /* Check if we're being asked to process a messages */
1712 if ( HC_ACTION
!= code
)
1715 /* Retrieve the current message being dispatched from lParam */
1716 pMsg
= (LPMSG
)lParam
;
1718 /* Check if the message is destined for a window we are interested in:
1719 * If the window has an OLEMenu property we may need to dispatch
1720 * the menu message to its active objects window instead. */
1722 hOleMenu
= GetPropW( pMsg
->hwnd
, prop_olemenuW
);
1726 /* Process menu messages */
1727 switch( pMsg
->message
)
1731 wCode
= HIWORD(pMsg
->wParam
); /* Get notification code */
1733 goto NEXTHOOK
; /* Not a menu message */
1740 /* Get the menu descriptor */
1741 pOleMenuDescriptor
= GlobalLock( hOleMenu
);
1742 if ( !pOleMenuDescriptor
) /* Bad descriptor! */
1745 /* If the message was for the server dispatch it accordingly */
1746 if ( pOleMenuDescriptor
->bIsServerItem
)
1748 /* Change the hWnd in the message to the active objects hWnd.
1749 * The message loop which reads this message will automatically
1750 * dispatch it to the embedded objects window. */
1751 pMsg
->hwnd
= pOleMenuDescriptor
->hwndActiveObject
;
1755 if ( pOleMenuDescriptor
)
1756 GlobalUnlock( hOleMenu
);
1758 /* Lookup the hook item for the current thread */
1759 if ( !( pHookItem
= OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1761 /* This should never fail!! */
1762 WARN("could not retrieve hHook for current thread!\n" );
1766 /* Pass on the message to the next hooker */
1767 return CallNextHookEx( pHookItem
->GetMsg_hHook
, code
, wParam
, lParam
);
1770 /***********************************************************************
1771 * OleCreateMenuDescriptor [OLE32.@]
1772 * Creates an OLE menu descriptor for OLE to use when dispatching
1773 * menu messages and commands.
1776 * hmenuCombined - Handle to the objects combined menu
1777 * lpMenuWidths - Pointer to array of 6 LONG's indicating menus per group
1780 HOLEMENU WINAPI
OleCreateMenuDescriptor(
1781 HMENU hmenuCombined
,
1782 LPOLEMENUGROUPWIDTHS lpMenuWidths
)
1785 OleMenuDescriptor
*pOleMenuDescriptor
;
1788 if ( !hmenuCombined
|| !lpMenuWidths
)
1791 /* Create an OLE menu descriptor */
1792 if ( !(hOleMenu
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_ZEROINIT
,
1793 sizeof(OleMenuDescriptor
) ) ) )
1796 pOleMenuDescriptor
= GlobalLock( hOleMenu
);
1797 if ( !pOleMenuDescriptor
)
1800 /* Initialize menu group widths and hmenu */
1801 for ( i
= 0; i
< 6; i
++ )
1802 pOleMenuDescriptor
->mgw
.width
[i
] = lpMenuWidths
->width
[i
];
1804 pOleMenuDescriptor
->hmenuCombined
= hmenuCombined
;
1805 pOleMenuDescriptor
->bIsServerItem
= FALSE
;
1806 GlobalUnlock( hOleMenu
);
1811 /***********************************************************************
1812 * OleDestroyMenuDescriptor [OLE32.@]
1813 * Destroy the shared menu descriptor
1815 HRESULT WINAPI
OleDestroyMenuDescriptor(
1816 HOLEMENU hmenuDescriptor
)
1818 if ( hmenuDescriptor
)
1819 GlobalFree( hmenuDescriptor
);
1823 /***********************************************************************
1824 * OleSetMenuDescriptor [OLE32.@]
1825 * Installs or removes OLE dispatching code for the containers frame window.
1828 * hOleMenu Handle to composite menu descriptor
1829 * hwndFrame Handle to containers frame window
1830 * hwndActiveObject Handle to objects in-place activation window
1831 * lpFrame Pointer to IOleInPlaceFrame on containers window
1832 * lpActiveObject Pointer to IOleInPlaceActiveObject on active in-place object
1835 * S_OK - menu installed correctly
1836 * E_FAIL, E_INVALIDARG, E_UNEXPECTED - failure
1839 * The lpFrame and lpActiveObject parameters are currently ignored
1840 * OLE should install context sensitive help F1 filtering for the app when
1841 * these are non null.
1843 HRESULT WINAPI
OleSetMenuDescriptor(
1846 HWND hwndActiveObject
,
1847 LPOLEINPLACEFRAME lpFrame
,
1848 LPOLEINPLACEACTIVEOBJECT lpActiveObject
)
1850 OleMenuDescriptor
*pOleMenuDescriptor
= NULL
;
1853 if ( !hwndFrame
|| (hOleMenu
&& !hwndActiveObject
) )
1854 return E_INVALIDARG
;
1856 if ( lpFrame
|| lpActiveObject
)
1858 FIXME("(%p, %p, %p, %p, %p), Context sensitive help filtering not implemented!\n",
1866 /* Set up a message hook to intercept the containers frame window messages.
1867 * The message filter is responsible for dispatching menu messages from the
1868 * shared menu which are intended for the object.
1871 if ( hOleMenu
) /* Want to install dispatching code */
1873 /* If OLEMenu hooks are already installed for this thread, fail
1874 * Note: This effectively means that OleSetMenuDescriptor cannot
1875 * be called twice in succession on the same frame window
1876 * without first calling it with a null hOleMenu to uninstall
1878 if ( OLEMenu_IsHookInstalled( GetCurrentThreadId() ) )
1881 /* Get the menu descriptor */
1882 pOleMenuDescriptor
= GlobalLock( hOleMenu
);
1883 if ( !pOleMenuDescriptor
)
1884 return E_UNEXPECTED
;
1886 /* Update the menu descriptor */
1887 pOleMenuDescriptor
->hwndFrame
= hwndFrame
;
1888 pOleMenuDescriptor
->hwndActiveObject
= hwndActiveObject
;
1890 GlobalUnlock( hOleMenu
);
1891 pOleMenuDescriptor
= NULL
;
1893 /* Add a menu descriptor windows property to the frame window */
1894 SetPropW( hwndFrame
, prop_olemenuW
, hOleMenu
);
1896 /* Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC */
1897 if ( !OLEMenu_InstallHooks( GetCurrentThreadId() ) )
1900 else /* Want to uninstall dispatching code */
1902 /* Uninstall the hooks */
1903 if ( !OLEMenu_UnInstallHooks( GetCurrentThreadId() ) )
1906 /* Remove the menu descriptor property from the frame window */
1907 RemovePropW( hwndFrame
, prop_olemenuW
);
1913 /******************************************************************************
1914 * IsAccelerator [OLE32.@]
1915 * Mostly copied from controls/menu.c TranslateAccelerator implementation
1917 BOOL WINAPI
IsAccelerator(HACCEL hAccel
, int cAccelEntries
, LPMSG lpMsg
, WORD
* lpwCmd
)
1922 if(!lpMsg
) return FALSE
;
1925 WARN_(accel
)("NULL accel handle\n");
1928 if((lpMsg
->message
!= WM_KEYDOWN
&&
1929 lpMsg
->message
!= WM_SYSKEYDOWN
&&
1930 lpMsg
->message
!= WM_SYSCHAR
&&
1931 lpMsg
->message
!= WM_CHAR
)) return FALSE
;
1932 lpAccelTbl
= HeapAlloc(GetProcessHeap(), 0, cAccelEntries
* sizeof(ACCEL
));
1933 if (NULL
== lpAccelTbl
)
1937 if (CopyAcceleratorTableW(hAccel
, lpAccelTbl
, cAccelEntries
) != cAccelEntries
)
1939 WARN_(accel
)("CopyAcceleratorTableW failed\n");
1940 HeapFree(GetProcessHeap(), 0, lpAccelTbl
);
1944 TRACE_(accel
)("hAccel=%p, cAccelEntries=%d,"
1945 "msg->hwnd=%p, msg->message=%04x, wParam=%#Ix, lParam=%#Ix\n",
1946 hAccel
, cAccelEntries
,
1947 lpMsg
->hwnd
, lpMsg
->message
, lpMsg
->wParam
, lpMsg
->lParam
);
1948 for(i
= 0; i
< cAccelEntries
; i
++)
1950 if(lpAccelTbl
[i
].key
!= lpMsg
->wParam
)
1953 if(lpMsg
->message
== WM_CHAR
)
1955 if(!(lpAccelTbl
[i
].fVirt
& FALT
) && !(lpAccelTbl
[i
].fVirt
& FVIRTKEY
))
1957 TRACE_(accel
)("found accel for WM_CHAR: ('%c')\n", LOWORD(lpMsg
->wParam
) & 0xff);
1963 if(lpAccelTbl
[i
].fVirt
& FVIRTKEY
)
1966 TRACE_(accel
)("found accel for virt_key %Ix (scan %04x)\n",
1967 lpMsg
->wParam
, HIWORD(lpMsg
->lParam
) & 0xff);
1968 if(GetKeyState(VK_SHIFT
) & 0x8000) mask
|= FSHIFT
;
1969 if(GetKeyState(VK_CONTROL
) & 0x8000) mask
|= FCONTROL
;
1970 if(GetKeyState(VK_MENU
) & 0x8000) mask
|= FALT
;
1971 if(mask
== (lpAccelTbl
[i
].fVirt
& (FSHIFT
| FCONTROL
| FALT
))) goto found
;
1972 TRACE_(accel
)("incorrect SHIFT/CTRL/ALT-state\n");
1976 if(!(lpMsg
->lParam
& 0x01000000)) /* no special_key */
1978 if((lpAccelTbl
[i
].fVirt
& FALT
) && (lpMsg
->lParam
& 0x20000000))
1979 { /* ^^ ALT pressed */
1980 TRACE_(accel
)("found accel for Alt-%c\n", LOWORD(lpMsg
->wParam
) & 0xff);
1988 WARN_(accel
)("couldn't translate accelerator key\n");
1989 HeapFree(GetProcessHeap(), 0, lpAccelTbl
);
1993 if(lpwCmd
) *lpwCmd
= lpAccelTbl
[i
].cmd
;
1994 HeapFree(GetProcessHeap(), 0, lpAccelTbl
);
1998 /***********************************************************************
1999 * ReleaseStgMedium [OLE32.@]
2001 void WINAPI
ReleaseStgMedium(
2004 if (!pmedium
) return;
2006 switch (pmedium
->tymed
)
2010 if ( (pmedium
->pUnkForRelease
==0) &&
2011 (pmedium
->hGlobal
!=0) )
2012 GlobalFree(pmedium
->hGlobal
);
2017 if (pmedium
->lpszFileName
!=0)
2019 if (pmedium
->pUnkForRelease
==0)
2021 DeleteFileW(pmedium
->lpszFileName
);
2024 CoTaskMemFree(pmedium
->lpszFileName
);
2030 if (pmedium
->pstm
!=0)
2032 IStream_Release(pmedium
->pstm
);
2036 case TYMED_ISTORAGE
:
2038 if (pmedium
->pstg
!=0)
2040 IStorage_Release(pmedium
->pstg
);
2046 if ( (pmedium
->pUnkForRelease
==0) &&
2047 (pmedium
->hBitmap
!=0) )
2048 DeleteObject(pmedium
->hBitmap
);
2053 if ( (pmedium
->pUnkForRelease
==0) &&
2054 (pmedium
->hMetaFilePict
!=0) )
2056 LPMETAFILEPICT pMP
= GlobalLock(pmedium
->hMetaFilePict
);
2057 DeleteMetaFile(pMP
->hMF
);
2058 GlobalUnlock(pmedium
->hMetaFilePict
);
2059 GlobalFree(pmedium
->hMetaFilePict
);
2065 if ( (pmedium
->pUnkForRelease
==0) &&
2066 (pmedium
->hEnhMetaFile
!=0) )
2068 DeleteEnhMetaFile(pmedium
->hEnhMetaFile
);
2076 pmedium
->tymed
=TYMED_NULL
;
2079 * After cleaning up, the unknown is released
2081 if (pmedium
->pUnkForRelease
!=0)
2083 IUnknown_Release(pmedium
->pUnkForRelease
);
2084 pmedium
->pUnkForRelease
= 0;
2089 * OLEDD_Initialize()
2091 * Initializes the OLE drag and drop data structures.
2093 static void OLEDD_Initialize(void)
2097 ZeroMemory (&wndClass
, sizeof(WNDCLASSW
));
2098 wndClass
.style
= CS_GLOBALCLASS
;
2099 wndClass
.lpfnWndProc
= OLEDD_DragTrackerWindowProc
;
2100 wndClass
.cbClsExtra
= 0;
2101 wndClass
.cbWndExtra
= sizeof(TrackerWindowInfo
*);
2102 wndClass
.hCursor
= 0;
2103 wndClass
.hbrBackground
= 0;
2104 wndClass
.lpszClassName
= OLEDD_DRAGTRACKERCLASS
;
2106 RegisterClassW (&wndClass
);
2110 * OLEDD_DragTrackerWindowProc()
2112 * This method is the WindowProcedure of the drag n drop tracking
2113 * window. During a drag n Drop operation, an invisible window is created
2114 * to receive the user input and act upon it. This procedure is in charge
2118 #define DRAG_TIMER_ID 1
2120 static LRESULT WINAPI
OLEDD_DragTrackerWindowProc(
2130 LPCREATESTRUCTA createStruct
= (LPCREATESTRUCTA
)lParam
;
2132 SetWindowLongPtrW(hwnd
, 0, (LONG_PTR
)createStruct
->lpCreateParams
);
2133 SetTimer(hwnd
, DRAG_TIMER_ID
, 50, NULL
);
2142 case WM_LBUTTONDOWN
:
2143 case WM_MBUTTONDOWN
:
2144 case WM_RBUTTONDOWN
:
2146 TrackerWindowInfo
*trackerInfo
= (TrackerWindowInfo
*)GetWindowLongPtrA(hwnd
, 0);
2147 if (trackerInfo
->trackingDone
) break;
2148 OLEDD_TrackStateChange(trackerInfo
);
2153 KillTimer(hwnd
, DRAG_TIMER_ID
);
2159 * This is a window proc after all. Let's call the default.
2161 return DefWindowProcW (hwnd
, uMsg
, wParam
, lParam
);
2164 static void drag_enter( TrackerWindowInfo
*info
, HWND new_target
)
2168 info
->curTargetHWND
= new_target
;
2170 while (new_target
&& !is_droptarget( new_target
))
2171 new_target
= GetParent( new_target
);
2173 info
->curDragTarget
= get_droptarget_pointer( new_target
);
2175 if (info
->curDragTarget
)
2177 *info
->pdwEffect
= info
->dwOKEffect
;
2178 hr
= IDropTarget_DragEnter( info
->curDragTarget
, info
->dataObject
,
2179 info
->dwKeyState
, info
->curMousePos
,
2181 *info
->pdwEffect
&= info
->dwOKEffect
;
2183 /* failed DragEnter() means invalid target */
2186 IDropTarget_Release( info
->curDragTarget
);
2187 info
->curDragTarget
= NULL
;
2188 info
->curTargetHWND
= NULL
;
2193 static void drag_end( TrackerWindowInfo
*info
)
2197 info
->trackingDone
= TRUE
;
2200 if (info
->curDragTarget
)
2202 if (info
->returnValue
== DRAGDROP_S_DROP
&&
2203 *info
->pdwEffect
!= DROPEFFECT_NONE
)
2205 *info
->pdwEffect
= info
->dwOKEffect
;
2206 hr
= IDropTarget_Drop( info
->curDragTarget
, info
->dataObject
, info
->dwKeyState
,
2207 info
->curMousePos
, info
->pdwEffect
);
2208 *info
->pdwEffect
&= info
->dwOKEffect
;
2211 info
->returnValue
= hr
;
2215 IDropTarget_DragLeave( info
->curDragTarget
);
2216 *info
->pdwEffect
= DROPEFFECT_NONE
;
2218 IDropTarget_Release( info
->curDragTarget
);
2219 info
->curDragTarget
= NULL
;
2222 *info
->pdwEffect
= DROPEFFECT_NONE
;
2225 static HRESULT
give_feedback( TrackerWindowInfo
*info
)
2231 if (info
->curDragTarget
== NULL
)
2232 *info
->pdwEffect
= DROPEFFECT_NONE
;
2234 hr
= IDropSource_GiveFeedback( info
->dropSource
, *info
->pdwEffect
);
2236 if (hr
== DRAGDROP_S_USEDEFAULTCURSORS
)
2238 if (*info
->pdwEffect
& DROPEFFECT_MOVE
)
2240 else if (*info
->pdwEffect
& DROPEFFECT_COPY
)
2242 else if (*info
->pdwEffect
& DROPEFFECT_LINK
)
2245 res
= CURSOR_NODROP
;
2247 cur
= LoadCursorW( hProxyDll
, MAKEINTRESOURCEW( res
) );
2255 * OLEDD_TrackStateChange()
2257 * This method is invoked while a drag and drop operation is in effect.
2260 * trackerInfo - Pointer to the structure identifying the
2261 * drag & drop operation that is currently
2264 static void OLEDD_TrackStateChange(TrackerWindowInfo
* trackerInfo
)
2266 HWND hwndNewTarget
= 0;
2270 * This method may be called from QueryContinueDrag again,
2271 * (i.e. by running message loop) so avoid recursive call chain.
2273 if (trackerInfo
->inTrackCall
) return;
2274 trackerInfo
->inTrackCall
= TRUE
;
2277 * Get the handle of the window under the mouse
2279 pt
.x
= trackerInfo
->curMousePos
.x
;
2280 pt
.y
= trackerInfo
->curMousePos
.y
;
2281 hwndNewTarget
= WindowFromPoint(pt
);
2283 trackerInfo
->returnValue
= IDropSource_QueryContinueDrag(trackerInfo
->dropSource
,
2284 trackerInfo
->escPressed
,
2285 trackerInfo
->dwKeyState
);
2287 if (trackerInfo
->curTargetHWND
!= hwndNewTarget
&&
2288 (trackerInfo
->returnValue
== S_OK
||
2289 trackerInfo
->returnValue
== DRAGDROP_S_DROP
))
2291 if (trackerInfo
->curDragTarget
)
2293 IDropTarget_DragLeave(trackerInfo
->curDragTarget
);
2294 IDropTarget_Release(trackerInfo
->curDragTarget
);
2295 trackerInfo
->curDragTarget
= NULL
;
2296 trackerInfo
->curTargetHWND
= NULL
;
2300 drag_enter( trackerInfo
, hwndNewTarget
);
2302 give_feedback( trackerInfo
);
2306 if (trackerInfo
->returnValue
== S_OK
)
2308 if (trackerInfo
->curDragTarget
)
2310 *trackerInfo
->pdwEffect
= trackerInfo
->dwOKEffect
;
2311 IDropTarget_DragOver(trackerInfo
->curDragTarget
,
2312 trackerInfo
->dwKeyState
,
2313 trackerInfo
->curMousePos
,
2314 trackerInfo
->pdwEffect
);
2315 *trackerInfo
->pdwEffect
&= trackerInfo
->dwOKEffect
;
2317 give_feedback( trackerInfo
);
2320 drag_end( trackerInfo
);
2322 trackerInfo
->inTrackCall
= FALSE
;
2326 * OLEDD_GetButtonState()
2328 * This method will use the current state of the keyboard to build
2329 * a button state mask equivalent to the one passed in the
2330 * WM_MOUSEMOVE wParam.
2332 static DWORD
OLEDD_GetButtonState(void)
2334 BYTE keyboardState
[256];
2337 GetKeyboardState(keyboardState
);
2339 if ( (keyboardState
[VK_SHIFT
] & 0x80) !=0)
2340 keyMask
|= MK_SHIFT
;
2342 if ( (keyboardState
[VK_CONTROL
] & 0x80) !=0)
2343 keyMask
|= MK_CONTROL
;
2345 if ( (keyboardState
[VK_MENU
] & 0x80) !=0)
2348 if ( (keyboardState
[VK_LBUTTON
] & 0x80) !=0)
2349 keyMask
|= MK_LBUTTON
;
2351 if ( (keyboardState
[VK_RBUTTON
] & 0x80) !=0)
2352 keyMask
|= MK_RBUTTON
;
2354 if ( (keyboardState
[VK_MBUTTON
] & 0x80) !=0)
2355 keyMask
|= MK_MBUTTON
;
2361 * OLEDD_GetButtonState()
2363 * This method will read the default value of the registry key in
2364 * parameter and extract a DWORD value from it. The registry key value
2365 * can be in a string key or a DWORD key.
2368 * regKey - Key to read the default value from
2369 * pdwValue - Pointer to the location where the DWORD
2370 * value is returned. This value is not modified
2371 * if the value is not found.
2374 static void OLEUTL_ReadRegistryDWORDValue(
2379 DWORD cbData
= sizeof(buffer
);
2383 lres
= RegQueryValueExW(regKey
, L
"", NULL
, &dwKeyType
, (BYTE
*)buffer
, &cbData
);
2385 if (lres
==ERROR_SUCCESS
)
2390 *pdwValue
= *(DWORD
*)buffer
;
2395 *pdwValue
= wcstoul(buffer
, NULL
, 10);
2401 /******************************************************************************
2404 * The operation of this function is documented literally in the WinAPI
2405 * documentation to involve a QueryInterface for the IViewObject interface,
2406 * followed by a call to IViewObject::Draw.
2408 HRESULT WINAPI
OleDraw(
2415 IViewObject
*viewobject
;
2417 if (!pUnk
) return E_INVALIDARG
;
2419 hres
= IUnknown_QueryInterface(pUnk
,
2421 (void**)&viewobject
);
2422 if (SUCCEEDED(hres
))
2424 hres
= IViewObject_Draw(viewobject
, dwAspect
, -1, 0, 0, 0, hdcDraw
, (RECTL
*)rect
, 0, 0, 0);
2425 IViewObject_Release(viewobject
);
2429 return DV_E_NOIVIEWOBJECT
;
2432 /***********************************************************************
2433 * OleTranslateAccelerator [OLE32.@]
2435 HRESULT WINAPI
OleTranslateAccelerator (LPOLEINPLACEFRAME lpFrame
,
2436 LPOLEINPLACEFRAMEINFO lpFrameInfo
, LPMSG lpmsg
)
2440 TRACE("(%p,%p,%p)\n", lpFrame
, lpFrameInfo
, lpmsg
);
2442 if (IsAccelerator(lpFrameInfo
->haccel
,lpFrameInfo
->cAccelEntries
,lpmsg
,&wID
))
2443 return IOleInPlaceFrame_TranslateAccelerator(lpFrame
,lpmsg
,wID
);
2448 /******************************************************************************
2449 * OleCreate [OLE32.@]
2452 HRESULT WINAPI
OleCreate(
2456 LPFORMATETC pFormatEtc
,
2457 LPOLECLIENTSITE pClientSite
,
2462 IUnknown
* pUnk
= NULL
;
2463 IOleObject
*pOleObject
= NULL
;
2465 TRACE("%s, %s, %ld, %p, %p, %p, %p.\n", debugstr_guid(rclsid
),
2466 debugstr_guid(riid
), renderopt
, pFormatEtc
, pClientSite
, pStg
, ppvObj
);
2468 hres
= CoCreateInstance(rclsid
, 0, CLSCTX_INPROC_SERVER
|CLSCTX_INPROC_HANDLER
, riid
, (LPVOID
*)&pUnk
);
2470 if (SUCCEEDED(hres
))
2471 hres
= IStorage_SetClass(pStg
, rclsid
);
2473 if (pClientSite
&& SUCCEEDED(hres
))
2475 hres
= IUnknown_QueryInterface(pUnk
, &IID_IOleObject
, (LPVOID
*)&pOleObject
);
2476 if (SUCCEEDED(hres
))
2479 IOleObject_GetMiscStatus(pOleObject
, DVASPECT_CONTENT
, &dwStatus
);
2483 if (SUCCEEDED(hres
))
2485 IPersistStorage
* pPS
;
2486 if (SUCCEEDED((hres
= IUnknown_QueryInterface(pUnk
, &IID_IPersistStorage
, (LPVOID
*)&pPS
))))
2488 TRACE("trying to set stg %p\n", pStg
);
2489 hres
= IPersistStorage_InitNew(pPS
, pStg
);
2490 TRACE("-- result %#lx\n", hres
);
2491 IPersistStorage_Release(pPS
);
2495 if (pClientSite
&& SUCCEEDED(hres
))
2497 TRACE("trying to set clientsite %p\n", pClientSite
);
2498 hres
= IOleObject_SetClientSite(pOleObject
, pClientSite
);
2499 TRACE("-- result %#lx\n", hres
);
2503 IOleObject_Release(pOleObject
);
2505 if (((renderopt
== OLERENDER_DRAW
) || (renderopt
== OLERENDER_FORMAT
)) &&
2508 hres
= OleRun(pUnk
);
2509 if (SUCCEEDED(hres
))
2511 IOleCache
*pOleCache
;
2513 if (SUCCEEDED(IUnknown_QueryInterface(pUnk
, &IID_IOleCache
, (void **)&pOleCache
)))
2516 if (renderopt
== OLERENDER_DRAW
&& !pFormatEtc
) {
2520 pfe
.dwAspect
= DVASPECT_CONTENT
;
2522 pfe
.tymed
= TYMED_NULL
;
2523 hres
= IOleCache_Cache(pOleCache
, &pfe
, ADVF_PRIMEFIRST
, &dwConnection
);
2526 hres
= IOleCache_Cache(pOleCache
, pFormatEtc
, ADVF_PRIMEFIRST
, &dwConnection
);
2527 IOleCache_Release(pOleCache
);
2532 if (FAILED(hres
) && pUnk
)
2534 IUnknown_Release(pUnk
);
2540 TRACE("-- %p\n", pUnk
);
2544 /******************************************************************************
2545 * OleGetAutoConvert [OLE32.@]
2547 HRESULT WINAPI
OleGetAutoConvert(REFCLSID clsidOld
, LPCLSID pClsidNew
)
2550 WCHAR buf
[CHARS_IN_GUID
];
2554 res
= COM_OpenKeyForCLSID(clsidOld
, L
"AutoConvertTo", KEY_READ
, &hkey
);
2559 if (RegQueryValueW(hkey
, NULL
, buf
, &len
))
2561 res
= REGDB_E_KEYMISSING
;
2564 res
= CLSIDFromString(buf
, pClsidNew
);
2566 if (hkey
) RegCloseKey(hkey
);
2570 /******************************************************************************
2571 * OleSetAutoConvert [OLE32.@]
2573 HRESULT WINAPI
OleSetAutoConvert(REFCLSID clsidOld
, REFCLSID clsidNew
)
2576 WCHAR szClsidNew
[CHARS_IN_GUID
];
2579 TRACE("(%s,%s)\n", debugstr_guid(clsidOld
), debugstr_guid(clsidNew
));
2581 res
= COM_OpenKeyForCLSID(clsidOld
, NULL
, KEY_READ
| KEY_WRITE
, &hkey
);
2584 StringFromGUID2(clsidNew
, szClsidNew
, CHARS_IN_GUID
);
2585 if (RegSetValueW(hkey
, L
"AutoConvertTo", REG_SZ
, szClsidNew
, (lstrlenW(szClsidNew
)+1) * sizeof(WCHAR
)))
2587 res
= REGDB_E_WRITEREGDB
;
2592 if (hkey
) RegCloseKey(hkey
);
2596 /******************************************************************************
2597 * OleDoAutoConvert [OLE32.@]
2599 HRESULT WINAPI
OleDoAutoConvert(LPSTORAGE pStg
, LPCLSID pClsidNew
)
2601 WCHAR
*user_type_old
, *user_type_new
;
2607 TRACE("(%p, %p)\n", pStg
, pClsidNew
);
2609 *pClsidNew
= CLSID_NULL
;
2611 return E_INVALIDARG
;
2612 hr
= IStorage_Stat(pStg
, &stat
, STATFLAG_NONAME
);
2616 *pClsidNew
= stat
.clsid
;
2617 hr
= OleGetAutoConvert(&stat
.clsid
, &clsid
);
2621 hr
= IStorage_SetClass(pStg
, &clsid
);
2625 hr
= ReadFmtUserTypeStg(pStg
, &cf
, &user_type_old
);
2628 user_type_new
= NULL
;
2631 hr
= OleRegGetUserType(&clsid
, USERCLASSTYPE_FULL
, &user_type_new
);
2633 user_type_new
= NULL
;
2635 hr
= WriteFmtUserTypeStg(pStg
, cf
, user_type_new
);
2636 CoTaskMemFree(user_type_new
);
2639 CoTaskMemFree(user_type_old
);
2640 IStorage_SetClass(pStg
, &stat
.clsid
);
2644 hr
= SetConvertStg(pStg
, TRUE
);
2647 WriteFmtUserTypeStg(pStg
, cf
, user_type_old
);
2648 IStorage_SetClass(pStg
, &stat
.clsid
);
2652 CoTaskMemFree(user_type_old
);
2656 /******************************************************************************
2657 * OleIsRunning [OLE32.@]
2659 BOOL WINAPI
OleIsRunning(LPOLEOBJECT object
)
2661 IRunnableObject
*pRunnable
;
2665 TRACE("(%p)\n", object
);
2667 if (!object
) return FALSE
;
2669 hr
= IOleObject_QueryInterface(object
, &IID_IRunnableObject
, (void **)&pRunnable
);
2672 running
= IRunnableObject_IsRunning(pRunnable
);
2673 IRunnableObject_Release(pRunnable
);
2677 /***********************************************************************
2678 * OleNoteObjectVisible [OLE32.@]
2680 HRESULT WINAPI
OleNoteObjectVisible(LPUNKNOWN pUnknown
, BOOL bVisible
)
2682 TRACE("(%p, %s)\n", pUnknown
, bVisible
? "TRUE" : "FALSE");
2683 return CoLockObjectExternal(pUnknown
, bVisible
, TRUE
);
2686 /***********************************************************************
2687 * PropSysAllocString [OLE32.@]
2689 * Forward to oleaut32.
2691 BSTR WINAPI
PropSysAllocString(LPCOLESTR str
)
2693 return SysAllocString(str
);
2696 /***********************************************************************
2697 * PropSysFreeString [OLE32.@]
2699 * Forward to oleaut32.
2701 void WINAPI
PropSysFreeString(LPOLESTR str
)
2706 /******************************************************************************
2707 * OleQueryCreateFromData [OLE32.@]
2709 * Checks whether an object can become an embedded object.
2710 * the clipboard or OLE drag and drop.
2711 * Returns : S_OK - Format that supports Embedded object creation are present.
2712 * OLE_E_STATIC - Format that supports static object creation are present.
2713 * S_FALSE - No acceptable format is available.
2716 HRESULT WINAPI
OleQueryCreateFromData(IDataObject
*data
)
2718 IEnumFORMATETC
*enum_fmt
;
2720 BOOL found_static
= FALSE
;
2723 hr
= IDataObject_EnumFormatEtc(data
, DATADIR_GET
, &enum_fmt
);
2725 if(FAILED(hr
)) return hr
;
2729 hr
= IEnumFORMATETC_Next(enum_fmt
, 1, &fmt
, NULL
);
2732 if(fmt
.cfFormat
== embedded_object_clipboard_format
||
2733 fmt
.cfFormat
== embed_source_clipboard_format
||
2734 fmt
.cfFormat
== filename_clipboard_format
)
2736 IEnumFORMATETC_Release(enum_fmt
);
2740 if(fmt
.cfFormat
== CF_METAFILEPICT
||
2741 fmt
.cfFormat
== CF_BITMAP
||
2742 fmt
.cfFormat
== CF_DIB
)
2743 found_static
= TRUE
;
2745 } while (hr
== S_OK
);
2747 IEnumFORMATETC_Release(enum_fmt
);
2749 return found_static
? OLE_S_STATIC
: S_FALSE
;
2752 static inline void init_fmtetc(FORMATETC
*fmt
, CLIPFORMAT cf
, TYMED tymed
)
2756 fmt
->dwAspect
= DVASPECT_CONTENT
;
2761 /***************************************************************************
2764 * Retrieve an object's storage from a variety of sources.
2766 * FIXME: CF_FILENAME.
2768 static HRESULT
get_storage(IDataObject
*data
, IStorage
*stg
, UINT
*src_cf
, BOOL other_fmts
)
2770 static const UINT fmt_id
[] = { CF_METAFILEPICT
, CF_BITMAP
, CF_DIB
};
2775 IPersistStorage
*persist
;
2778 if (src_cf
) *src_cf
= 0;
2780 /* CF_EMBEDEDOBJECT */
2781 init_fmtetc(&fmt
, embedded_object_clipboard_format
, TYMED_ISTORAGE
);
2782 med
.tymed
= TYMED_ISTORAGE
;
2784 med
.pUnkForRelease
= NULL
;
2785 hr
= IDataObject_GetDataHere(data
, &fmt
, &med
);
2788 if (src_cf
) *src_cf
= embedded_object_clipboard_format
;
2792 /* CF_EMBEDSOURCE */
2793 init_fmtetc(&fmt
, embed_source_clipboard_format
, TYMED_ISTORAGE
);
2794 med
.tymed
= TYMED_ISTORAGE
;
2796 med
.pUnkForRelease
= NULL
;
2797 hr
= IDataObject_GetDataHere(data
, &fmt
, &med
);
2800 if (src_cf
) *src_cf
= embed_source_clipboard_format
;
2806 for (i
= 0; i
< ARRAY_SIZE(fmt_id
); i
++)
2808 init_fmtetc(&fmt
, fmt_id
[i
], TYMED_ISTORAGE
);
2809 hr
= IDataObject_QueryGetData(data
, &fmt
);
2812 if (src_cf
) *src_cf
= fmt_id
[i
];
2818 /* IPersistStorage */
2819 hr
= IDataObject_QueryInterface(data
, &IID_IPersistStorage
, (void**)&persist
);
2820 if(FAILED(hr
)) return hr
;
2822 hr
= IPersistStorage_GetClassID(persist
, &clsid
);
2823 if(FAILED(hr
)) goto end
;
2825 hr
= IStorage_SetClass(stg
, &clsid
);
2826 if(FAILED(hr
)) goto end
;
2828 hr
= IPersistStorage_Save(persist
, stg
, FALSE
);
2829 if(FAILED(hr
)) goto end
;
2831 hr
= IPersistStorage_SaveCompleted(persist
, NULL
);
2834 IPersistStorage_Release(persist
);
2839 /******************************************************************************
2840 * OleCreateFromDataEx [OLE32.@]
2842 * Creates an embedded object from data transfer object retrieved from
2843 * the clipboard or OLE drag and drop.
2845 HRESULT WINAPI
OleCreateFromDataEx(IDataObject
*data
, REFIID iid
, DWORD flags
,
2846 DWORD renderopt
, ULONG num_cache_fmts
, DWORD
*adv_flags
, FORMATETC
*cache_fmts
,
2847 IAdviseSink
*sink
, DWORD
*conns
,
2848 IOleClientSite
*client_site
, IStorage
*stg
, void **obj
)
2853 FIXME("%p, %s, %#lx, %#lx, %ld, %p, %p, %p, %p, %p, %p, %p: stub\n",
2854 data
, debugstr_guid(iid
), flags
, renderopt
, num_cache_fmts
, adv_flags
, cache_fmts
,
2855 sink
, conns
, client_site
, stg
, obj
);
2857 hr
= get_storage(data
, stg
, &src_cf
, TRUE
);
2858 if(FAILED(hr
)) return hr
;
2860 hr
= OleLoad(stg
, iid
, client_site
, obj
);
2861 if(FAILED(hr
)) return hr
;
2863 /* FIXME: Init cache */
2868 /******************************************************************************
2869 * OleCreateFromData [OLE32.@]
2871 HRESULT WINAPI
OleCreateFromData(IDataObject
*data
, REFIID iid
, DWORD renderopt
, FORMATETC
*fmt
,
2872 IOleClientSite
*client_site
, IStorage
*stg
, void **obj
)
2874 DWORD advf
= ADVF_PRIMEFIRST
;
2876 return OleCreateFromDataEx(data
, iid
, 0, renderopt
, fmt
? 1 : 0, fmt
? &advf
: NULL
,
2877 fmt
, NULL
, NULL
, client_site
, stg
, obj
);
2880 /******************************************************************************
2881 * OleCreateLinkFromData [OLE32.@]
2883 HRESULT WINAPI
OleCreateLinkFromData(IDataObject
*data
, REFIID iid
, DWORD renderopt
, FORMATETC
*fmt
,
2884 IOleClientSite
*client_site
, IStorage
*stg
, void **obj
)
2886 FIXME("%p, %s, %#lx, %p, %p, %p, %p: semi-stub\n",
2887 data
, debugstr_guid(iid
), renderopt
, fmt
, client_site
, stg
, obj
);
2888 return OleCreateFromData(data
, iid
, renderopt
, fmt
, client_site
, stg
, obj
);
2891 /******************************************************************************
2892 * OleCreateStaticFromData [OLE32.@]
2894 HRESULT WINAPI
OleCreateStaticFromData(IDataObject
*data
, REFIID iid
, DWORD renderopt
, FORMATETC
*fmt
,
2895 IOleClientSite
*client_site
, IStorage
*stg
, void **obj
)
2899 IOleObject
* ole_object
= NULL
;
2900 IOleCache2
*ole_cache
= NULL
;
2901 IPersistStorage
*persist
= NULL
;
2903 STGMEDIUM stgmedium
;
2904 LPOLESTR ole_typename
;
2906 TRACE("%p, %s, %#lx, %p, %p, %p, %p.\n", data
, debugstr_guid(iid
), renderopt
, fmt
, client_site
, stg
, obj
);
2909 return E_INVALIDARG
;
2911 if (renderopt
!= OLERENDER_FORMAT
)
2913 FIXME("semi-stub\n");
2914 return OleCreateFromData(data
, iid
, renderopt
, fmt
, client_site
, stg
, obj
);
2918 return E_INVALIDARG
;
2920 hr
= IDataObject_GetData(data
, fmt
, &stgmedium
);
2921 if (FAILED(hr
)) return hr
;
2923 switch (fmt
->cfFormat
)
2927 clsid
= CLSID_Picture_Dib
;
2929 case CF_ENHMETAFILE
:
2930 clsid
= CLSID_Picture_EnhMetafile
;
2932 case CF_METAFILEPICT
:
2933 clsid
= CLSID_Picture_Metafile
;
2936 ReleaseStgMedium(&stgmedium
);
2937 return DV_E_CLIPFORMAT
;
2939 hr
= OleCreateDefaultHandler(&clsid
, NULL
, &IID_IOleObject
, (void **)&ole_object
);
2940 if (FAILED(hr
)) goto end
;
2944 hr
= IOleObject_SetClientSite(ole_object
, client_site
);
2945 if (FAILED(hr
)) goto end
;
2948 hr
= IOleObject_QueryInterface(ole_object
, &IID_IOleCache2
, (void **)&ole_cache
);
2949 if (FAILED(hr
)) goto end
;
2951 hr
= IOleObject_QueryInterface(ole_object
, &IID_IPersistStorage
, (void **)&persist
);
2952 if (FAILED(hr
)) goto end
;
2954 hr
= WriteClassStg(stg
, &clsid
);
2955 if (FAILED(hr
)) goto end
;
2957 hr
= IPersistStorage_InitNew(persist
, stg
);
2958 if (FAILED(hr
)) goto end
;
2960 hr
= IOleCache2_Cache(ole_cache
, fmt
, ADVF_PRIMEFIRST
, &connection
);
2961 if (FAILED(hr
)) goto end
;
2963 hr
= IOleCache2_SetData(ole_cache
, fmt
, &stgmedium
, TRUE
);
2964 if (FAILED(hr
)) goto end
;
2965 stgmedium
.tymed
= TYMED_NULL
;
2967 hr
= IOleObject_GetUserType(ole_object
, USERCLASSTYPE_FULL
, &ole_typename
);
2969 ole_typename
= NULL
;
2970 hr
= WriteFmtUserTypeStg(stg
, fmt
->cfFormat
, ole_typename
);
2971 CoTaskMemFree(ole_typename
);
2972 if (FAILED(hr
)) goto end
;
2974 hr
= IPersistStorage_Save(persist
, stg
, TRUE
);
2975 if (FAILED(hr
)) goto end
;
2977 hr
= IPersistStorage_SaveCompleted(persist
, NULL
);
2978 if (FAILED(hr
)) goto end
;
2980 hr
= IOleObject_QueryInterface(ole_object
, iid
, obj
);
2983 if (stgmedium
.tymed
== TYMED_NULL
)
2984 ReleaseStgMedium(&stgmedium
);
2986 IPersistStorage_Release(persist
);
2988 IOleCache2_Release(ole_cache
);
2990 IOleObject_Release(ole_object
);
2994 /******************************************************************************
2995 * OleCreateFromFileEx [OLE32.@]
2997 HRESULT WINAPI
OleCreateFromFileEx(REFCLSID clsid
, const OLECHAR
*filename
, REFIID iid
, DWORD flags
,
2998 DWORD renderopt
, ULONG num_fmts
, DWORD
*adv_flags
, FORMATETC
*fmts
, IAdviseSink
*sink
,
2999 DWORD
*conns
, IOleClientSite
*client_site
, IStorage
*stg
, void **obj
)
3004 IUnknown
*unk
= NULL
;
3005 IOleCache
*cache
= NULL
;
3008 TRACE("cls %s, %s, iid %s, flags %ld, render opts %ld, num fmts %ld, adv flags %p, fmts %p\n", debugstr_guid(clsid
),
3009 debugstr_w(filename
), debugstr_guid(iid
), flags
, renderopt
, num_fmts
, adv_flags
, fmts
);
3010 TRACE("sink %p, conns %p, client site %p, storage %p, obj %p\n", sink
, conns
, client_site
, stg
, obj
);
3011 for (i
= 0; i
< num_fmts
; i
++)
3012 TRACE("\t%ld: fmt %s adv flags %ld\n", i
, debugstr_formatetc(fmts
+ i
), adv_flags
[i
]);
3014 hr
= CreateFileMoniker( filename
, &mon
);
3015 if (FAILED(hr
)) return hr
;
3017 hr
= BindMoniker( mon
, 0, &IID_IDataObject
, (void**)&data
);
3018 IMoniker_Release( mon
);
3019 if (FAILED(hr
)) return hr
;
3021 hr
= get_storage( data
, stg
, NULL
, FALSE
);
3022 if (FAILED(hr
)) goto end
;
3024 hr
= OleLoad( stg
, &IID_IUnknown
, client_site
, (void**)&unk
);
3025 if (FAILED(hr
)) goto end
;
3027 if (renderopt
== OLERENDER_FORMAT
)
3029 hr
= IUnknown_QueryInterface( unk
, &IID_IOleCache
, (void**)&cache
);
3030 if (FAILED(hr
)) goto end
;
3032 for (i
= 0; i
< num_fmts
; i
++)
3037 memset( &med
, 0, sizeof(med
) );
3038 hr
= IDataObject_GetData( data
, fmts
+ i
, &med
);
3039 if (FAILED(hr
)) goto end
;
3040 hr
= IOleCache_Cache( cache
, fmts
+ i
, adv_flags
[i
], &dummy_conn
);
3042 hr
= IOleCache_SetData( cache
, fmts
+ i
, &med
, TRUE
);
3045 ReleaseStgMedium( &med
);
3051 hr
= IUnknown_QueryInterface( unk
, iid
, obj
);
3054 if (cache
) IOleCache_Release( cache
);
3055 if (unk
) IUnknown_Release( unk
);
3056 IDataObject_Release( data
);
3060 /******************************************************************************
3061 * OleCreateFromFile [OLE32.@]
3063 HRESULT WINAPI
OleCreateFromFile(REFCLSID clsid
, const OLECHAR
*filename
, REFIID iid
, DWORD renderopt
,
3064 FORMATETC
*fmt
, IOleClientSite
*client_site
, IStorage
*storage
, void **obj
)
3066 DWORD advf
= ADVF_PRIMEFIRST
;
3068 return OleCreateFromFileEx(clsid
, filename
, iid
, 0, renderopt
, fmt
? 1 : 0, fmt
? &advf
: NULL
, fmt
,
3069 NULL
, NULL
, client_site
, storage
, obj
);
3072 /******************************************************************************
3073 * OleDuplicateData [OLE32.@]
3075 HANDLE WINAPI
OleDuplicateData(HANDLE hSrc
, CLIPFORMAT cfFormat
, UINT uiFlags
)
3079 TRACE("(%p,%x,%x)\n", hSrc
, cfFormat
, uiFlags
);
3081 if (!uiFlags
) uiFlags
= GMEM_MOVEABLE
;
3085 case CF_ENHMETAFILE
:
3086 hDst
= CopyEnhMetaFileW(hSrc
, NULL
);
3088 case CF_METAFILEPICT
:
3089 hDst
= CopyMetaFileW(hSrc
, NULL
);
3093 LOGPALETTE
* logpalette
;
3094 UINT nEntries
= GetPaletteEntries(hSrc
, 0, 0, NULL
);
3095 if (!nEntries
) return NULL
;
3096 logpalette
= HeapAlloc(GetProcessHeap(), 0,
3097 FIELD_OFFSET(LOGPALETTE
, palPalEntry
[nEntries
]));
3098 if (!logpalette
) return NULL
;
3099 if (!GetPaletteEntries(hSrc
, 0, nEntries
, logpalette
->palPalEntry
))
3101 HeapFree(GetProcessHeap(), 0, logpalette
);
3104 logpalette
->palVersion
= 0x300;
3105 logpalette
->palNumEntries
= (WORD
)nEntries
;
3107 hDst
= CreatePalette(logpalette
);
3109 HeapFree(GetProcessHeap(), 0, logpalette
);
3116 if (!GetObjectW(hSrc
, sizeof(bm
), &bm
))
3118 size
= GetBitmapBits(hSrc
, 0, NULL
);
3119 if (!size
) return NULL
;
3120 bm
.bmBits
= HeapAlloc(GetProcessHeap(), 0, size
);
3121 if (!bm
.bmBits
) return NULL
;
3122 if (GetBitmapBits(hSrc
, size
, bm
.bmBits
))
3123 hDst
= CreateBitmapIndirect(&bm
);
3124 HeapFree(GetProcessHeap(), 0, bm
.bmBits
);
3129 SIZE_T size
= GlobalSize(hSrc
);
3130 LPVOID pvSrc
= NULL
;
3131 LPVOID pvDst
= NULL
;
3133 /* allocate space for object */
3134 if (!size
) return NULL
;
3135 hDst
= GlobalAlloc(uiFlags
, size
);
3136 if (!hDst
) return NULL
;
3139 pvSrc
= GlobalLock(hSrc
);
3145 pvDst
= GlobalLock(hDst
);
3153 memcpy(pvDst
, pvSrc
, size
);
3161 TRACE("returning %p\n", hDst
);