ntdll: Move retrieving the startup info to the Unix library.
[wine.git] / dlls / ole32 / ole2.c
blob6653fac342293cc2649fa0a1b64db5843065dfc2
1 /*
2 * OLE2 library
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
26 #include <assert.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <string.h>
32 #define COBJMACROS
33 #define NONAMELESSUNION
35 #include "windef.h"
36 #include "winbase.h"
37 #include "winerror.h"
38 #include "wingdi.h"
39 #include "winuser.h"
40 #include "winnls.h"
41 #include "winreg.h"
42 #include "ole2.h"
43 #include "ole2ver.h"
45 #include "compobj_private.h"
46 #include "olestd.h"
47 #include "wine/list.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(ole);
52 WINE_DECLARE_DEBUG_CHANNEL(accel);
54 /******************************************************************************
55 * These are static/global variables and internal data structures that the
56 * OLE module uses to maintain its state.
58 typedef struct tagTrackerWindowInfo
60 IDataObject* dataObject;
61 IDropSource* dropSource;
62 DWORD dwOKEffect;
63 DWORD* pdwEffect;
64 BOOL trackingDone;
65 BOOL inTrackCall;
66 HRESULT returnValue;
68 BOOL escPressed;
69 HWND curTargetHWND; /* window the mouse is hovering over */
70 IDropTarget* curDragTarget;
71 POINTL curMousePos; /* current position of the mouse in screen coordinates */
72 DWORD dwKeyState; /* current state of the shift and ctrl keys and the mouse buttons */
73 } TrackerWindowInfo;
75 typedef struct tagOleMenuDescriptor /* OleMenuDescriptor */
77 HWND hwndFrame; /* The containers frame window */
78 HWND hwndActiveObject; /* The active objects window */
79 OLEMENUGROUPWIDTHS mgw; /* OLE menu group widths for the shared menu */
80 HMENU hmenuCombined; /* The combined menu */
81 BOOL bIsServerItem; /* True if the currently open popup belongs to the server */
82 } OleMenuDescriptor;
84 typedef struct tagOleMenuHookItem /* OleMenu hook item in per thread hook list */
86 DWORD tid; /* Thread Id */
87 HANDLE hHeap; /* Heap this is allocated from */
88 HHOOK GetMsg_hHook; /* message hook for WH_GETMESSAGE */
89 HHOOK CallWndProc_hHook; /* message hook for WH_CALLWNDPROC */
90 struct tagOleMenuHookItem *next;
91 } OleMenuHookItem;
93 static OleMenuHookItem *hook_list;
96 * This is the lock count on the OLE library. It is controlled by the
97 * OLEInitialize/OLEUninitialize methods.
99 static LONG OLE_moduleLockCount = 0;
102 * Name of our registered window class.
104 static const WCHAR OLEDD_DRAGTRACKERCLASS[] =
105 {'W','i','n','e','D','r','a','g','D','r','o','p','T','r','a','c','k','e','r','3','2',0};
108 * Name of menu descriptor property.
110 static const WCHAR prop_olemenuW[] =
111 {'P','R','O','P','_','O','L','E','M','e','n','u','D','e','s','c','r','i','p','t','o','r',0};
113 /* property to store IDropTarget pointer */
114 static const WCHAR prop_oledroptarget[] =
115 {'O','l','e','D','r','o','p','T','a','r','g','e','t','I','n','t','e','r','f','a','c','e',0};
117 /* property to store Marshalled IDropTarget pointer */
118 static const WCHAR prop_marshalleddroptarget[] =
119 {'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};
121 static const WCHAR emptyW[] = { 0 };
123 /******************************************************************************
124 * These are the prototypes of miscellaneous utility methods
126 static void OLEUTL_ReadRegistryDWORDValue(HKEY regKey, DWORD* pdwValue);
128 /******************************************************************************
129 * These are the prototypes of the utility methods used to manage a shared menu
131 static void OLEMenu_Initialize(void);
132 static void OLEMenu_UnInitialize(void);
133 static BOOL OLEMenu_InstallHooks( DWORD tid );
134 static BOOL OLEMenu_UnInstallHooks( DWORD tid );
135 static OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid );
136 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos );
137 static BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor );
138 static LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam);
139 static LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam);
141 /******************************************************************************
142 * These are the prototypes of the OLE Clipboard initialization methods (in clipboard.c)
144 extern void OLEClipbrd_UnInitialize(void);
145 extern void OLEClipbrd_Initialize(void);
147 /******************************************************************************
148 * These are the prototypes of the utility methods used for OLE Drag n Drop
150 static void OLEDD_Initialize(void);
151 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
152 static void OLEDD_TrackStateChange(TrackerWindowInfo* trackerInfo);
153 static DWORD OLEDD_GetButtonState(void);
155 /******************************************************************************
156 * OleBuildVersion [OLE32.@]
158 DWORD WINAPI OleBuildVersion(void)
160 TRACE("Returning version %d, build %d.\n", rmm, rup);
161 return (rmm<<16)+rup;
164 /***********************************************************************
165 * OleInitialize (OLE32.@)
167 HRESULT WINAPI DECLSPEC_HOTPATCH OleInitialize(LPVOID reserved)
169 HRESULT hr;
171 TRACE("(%p)\n", reserved);
174 * The first duty of the OleInitialize is to initialize the COM libraries.
176 hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
179 * If the CoInitializeEx call failed, the OLE libraries can't be
180 * initialized.
182 if (FAILED(hr))
183 return hr;
185 if (!COM_CurrentInfo()->ole_inits)
186 hr = S_OK;
187 else
188 hr = S_FALSE;
191 * Then, it has to initialize the OLE specific modules.
192 * This includes:
193 * Clipboard
194 * Drag and Drop
195 * Object linking and Embedding
196 * In-place activation
198 if (!COM_CurrentInfo()->ole_inits++ &&
199 InterlockedIncrement(&OLE_moduleLockCount) == 1)
202 * Initialize the libraries.
204 TRACE("() - Initializing the OLE libraries\n");
207 * OLE Clipboard
209 OLEClipbrd_Initialize();
212 * Drag and Drop
214 OLEDD_Initialize();
217 * OLE shared menu
219 OLEMenu_Initialize();
222 return hr;
225 /******************************************************************************
226 * OleUninitialize [OLE32.@]
228 void WINAPI DECLSPEC_HOTPATCH OleUninitialize(void)
230 TRACE("()\n");
232 if (COM_CurrentInfo()->ole_inits == 0)
234 WARN("ole_inits is already 0\n");
235 return ;
238 * If we hit the bottom of the lock stack, free the libraries.
240 if (!--COM_CurrentInfo()->ole_inits && !InterlockedDecrement(&OLE_moduleLockCount))
243 * Actually free the libraries.
245 TRACE("() - Freeing the last reference count\n");
248 * OLE Clipboard
250 OLEClipbrd_UnInitialize();
253 * OLE shared menu
255 OLEMenu_UnInitialize();
259 * Then, uninitialize the COM libraries.
261 CoUninitialize();
264 /******************************************************************************
265 * OleInitializeWOW [OLE32.@]
267 HRESULT WINAPI OleInitializeWOW(DWORD x, DWORD y) {
268 FIXME("(0x%08x, 0x%08x),stub!\n",x, y);
269 return 0;
272 /*************************************************************
273 * get_droptarget_handle
275 * Retrieve a handle to the map containing the marshalled IDropTarget.
276 * This handle belongs to the process that called RegisterDragDrop.
277 * See get_droptarget_local_handle().
279 static inline HANDLE get_droptarget_handle(HWND hwnd)
281 return GetPropW(hwnd, prop_marshalleddroptarget);
284 /*************************************************************
285 * is_droptarget
287 * Is the window a droptarget.
289 static inline BOOL is_droptarget(HWND hwnd)
291 return get_droptarget_handle(hwnd) != 0;
294 /*************************************************************
295 * get_droptarget_local_handle
297 * Retrieve a handle to the map containing the marshalled IDropTarget.
298 * The handle should be closed when finished with.
300 static HANDLE get_droptarget_local_handle(HWND hwnd)
302 HANDLE handle, local_handle = 0;
304 handle = get_droptarget_handle(hwnd);
306 if(handle)
308 DWORD pid;
309 HANDLE process;
311 GetWindowThreadProcessId(hwnd, &pid);
312 process = OpenProcess(PROCESS_DUP_HANDLE, FALSE, pid);
313 if(process)
315 DuplicateHandle(process, handle, GetCurrentProcess(), &local_handle, 0, FALSE, DUPLICATE_SAME_ACCESS);
316 CloseHandle(process);
319 return local_handle;
322 /***********************************************************************
323 * create_map_from_stream
325 * Helper for RegisterDragDrop. Creates a file mapping object
326 * with the contents of the provided stream. The stream must
327 * be a global memory backed stream.
329 static HRESULT create_map_from_stream(IStream *stream, HANDLE *map)
331 HGLOBAL hmem;
332 DWORD size;
333 HRESULT hr;
334 void *data;
336 hr = GetHGlobalFromStream(stream, &hmem);
337 if(FAILED(hr)) return hr;
339 size = GlobalSize(hmem);
340 *map = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, size, NULL);
341 if(!*map) return E_OUTOFMEMORY;
343 data = MapViewOfFile(*map, FILE_MAP_WRITE, 0, 0, size);
344 memcpy(data, GlobalLock(hmem), size);
345 GlobalUnlock(hmem);
346 UnmapViewOfFile(data);
347 return S_OK;
350 /***********************************************************************
351 * create_stream_from_map
353 * Creates a stream from the provided map.
355 static HRESULT create_stream_from_map(HANDLE map, IStream **stream)
357 HRESULT hr = E_OUTOFMEMORY;
358 HGLOBAL hmem;
359 void *data;
360 MEMORY_BASIC_INFORMATION info;
362 data = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
363 if(!data) return hr;
365 VirtualQuery(data, &info, sizeof(info));
366 TRACE("size %d\n", (int)info.RegionSize);
368 hmem = GlobalAlloc(GMEM_MOVEABLE, info.RegionSize);
369 if(hmem)
371 memcpy(GlobalLock(hmem), data, info.RegionSize);
372 GlobalUnlock(hmem);
373 hr = CreateStreamOnHGlobal(hmem, TRUE, stream);
375 UnmapViewOfFile(data);
376 return hr;
379 /* This is to work around apps which break COM rules by not implementing
380 * IDropTarget::QueryInterface(). Windows doesn't expose this because it
381 * doesn't call CoMarshallInterface() in RegisterDragDrop().
382 * The wrapper is only used internally, and only exists for the life of
383 * the marshal. We don't want to hold a ref on the app provided target
384 * as some apps destroy this prior to CoUninitialize without calling
385 * RevokeDragDrop. The only (long-term) ref is held by the window prop. */
386 typedef struct {
387 IDropTarget IDropTarget_iface;
388 HWND hwnd;
389 LONG refs;
390 } DropTargetWrapper;
392 static inline DropTargetWrapper* impl_from_IDropTarget(IDropTarget* iface)
394 return CONTAINING_RECORD(iface, DropTargetWrapper, IDropTarget_iface);
397 static HRESULT WINAPI DropTargetWrapper_QueryInterface(IDropTarget* iface,
398 REFIID riid,
399 void** ppvObject)
401 DropTargetWrapper* This = impl_from_IDropTarget(iface);
402 if (IsEqualIID(riid, &IID_IUnknown) ||
403 IsEqualIID(riid, &IID_IDropTarget))
405 IDropTarget_AddRef(&This->IDropTarget_iface);
406 *ppvObject = &This->IDropTarget_iface;
407 return S_OK;
409 *ppvObject = NULL;
410 return E_NOINTERFACE;
413 static ULONG WINAPI DropTargetWrapper_AddRef(IDropTarget* iface)
415 DropTargetWrapper* This = impl_from_IDropTarget(iface);
416 return InterlockedIncrement(&This->refs);
419 static ULONG WINAPI DropTargetWrapper_Release(IDropTarget* iface)
421 DropTargetWrapper* This = impl_from_IDropTarget(iface);
422 ULONG refs = InterlockedDecrement(&This->refs);
423 if (!refs) HeapFree(GetProcessHeap(), 0, This);
424 return refs;
427 static inline HRESULT get_target_from_wrapper( IDropTarget *wrapper, IDropTarget **target )
429 DropTargetWrapper* This = impl_from_IDropTarget( wrapper );
430 *target = GetPropW( This->hwnd, prop_oledroptarget );
431 if (!*target) return DRAGDROP_E_NOTREGISTERED;
432 IDropTarget_AddRef( *target );
433 return S_OK;
436 static HRESULT WINAPI DropTargetWrapper_DragEnter(IDropTarget* iface,
437 IDataObject* pDataObj,
438 DWORD grfKeyState,
439 POINTL pt,
440 DWORD* pdwEffect)
442 IDropTarget *target;
443 HRESULT r = get_target_from_wrapper( iface, &target );
445 if (SUCCEEDED( r ))
447 r = IDropTarget_DragEnter( target, pDataObj, grfKeyState, pt, pdwEffect );
448 IDropTarget_Release( target );
450 return r;
453 static HRESULT WINAPI DropTargetWrapper_DragOver(IDropTarget* iface,
454 DWORD grfKeyState,
455 POINTL pt,
456 DWORD* pdwEffect)
458 IDropTarget *target;
459 HRESULT r = get_target_from_wrapper( iface, &target );
461 if (SUCCEEDED( r ))
463 r = IDropTarget_DragOver( target, grfKeyState, pt, pdwEffect );
464 IDropTarget_Release( target );
466 return r;
469 static HRESULT WINAPI DropTargetWrapper_DragLeave(IDropTarget* iface)
471 IDropTarget *target;
472 HRESULT r = get_target_from_wrapper( iface, &target );
474 if (SUCCEEDED( r ))
476 r = IDropTarget_DragLeave( target );
477 IDropTarget_Release( target );
479 return r;
482 static HRESULT WINAPI DropTargetWrapper_Drop(IDropTarget* iface,
483 IDataObject* pDataObj,
484 DWORD grfKeyState,
485 POINTL pt,
486 DWORD* pdwEffect)
488 IDropTarget *target;
489 HRESULT r = get_target_from_wrapper( iface, &target );
491 if (SUCCEEDED( r ))
493 r = IDropTarget_Drop( target, pDataObj, grfKeyState, pt, pdwEffect );
494 IDropTarget_Release( target );
496 return r;
499 static const IDropTargetVtbl DropTargetWrapperVTbl =
501 DropTargetWrapper_QueryInterface,
502 DropTargetWrapper_AddRef,
503 DropTargetWrapper_Release,
504 DropTargetWrapper_DragEnter,
505 DropTargetWrapper_DragOver,
506 DropTargetWrapper_DragLeave,
507 DropTargetWrapper_Drop
510 static IDropTarget* WrapDropTarget( HWND hwnd )
512 DropTargetWrapper* This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
514 if (This)
516 This->IDropTarget_iface.lpVtbl = &DropTargetWrapperVTbl;
517 This->hwnd = hwnd;
518 This->refs = 1;
520 return &This->IDropTarget_iface;
523 /***********************************************************************
524 * get_droptarget_pointer
526 * Retrieves the marshalled IDropTarget from the window.
528 static IDropTarget* get_droptarget_pointer(HWND hwnd)
530 IDropTarget *droptarget = NULL;
531 HANDLE map;
532 IStream *stream;
534 map = get_droptarget_local_handle(hwnd);
535 if(!map) return NULL;
537 if(SUCCEEDED(create_stream_from_map(map, &stream)))
539 CoUnmarshalInterface(stream, &IID_IDropTarget, (void**)&droptarget);
540 IStream_Release(stream);
542 CloseHandle(map);
543 return droptarget;
546 /***********************************************************************
547 * RegisterDragDrop (OLE32.@)
549 HRESULT WINAPI RegisterDragDrop(HWND hwnd, LPDROPTARGET pDropTarget)
551 DWORD pid = 0;
552 HRESULT hr;
553 IStream *stream;
554 HANDLE map;
555 IDropTarget *wrapper;
557 TRACE("(%p,%p)\n", hwnd, pDropTarget);
559 if (!COM_CurrentApt())
561 ERR("COM not initialized\n");
562 return E_OUTOFMEMORY;
565 if (!pDropTarget)
566 return E_INVALIDARG;
568 if (!IsWindow(hwnd))
570 ERR("invalid hwnd %p\n", hwnd);
571 return DRAGDROP_E_INVALIDHWND;
574 /* block register for other processes windows */
575 GetWindowThreadProcessId(hwnd, &pid);
576 if (pid != GetCurrentProcessId())
578 FIXME("register for another process windows is disabled\n");
579 return DRAGDROP_E_INVALIDHWND;
582 /* check if the window is already registered */
583 if (is_droptarget(hwnd))
584 return DRAGDROP_E_ALREADYREGISTERED;
587 * Marshal the drop target pointer into a shared memory map and
588 * store the map's handle in a Wine specific window prop. We also
589 * store the drop target pointer itself in the
590 * "OleDropTargetInterface" prop for compatibility with Windows.
593 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
594 if(FAILED(hr)) return hr;
596 /* IDropTarget::QueryInterface() shouldn't be called, some (broken) apps depend on this. */
597 wrapper = WrapDropTarget( hwnd );
598 if(!wrapper)
600 IStream_Release(stream);
601 return E_OUTOFMEMORY;
603 hr = CoMarshalInterface(stream, &IID_IDropTarget, (IUnknown*)wrapper, MSHCTX_LOCAL, NULL, MSHLFLAGS_TABLESTRONG);
604 IDropTarget_Release(wrapper);
606 if(SUCCEEDED(hr))
608 hr = create_map_from_stream(stream, &map);
609 if(SUCCEEDED(hr))
611 IDropTarget_AddRef(pDropTarget);
612 SetPropW(hwnd, prop_oledroptarget, pDropTarget);
613 SetPropW(hwnd, prop_marshalleddroptarget, map);
615 else
617 LARGE_INTEGER zero;
618 zero.QuadPart = 0;
619 IStream_Seek(stream, zero, STREAM_SEEK_SET, NULL);
620 CoReleaseMarshalData(stream);
623 IStream_Release(stream);
625 return hr;
628 /***********************************************************************
629 * RevokeDragDrop (OLE32.@)
631 HRESULT WINAPI RevokeDragDrop(HWND hwnd)
633 HANDLE map;
634 IStream *stream;
635 IDropTarget *drop_target;
636 HRESULT hr;
638 TRACE("(%p)\n", hwnd);
640 if (!IsWindow(hwnd))
642 ERR("invalid hwnd %p\n", hwnd);
643 return DRAGDROP_E_INVALIDHWND;
646 /* no registration data */
647 if (!(map = get_droptarget_handle(hwnd)))
648 return DRAGDROP_E_NOTREGISTERED;
650 drop_target = GetPropW(hwnd, prop_oledroptarget);
651 if(drop_target) IDropTarget_Release(drop_target);
653 RemovePropW(hwnd, prop_oledroptarget);
654 RemovePropW(hwnd, prop_marshalleddroptarget);
656 hr = create_stream_from_map(map, &stream);
657 if(SUCCEEDED(hr))
659 CoReleaseMarshalData(stream);
660 IStream_Release(stream);
662 CloseHandle(map);
664 return hr;
667 /***********************************************************************
668 * OleRegGetUserType (OLE32.@)
670 HRESULT WINAPI OleRegGetUserType(REFCLSID clsid, DWORD form, LPOLESTR *usertype)
672 static const WCHAR auxusertypeW[] = {'A','u','x','U','s','e','r','T','y','p','e','\\','%','d',0};
673 DWORD valuetype, valuelen;
674 WCHAR auxkeynameW[16];
675 HKEY usertypekey;
676 HRESULT hres;
677 LONG ret;
679 TRACE("(%s, %u, %p)\n", debugstr_guid(clsid), form, usertype);
681 if (!usertype)
682 return E_INVALIDARG;
684 *usertype = NULL;
686 /* Return immediately if it's not registered. */
687 hres = COM_OpenKeyForCLSID(clsid, NULL, KEY_READ, &usertypekey);
688 if (FAILED(hres))
689 return hres;
691 valuelen = 0;
693 /* Try additional types if requested. If they don't exist fall back to USERCLASSTYPE_FULL. */
694 if (form != USERCLASSTYPE_FULL)
696 HKEY auxkey;
698 swprintf(auxkeynameW, ARRAY_SIZE(auxkeynameW), auxusertypeW, form);
699 if (COM_OpenKeyForCLSID(clsid, auxkeynameW, KEY_READ, &auxkey) == S_OK)
701 if (!RegQueryValueExW(auxkey, emptyW, NULL, &valuetype, NULL, &valuelen) && valuelen)
703 RegCloseKey(usertypekey);
704 usertypekey = auxkey;
706 else
707 RegCloseKey(auxkey);
711 valuelen = 0;
712 if (RegQueryValueExW(usertypekey, emptyW, NULL, &valuetype, NULL, &valuelen))
714 RegCloseKey(usertypekey);
715 return REGDB_E_READREGDB;
718 *usertype = CoTaskMemAlloc(valuelen);
719 if (!*usertype)
721 RegCloseKey(usertypekey);
722 return E_OUTOFMEMORY;
725 ret = RegQueryValueExW(usertypekey,
726 emptyW,
727 NULL,
728 &valuetype,
729 (LPBYTE)*usertype,
730 &valuelen);
731 RegCloseKey(usertypekey);
732 if (ret != ERROR_SUCCESS)
734 CoTaskMemFree(*usertype);
735 *usertype = NULL;
736 return REGDB_E_READREGDB;
739 return S_OK;
742 /***********************************************************************
743 * DoDragDrop [OLE32.@]
745 HRESULT WINAPI DoDragDrop (
746 IDataObject *pDataObject, /* [in] ptr to the data obj */
747 IDropSource* pDropSource, /* [in] ptr to the source obj */
748 DWORD dwOKEffect, /* [in] effects allowed by the source */
749 DWORD *pdwEffect) /* [out] ptr to effects of the source */
751 static const WCHAR trackerW[] = {'T','r','a','c','k','e','r','W','i','n','d','o','w',0};
752 TrackerWindowInfo trackerInfo;
753 HWND hwndTrackWindow;
754 MSG msg;
756 TRACE("(%p, %p, %08x, %p)\n", pDataObject, pDropSource, dwOKEffect, pdwEffect);
758 if (!pDataObject || !pDropSource || !pdwEffect)
759 return E_INVALIDARG;
762 * Setup the drag n drop tracking window.
765 trackerInfo.dataObject = pDataObject;
766 trackerInfo.dropSource = pDropSource;
767 trackerInfo.dwOKEffect = dwOKEffect;
768 trackerInfo.pdwEffect = pdwEffect;
769 trackerInfo.trackingDone = FALSE;
770 trackerInfo.inTrackCall = FALSE;
771 trackerInfo.escPressed = FALSE;
772 trackerInfo.curTargetHWND = 0;
773 trackerInfo.curDragTarget = 0;
775 hwndTrackWindow = CreateWindowW(OLEDD_DRAGTRACKERCLASS, trackerW,
776 WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT,
777 CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, 0,
778 &trackerInfo);
780 if (hwndTrackWindow)
783 * Capture the mouse input
785 SetCapture(hwndTrackWindow);
787 msg.message = 0;
790 * Pump messages. All mouse input should go to the capture window.
792 while (!trackerInfo.trackingDone && GetMessageW(&msg, 0, 0, 0) )
794 trackerInfo.curMousePos.x = msg.pt.x;
795 trackerInfo.curMousePos.y = msg.pt.y;
796 trackerInfo.dwKeyState = OLEDD_GetButtonState();
798 if ( (msg.message >= WM_KEYFIRST) &&
799 (msg.message <= WM_KEYLAST) )
802 * When keyboard messages are sent to windows on this thread, we
803 * want to ignore notify the drop source that the state changed.
804 * in the case of the Escape key, we also notify the drop source
805 * we give it a special meaning.
807 if ( (msg.message==WM_KEYDOWN) &&
808 (msg.wParam==VK_ESCAPE) )
810 trackerInfo.escPressed = TRUE;
814 * Notify the drop source.
816 OLEDD_TrackStateChange(&trackerInfo);
818 else
821 * Dispatch the messages only when it's not a keyboard message.
823 DispatchMessageW(&msg);
827 /* re-post the quit message to outer message loop */
828 if (msg.message == WM_QUIT)
829 PostQuitMessage(msg.wParam);
831 * Destroy the temporary window.
833 DestroyWindow(hwndTrackWindow);
835 return trackerInfo.returnValue;
838 return E_FAIL;
841 /***********************************************************************
842 * OleQueryLinkFromData [OLE32.@]
844 HRESULT WINAPI OleQueryLinkFromData(
845 IDataObject* pSrcDataObject)
847 FIXME("(%p),stub!\n", pSrcDataObject);
848 return S_FALSE;
851 /***********************************************************************
852 * OleRegGetMiscStatus [OLE32.@]
854 HRESULT WINAPI OleRegGetMiscStatus(
855 REFCLSID clsid,
856 DWORD dwAspect,
857 DWORD* pdwStatus)
859 static const WCHAR miscstatusW[] = {'M','i','s','c','S','t','a','t','u','s',0};
860 static const WCHAR dfmtW[] = {'%','d',0};
861 WCHAR keyName[16];
862 HKEY miscStatusKey;
863 HKEY aspectKey;
864 LONG result;
865 HRESULT hr;
867 TRACE("(%s, %d, %p)\n", debugstr_guid(clsid), dwAspect, pdwStatus);
869 if (!pdwStatus) return E_INVALIDARG;
871 *pdwStatus = 0;
873 if (actctx_get_miscstatus(clsid, dwAspect, pdwStatus)) return S_OK;
875 hr = COM_OpenKeyForCLSID(clsid, miscstatusW, KEY_READ, &miscStatusKey);
876 if (FAILED(hr))
877 /* missing key is not a failure */
878 return hr == REGDB_E_KEYMISSING ? S_OK : hr;
880 OLEUTL_ReadRegistryDWORDValue(miscStatusKey, pdwStatus);
883 * Open the key specific to the requested aspect.
885 swprintf(keyName, ARRAY_SIZE(keyName), dfmtW, dwAspect);
887 result = open_classes_key(miscStatusKey, keyName, KEY_READ, &aspectKey);
888 if (result == ERROR_SUCCESS)
890 OLEUTL_ReadRegistryDWORDValue(aspectKey, pdwStatus);
891 RegCloseKey(aspectKey);
894 RegCloseKey(miscStatusKey);
895 return S_OK;
898 static HRESULT EnumOLEVERB_Construct(HKEY hkeyVerb, ULONG index, IEnumOLEVERB **ppenum);
900 typedef struct
902 IEnumOLEVERB IEnumOLEVERB_iface;
903 LONG ref;
905 HKEY hkeyVerb;
906 ULONG index;
907 } EnumOLEVERB;
909 static inline EnumOLEVERB *impl_from_IEnumOLEVERB(IEnumOLEVERB *iface)
911 return CONTAINING_RECORD(iface, EnumOLEVERB, IEnumOLEVERB_iface);
914 static HRESULT WINAPI EnumOLEVERB_QueryInterface(
915 IEnumOLEVERB *iface, REFIID riid, void **ppv)
917 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
918 if (IsEqualIID(riid, &IID_IUnknown) ||
919 IsEqualIID(riid, &IID_IEnumOLEVERB))
921 IEnumOLEVERB_AddRef(iface);
922 *ppv = iface;
923 return S_OK;
925 return E_NOINTERFACE;
928 static ULONG WINAPI EnumOLEVERB_AddRef(
929 IEnumOLEVERB *iface)
931 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
932 TRACE("()\n");
933 return InterlockedIncrement(&This->ref);
936 static ULONG WINAPI EnumOLEVERB_Release(
937 IEnumOLEVERB *iface)
939 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
940 LONG refs = InterlockedDecrement(&This->ref);
941 TRACE("()\n");
942 if (!refs)
944 RegCloseKey(This->hkeyVerb);
945 HeapFree(GetProcessHeap(), 0, This);
947 return refs;
950 static HRESULT WINAPI EnumOLEVERB_Next(
951 IEnumOLEVERB *iface, ULONG celt, LPOLEVERB rgelt,
952 ULONG *pceltFetched)
954 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
955 HRESULT hr = S_OK;
957 TRACE("(%d, %p, %p)\n", celt, rgelt, pceltFetched);
959 if (pceltFetched)
960 *pceltFetched = 0;
962 for (; celt; celt--, rgelt++)
964 WCHAR wszSubKey[20];
965 LONG cbData;
966 LPWSTR pwszOLEVERB;
967 LPWSTR pwszMenuFlags;
968 LPWSTR pwszAttribs;
969 LONG res = RegEnumKeyW(This->hkeyVerb, This->index, wszSubKey, ARRAY_SIZE(wszSubKey));
970 if (res == ERROR_NO_MORE_ITEMS)
972 hr = S_FALSE;
973 break;
975 else if (res != ERROR_SUCCESS)
977 ERR("RegEnumKeyW failed with error %d\n", res);
978 hr = REGDB_E_READREGDB;
979 break;
981 res = RegQueryValueW(This->hkeyVerb, wszSubKey, NULL, &cbData);
982 if (res != ERROR_SUCCESS)
984 ERR("RegQueryValueW failed with error %d\n", res);
985 hr = REGDB_E_READREGDB;
986 break;
988 pwszOLEVERB = CoTaskMemAlloc(cbData);
989 if (!pwszOLEVERB)
991 hr = E_OUTOFMEMORY;
992 break;
994 res = RegQueryValueW(This->hkeyVerb, wszSubKey, pwszOLEVERB, &cbData);
995 if (res != ERROR_SUCCESS)
997 ERR("RegQueryValueW failed with error %d\n", res);
998 hr = REGDB_E_READREGDB;
999 CoTaskMemFree(pwszOLEVERB);
1000 break;
1003 TRACE("verb string: %s\n", debugstr_w(pwszOLEVERB));
1004 pwszMenuFlags = wcschr(pwszOLEVERB, ',');
1005 if (!pwszMenuFlags)
1007 hr = OLEOBJ_E_INVALIDVERB;
1008 CoTaskMemFree(pwszOLEVERB);
1009 break;
1011 /* nul terminate the name string and advance to first character */
1012 *pwszMenuFlags = '\0';
1013 pwszMenuFlags++;
1014 pwszAttribs = wcschr(pwszMenuFlags, ',');
1015 if (!pwszAttribs)
1017 hr = OLEOBJ_E_INVALIDVERB;
1018 CoTaskMemFree(pwszOLEVERB);
1019 break;
1021 /* nul terminate the menu string and advance to first character */
1022 *pwszAttribs = '\0';
1023 pwszAttribs++;
1025 /* fill out structure for this verb */
1026 rgelt->lVerb = wcstol(wszSubKey, NULL, 10);
1027 rgelt->lpszVerbName = pwszOLEVERB; /* user should free */
1028 rgelt->fuFlags = wcstol(pwszMenuFlags, NULL, 10);
1029 rgelt->grfAttribs = wcstol(pwszAttribs, NULL, 10);
1031 if (pceltFetched)
1032 (*pceltFetched)++;
1033 This->index++;
1035 return hr;
1038 static HRESULT WINAPI EnumOLEVERB_Skip(
1039 IEnumOLEVERB *iface, ULONG celt)
1041 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
1043 TRACE("(%d)\n", celt);
1045 This->index += celt;
1046 return S_OK;
1049 static HRESULT WINAPI EnumOLEVERB_Reset(
1050 IEnumOLEVERB *iface)
1052 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
1054 TRACE("()\n");
1056 This->index = 0;
1057 return S_OK;
1060 static HRESULT WINAPI EnumOLEVERB_Clone(
1061 IEnumOLEVERB *iface,
1062 IEnumOLEVERB **ppenum)
1064 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
1065 HKEY hkeyVerb;
1066 TRACE("(%p)\n", ppenum);
1067 if (!DuplicateHandle(GetCurrentProcess(), This->hkeyVerb, GetCurrentProcess(), (HANDLE *)&hkeyVerb, 0, FALSE, DUPLICATE_SAME_ACCESS))
1068 return HRESULT_FROM_WIN32(GetLastError());
1069 return EnumOLEVERB_Construct(hkeyVerb, This->index, ppenum);
1072 static const IEnumOLEVERBVtbl EnumOLEVERB_VTable =
1074 EnumOLEVERB_QueryInterface,
1075 EnumOLEVERB_AddRef,
1076 EnumOLEVERB_Release,
1077 EnumOLEVERB_Next,
1078 EnumOLEVERB_Skip,
1079 EnumOLEVERB_Reset,
1080 EnumOLEVERB_Clone
1083 static HRESULT EnumOLEVERB_Construct(HKEY hkeyVerb, ULONG index, IEnumOLEVERB **ppenum)
1085 EnumOLEVERB *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1086 if (!This)
1088 RegCloseKey(hkeyVerb);
1089 return E_OUTOFMEMORY;
1091 This->IEnumOLEVERB_iface.lpVtbl = &EnumOLEVERB_VTable;
1092 This->ref = 1;
1093 This->index = index;
1094 This->hkeyVerb = hkeyVerb;
1095 *ppenum = &This->IEnumOLEVERB_iface;
1096 return S_OK;
1099 /***********************************************************************
1100 * OleRegEnumVerbs [OLE32.@]
1102 * Enumerates verbs associated with a class stored in the registry.
1104 * PARAMS
1105 * clsid [I] Class ID to enumerate the verbs for.
1106 * ppenum [O] Enumerator.
1108 * RETURNS
1109 * S_OK: Success.
1110 * REGDB_E_CLASSNOTREG: The specified class does not have a key in the registry.
1111 * REGDB_E_READREGDB: The class key could not be opened for some other reason.
1112 * OLE_E_REGDB_KEY: The Verb subkey for the class is not present.
1113 * OLEOBJ_E_NOVERBS: The Verb subkey for the class is empty.
1115 HRESULT WINAPI OleRegEnumVerbs (REFCLSID clsid, LPENUMOLEVERB* ppenum)
1117 LONG res;
1118 HKEY hkeyVerb;
1119 DWORD dwSubKeys;
1120 static const WCHAR wszVerb[] = {'V','e','r','b',0};
1122 TRACE("(%s, %p)\n", debugstr_guid(clsid), ppenum);
1124 res = COM_OpenKeyForCLSID(clsid, wszVerb, KEY_READ, &hkeyVerb);
1125 if (FAILED(res))
1127 if (res == REGDB_E_CLASSNOTREG)
1128 ERR("CLSID %s not registered\n", debugstr_guid(clsid));
1129 else if (res == REGDB_E_KEYMISSING)
1130 ERR("no Verbs key for class %s\n", debugstr_guid(clsid));
1131 else
1132 ERR("failed to open Verbs key for CLSID %s with error %d\n",
1133 debugstr_guid(clsid), res);
1134 return res;
1137 res = RegQueryInfoKeyW(hkeyVerb, NULL, NULL, NULL, &dwSubKeys, NULL,
1138 NULL, NULL, NULL, NULL, NULL, NULL);
1139 if (res != ERROR_SUCCESS)
1141 ERR("failed to get subkey count with error %d\n", GetLastError());
1142 return REGDB_E_READREGDB;
1145 if (!dwSubKeys)
1147 WARN("class %s has no verbs\n", debugstr_guid(clsid));
1148 RegCloseKey(hkeyVerb);
1149 return OLEOBJ_E_NOVERBS;
1152 return EnumOLEVERB_Construct(hkeyVerb, 0, ppenum);
1155 /******************************************************************************
1156 * OleSetContainedObject [OLE32.@]
1158 HRESULT WINAPI OleSetContainedObject(
1159 LPUNKNOWN pUnknown,
1160 BOOL fContained)
1162 IRunnableObject* runnable = NULL;
1163 HRESULT hres;
1165 TRACE("(%p,%x)\n", pUnknown, fContained);
1167 hres = IUnknown_QueryInterface(pUnknown,
1168 &IID_IRunnableObject,
1169 (void**)&runnable);
1171 if (SUCCEEDED(hres))
1173 hres = IRunnableObject_SetContainedObject(runnable, fContained);
1175 IRunnableObject_Release(runnable);
1177 return hres;
1180 return S_OK;
1183 /******************************************************************************
1184 * OleRun [OLE32.@]
1186 * Set the OLE object to the running state.
1188 * PARAMS
1189 * pUnknown [I] OLE object to run.
1191 * RETURNS
1192 * Success: S_OK.
1193 * Failure: Any HRESULT code.
1195 HRESULT WINAPI DECLSPEC_HOTPATCH OleRun(LPUNKNOWN pUnknown)
1197 IRunnableObject *runable;
1198 HRESULT hres;
1200 TRACE("(%p)\n", pUnknown);
1202 hres = IUnknown_QueryInterface(pUnknown, &IID_IRunnableObject, (void**)&runable);
1203 if (FAILED(hres))
1204 return S_OK; /* Appears to return no error. */
1206 hres = IRunnableObject_Run(runable, NULL);
1207 IRunnableObject_Release(runable);
1208 return hres;
1211 /******************************************************************************
1212 * OleLoad [OLE32.@]
1214 HRESULT WINAPI OleLoad(
1215 LPSTORAGE pStg,
1216 REFIID riid,
1217 LPOLECLIENTSITE pClientSite,
1218 LPVOID* ppvObj)
1220 IPersistStorage* persistStorage = NULL;
1221 IUnknown* pUnk;
1222 IOleObject* pOleObject = NULL;
1223 STATSTG storageInfo;
1224 HRESULT hres;
1226 TRACE("(%p, %s, %p, %p)\n", pStg, debugstr_guid(riid), pClientSite, ppvObj);
1228 *ppvObj = NULL;
1231 * TODO, Conversion ... OleDoAutoConvert
1235 * Get the class ID for the object.
1237 hres = IStorage_Stat(pStg, &storageInfo, STATFLAG_NONAME);
1238 if (FAILED(hres))
1239 return hres;
1242 * Now, try and create the handler for the object
1244 hres = CoCreateInstance(&storageInfo.clsid,
1245 NULL,
1246 CLSCTX_INPROC_HANDLER|CLSCTX_INPROC_SERVER,
1247 riid,
1248 (void**)&pUnk);
1251 * If that fails, as it will most times, load the default
1252 * OLE handler.
1254 if (FAILED(hres))
1256 hres = OleCreateDefaultHandler(&storageInfo.clsid,
1257 NULL,
1258 riid,
1259 (void**)&pUnk);
1263 * If we couldn't find a handler... this is bad. Abort the whole thing.
1265 if (FAILED(hres))
1266 return hres;
1268 if (pClientSite)
1270 hres = IUnknown_QueryInterface(pUnk, &IID_IOleObject, (void **)&pOleObject);
1271 if (SUCCEEDED(hres))
1273 DWORD dwStatus;
1274 hres = IOleObject_GetMiscStatus(pOleObject, DVASPECT_CONTENT, &dwStatus);
1279 * Initialize the object with its IPersistStorage interface.
1281 hres = IUnknown_QueryInterface(pUnk, &IID_IPersistStorage, (void**)&persistStorage);
1282 if (SUCCEEDED(hres))
1284 hres = IPersistStorage_Load(persistStorage, pStg);
1286 IPersistStorage_Release(persistStorage);
1287 persistStorage = NULL;
1290 if (SUCCEEDED(hres) && pClientSite)
1292 * Inform the new object of its client site.
1294 hres = IOleObject_SetClientSite(pOleObject, pClientSite);
1297 * Cleanup interfaces used internally
1299 if (pOleObject)
1300 IOleObject_Release(pOleObject);
1302 if (SUCCEEDED(hres))
1304 IOleLink *pOleLink;
1305 HRESULT hres1;
1306 hres1 = IUnknown_QueryInterface(pUnk, &IID_IOleLink, (void **)&pOleLink);
1307 if (SUCCEEDED(hres1))
1309 FIXME("handle OLE link\n");
1310 IOleLink_Release(pOleLink);
1314 if (FAILED(hres))
1316 IUnknown_Release(pUnk);
1317 pUnk = NULL;
1320 *ppvObj = pUnk;
1322 return hres;
1325 /***********************************************************************
1326 * OleSave [OLE32.@]
1328 HRESULT WINAPI OleSave(
1329 LPPERSISTSTORAGE pPS,
1330 LPSTORAGE pStg,
1331 BOOL fSameAsLoad)
1333 HRESULT hres;
1334 CLSID objectClass;
1336 TRACE("(%p,%p,%x)\n", pPS, pStg, fSameAsLoad);
1339 * First, we transfer the class ID (if available)
1341 hres = IPersistStorage_GetClassID(pPS, &objectClass);
1343 if (SUCCEEDED(hres))
1345 WriteClassStg(pStg, &objectClass);
1349 * Then, we ask the object to save itself to the
1350 * storage. If it is successful, we commit the storage.
1352 hres = IPersistStorage_Save(pPS, pStg, fSameAsLoad);
1354 if (SUCCEEDED(hres))
1356 IStorage_Commit(pStg,
1357 STGC_DEFAULT);
1360 return hres;
1364 /******************************************************************************
1365 * OleLockRunning [OLE32.@]
1367 HRESULT WINAPI OleLockRunning(LPUNKNOWN pUnknown, BOOL fLock, BOOL fLastUnlockCloses)
1369 IRunnableObject* runnable = NULL;
1370 HRESULT hres;
1372 TRACE("(%p,%x,%x)\n", pUnknown, fLock, fLastUnlockCloses);
1374 hres = IUnknown_QueryInterface(pUnknown,
1375 &IID_IRunnableObject,
1376 (void**)&runnable);
1378 if (SUCCEEDED(hres))
1380 hres = IRunnableObject_LockRunning(runnable, fLock, fLastUnlockCloses);
1382 IRunnableObject_Release(runnable);
1384 return hres;
1387 return S_OK;
1391 /**************************************************************************
1392 * Internal methods to manage the shared OLE menu in response to the
1393 * OLE***MenuDescriptor API
1396 /***
1397 * OLEMenu_Initialize()
1399 * Initializes the OLEMENU data structures.
1401 static void OLEMenu_Initialize(void)
1405 /***
1406 * OLEMenu_UnInitialize()
1408 * Releases the OLEMENU data structures.
1410 static void OLEMenu_UnInitialize(void)
1414 /*************************************************************************
1415 * OLEMenu_InstallHooks
1416 * Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
1418 * RETURNS: TRUE if message hooks were successfully installed
1419 * FALSE on failure
1421 static BOOL OLEMenu_InstallHooks( DWORD tid )
1423 OleMenuHookItem *pHookItem;
1425 /* Create an entry for the hook table */
1426 if ( !(pHookItem = HeapAlloc(GetProcessHeap(), 0,
1427 sizeof(OleMenuHookItem)) ) )
1428 return FALSE;
1430 pHookItem->tid = tid;
1431 pHookItem->hHeap = GetProcessHeap();
1432 pHookItem->CallWndProc_hHook = NULL;
1434 /* Install a thread scope message hook for WH_GETMESSAGE */
1435 pHookItem->GetMsg_hHook = SetWindowsHookExW( WH_GETMESSAGE, OLEMenu_GetMsgProc,
1436 0, GetCurrentThreadId() );
1437 if ( !pHookItem->GetMsg_hHook )
1438 goto CLEANUP;
1440 /* Install a thread scope message hook for WH_CALLWNDPROC */
1441 pHookItem->CallWndProc_hHook = SetWindowsHookExW( WH_CALLWNDPROC, OLEMenu_CallWndProc,
1442 0, GetCurrentThreadId() );
1443 if ( !pHookItem->CallWndProc_hHook )
1444 goto CLEANUP;
1446 /* Insert the hook table entry */
1447 pHookItem->next = hook_list;
1448 hook_list = pHookItem;
1450 return TRUE;
1452 CLEANUP:
1453 /* Unhook any hooks */
1454 if ( pHookItem->GetMsg_hHook )
1455 UnhookWindowsHookEx( pHookItem->GetMsg_hHook );
1456 if ( pHookItem->CallWndProc_hHook )
1457 UnhookWindowsHookEx( pHookItem->CallWndProc_hHook );
1458 /* Release the hook table entry */
1459 HeapFree(pHookItem->hHeap, 0, pHookItem );
1461 return FALSE;
1464 /*************************************************************************
1465 * OLEMenu_UnInstallHooks
1466 * UnInstall thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
1468 * RETURNS: TRUE if message hooks were successfully installed
1469 * FALSE on failure
1471 static BOOL OLEMenu_UnInstallHooks( DWORD tid )
1473 OleMenuHookItem *pHookItem = NULL;
1474 OleMenuHookItem **ppHook = &hook_list;
1476 while (*ppHook)
1478 if ((*ppHook)->tid == tid)
1480 pHookItem = *ppHook;
1481 *ppHook = pHookItem->next;
1482 break;
1484 ppHook = &(*ppHook)->next;
1486 if (!pHookItem) return FALSE;
1488 /* Uninstall the hooks installed for this thread */
1489 if ( !UnhookWindowsHookEx( pHookItem->GetMsg_hHook ) )
1490 goto CLEANUP;
1491 if ( !UnhookWindowsHookEx( pHookItem->CallWndProc_hHook ) )
1492 goto CLEANUP;
1494 /* Release the hook table entry */
1495 HeapFree(pHookItem->hHeap, 0, pHookItem );
1497 return TRUE;
1499 CLEANUP:
1500 /* Release the hook table entry */
1501 HeapFree(pHookItem->hHeap, 0, pHookItem );
1503 return FALSE;
1506 /*************************************************************************
1507 * OLEMenu_IsHookInstalled
1508 * Tests if OLEMenu hooks have been installed for a thread
1510 * RETURNS: The pointer and index of the hook table entry for the tid
1511 * NULL and -1 for the index if no hooks were installed for this thread
1513 static OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid )
1515 OleMenuHookItem *pHookItem;
1517 /* Do a simple linear search for an entry whose tid matches ours.
1518 * We really need a map but efficiency is not a concern here. */
1519 for (pHookItem = hook_list; pHookItem; pHookItem = pHookItem->next)
1521 if ( tid == pHookItem->tid )
1522 return pHookItem;
1525 return NULL;
1528 /***********************************************************************
1529 * OLEMenu_FindMainMenuIndex
1531 * Used by OLEMenu API to find the top level group a menu item belongs to.
1532 * On success pnPos contains the index of the item in the top level menu group
1534 * RETURNS: TRUE if the ID was found, FALSE on failure
1536 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos )
1538 INT i, nItems;
1540 nItems = GetMenuItemCount( hMainMenu );
1542 for (i = 0; i < nItems; i++)
1544 HMENU hsubmenu;
1546 /* Is the current item a submenu? */
1547 if ( (hsubmenu = GetSubMenu(hMainMenu, i)) )
1549 /* If the handle is the same we're done */
1550 if ( hsubmenu == hPopupMenu )
1552 if (pnPos)
1553 *pnPos = i;
1554 return TRUE;
1556 /* Recursively search without updating pnPos */
1557 else if ( OLEMenu_FindMainMenuIndex( hsubmenu, hPopupMenu, NULL ) )
1559 if (pnPos)
1560 *pnPos = i;
1561 return TRUE;
1566 return FALSE;
1569 /***********************************************************************
1570 * OLEMenu_SetIsServerMenu
1572 * Checks whether a popup menu belongs to a shared menu group which is
1573 * owned by the server, and sets the menu descriptor state accordingly.
1574 * All menu messages from these groups should be routed to the server.
1576 * RETURNS: TRUE if the popup menu is part of a server owned group
1577 * FALSE if the popup menu is part of a container owned group
1579 static BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor )
1581 UINT nPos = 0, nWidth, i;
1583 pOleMenuDescriptor->bIsServerItem = FALSE;
1585 /* Don't bother searching if the popup is the combined menu itself */
1586 if ( hmenu == pOleMenuDescriptor->hmenuCombined )
1587 return FALSE;
1589 /* Find the menu item index in the shared OLE menu that this item belongs to */
1590 if ( !OLEMenu_FindMainMenuIndex( pOleMenuDescriptor->hmenuCombined, hmenu, &nPos ) )
1591 return FALSE;
1593 /* The group widths array has counts for the number of elements
1594 * in the groups File, Edit, Container, Object, Window, Help.
1595 * The Edit, Object & Help groups belong to the server object
1596 * and the other three belong to the container.
1597 * Loop through the group widths and locate the group we are a member of.
1599 for ( i = 0, nWidth = 0; i < 6; i++ )
1601 nWidth += pOleMenuDescriptor->mgw.width[i];
1602 if ( nPos < nWidth )
1604 /* Odd elements are server menu widths */
1605 pOleMenuDescriptor->bIsServerItem = i%2;
1606 break;
1610 return pOleMenuDescriptor->bIsServerItem;
1613 /*************************************************************************
1614 * OLEMenu_CallWndProc
1615 * Thread scope WH_CALLWNDPROC hook proc filter function (callback)
1616 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1618 static LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam)
1620 LPCWPSTRUCT pMsg;
1621 HOLEMENU hOleMenu = 0;
1622 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1623 OleMenuHookItem *pHookItem = NULL;
1624 WORD fuFlags;
1626 TRACE("%i, %04lx, %08lx\n", code, wParam, lParam );
1628 /* Check if we're being asked to process the message */
1629 if ( HC_ACTION != code )
1630 goto NEXTHOOK;
1632 /* Retrieve the current message being dispatched from lParam */
1633 pMsg = (LPCWPSTRUCT)lParam;
1635 /* Check if the message is destined for a window we are interested in:
1636 * If the window has an OLEMenu property we may need to dispatch
1637 * the menu message to its active objects window instead. */
1639 hOleMenu = GetPropW( pMsg->hwnd, prop_olemenuW );
1640 if ( !hOleMenu )
1641 goto NEXTHOOK;
1643 /* Get the menu descriptor */
1644 pOleMenuDescriptor = GlobalLock( hOleMenu );
1645 if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1646 goto NEXTHOOK;
1648 /* Process menu messages */
1649 switch( pMsg->message )
1651 case WM_INITMENU:
1653 /* Reset the menu descriptor state */
1654 pOleMenuDescriptor->bIsServerItem = FALSE;
1656 /* Send this message to the server as well */
1657 SendMessageW( pOleMenuDescriptor->hwndActiveObject,
1658 pMsg->message, pMsg->wParam, pMsg->lParam );
1659 goto NEXTHOOK;
1662 case WM_INITMENUPOPUP:
1664 /* Save the state for whether this is a server owned menu */
1665 OLEMenu_SetIsServerMenu( (HMENU)pMsg->wParam, pOleMenuDescriptor );
1666 break;
1669 case WM_MENUSELECT:
1671 fuFlags = HIWORD(pMsg->wParam); /* Get flags */
1672 if ( fuFlags & MF_SYSMENU )
1673 goto NEXTHOOK;
1675 /* Save the state for whether this is a server owned popup menu */
1676 else if ( fuFlags & MF_POPUP )
1677 OLEMenu_SetIsServerMenu( (HMENU)pMsg->lParam, pOleMenuDescriptor );
1679 break;
1682 case WM_DRAWITEM:
1684 LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT) pMsg->lParam;
1685 if ( pMsg->wParam != 0 || lpdis->CtlType != ODT_MENU )
1686 goto NEXTHOOK; /* Not a menu message */
1688 break;
1691 default:
1692 goto NEXTHOOK;
1695 /* If the message was for the server dispatch it accordingly */
1696 if ( pOleMenuDescriptor->bIsServerItem )
1698 SendMessageW( pOleMenuDescriptor->hwndActiveObject,
1699 pMsg->message, pMsg->wParam, pMsg->lParam );
1702 NEXTHOOK:
1703 if ( pOleMenuDescriptor )
1704 GlobalUnlock( hOleMenu );
1706 /* Lookup the hook item for the current thread */
1707 if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1709 /* This should never fail!! */
1710 WARN("could not retrieve hHook for current thread!\n" );
1711 return 0;
1714 /* Pass on the message to the next hooker */
1715 return CallNextHookEx( pHookItem->CallWndProc_hHook, code, wParam, lParam );
1718 /*************************************************************************
1719 * OLEMenu_GetMsgProc
1720 * Thread scope WH_GETMESSAGE hook proc filter function (callback)
1721 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1723 static LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam)
1725 LPMSG pMsg;
1726 HOLEMENU hOleMenu = 0;
1727 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1728 OleMenuHookItem *pHookItem = NULL;
1729 WORD wCode;
1731 TRACE("%i, %04lx, %08lx\n", code, wParam, lParam );
1733 /* Check if we're being asked to process a messages */
1734 if ( HC_ACTION != code )
1735 goto NEXTHOOK;
1737 /* Retrieve the current message being dispatched from lParam */
1738 pMsg = (LPMSG)lParam;
1740 /* Check if the message is destined for a window we are interested in:
1741 * If the window has an OLEMenu property we may need to dispatch
1742 * the menu message to its active objects window instead. */
1744 hOleMenu = GetPropW( pMsg->hwnd, prop_olemenuW );
1745 if ( !hOleMenu )
1746 goto NEXTHOOK;
1748 /* Process menu messages */
1749 switch( pMsg->message )
1751 case WM_COMMAND:
1753 wCode = HIWORD(pMsg->wParam); /* Get notification code */
1754 if ( wCode )
1755 goto NEXTHOOK; /* Not a menu message */
1756 break;
1758 default:
1759 goto NEXTHOOK;
1762 /* Get the menu descriptor */
1763 pOleMenuDescriptor = GlobalLock( hOleMenu );
1764 if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1765 goto NEXTHOOK;
1767 /* If the message was for the server dispatch it accordingly */
1768 if ( pOleMenuDescriptor->bIsServerItem )
1770 /* Change the hWnd in the message to the active objects hWnd.
1771 * The message loop which reads this message will automatically
1772 * dispatch it to the embedded objects window. */
1773 pMsg->hwnd = pOleMenuDescriptor->hwndActiveObject;
1776 NEXTHOOK:
1777 if ( pOleMenuDescriptor )
1778 GlobalUnlock( hOleMenu );
1780 /* Lookup the hook item for the current thread */
1781 if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1783 /* This should never fail!! */
1784 WARN("could not retrieve hHook for current thread!\n" );
1785 return FALSE;
1788 /* Pass on the message to the next hooker */
1789 return CallNextHookEx( pHookItem->GetMsg_hHook, code, wParam, lParam );
1792 /***********************************************************************
1793 * OleCreateMenuDescriptor [OLE32.@]
1794 * Creates an OLE menu descriptor for OLE to use when dispatching
1795 * menu messages and commands.
1797 * PARAMS:
1798 * hmenuCombined - Handle to the objects combined menu
1799 * lpMenuWidths - Pointer to array of 6 LONG's indicating menus per group
1802 HOLEMENU WINAPI OleCreateMenuDescriptor(
1803 HMENU hmenuCombined,
1804 LPOLEMENUGROUPWIDTHS lpMenuWidths)
1806 HOLEMENU hOleMenu;
1807 OleMenuDescriptor *pOleMenuDescriptor;
1808 int i;
1810 if ( !hmenuCombined || !lpMenuWidths )
1811 return 0;
1813 /* Create an OLE menu descriptor */
1814 if ( !(hOleMenu = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT,
1815 sizeof(OleMenuDescriptor) ) ) )
1816 return 0;
1818 pOleMenuDescriptor = GlobalLock( hOleMenu );
1819 if ( !pOleMenuDescriptor )
1820 return 0;
1822 /* Initialize menu group widths and hmenu */
1823 for ( i = 0; i < 6; i++ )
1824 pOleMenuDescriptor->mgw.width[i] = lpMenuWidths->width[i];
1826 pOleMenuDescriptor->hmenuCombined = hmenuCombined;
1827 pOleMenuDescriptor->bIsServerItem = FALSE;
1828 GlobalUnlock( hOleMenu );
1830 return hOleMenu;
1833 /***********************************************************************
1834 * OleDestroyMenuDescriptor [OLE32.@]
1835 * Destroy the shared menu descriptor
1837 HRESULT WINAPI OleDestroyMenuDescriptor(
1838 HOLEMENU hmenuDescriptor)
1840 if ( hmenuDescriptor )
1841 GlobalFree( hmenuDescriptor );
1842 return S_OK;
1845 /***********************************************************************
1846 * OleSetMenuDescriptor [OLE32.@]
1847 * Installs or removes OLE dispatching code for the containers frame window.
1849 * PARAMS
1850 * hOleMenu Handle to composite menu descriptor
1851 * hwndFrame Handle to containers frame window
1852 * hwndActiveObject Handle to objects in-place activation window
1853 * lpFrame Pointer to IOleInPlaceFrame on containers window
1854 * lpActiveObject Pointer to IOleInPlaceActiveObject on active in-place object
1856 * RETURNS
1857 * S_OK - menu installed correctly
1858 * E_FAIL, E_INVALIDARG, E_UNEXPECTED - failure
1860 * FIXME
1861 * The lpFrame and lpActiveObject parameters are currently ignored
1862 * OLE should install context sensitive help F1 filtering for the app when
1863 * these are non null.
1865 HRESULT WINAPI OleSetMenuDescriptor(
1866 HOLEMENU hOleMenu,
1867 HWND hwndFrame,
1868 HWND hwndActiveObject,
1869 LPOLEINPLACEFRAME lpFrame,
1870 LPOLEINPLACEACTIVEOBJECT lpActiveObject)
1872 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1874 /* Check args */
1875 if ( !hwndFrame || (hOleMenu && !hwndActiveObject) )
1876 return E_INVALIDARG;
1878 if ( lpFrame || lpActiveObject )
1880 FIXME("(%p, %p, %p, %p, %p), Context sensitive help filtering not implemented!\n",
1881 hOleMenu,
1882 hwndFrame,
1883 hwndActiveObject,
1884 lpFrame,
1885 lpActiveObject);
1888 /* Set up a message hook to intercept the containers frame window messages.
1889 * The message filter is responsible for dispatching menu messages from the
1890 * shared menu which are intended for the object.
1893 if ( hOleMenu ) /* Want to install dispatching code */
1895 /* If OLEMenu hooks are already installed for this thread, fail
1896 * Note: This effectively means that OleSetMenuDescriptor cannot
1897 * be called twice in succession on the same frame window
1898 * without first calling it with a null hOleMenu to uninstall
1900 if ( OLEMenu_IsHookInstalled( GetCurrentThreadId() ) )
1901 return E_FAIL;
1903 /* Get the menu descriptor */
1904 pOleMenuDescriptor = GlobalLock( hOleMenu );
1905 if ( !pOleMenuDescriptor )
1906 return E_UNEXPECTED;
1908 /* Update the menu descriptor */
1909 pOleMenuDescriptor->hwndFrame = hwndFrame;
1910 pOleMenuDescriptor->hwndActiveObject = hwndActiveObject;
1912 GlobalUnlock( hOleMenu );
1913 pOleMenuDescriptor = NULL;
1915 /* Add a menu descriptor windows property to the frame window */
1916 SetPropW( hwndFrame, prop_olemenuW, hOleMenu );
1918 /* Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC */
1919 if ( !OLEMenu_InstallHooks( GetCurrentThreadId() ) )
1920 return E_FAIL;
1922 else /* Want to uninstall dispatching code */
1924 /* Uninstall the hooks */
1925 if ( !OLEMenu_UnInstallHooks( GetCurrentThreadId() ) )
1926 return E_FAIL;
1928 /* Remove the menu descriptor property from the frame window */
1929 RemovePropW( hwndFrame, prop_olemenuW );
1932 return S_OK;
1935 /******************************************************************************
1936 * IsAccelerator [OLE32.@]
1937 * Mostly copied from controls/menu.c TranslateAccelerator implementation
1939 BOOL WINAPI IsAccelerator(HACCEL hAccel, int cAccelEntries, LPMSG lpMsg, WORD* lpwCmd)
1941 LPACCEL lpAccelTbl;
1942 int i;
1944 if(!lpMsg) return FALSE;
1945 if (!hAccel)
1947 WARN_(accel)("NULL accel handle\n");
1948 return FALSE;
1950 if((lpMsg->message != WM_KEYDOWN &&
1951 lpMsg->message != WM_SYSKEYDOWN &&
1952 lpMsg->message != WM_SYSCHAR &&
1953 lpMsg->message != WM_CHAR)) return FALSE;
1954 lpAccelTbl = HeapAlloc(GetProcessHeap(), 0, cAccelEntries * sizeof(ACCEL));
1955 if (NULL == lpAccelTbl)
1957 return FALSE;
1959 if (CopyAcceleratorTableW(hAccel, lpAccelTbl, cAccelEntries) != cAccelEntries)
1961 WARN_(accel)("CopyAcceleratorTableW failed\n");
1962 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
1963 return FALSE;
1966 TRACE_(accel)("hAccel=%p, cAccelEntries=%d,"
1967 "msg->hwnd=%p, msg->message=%04x, wParam=%08lx, lParam=%08lx\n",
1968 hAccel, cAccelEntries,
1969 lpMsg->hwnd, lpMsg->message, lpMsg->wParam, lpMsg->lParam);
1970 for(i = 0; i < cAccelEntries; i++)
1972 if(lpAccelTbl[i].key != lpMsg->wParam)
1973 continue;
1975 if(lpMsg->message == WM_CHAR)
1977 if(!(lpAccelTbl[i].fVirt & FALT) && !(lpAccelTbl[i].fVirt & FVIRTKEY))
1979 TRACE_(accel)("found accel for WM_CHAR: ('%c')\n", LOWORD(lpMsg->wParam) & 0xff);
1980 goto found;
1983 else
1985 if(lpAccelTbl[i].fVirt & FVIRTKEY)
1987 INT mask = 0;
1988 TRACE_(accel)("found accel for virt_key %04lx (scan %04x)\n",
1989 lpMsg->wParam, HIWORD(lpMsg->lParam) & 0xff);
1990 if(GetKeyState(VK_SHIFT) & 0x8000) mask |= FSHIFT;
1991 if(GetKeyState(VK_CONTROL) & 0x8000) mask |= FCONTROL;
1992 if(GetKeyState(VK_MENU) & 0x8000) mask |= FALT;
1993 if(mask == (lpAccelTbl[i].fVirt & (FSHIFT | FCONTROL | FALT))) goto found;
1994 TRACE_(accel)("incorrect SHIFT/CTRL/ALT-state\n");
1996 else
1998 if(!(lpMsg->lParam & 0x01000000)) /* no special_key */
2000 if((lpAccelTbl[i].fVirt & FALT) && (lpMsg->lParam & 0x20000000))
2001 { /* ^^ ALT pressed */
2002 TRACE_(accel)("found accel for Alt-%c\n", LOWORD(lpMsg->wParam) & 0xff);
2003 goto found;
2010 WARN_(accel)("couldn't translate accelerator key\n");
2011 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
2012 return FALSE;
2014 found:
2015 if(lpwCmd) *lpwCmd = lpAccelTbl[i].cmd;
2016 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
2017 return TRUE;
2020 /***********************************************************************
2021 * ReleaseStgMedium [OLE32.@]
2023 void WINAPI ReleaseStgMedium(
2024 STGMEDIUM* pmedium)
2026 switch (pmedium->tymed)
2028 case TYMED_HGLOBAL:
2030 if ( (pmedium->pUnkForRelease==0) &&
2031 (pmedium->u.hGlobal!=0) )
2032 GlobalFree(pmedium->u.hGlobal);
2033 break;
2035 case TYMED_FILE:
2037 if (pmedium->u.lpszFileName!=0)
2039 if (pmedium->pUnkForRelease==0)
2041 DeleteFileW(pmedium->u.lpszFileName);
2044 CoTaskMemFree(pmedium->u.lpszFileName);
2046 break;
2048 case TYMED_ISTREAM:
2050 if (pmedium->u.pstm!=0)
2052 IStream_Release(pmedium->u.pstm);
2054 break;
2056 case TYMED_ISTORAGE:
2058 if (pmedium->u.pstg!=0)
2060 IStorage_Release(pmedium->u.pstg);
2062 break;
2064 case TYMED_GDI:
2066 if ( (pmedium->pUnkForRelease==0) &&
2067 (pmedium->u.hBitmap!=0) )
2068 DeleteObject(pmedium->u.hBitmap);
2069 break;
2071 case TYMED_MFPICT:
2073 if ( (pmedium->pUnkForRelease==0) &&
2074 (pmedium->u.hMetaFilePict!=0) )
2076 LPMETAFILEPICT pMP = GlobalLock(pmedium->u.hMetaFilePict);
2077 DeleteMetaFile(pMP->hMF);
2078 GlobalUnlock(pmedium->u.hMetaFilePict);
2079 GlobalFree(pmedium->u.hMetaFilePict);
2081 break;
2083 case TYMED_ENHMF:
2085 if ( (pmedium->pUnkForRelease==0) &&
2086 (pmedium->u.hEnhMetaFile!=0) )
2088 DeleteEnhMetaFile(pmedium->u.hEnhMetaFile);
2090 break;
2092 case TYMED_NULL:
2093 default:
2094 break;
2096 pmedium->tymed=TYMED_NULL;
2099 * After cleaning up, the unknown is released
2101 if (pmedium->pUnkForRelease!=0)
2103 IUnknown_Release(pmedium->pUnkForRelease);
2104 pmedium->pUnkForRelease = 0;
2108 /***
2109 * OLEDD_Initialize()
2111 * Initializes the OLE drag and drop data structures.
2113 static void OLEDD_Initialize(void)
2115 WNDCLASSW wndClass;
2117 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
2118 wndClass.style = CS_GLOBALCLASS;
2119 wndClass.lpfnWndProc = OLEDD_DragTrackerWindowProc;
2120 wndClass.cbClsExtra = 0;
2121 wndClass.cbWndExtra = sizeof(TrackerWindowInfo*);
2122 wndClass.hCursor = 0;
2123 wndClass.hbrBackground = 0;
2124 wndClass.lpszClassName = OLEDD_DRAGTRACKERCLASS;
2126 RegisterClassW (&wndClass);
2129 /***
2130 * OLEDD_DragTrackerWindowProc()
2132 * This method is the WindowProcedure of the drag n drop tracking
2133 * window. During a drag n Drop operation, an invisible window is created
2134 * to receive the user input and act upon it. This procedure is in charge
2135 * of this behavior.
2138 #define DRAG_TIMER_ID 1
2140 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(
2141 HWND hwnd,
2142 UINT uMsg,
2143 WPARAM wParam,
2144 LPARAM lParam)
2146 switch (uMsg)
2148 case WM_CREATE:
2150 LPCREATESTRUCTA createStruct = (LPCREATESTRUCTA)lParam;
2152 SetWindowLongPtrW(hwnd, 0, (LONG_PTR)createStruct->lpCreateParams);
2153 SetTimer(hwnd, DRAG_TIMER_ID, 50, NULL);
2155 break;
2157 case WM_TIMER:
2158 case WM_MOUSEMOVE:
2159 case WM_LBUTTONUP:
2160 case WM_MBUTTONUP:
2161 case WM_RBUTTONUP:
2162 case WM_LBUTTONDOWN:
2163 case WM_MBUTTONDOWN:
2164 case WM_RBUTTONDOWN:
2166 TrackerWindowInfo *trackerInfo = (TrackerWindowInfo*)GetWindowLongPtrA(hwnd, 0);
2167 if (trackerInfo->trackingDone) break;
2168 OLEDD_TrackStateChange(trackerInfo);
2169 break;
2171 case WM_DESTROY:
2173 KillTimer(hwnd, DRAG_TIMER_ID);
2174 break;
2179 * This is a window proc after all. Let's call the default.
2181 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
2184 static void drag_enter( TrackerWindowInfo *info, HWND new_target )
2186 HRESULT hr;
2188 info->curTargetHWND = new_target;
2190 while (new_target && !is_droptarget( new_target ))
2191 new_target = GetParent( new_target );
2193 info->curDragTarget = get_droptarget_pointer( new_target );
2195 if (info->curDragTarget)
2197 *info->pdwEffect = info->dwOKEffect;
2198 hr = IDropTarget_DragEnter( info->curDragTarget, info->dataObject,
2199 info->dwKeyState, info->curMousePos,
2200 info->pdwEffect );
2201 *info->pdwEffect &= info->dwOKEffect;
2203 /* failed DragEnter() means invalid target */
2204 if (hr != S_OK)
2206 IDropTarget_Release( info->curDragTarget );
2207 info->curDragTarget = NULL;
2208 info->curTargetHWND = NULL;
2213 static void drag_end( TrackerWindowInfo *info )
2215 HRESULT hr;
2217 info->trackingDone = TRUE;
2218 ReleaseCapture();
2220 if (info->curDragTarget)
2222 if (info->returnValue == DRAGDROP_S_DROP &&
2223 *info->pdwEffect != DROPEFFECT_NONE)
2225 *info->pdwEffect = info->dwOKEffect;
2226 hr = IDropTarget_Drop( info->curDragTarget, info->dataObject, info->dwKeyState,
2227 info->curMousePos, info->pdwEffect );
2228 *info->pdwEffect &= info->dwOKEffect;
2230 if (FAILED( hr ))
2231 info->returnValue = hr;
2233 else
2235 IDropTarget_DragLeave( info->curDragTarget );
2236 *info->pdwEffect = DROPEFFECT_NONE;
2238 IDropTarget_Release( info->curDragTarget );
2239 info->curDragTarget = NULL;
2241 else
2242 *info->pdwEffect = DROPEFFECT_NONE;
2245 static HRESULT give_feedback( TrackerWindowInfo *info )
2247 HRESULT hr;
2248 int res;
2249 HCURSOR cur;
2251 if (info->curDragTarget == NULL)
2252 *info->pdwEffect = DROPEFFECT_NONE;
2254 hr = IDropSource_GiveFeedback( info->dropSource, *info->pdwEffect );
2256 if (hr == DRAGDROP_S_USEDEFAULTCURSORS)
2258 if (*info->pdwEffect & DROPEFFECT_MOVE)
2259 res = CURSOR_MOVE;
2260 else if (*info->pdwEffect & DROPEFFECT_COPY)
2261 res = CURSOR_COPY;
2262 else if (*info->pdwEffect & DROPEFFECT_LINK)
2263 res = CURSOR_LINK;
2264 else
2265 res = CURSOR_NODROP;
2267 cur = LoadCursorW( hProxyDll, MAKEINTRESOURCEW( res ) );
2268 SetCursor( cur );
2271 return hr;
2274 /***
2275 * OLEDD_TrackStateChange()
2277 * This method is invoked while a drag and drop operation is in effect.
2279 * params:
2280 * trackerInfo - Pointer to the structure identifying the
2281 * drag & drop operation that is currently
2282 * active.
2284 static void OLEDD_TrackStateChange(TrackerWindowInfo* trackerInfo)
2286 HWND hwndNewTarget = 0;
2287 POINT pt;
2290 * This method may be called from QueryContinueDrag again,
2291 * (i.e. by running message loop) so avoid recursive call chain.
2293 if (trackerInfo->inTrackCall) return;
2294 trackerInfo->inTrackCall = TRUE;
2297 * Get the handle of the window under the mouse
2299 pt.x = trackerInfo->curMousePos.x;
2300 pt.y = trackerInfo->curMousePos.y;
2301 hwndNewTarget = WindowFromPoint(pt);
2303 trackerInfo->returnValue = IDropSource_QueryContinueDrag(trackerInfo->dropSource,
2304 trackerInfo->escPressed,
2305 trackerInfo->dwKeyState);
2307 if (trackerInfo->curTargetHWND != hwndNewTarget &&
2308 (trackerInfo->returnValue == S_OK ||
2309 trackerInfo->returnValue == DRAGDROP_S_DROP))
2311 if (trackerInfo->curDragTarget)
2313 IDropTarget_DragLeave(trackerInfo->curDragTarget);
2314 IDropTarget_Release(trackerInfo->curDragTarget);
2315 trackerInfo->curDragTarget = NULL;
2316 trackerInfo->curTargetHWND = NULL;
2319 if (hwndNewTarget)
2320 drag_enter( trackerInfo, hwndNewTarget );
2322 give_feedback( trackerInfo );
2326 if (trackerInfo->returnValue == S_OK)
2328 if (trackerInfo->curDragTarget)
2330 *trackerInfo->pdwEffect = trackerInfo->dwOKEffect;
2331 IDropTarget_DragOver(trackerInfo->curDragTarget,
2332 trackerInfo->dwKeyState,
2333 trackerInfo->curMousePos,
2334 trackerInfo->pdwEffect);
2335 *trackerInfo->pdwEffect &= trackerInfo->dwOKEffect;
2337 give_feedback( trackerInfo );
2339 else
2340 drag_end( trackerInfo );
2342 trackerInfo->inTrackCall = FALSE;
2345 /***
2346 * OLEDD_GetButtonState()
2348 * This method will use the current state of the keyboard to build
2349 * a button state mask equivalent to the one passed in the
2350 * WM_MOUSEMOVE wParam.
2352 static DWORD OLEDD_GetButtonState(void)
2354 BYTE keyboardState[256];
2355 DWORD keyMask = 0;
2357 GetKeyboardState(keyboardState);
2359 if ( (keyboardState[VK_SHIFT] & 0x80) !=0)
2360 keyMask |= MK_SHIFT;
2362 if ( (keyboardState[VK_CONTROL] & 0x80) !=0)
2363 keyMask |= MK_CONTROL;
2365 if ( (keyboardState[VK_MENU] & 0x80) !=0)
2366 keyMask |= MK_ALT;
2368 if ( (keyboardState[VK_LBUTTON] & 0x80) !=0)
2369 keyMask |= MK_LBUTTON;
2371 if ( (keyboardState[VK_RBUTTON] & 0x80) !=0)
2372 keyMask |= MK_RBUTTON;
2374 if ( (keyboardState[VK_MBUTTON] & 0x80) !=0)
2375 keyMask |= MK_MBUTTON;
2377 return keyMask;
2380 /***
2381 * OLEDD_GetButtonState()
2383 * This method will read the default value of the registry key in
2384 * parameter and extract a DWORD value from it. The registry key value
2385 * can be in a string key or a DWORD key.
2387 * params:
2388 * regKey - Key to read the default value from
2389 * pdwValue - Pointer to the location where the DWORD
2390 * value is returned. This value is not modified
2391 * if the value is not found.
2394 static void OLEUTL_ReadRegistryDWORDValue(
2395 HKEY regKey,
2396 DWORD* pdwValue)
2398 WCHAR buffer[20];
2399 DWORD cbData = sizeof(buffer);
2400 DWORD dwKeyType;
2401 LONG lres;
2403 lres = RegQueryValueExW(regKey,
2404 emptyW,
2405 NULL,
2406 &dwKeyType,
2407 (LPBYTE)buffer,
2408 &cbData);
2410 if (lres==ERROR_SUCCESS)
2412 switch (dwKeyType)
2414 case REG_DWORD:
2415 *pdwValue = *(DWORD*)buffer;
2416 break;
2417 case REG_EXPAND_SZ:
2418 case REG_MULTI_SZ:
2419 case REG_SZ:
2420 *pdwValue = wcstoul(buffer, NULL, 10);
2421 break;
2426 /******************************************************************************
2427 * OleDraw (OLE32.@)
2429 * The operation of this function is documented literally in the WinAPI
2430 * documentation to involve a QueryInterface for the IViewObject interface,
2431 * followed by a call to IViewObject::Draw.
2433 HRESULT WINAPI OleDraw(
2434 IUnknown *pUnk,
2435 DWORD dwAspect,
2436 HDC hdcDraw,
2437 LPCRECT rect)
2439 HRESULT hres;
2440 IViewObject *viewobject;
2442 if (!pUnk) return E_INVALIDARG;
2444 hres = IUnknown_QueryInterface(pUnk,
2445 &IID_IViewObject,
2446 (void**)&viewobject);
2447 if (SUCCEEDED(hres))
2449 hres = IViewObject_Draw(viewobject, dwAspect, -1, 0, 0, 0, hdcDraw, (RECTL*)rect, 0, 0, 0);
2450 IViewObject_Release(viewobject);
2451 return hres;
2453 else
2454 return DV_E_NOIVIEWOBJECT;
2457 /***********************************************************************
2458 * OleTranslateAccelerator [OLE32.@]
2460 HRESULT WINAPI OleTranslateAccelerator (LPOLEINPLACEFRAME lpFrame,
2461 LPOLEINPLACEFRAMEINFO lpFrameInfo, LPMSG lpmsg)
2463 WORD wID;
2465 TRACE("(%p,%p,%p)\n", lpFrame, lpFrameInfo, lpmsg);
2467 if (IsAccelerator(lpFrameInfo->haccel,lpFrameInfo->cAccelEntries,lpmsg,&wID))
2468 return IOleInPlaceFrame_TranslateAccelerator(lpFrame,lpmsg,wID);
2470 return S_FALSE;
2473 /******************************************************************************
2474 * OleCreate [OLE32.@]
2477 HRESULT WINAPI OleCreate(
2478 REFCLSID rclsid,
2479 REFIID riid,
2480 DWORD renderopt,
2481 LPFORMATETC pFormatEtc,
2482 LPOLECLIENTSITE pClientSite,
2483 LPSTORAGE pStg,
2484 LPVOID* ppvObj)
2486 HRESULT hres;
2487 IUnknown * pUnk = NULL;
2488 IOleObject *pOleObject = NULL;
2490 TRACE("(%s, %s, %d, %p, %p, %p, %p)\n", debugstr_guid(rclsid),
2491 debugstr_guid(riid), renderopt, pFormatEtc, pClientSite, pStg, ppvObj);
2493 hres = CoCreateInstance(rclsid, 0, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER, riid, (LPVOID*)&pUnk);
2495 if (SUCCEEDED(hres))
2496 hres = IStorage_SetClass(pStg, rclsid);
2498 if (pClientSite && SUCCEEDED(hres))
2500 hres = IUnknown_QueryInterface(pUnk, &IID_IOleObject, (LPVOID*)&pOleObject);
2501 if (SUCCEEDED(hres))
2503 DWORD dwStatus;
2504 IOleObject_GetMiscStatus(pOleObject, DVASPECT_CONTENT, &dwStatus);
2508 if (SUCCEEDED(hres))
2510 IPersistStorage * pPS;
2511 if (SUCCEEDED((hres = IUnknown_QueryInterface(pUnk, &IID_IPersistStorage, (LPVOID*)&pPS))))
2513 TRACE("trying to set stg %p\n", pStg);
2514 hres = IPersistStorage_InitNew(pPS, pStg);
2515 TRACE("-- result 0x%08x\n", hres);
2516 IPersistStorage_Release(pPS);
2520 if (pClientSite && SUCCEEDED(hres))
2522 TRACE("trying to set clientsite %p\n", pClientSite);
2523 hres = IOleObject_SetClientSite(pOleObject, pClientSite);
2524 TRACE("-- result 0x%08x\n", hres);
2527 if (pOleObject)
2528 IOleObject_Release(pOleObject);
2530 if (((renderopt == OLERENDER_DRAW) || (renderopt == OLERENDER_FORMAT)) &&
2531 SUCCEEDED(hres))
2533 hres = OleRun(pUnk);
2534 if (SUCCEEDED(hres))
2536 IOleCache *pOleCache;
2538 if (SUCCEEDED(IUnknown_QueryInterface(pUnk, &IID_IOleCache, (void **)&pOleCache)))
2540 DWORD dwConnection;
2541 if (renderopt == OLERENDER_DRAW && !pFormatEtc) {
2542 FORMATETC pfe;
2543 pfe.cfFormat = 0;
2544 pfe.ptd = NULL;
2545 pfe.dwAspect = DVASPECT_CONTENT;
2546 pfe.lindex = -1;
2547 pfe.tymed = TYMED_NULL;
2548 hres = IOleCache_Cache(pOleCache, &pfe, ADVF_PRIMEFIRST, &dwConnection);
2550 else
2551 hres = IOleCache_Cache(pOleCache, pFormatEtc, ADVF_PRIMEFIRST, &dwConnection);
2552 IOleCache_Release(pOleCache);
2557 if (FAILED(hres) && pUnk)
2559 IUnknown_Release(pUnk);
2560 pUnk = NULL;
2563 *ppvObj = pUnk;
2565 TRACE("-- %p\n", pUnk);
2566 return hres;
2569 /******************************************************************************
2570 * OleGetAutoConvert [OLE32.@]
2572 HRESULT WINAPI OleGetAutoConvert(REFCLSID clsidOld, LPCLSID pClsidNew)
2574 static const WCHAR wszAutoConvertTo[] = {'A','u','t','o','C','o','n','v','e','r','t','T','o',0};
2575 HKEY hkey = NULL;
2576 WCHAR buf[CHARS_IN_GUID];
2577 LONG len;
2578 HRESULT res = S_OK;
2580 res = COM_OpenKeyForCLSID(clsidOld, wszAutoConvertTo, KEY_READ, &hkey);
2581 if (FAILED(res))
2582 goto done;
2584 len = sizeof(buf);
2585 if (RegQueryValueW(hkey, NULL, buf, &len))
2587 res = REGDB_E_KEYMISSING;
2588 goto done;
2590 res = CLSIDFromString(buf, pClsidNew);
2591 done:
2592 if (hkey) RegCloseKey(hkey);
2593 return res;
2596 /******************************************************************************
2597 * OleSetAutoConvert [OLE32.@]
2599 HRESULT WINAPI OleSetAutoConvert(REFCLSID clsidOld, REFCLSID clsidNew)
2601 static const WCHAR wszAutoConvertTo[] = {'A','u','t','o','C','o','n','v','e','r','t','T','o',0};
2602 HKEY hkey = NULL;
2603 WCHAR szClsidNew[CHARS_IN_GUID];
2604 HRESULT res = S_OK;
2606 TRACE("(%s,%s)\n", debugstr_guid(clsidOld), debugstr_guid(clsidNew));
2608 res = COM_OpenKeyForCLSID(clsidOld, NULL, KEY_READ | KEY_WRITE, &hkey);
2609 if (FAILED(res))
2610 goto done;
2611 StringFromGUID2(clsidNew, szClsidNew, CHARS_IN_GUID);
2612 if (RegSetValueW(hkey, wszAutoConvertTo, REG_SZ, szClsidNew, (lstrlenW(szClsidNew)+1) * sizeof(WCHAR)))
2614 res = REGDB_E_WRITEREGDB;
2615 goto done;
2618 done:
2619 if (hkey) RegCloseKey(hkey);
2620 return res;
2623 /******************************************************************************
2624 * OleDoAutoConvert [OLE32.@]
2626 HRESULT WINAPI OleDoAutoConvert(LPSTORAGE pStg, LPCLSID pClsidNew)
2628 WCHAR *user_type_old, *user_type_new;
2629 CLIPFORMAT cf;
2630 STATSTG stat;
2631 CLSID clsid;
2632 HRESULT hr;
2634 TRACE("(%p, %p)\n", pStg, pClsidNew);
2636 *pClsidNew = CLSID_NULL;
2637 if(!pStg)
2638 return E_INVALIDARG;
2639 hr = IStorage_Stat(pStg, &stat, STATFLAG_NONAME);
2640 if(FAILED(hr))
2641 return hr;
2643 *pClsidNew = stat.clsid;
2644 hr = OleGetAutoConvert(&stat.clsid, &clsid);
2645 if(FAILED(hr))
2646 return hr;
2648 hr = IStorage_SetClass(pStg, &clsid);
2649 if(FAILED(hr))
2650 return hr;
2652 hr = ReadFmtUserTypeStg(pStg, &cf, &user_type_old);
2653 if(FAILED(hr)) {
2654 cf = 0;
2655 user_type_new = NULL;
2658 hr = OleRegGetUserType(&clsid, USERCLASSTYPE_FULL, &user_type_new);
2659 if(FAILED(hr))
2660 user_type_new = NULL;
2662 hr = WriteFmtUserTypeStg(pStg, cf, user_type_new);
2663 CoTaskMemFree(user_type_new);
2664 if(FAILED(hr))
2666 CoTaskMemFree(user_type_old);
2667 IStorage_SetClass(pStg, &stat.clsid);
2668 return hr;
2671 hr = SetConvertStg(pStg, TRUE);
2672 if(FAILED(hr))
2674 WriteFmtUserTypeStg(pStg, cf, user_type_old);
2675 IStorage_SetClass(pStg, &stat.clsid);
2677 else
2678 *pClsidNew = clsid;
2679 CoTaskMemFree(user_type_old);
2680 return hr;
2683 /******************************************************************************
2684 * OleIsRunning [OLE32.@]
2686 BOOL WINAPI OleIsRunning(LPOLEOBJECT object)
2688 IRunnableObject *pRunnable;
2689 HRESULT hr;
2690 BOOL running;
2692 TRACE("(%p)\n", object);
2694 if (!object) return FALSE;
2696 hr = IOleObject_QueryInterface(object, &IID_IRunnableObject, (void **)&pRunnable);
2697 if (FAILED(hr))
2698 return TRUE;
2699 running = IRunnableObject_IsRunning(pRunnable);
2700 IRunnableObject_Release(pRunnable);
2701 return running;
2704 /***********************************************************************
2705 * OleNoteObjectVisible [OLE32.@]
2707 HRESULT WINAPI OleNoteObjectVisible(LPUNKNOWN pUnknown, BOOL bVisible)
2709 TRACE("(%p, %s)\n", pUnknown, bVisible ? "TRUE" : "FALSE");
2710 return CoLockObjectExternal(pUnknown, bVisible, TRUE);
2714 /***********************************************************************
2715 * OLE_FreeClipDataArray [internal]
2717 * NOTES:
2718 * frees the data associated with an array of CLIPDATAs
2720 static void OLE_FreeClipDataArray(ULONG count, CLIPDATA * pClipDataArray)
2722 ULONG i;
2723 for (i = 0; i < count; i++)
2724 CoTaskMemFree(pClipDataArray[i].pClipData);
2727 /***********************************************************************
2728 * PropSysAllocString [OLE32.@]
2729 * NOTES
2730 * Forward to oleaut32.
2732 BSTR WINAPI PropSysAllocString(LPCOLESTR str)
2734 return SysAllocString(str);
2737 /***********************************************************************
2738 * PropSysFreeString [OLE32.@]
2739 * NOTES
2740 * Forward to oleaut32.
2742 void WINAPI PropSysFreeString(LPOLESTR str)
2744 SysFreeString(str);
2747 /******************************************************************************
2748 * Check if a PROPVARIANT's type is valid.
2750 static inline HRESULT PROPVARIANT_ValidateType(VARTYPE vt)
2752 switch (vt)
2754 case VT_EMPTY:
2755 case VT_NULL:
2756 case VT_I1:
2757 case VT_I2:
2758 case VT_I4:
2759 case VT_I8:
2760 case VT_R4:
2761 case VT_R8:
2762 case VT_CY:
2763 case VT_DATE:
2764 case VT_BSTR:
2765 case VT_ERROR:
2766 case VT_BOOL:
2767 case VT_DECIMAL:
2768 case VT_UI1:
2769 case VT_UI2:
2770 case VT_UI4:
2771 case VT_UI8:
2772 case VT_INT:
2773 case VT_UINT:
2774 case VT_LPSTR:
2775 case VT_LPWSTR:
2776 case VT_FILETIME:
2777 case VT_BLOB:
2778 case VT_DISPATCH:
2779 case VT_UNKNOWN:
2780 case VT_STREAM:
2781 case VT_STORAGE:
2782 case VT_STREAMED_OBJECT:
2783 case VT_STORED_OBJECT:
2784 case VT_BLOB_OBJECT:
2785 case VT_CF:
2786 case VT_CLSID:
2787 case VT_I1|VT_VECTOR:
2788 case VT_I2|VT_VECTOR:
2789 case VT_I4|VT_VECTOR:
2790 case VT_I8|VT_VECTOR:
2791 case VT_R4|VT_VECTOR:
2792 case VT_R8|VT_VECTOR:
2793 case VT_CY|VT_VECTOR:
2794 case VT_DATE|VT_VECTOR:
2795 case VT_BSTR|VT_VECTOR:
2796 case VT_ERROR|VT_VECTOR:
2797 case VT_BOOL|VT_VECTOR:
2798 case VT_VARIANT|VT_VECTOR:
2799 case VT_UI1|VT_VECTOR:
2800 case VT_UI2|VT_VECTOR:
2801 case VT_UI4|VT_VECTOR:
2802 case VT_UI8|VT_VECTOR:
2803 case VT_LPSTR|VT_VECTOR:
2804 case VT_LPWSTR|VT_VECTOR:
2805 case VT_FILETIME|VT_VECTOR:
2806 case VT_CF|VT_VECTOR:
2807 case VT_CLSID|VT_VECTOR:
2808 case VT_ARRAY|VT_I1:
2809 case VT_ARRAY|VT_UI1:
2810 case VT_ARRAY|VT_I2:
2811 case VT_ARRAY|VT_UI2:
2812 case VT_ARRAY|VT_I4:
2813 case VT_ARRAY|VT_UI4:
2814 case VT_ARRAY|VT_INT:
2815 case VT_ARRAY|VT_UINT:
2816 case VT_ARRAY|VT_R4:
2817 case VT_ARRAY|VT_R8:
2818 case VT_ARRAY|VT_CY:
2819 case VT_ARRAY|VT_DATE:
2820 case VT_ARRAY|VT_BSTR:
2821 case VT_ARRAY|VT_BOOL:
2822 case VT_ARRAY|VT_DECIMAL:
2823 case VT_ARRAY|VT_DISPATCH:
2824 case VT_ARRAY|VT_UNKNOWN:
2825 case VT_ARRAY|VT_ERROR:
2826 case VT_ARRAY|VT_VARIANT:
2827 return S_OK;
2829 WARN("Bad type %d\n", vt);
2830 return STG_E_INVALIDPARAMETER;
2833 /***********************************************************************
2834 * PropVariantClear [OLE32.@]
2836 HRESULT WINAPI PropVariantClear(PROPVARIANT * pvar) /* [in/out] */
2838 HRESULT hr;
2840 TRACE("(%p)\n", pvar);
2842 if (!pvar)
2843 return S_OK;
2845 hr = PROPVARIANT_ValidateType(pvar->vt);
2846 if (FAILED(hr))
2848 memset(pvar, 0, sizeof(*pvar));
2849 return hr;
2852 switch(pvar->vt)
2854 case VT_EMPTY:
2855 case VT_NULL:
2856 case VT_I1:
2857 case VT_I2:
2858 case VT_I4:
2859 case VT_I8:
2860 case VT_R4:
2861 case VT_R8:
2862 case VT_CY:
2863 case VT_DATE:
2864 case VT_ERROR:
2865 case VT_BOOL:
2866 case VT_DECIMAL:
2867 case VT_UI1:
2868 case VT_UI2:
2869 case VT_UI4:
2870 case VT_UI8:
2871 case VT_INT:
2872 case VT_UINT:
2873 case VT_FILETIME:
2874 break;
2875 case VT_DISPATCH:
2876 case VT_UNKNOWN:
2877 case VT_STREAM:
2878 case VT_STREAMED_OBJECT:
2879 case VT_STORAGE:
2880 case VT_STORED_OBJECT:
2881 if (pvar->u.pStream)
2882 IStream_Release(pvar->u.pStream);
2883 break;
2884 case VT_CLSID:
2885 case VT_LPSTR:
2886 case VT_LPWSTR:
2887 /* pick an arbitrary typed pointer - we don't care about the type
2888 * as we are just freeing it */
2889 CoTaskMemFree(pvar->u.puuid);
2890 break;
2891 case VT_BLOB:
2892 case VT_BLOB_OBJECT:
2893 CoTaskMemFree(pvar->u.blob.pBlobData);
2894 break;
2895 case VT_BSTR:
2896 PropSysFreeString(pvar->u.bstrVal);
2897 break;
2898 case VT_CF:
2899 if (pvar->u.pclipdata)
2901 OLE_FreeClipDataArray(1, pvar->u.pclipdata);
2902 CoTaskMemFree(pvar->u.pclipdata);
2904 break;
2905 default:
2906 if (pvar->vt & VT_VECTOR)
2908 ULONG i;
2910 switch (pvar->vt & ~VT_VECTOR)
2912 case VT_VARIANT:
2913 FreePropVariantArray(pvar->u.capropvar.cElems, pvar->u.capropvar.pElems);
2914 break;
2915 case VT_CF:
2916 OLE_FreeClipDataArray(pvar->u.caclipdata.cElems, pvar->u.caclipdata.pElems);
2917 break;
2918 case VT_BSTR:
2919 for (i = 0; i < pvar->u.cabstr.cElems; i++)
2920 PropSysFreeString(pvar->u.cabstr.pElems[i]);
2921 break;
2922 case VT_LPSTR:
2923 for (i = 0; i < pvar->u.calpstr.cElems; i++)
2924 CoTaskMemFree(pvar->u.calpstr.pElems[i]);
2925 break;
2926 case VT_LPWSTR:
2927 for (i = 0; i < pvar->u.calpwstr.cElems; i++)
2928 CoTaskMemFree(pvar->u.calpwstr.pElems[i]);
2929 break;
2931 if (pvar->vt & ~VT_VECTOR)
2933 /* pick an arbitrary VT_VECTOR structure - they all have the same
2934 * memory layout */
2935 CoTaskMemFree(pvar->u.capropvar.pElems);
2938 else if (pvar->vt & VT_ARRAY)
2939 hr = SafeArrayDestroy(pvar->u.parray);
2940 else
2942 WARN("Invalid/unsupported type %d\n", pvar->vt);
2943 hr = STG_E_INVALIDPARAMETER;
2947 memset(pvar, 0, sizeof(*pvar));
2948 return hr;
2951 /***********************************************************************
2952 * PropVariantCopy [OLE32.@]
2954 HRESULT WINAPI PropVariantCopy(PROPVARIANT *pvarDest, /* [out] */
2955 const PROPVARIANT *pvarSrc) /* [in] */
2957 ULONG len;
2958 HRESULT hr;
2960 TRACE("(%p, %p vt %04x)\n", pvarDest, pvarSrc, pvarSrc->vt);
2962 hr = PROPVARIANT_ValidateType(pvarSrc->vt);
2963 if (FAILED(hr))
2964 return DISP_E_BADVARTYPE;
2966 /* this will deal with most cases */
2967 *pvarDest = *pvarSrc;
2969 switch(pvarSrc->vt)
2971 case VT_EMPTY:
2972 case VT_NULL:
2973 case VT_I1:
2974 case VT_UI1:
2975 case VT_I2:
2976 case VT_UI2:
2977 case VT_BOOL:
2978 case VT_DECIMAL:
2979 case VT_I4:
2980 case VT_UI4:
2981 case VT_R4:
2982 case VT_ERROR:
2983 case VT_I8:
2984 case VT_UI8:
2985 case VT_INT:
2986 case VT_UINT:
2987 case VT_R8:
2988 case VT_CY:
2989 case VT_DATE:
2990 case VT_FILETIME:
2991 break;
2992 case VT_DISPATCH:
2993 case VT_UNKNOWN:
2994 case VT_STREAM:
2995 case VT_STREAMED_OBJECT:
2996 case VT_STORAGE:
2997 case VT_STORED_OBJECT:
2998 if (pvarDest->u.pStream)
2999 IStream_AddRef(pvarDest->u.pStream);
3000 break;
3001 case VT_CLSID:
3002 pvarDest->u.puuid = CoTaskMemAlloc(sizeof(CLSID));
3003 *pvarDest->u.puuid = *pvarSrc->u.puuid;
3004 break;
3005 case VT_LPSTR:
3006 if (pvarSrc->u.pszVal)
3008 len = strlen(pvarSrc->u.pszVal);
3009 pvarDest->u.pszVal = CoTaskMemAlloc((len+1)*sizeof(CHAR));
3010 CopyMemory(pvarDest->u.pszVal, pvarSrc->u.pszVal, (len+1)*sizeof(CHAR));
3012 break;
3013 case VT_LPWSTR:
3014 if (pvarSrc->u.pwszVal)
3016 len = lstrlenW(pvarSrc->u.pwszVal);
3017 pvarDest->u.pwszVal = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
3018 CopyMemory(pvarDest->u.pwszVal, pvarSrc->u.pwszVal, (len+1)*sizeof(WCHAR));
3020 break;
3021 case VT_BLOB:
3022 case VT_BLOB_OBJECT:
3023 if (pvarSrc->u.blob.pBlobData)
3025 len = pvarSrc->u.blob.cbSize;
3026 pvarDest->u.blob.pBlobData = CoTaskMemAlloc(len);
3027 CopyMemory(pvarDest->u.blob.pBlobData, pvarSrc->u.blob.pBlobData, len);
3029 break;
3030 case VT_BSTR:
3031 pvarDest->u.bstrVal = PropSysAllocString(pvarSrc->u.bstrVal);
3032 break;
3033 case VT_CF:
3034 if (pvarSrc->u.pclipdata)
3036 len = pvarSrc->u.pclipdata->cbSize - sizeof(pvarSrc->u.pclipdata->ulClipFmt);
3037 pvarDest->u.pclipdata = CoTaskMemAlloc(sizeof (CLIPDATA));
3038 pvarDest->u.pclipdata->cbSize = pvarSrc->u.pclipdata->cbSize;
3039 pvarDest->u.pclipdata->ulClipFmt = pvarSrc->u.pclipdata->ulClipFmt;
3040 pvarDest->u.pclipdata->pClipData = CoTaskMemAlloc(len);
3041 CopyMemory(pvarDest->u.pclipdata->pClipData, pvarSrc->u.pclipdata->pClipData, len);
3043 break;
3044 default:
3045 if (pvarSrc->vt & VT_VECTOR)
3047 int elemSize;
3048 ULONG i;
3050 switch(pvarSrc->vt & ~VT_VECTOR)
3052 case VT_I1: elemSize = sizeof(pvarSrc->u.cVal); break;
3053 case VT_UI1: elemSize = sizeof(pvarSrc->u.bVal); break;
3054 case VT_I2: elemSize = sizeof(pvarSrc->u.iVal); break;
3055 case VT_UI2: elemSize = sizeof(pvarSrc->u.uiVal); break;
3056 case VT_BOOL: elemSize = sizeof(pvarSrc->u.boolVal); break;
3057 case VT_I4: elemSize = sizeof(pvarSrc->u.lVal); break;
3058 case VT_UI4: elemSize = sizeof(pvarSrc->u.ulVal); break;
3059 case VT_R4: elemSize = sizeof(pvarSrc->u.fltVal); break;
3060 case VT_R8: elemSize = sizeof(pvarSrc->u.dblVal); break;
3061 case VT_ERROR: elemSize = sizeof(pvarSrc->u.scode); break;
3062 case VT_I8: elemSize = sizeof(pvarSrc->u.hVal); break;
3063 case VT_UI8: elemSize = sizeof(pvarSrc->u.uhVal); break;
3064 case VT_CY: elemSize = sizeof(pvarSrc->u.cyVal); break;
3065 case VT_DATE: elemSize = sizeof(pvarSrc->u.date); break;
3066 case VT_FILETIME: elemSize = sizeof(pvarSrc->u.filetime); break;
3067 case VT_CLSID: elemSize = sizeof(*pvarSrc->u.puuid); break;
3068 case VT_CF: elemSize = sizeof(*pvarSrc->u.pclipdata); break;
3069 case VT_BSTR: elemSize = sizeof(pvarSrc->u.bstrVal); break;
3070 case VT_LPSTR: elemSize = sizeof(pvarSrc->u.pszVal); break;
3071 case VT_LPWSTR: elemSize = sizeof(pvarSrc->u.pwszVal); break;
3072 case VT_VARIANT: elemSize = sizeof(*pvarSrc->u.pvarVal); break;
3074 default:
3075 FIXME("Invalid element type: %ul\n", pvarSrc->vt & ~VT_VECTOR);
3076 return E_INVALIDARG;
3078 len = pvarSrc->u.capropvar.cElems;
3079 pvarDest->u.capropvar.pElems = len ? CoTaskMemAlloc(len * elemSize) : NULL;
3080 if (pvarSrc->vt == (VT_VECTOR | VT_VARIANT))
3082 for (i = 0; i < len; i++)
3083 PropVariantCopy(&pvarDest->u.capropvar.pElems[i], &pvarSrc->u.capropvar.pElems[i]);
3085 else if (pvarSrc->vt == (VT_VECTOR | VT_CF))
3087 FIXME("Copy clipformats\n");
3089 else if (pvarSrc->vt == (VT_VECTOR | VT_BSTR))
3091 for (i = 0; i < len; i++)
3092 pvarDest->u.cabstr.pElems[i] = PropSysAllocString(pvarSrc->u.cabstr.pElems[i]);
3094 else if (pvarSrc->vt == (VT_VECTOR | VT_LPSTR))
3096 size_t strLen;
3097 for (i = 0; i < len; i++)
3099 strLen = lstrlenA(pvarSrc->u.calpstr.pElems[i]) + 1;
3100 pvarDest->u.calpstr.pElems[i] = CoTaskMemAlloc(strLen);
3101 memcpy(pvarDest->u.calpstr.pElems[i],
3102 pvarSrc->u.calpstr.pElems[i], strLen);
3105 else if (pvarSrc->vt == (VT_VECTOR | VT_LPWSTR))
3107 size_t strLen;
3108 for (i = 0; i < len; i++)
3110 strLen = (lstrlenW(pvarSrc->u.calpwstr.pElems[i]) + 1) *
3111 sizeof(WCHAR);
3112 pvarDest->u.calpstr.pElems[i] = CoTaskMemAlloc(strLen);
3113 memcpy(pvarDest->u.calpstr.pElems[i],
3114 pvarSrc->u.calpstr.pElems[i], strLen);
3117 else
3118 CopyMemory(pvarDest->u.capropvar.pElems, pvarSrc->u.capropvar.pElems, len * elemSize);
3120 else if (pvarSrc->vt & VT_ARRAY)
3122 pvarDest->u.uhVal.QuadPart = 0;
3123 return SafeArrayCopy(pvarSrc->u.parray, &pvarDest->u.parray);
3125 else
3126 WARN("Invalid/unsupported type %d\n", pvarSrc->vt);
3129 return S_OK;
3132 /***********************************************************************
3133 * FreePropVariantArray [OLE32.@]
3135 HRESULT WINAPI FreePropVariantArray(ULONG cVariants, /* [in] */
3136 PROPVARIANT *rgvars) /* [in/out] */
3138 ULONG i;
3140 TRACE("(%u, %p)\n", cVariants, rgvars);
3142 if (!rgvars)
3143 return E_INVALIDARG;
3145 for(i = 0; i < cVariants; i++)
3146 PropVariantClear(&rgvars[i]);
3148 return S_OK;
3151 /******************************************************************************
3152 * DllDebugObjectRPCHook (OLE32.@)
3153 * turns on and off internal debugging, pointer is only used on macintosh
3156 BOOL WINAPI DllDebugObjectRPCHook(BOOL b, void *dummy)
3158 FIXME("stub\n");
3159 return TRUE;