Assorted spelling fixes.
[wine/multimedia.git] / dlls / ole32 / ole2.c
blobbaaf02e9911b656c8b9a1d5a8a51c465b44302ca
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 "config.h"
28 #include <assert.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <string.h>
34 #define COBJMACROS
35 #define NONAMELESSUNION
36 #define NONAMELESSSTRUCT
38 #include "windef.h"
39 #include "winbase.h"
40 #include "winerror.h"
41 #include "wingdi.h"
42 #include "winuser.h"
43 #include "winnls.h"
44 #include "winreg.h"
45 #include "ole2.h"
46 #include "ole2ver.h"
48 #include "wine/unicode.h"
49 #include "compobj_private.h"
50 #include "wine/list.h"
52 #include "wine/debug.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(ole);
55 WINE_DECLARE_DEBUG_CHANNEL(accel);
57 /******************************************************************************
58 * These are static/global variables and internal data structures that the
59 * OLE module uses to maintain its state.
61 typedef struct tagTrackerWindowInfo
63 IDataObject* dataObject;
64 IDropSource* dropSource;
65 DWORD dwOKEffect;
66 DWORD* pdwEffect;
67 BOOL trackingDone;
68 HRESULT returnValue;
70 BOOL escPressed;
71 HWND curTargetHWND; /* window the mouse is hovering over */
72 HWND curDragTargetHWND; /* might be an ancestor of curTargetHWND */
73 IDropTarget* curDragTarget;
74 POINTL curMousePos; /* current position of the mouse in screen coordinates */
75 DWORD dwKeyState; /* current state of the shift and ctrl keys and the mouse buttons */
76 } TrackerWindowInfo;
78 typedef struct tagOleMenuDescriptor /* OleMenuDescriptor */
80 HWND hwndFrame; /* The containers frame window */
81 HWND hwndActiveObject; /* The active objects window */
82 OLEMENUGROUPWIDTHS mgw; /* OLE menu group widths for the shared menu */
83 HMENU hmenuCombined; /* The combined menu */
84 BOOL bIsServerItem; /* True if the currently open popup belongs to the server */
85 } OleMenuDescriptor;
87 typedef struct tagOleMenuHookItem /* OleMenu hook item in per thread hook list */
89 DWORD tid; /* Thread Id */
90 HANDLE hHeap; /* Heap this is allocated from */
91 HHOOK GetMsg_hHook; /* message hook for WH_GETMESSAGE */
92 HHOOK CallWndProc_hHook; /* message hook for WH_CALLWNDPROC */
93 struct tagOleMenuHookItem *next;
94 } OleMenuHookItem;
96 static OleMenuHookItem *hook_list;
99 * This is the lock count on the OLE library. It is controlled by the
100 * OLEInitialize/OLEUninitialize methods.
102 static LONG OLE_moduleLockCount = 0;
105 * Name of our registered window class.
107 static const WCHAR OLEDD_DRAGTRACKERCLASS[] =
108 {'W','i','n','e','D','r','a','g','D','r','o','p','T','r','a','c','k','e','r','3','2',0};
111 * Name of menu descriptor property.
113 static const WCHAR prop_olemenuW[] =
114 {'P','R','O','P','_','O','L','E','M','e','n','u','D','e','s','c','r','i','p','t','o','r',0};
116 /* property to store IDropTarget pointer */
117 static const WCHAR prop_oledroptarget[] =
118 {'O','l','e','D','r','o','p','T','a','r','g','e','t','I','n','t','e','r','f','a','c','e',0};
120 /* property to store Marshalled IDropTarget pointer */
121 static const WCHAR prop_marshalleddroptarget[] =
122 {'W','i','n','e','M','a','r','s','h','a','l','l','e','d','D','r','o','p','T','a','r','g','e','t',0};
124 static const WCHAR clsidfmtW[] =
125 {'C','L','S','I','D','\\','{','%','0','8','x','-','%','0','4','x','-','%','0','4','x','-',
126 '%','0','2','x','%','0','2','x','-','%','0','2','x','%','0','2','x','%','0','2','x','%','0','2','x',
127 '%','0','2','x','%','0','2','x','}','\\',0};
129 static const WCHAR emptyW[] = { 0 };
131 /******************************************************************************
132 * These are the prototypes of miscellaneous utility methods
134 static void OLEUTL_ReadRegistryDWORDValue(HKEY regKey, DWORD* pdwValue);
136 /******************************************************************************
137 * These are the prototypes of the utility methods used to manage a shared menu
139 static void OLEMenu_Initialize(void);
140 static void OLEMenu_UnInitialize(void);
141 static BOOL OLEMenu_InstallHooks( DWORD tid );
142 static BOOL OLEMenu_UnInstallHooks( DWORD tid );
143 static OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid );
144 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos );
145 static BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor );
146 static LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam);
147 static LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam);
149 /******************************************************************************
150 * These are the prototypes of the OLE Clipboard initialization methods (in clipboard.c)
152 extern void OLEClipbrd_UnInitialize(void);
153 extern void OLEClipbrd_Initialize(void);
155 /******************************************************************************
156 * These are the prototypes of the utility methods used for OLE Drag n Drop
158 static void OLEDD_Initialize(void);
159 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
160 static void OLEDD_TrackMouseMove(TrackerWindowInfo* trackerInfo);
161 static void OLEDD_TrackStateChange(TrackerWindowInfo* trackerInfo);
162 static DWORD OLEDD_GetButtonState(void);
164 /******************************************************************************
165 * OleBuildVersion [OLE32.@]
167 DWORD WINAPI OleBuildVersion(void)
169 TRACE("Returning version %d, build %d.\n", rmm, rup);
170 return (rmm<<16)+rup;
173 /***********************************************************************
174 * OleInitialize (OLE32.@)
176 HRESULT WINAPI OleInitialize(LPVOID reserved)
178 HRESULT hr;
180 TRACE("(%p)\n", reserved);
183 * The first duty of the OleInitialize is to initialize the COM libraries.
185 hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
188 * If the CoInitializeEx call failed, the OLE libraries can't be
189 * initialized.
191 if (FAILED(hr))
192 return hr;
194 if (!COM_CurrentInfo()->ole_inits)
195 hr = S_OK;
198 * Then, it has to initialize the OLE specific modules.
199 * This includes:
200 * Clipboard
201 * Drag and Drop
202 * Object linking and Embedding
203 * In-place activation
205 if (!COM_CurrentInfo()->ole_inits++ &&
206 InterlockedIncrement(&OLE_moduleLockCount) == 1)
209 * Initialize the libraries.
211 TRACE("() - Initializing the OLE libraries\n");
214 * OLE Clipboard
216 OLEClipbrd_Initialize();
219 * Drag and Drop
221 OLEDD_Initialize();
224 * OLE shared menu
226 OLEMenu_Initialize();
229 return hr;
232 /******************************************************************************
233 * OleUninitialize [OLE32.@]
235 void WINAPI OleUninitialize(void)
237 TRACE("()\n");
240 * If we hit the bottom of the lock stack, free the libraries.
242 if (!--COM_CurrentInfo()->ole_inits && !InterlockedDecrement(&OLE_moduleLockCount))
245 * Actually free the libraries.
247 TRACE("() - Freeing the last reference count\n");
250 * OLE Clipboard
252 OLEClipbrd_UnInitialize();
255 * OLE shared menu
257 OLEMenu_UnInitialize();
261 * Then, uninitialize the COM libraries.
263 CoUninitialize();
266 /******************************************************************************
267 * OleInitializeWOW [OLE32.@]
269 HRESULT WINAPI OleInitializeWOW(DWORD x, DWORD y) {
270 FIXME("(0x%08x, 0x%08x),stub!\n",x, y);
271 return 0;
274 /*************************************************************
275 * get_droptarget_handle
277 * Retrieve a handle to the map containing the marshalled IDropTarget.
278 * This handle belongs to the process that called RegisterDragDrop.
279 * See get_droptarget_local_handle().
281 static inline HANDLE get_droptarget_handle(HWND hwnd)
283 return GetPropW(hwnd, prop_marshalleddroptarget);
286 /*************************************************************
287 * is_droptarget
289 * Is the window a droptarget.
291 static inline BOOL is_droptarget(HWND hwnd)
293 return get_droptarget_handle(hwnd) != 0;
296 /*************************************************************
297 * get_droptarget_local_handle
299 * Retrieve a handle to the map containing the marshalled IDropTarget.
300 * The handle should be closed when finished with.
302 static HANDLE get_droptarget_local_handle(HWND hwnd)
304 HANDLE handle, local_handle = 0;
306 handle = get_droptarget_handle(hwnd);
308 if(handle)
310 DWORD pid;
311 HANDLE process;
313 GetWindowThreadProcessId(hwnd, &pid);
314 process = OpenProcess(PROCESS_DUP_HANDLE, FALSE, pid);
315 if(process)
317 DuplicateHandle(process, handle, GetCurrentProcess(), &local_handle, 0, FALSE, DUPLICATE_SAME_ACCESS);
318 CloseHandle(process);
321 return local_handle;
324 /***********************************************************************
325 * create_map_from_stream
327 * Helper for RegisterDragDrop. Creates a file mapping object
328 * with the contents of the provided stream. The stream must
329 * be a global memory backed stream.
331 static HRESULT create_map_from_stream(IStream *stream, HANDLE *map)
333 HGLOBAL hmem;
334 DWORD size;
335 HRESULT hr;
336 void *data;
338 hr = GetHGlobalFromStream(stream, &hmem);
339 if(FAILED(hr)) return hr;
341 size = GlobalSize(hmem);
342 *map = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, size, NULL);
343 if(!*map) return E_OUTOFMEMORY;
345 data = MapViewOfFile(*map, FILE_MAP_WRITE, 0, 0, size);
346 memcpy(data, GlobalLock(hmem), size);
347 GlobalUnlock(hmem);
348 UnmapViewOfFile(data);
349 return S_OK;
352 /***********************************************************************
353 * create_stream_from_map
355 * Creates a stream from the provided map.
357 static HRESULT create_stream_from_map(HANDLE map, IStream **stream)
359 HRESULT hr = E_OUTOFMEMORY;
360 HGLOBAL hmem;
361 void *data;
362 MEMORY_BASIC_INFORMATION info;
364 data = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
365 if(!data) return hr;
367 VirtualQuery(data, &info, sizeof(info));
368 TRACE("size %d\n", (int)info.RegionSize);
370 hmem = GlobalAlloc(GMEM_MOVEABLE, info.RegionSize);
371 if(hmem)
373 memcpy(GlobalLock(hmem), data, info.RegionSize);
374 GlobalUnlock(hmem);
375 hr = CreateStreamOnHGlobal(hmem, TRUE, stream);
377 UnmapViewOfFile(data);
378 return hr;
381 /* This is to work around apps which break COM rules by not implementing
382 * IDropTarget::QueryInterface(). Windows doesn't expose this because it
383 * doesn't call CoMarshallInterface() in RegisterDragDrop().
384 * The wrapper is only used internally, and only exists for the life of
385 * the marshal. We don't want to hold a ref on the app provided target
386 * as some apps destroy this prior to CoUninitialize without calling
387 * RevokeDragDrop. The only (long-term) ref is held by the window prop. */
388 typedef struct {
389 IDropTarget IDropTarget_iface;
390 HWND hwnd;
391 LONG refs;
392 } DropTargetWrapper;
394 static inline DropTargetWrapper* impl_from_IDropTarget(IDropTarget* iface)
396 return CONTAINING_RECORD(iface, DropTargetWrapper, IDropTarget_iface);
399 static HRESULT WINAPI DropTargetWrapper_QueryInterface(IDropTarget* iface,
400 REFIID riid,
401 void** ppvObject)
403 DropTargetWrapper* This = impl_from_IDropTarget(iface);
404 if (IsEqualIID(riid, &IID_IUnknown) ||
405 IsEqualIID(riid, &IID_IDropTarget))
407 IDropTarget_AddRef(&This->IDropTarget_iface);
408 *ppvObject = &This->IDropTarget_iface;
409 return S_OK;
411 *ppvObject = NULL;
412 return E_NOINTERFACE;
415 static ULONG WINAPI DropTargetWrapper_AddRef(IDropTarget* iface)
417 DropTargetWrapper* This = impl_from_IDropTarget(iface);
418 return InterlockedIncrement(&This->refs);
421 static ULONG WINAPI DropTargetWrapper_Release(IDropTarget* iface)
423 DropTargetWrapper* This = impl_from_IDropTarget(iface);
424 ULONG refs = InterlockedDecrement(&This->refs);
425 if (!refs) HeapFree(GetProcessHeap(), 0, This);
426 return refs;
429 static inline HRESULT get_target_from_wrapper( IDropTarget *wrapper, IDropTarget **target )
431 DropTargetWrapper* This = impl_from_IDropTarget( wrapper );
432 *target = GetPropW( This->hwnd, prop_oledroptarget );
433 if (!*target) return DRAGDROP_E_NOTREGISTERED;
434 IDropTarget_AddRef( *target );
435 return S_OK;
438 static HRESULT WINAPI DropTargetWrapper_DragEnter(IDropTarget* iface,
439 IDataObject* pDataObj,
440 DWORD grfKeyState,
441 POINTL pt,
442 DWORD* pdwEffect)
444 IDropTarget *target;
445 HRESULT r = get_target_from_wrapper( iface, &target );
447 if (SUCCEEDED( r ))
449 r = IDropTarget_DragEnter( target, pDataObj, grfKeyState, pt, pdwEffect );
450 IDropTarget_Release( target );
452 return r;
455 static HRESULT WINAPI DropTargetWrapper_DragOver(IDropTarget* iface,
456 DWORD grfKeyState,
457 POINTL pt,
458 DWORD* pdwEffect)
460 IDropTarget *target;
461 HRESULT r = get_target_from_wrapper( iface, &target );
463 if (SUCCEEDED( r ))
465 r = IDropTarget_DragOver( target, grfKeyState, pt, pdwEffect );
466 IDropTarget_Release( target );
468 return r;
471 static HRESULT WINAPI DropTargetWrapper_DragLeave(IDropTarget* iface)
473 IDropTarget *target;
474 HRESULT r = get_target_from_wrapper( iface, &target );
476 if (SUCCEEDED( r ))
478 r = IDropTarget_DragLeave( target );
479 IDropTarget_Release( target );
481 return r;
484 static HRESULT WINAPI DropTargetWrapper_Drop(IDropTarget* iface,
485 IDataObject* pDataObj,
486 DWORD grfKeyState,
487 POINTL pt,
488 DWORD* pdwEffect)
490 IDropTarget *target;
491 HRESULT r = get_target_from_wrapper( iface, &target );
493 if (SUCCEEDED( r ))
495 r = IDropTarget_Drop( target, pDataObj, grfKeyState, pt, pdwEffect );
496 IDropTarget_Release( target );
498 return r;
501 static const IDropTargetVtbl DropTargetWrapperVTbl =
503 DropTargetWrapper_QueryInterface,
504 DropTargetWrapper_AddRef,
505 DropTargetWrapper_Release,
506 DropTargetWrapper_DragEnter,
507 DropTargetWrapper_DragOver,
508 DropTargetWrapper_DragLeave,
509 DropTargetWrapper_Drop
512 static IDropTarget* WrapDropTarget( HWND hwnd )
514 DropTargetWrapper* This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
516 if (This)
518 This->IDropTarget_iface.lpVtbl = &DropTargetWrapperVTbl;
519 This->hwnd = hwnd;
520 This->refs = 1;
522 return &This->IDropTarget_iface;
525 /***********************************************************************
526 * get_droptarget_pointer
528 * Retrieves the marshalled IDropTarget from the window.
530 static IDropTarget* get_droptarget_pointer(HWND hwnd)
532 IDropTarget *droptarget = NULL;
533 HANDLE map;
534 IStream *stream;
536 map = get_droptarget_local_handle(hwnd);
537 if(!map) return NULL;
539 if(SUCCEEDED(create_stream_from_map(map, &stream)))
541 CoUnmarshalInterface(stream, &IID_IDropTarget, (void**)&droptarget);
542 IStream_Release(stream);
544 CloseHandle(map);
545 return droptarget;
548 /***********************************************************************
549 * RegisterDragDrop (OLE32.@)
551 HRESULT WINAPI RegisterDragDrop(HWND hwnd, LPDROPTARGET pDropTarget)
553 DWORD pid = 0;
554 HRESULT hr;
555 IStream *stream;
556 HANDLE map;
557 IDropTarget *wrapper;
559 TRACE("(%p,%p)\n", hwnd, pDropTarget);
561 if (!COM_CurrentApt())
563 ERR("COM not initialized\n");
564 return E_OUTOFMEMORY;
567 if (!pDropTarget)
568 return E_INVALIDARG;
570 if (!IsWindow(hwnd))
572 ERR("invalid hwnd %p\n", hwnd);
573 return DRAGDROP_E_INVALIDHWND;
576 /* block register for other processes windows */
577 GetWindowThreadProcessId(hwnd, &pid);
578 if (pid != GetCurrentProcessId())
580 FIXME("register for another process windows is disabled\n");
581 return DRAGDROP_E_INVALIDHWND;
584 /* check if the window is already registered */
585 if (is_droptarget(hwnd))
586 return DRAGDROP_E_ALREADYREGISTERED;
589 * Marshal the drop target pointer into a shared memory map and
590 * store the map's handle in a Wine specific window prop. We also
591 * store the drop target pointer itself in the
592 * "OleDropTargetInterface" prop for compatibility with Windows.
595 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
596 if(FAILED(hr)) return hr;
598 /* IDropTarget::QueryInterface() shouldn't be called, some (broken) apps depend on this. */
599 wrapper = WrapDropTarget( hwnd );
600 if(!wrapper)
602 IStream_Release(stream);
603 return E_OUTOFMEMORY;
605 hr = CoMarshalInterface(stream, &IID_IDropTarget, (IUnknown*)wrapper, MSHCTX_LOCAL, NULL, MSHLFLAGS_TABLESTRONG);
606 IDropTarget_Release(wrapper);
608 if(SUCCEEDED(hr))
610 hr = create_map_from_stream(stream, &map);
611 if(SUCCEEDED(hr))
613 IDropTarget_AddRef(pDropTarget);
614 SetPropW(hwnd, prop_oledroptarget, pDropTarget);
615 SetPropW(hwnd, prop_marshalleddroptarget, map);
617 else
619 LARGE_INTEGER zero;
620 zero.QuadPart = 0;
621 IStream_Seek(stream, zero, STREAM_SEEK_SET, NULL);
622 CoReleaseMarshalData(stream);
625 IStream_Release(stream);
627 return hr;
630 /***********************************************************************
631 * RevokeDragDrop (OLE32.@)
633 HRESULT WINAPI RevokeDragDrop(HWND hwnd)
635 HANDLE map;
636 IStream *stream;
637 IDropTarget *drop_target;
638 HRESULT hr;
640 TRACE("(%p)\n", hwnd);
642 if (!IsWindow(hwnd))
644 ERR("invalid hwnd %p\n", hwnd);
645 return DRAGDROP_E_INVALIDHWND;
648 /* no registration data */
649 if (!(map = get_droptarget_handle(hwnd)))
650 return DRAGDROP_E_NOTREGISTERED;
652 drop_target = GetPropW(hwnd, prop_oledroptarget);
653 if(drop_target) IDropTarget_Release(drop_target);
655 RemovePropW(hwnd, prop_oledroptarget);
656 RemovePropW(hwnd, prop_marshalleddroptarget);
658 hr = create_stream_from_map(map, &stream);
659 if(SUCCEEDED(hr))
661 CoReleaseMarshalData(stream);
662 IStream_Release(stream);
664 CloseHandle(map);
666 return hr;
669 /***********************************************************************
670 * OleRegGetUserType (OLE32.@)
672 * This implementation of OleRegGetUserType ignores the dwFormOfType
673 * parameter and always returns the full name of the object. This is
674 * not too bad since this is the case for many objects because of the
675 * way they are registered.
677 HRESULT WINAPI OleRegGetUserType(
678 REFCLSID clsid,
679 DWORD dwFormOfType,
680 LPOLESTR* pszUserType)
682 WCHAR keyName[60];
683 DWORD dwKeyType;
684 DWORD cbData;
685 HKEY clsidKey;
686 LONG hres;
689 * Initialize the out parameter.
691 *pszUserType = NULL;
694 * Build the key name we're looking for
696 sprintfW( keyName, clsidfmtW,
697 clsid->Data1, clsid->Data2, clsid->Data3,
698 clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
699 clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );
701 TRACE("(%s, %d, %p)\n", debugstr_w(keyName), dwFormOfType, pszUserType);
704 * Open the class id Key
706 hres = open_classes_key(HKEY_CLASSES_ROOT, keyName, MAXIMUM_ALLOWED, &clsidKey);
707 if (hres != ERROR_SUCCESS)
708 return REGDB_E_CLASSNOTREG;
711 * Retrieve the size of the name string.
713 cbData = 0;
715 hres = RegQueryValueExW(clsidKey,
716 emptyW,
717 NULL,
718 &dwKeyType,
719 NULL,
720 &cbData);
722 if (hres!=ERROR_SUCCESS)
724 RegCloseKey(clsidKey);
725 return REGDB_E_READREGDB;
729 * Allocate a buffer for the registry value.
731 *pszUserType = CoTaskMemAlloc(cbData);
733 if (*pszUserType==NULL)
735 RegCloseKey(clsidKey);
736 return E_OUTOFMEMORY;
739 hres = RegQueryValueExW(clsidKey,
740 emptyW,
741 NULL,
742 &dwKeyType,
743 (LPBYTE) *pszUserType,
744 &cbData);
746 RegCloseKey(clsidKey);
748 if (hres != ERROR_SUCCESS)
750 CoTaskMemFree(*pszUserType);
751 *pszUserType = NULL;
753 return REGDB_E_READREGDB;
756 return S_OK;
759 /***********************************************************************
760 * DoDragDrop [OLE32.@]
762 HRESULT WINAPI DoDragDrop (
763 IDataObject *pDataObject, /* [in] ptr to the data obj */
764 IDropSource* pDropSource, /* [in] ptr to the source obj */
765 DWORD dwOKEffect, /* [in] effects allowed by the source */
766 DWORD *pdwEffect) /* [out] ptr to effects of the source */
768 static const WCHAR trackerW[] = {'T','r','a','c','k','e','r','W','i','n','d','o','w',0};
769 TrackerWindowInfo trackerInfo;
770 HWND hwndTrackWindow;
771 MSG msg;
773 TRACE("(%p, %p, %08x, %p)\n", pDataObject, pDropSource, dwOKEffect, pdwEffect);
775 if (!pDataObject || !pDropSource || !pdwEffect)
776 return E_INVALIDARG;
779 * Setup the drag n drop tracking window.
782 trackerInfo.dataObject = pDataObject;
783 trackerInfo.dropSource = pDropSource;
784 trackerInfo.dwOKEffect = dwOKEffect;
785 trackerInfo.pdwEffect = pdwEffect;
786 trackerInfo.trackingDone = FALSE;
787 trackerInfo.escPressed = FALSE;
788 trackerInfo.curDragTargetHWND = 0;
789 trackerInfo.curTargetHWND = 0;
790 trackerInfo.curDragTarget = 0;
792 hwndTrackWindow = CreateWindowW(OLEDD_DRAGTRACKERCLASS, trackerW,
793 WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT,
794 CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, 0,
795 &trackerInfo);
797 if (hwndTrackWindow)
800 * Capture the mouse input
802 SetCapture(hwndTrackWindow);
804 msg.message = 0;
807 * Pump messages. All mouse input should go to the capture window.
809 while (!trackerInfo.trackingDone && GetMessageW(&msg, 0, 0, 0) )
811 trackerInfo.curMousePos.x = msg.pt.x;
812 trackerInfo.curMousePos.y = msg.pt.y;
813 trackerInfo.dwKeyState = OLEDD_GetButtonState();
815 if ( (msg.message >= WM_KEYFIRST) &&
816 (msg.message <= WM_KEYLAST) )
819 * When keyboard messages are sent to windows on this thread, we
820 * want to ignore notify the drop source that the state changed.
821 * in the case of the Escape key, we also notify the drop source
822 * we give it a special meaning.
824 if ( (msg.message==WM_KEYDOWN) &&
825 (msg.wParam==VK_ESCAPE) )
827 trackerInfo.escPressed = TRUE;
831 * Notify the drop source.
833 OLEDD_TrackStateChange(&trackerInfo);
835 else
838 * Dispatch the messages only when it's not a keyboard message.
840 DispatchMessageW(&msg);
844 /* re-post the quit message to outer message loop */
845 if (msg.message == WM_QUIT)
846 PostQuitMessage(msg.wParam);
848 * Destroy the temporary window.
850 DestroyWindow(hwndTrackWindow);
852 return trackerInfo.returnValue;
855 return E_FAIL;
858 /***********************************************************************
859 * OleQueryLinkFromData [OLE32.@]
861 HRESULT WINAPI OleQueryLinkFromData(
862 IDataObject* pSrcDataObject)
864 FIXME("(%p),stub!\n", pSrcDataObject);
865 return S_FALSE;
868 /***********************************************************************
869 * OleRegGetMiscStatus [OLE32.@]
871 HRESULT WINAPI OleRegGetMiscStatus(
872 REFCLSID clsid,
873 DWORD dwAspect,
874 DWORD* pdwStatus)
876 static const WCHAR miscstatusW[] = {'M','i','s','c','S','t','a','t','u','s',0};
877 static const WCHAR dfmtW[] = {'%','d',0};
878 WCHAR keyName[60];
879 HKEY clsidKey;
880 HKEY miscStatusKey;
881 HKEY aspectKey;
882 LONG result;
885 * Build the key name we're looking for
887 sprintfW( keyName, clsidfmtW,
888 clsid->Data1, clsid->Data2, clsid->Data3,
889 clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
890 clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );
892 TRACE("(%s, %d, %p)\n", debugstr_w(keyName), dwAspect, pdwStatus);
894 if (!pdwStatus) return E_INVALIDARG;
896 *pdwStatus = 0;
898 if (actctx_get_miscstatus(clsid, dwAspect, pdwStatus)) return S_OK;
901 * Open the class id Key
903 result = open_classes_key(HKEY_CLASSES_ROOT, keyName, MAXIMUM_ALLOWED, &clsidKey);
904 if (result != ERROR_SUCCESS)
905 return REGDB_E_CLASSNOTREG;
908 * Get the MiscStatus
910 result = open_classes_key(clsidKey, miscstatusW, MAXIMUM_ALLOWED, &miscStatusKey);
911 if (result != ERROR_SUCCESS)
913 RegCloseKey(clsidKey);
914 return S_OK;
918 * Read the default value
920 OLEUTL_ReadRegistryDWORDValue(miscStatusKey, pdwStatus);
923 * Open the key specific to the requested aspect.
925 sprintfW(keyName, dfmtW, dwAspect);
927 result = open_classes_key(miscStatusKey, keyName, MAXIMUM_ALLOWED, &aspectKey);
928 if (result == ERROR_SUCCESS)
930 OLEUTL_ReadRegistryDWORDValue(aspectKey, pdwStatus);
931 RegCloseKey(aspectKey);
935 * Cleanup
937 RegCloseKey(miscStatusKey);
938 RegCloseKey(clsidKey);
940 return S_OK;
943 static HRESULT EnumOLEVERB_Construct(HKEY hkeyVerb, ULONG index, IEnumOLEVERB **ppenum);
945 typedef struct
947 IEnumOLEVERB IEnumOLEVERB_iface;
948 LONG ref;
950 HKEY hkeyVerb;
951 ULONG index;
952 } EnumOLEVERB;
954 static inline EnumOLEVERB *impl_from_IEnumOLEVERB(IEnumOLEVERB *iface)
956 return CONTAINING_RECORD(iface, EnumOLEVERB, IEnumOLEVERB_iface);
959 static HRESULT WINAPI EnumOLEVERB_QueryInterface(
960 IEnumOLEVERB *iface, REFIID riid, void **ppv)
962 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
963 if (IsEqualIID(riid, &IID_IUnknown) ||
964 IsEqualIID(riid, &IID_IEnumOLEVERB))
966 IEnumOLEVERB_AddRef(iface);
967 *ppv = iface;
968 return S_OK;
970 return E_NOINTERFACE;
973 static ULONG WINAPI EnumOLEVERB_AddRef(
974 IEnumOLEVERB *iface)
976 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
977 TRACE("()\n");
978 return InterlockedIncrement(&This->ref);
981 static ULONG WINAPI EnumOLEVERB_Release(
982 IEnumOLEVERB *iface)
984 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
985 LONG refs = InterlockedDecrement(&This->ref);
986 TRACE("()\n");
987 if (!refs)
989 RegCloseKey(This->hkeyVerb);
990 HeapFree(GetProcessHeap(), 0, This);
992 return refs;
995 static HRESULT WINAPI EnumOLEVERB_Next(
996 IEnumOLEVERB *iface, ULONG celt, LPOLEVERB rgelt,
997 ULONG *pceltFetched)
999 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
1000 HRESULT hr = S_OK;
1002 TRACE("(%d, %p, %p)\n", celt, rgelt, pceltFetched);
1004 if (pceltFetched)
1005 *pceltFetched = 0;
1007 for (; celt; celt--, rgelt++)
1009 WCHAR wszSubKey[20];
1010 LONG cbData;
1011 LPWSTR pwszOLEVERB;
1012 LPWSTR pwszMenuFlags;
1013 LPWSTR pwszAttribs;
1014 LONG res = RegEnumKeyW(This->hkeyVerb, This->index, wszSubKey, sizeof(wszSubKey)/sizeof(wszSubKey[0]));
1015 if (res == ERROR_NO_MORE_ITEMS)
1017 hr = S_FALSE;
1018 break;
1020 else if (res != ERROR_SUCCESS)
1022 ERR("RegEnumKeyW failed with error %d\n", res);
1023 hr = REGDB_E_READREGDB;
1024 break;
1026 res = RegQueryValueW(This->hkeyVerb, wszSubKey, NULL, &cbData);
1027 if (res != ERROR_SUCCESS)
1029 ERR("RegQueryValueW failed with error %d\n", res);
1030 hr = REGDB_E_READREGDB;
1031 break;
1033 pwszOLEVERB = CoTaskMemAlloc(cbData);
1034 if (!pwszOLEVERB)
1036 hr = E_OUTOFMEMORY;
1037 break;
1039 res = RegQueryValueW(This->hkeyVerb, wszSubKey, pwszOLEVERB, &cbData);
1040 if (res != ERROR_SUCCESS)
1042 ERR("RegQueryValueW failed with error %d\n", res);
1043 hr = REGDB_E_READREGDB;
1044 CoTaskMemFree(pwszOLEVERB);
1045 break;
1048 TRACE("verb string: %s\n", debugstr_w(pwszOLEVERB));
1049 pwszMenuFlags = strchrW(pwszOLEVERB, ',');
1050 if (!pwszMenuFlags)
1052 hr = OLEOBJ_E_INVALIDVERB;
1053 CoTaskMemFree(pwszOLEVERB);
1054 break;
1056 /* nul terminate the name string and advance to first character */
1057 *pwszMenuFlags = '\0';
1058 pwszMenuFlags++;
1059 pwszAttribs = strchrW(pwszMenuFlags, ',');
1060 if (!pwszAttribs)
1062 hr = OLEOBJ_E_INVALIDVERB;
1063 CoTaskMemFree(pwszOLEVERB);
1064 break;
1066 /* nul terminate the menu string and advance to first character */
1067 *pwszAttribs = '\0';
1068 pwszAttribs++;
1070 /* fill out structure for this verb */
1071 rgelt->lVerb = atolW(wszSubKey);
1072 rgelt->lpszVerbName = pwszOLEVERB; /* user should free */
1073 rgelt->fuFlags = atolW(pwszMenuFlags);
1074 rgelt->grfAttribs = atolW(pwszAttribs);
1076 if (pceltFetched)
1077 (*pceltFetched)++;
1078 This->index++;
1080 return hr;
1083 static HRESULT WINAPI EnumOLEVERB_Skip(
1084 IEnumOLEVERB *iface, ULONG celt)
1086 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
1088 TRACE("(%d)\n", celt);
1090 This->index += celt;
1091 return S_OK;
1094 static HRESULT WINAPI EnumOLEVERB_Reset(
1095 IEnumOLEVERB *iface)
1097 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
1099 TRACE("()\n");
1101 This->index = 0;
1102 return S_OK;
1105 static HRESULT WINAPI EnumOLEVERB_Clone(
1106 IEnumOLEVERB *iface,
1107 IEnumOLEVERB **ppenum)
1109 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
1110 HKEY hkeyVerb;
1111 TRACE("(%p)\n", ppenum);
1112 if (!DuplicateHandle(GetCurrentProcess(), This->hkeyVerb, GetCurrentProcess(), (HANDLE *)&hkeyVerb, 0, FALSE, DUPLICATE_SAME_ACCESS))
1113 return HRESULT_FROM_WIN32(GetLastError());
1114 return EnumOLEVERB_Construct(hkeyVerb, This->index, ppenum);
1117 static const IEnumOLEVERBVtbl EnumOLEVERB_VTable =
1119 EnumOLEVERB_QueryInterface,
1120 EnumOLEVERB_AddRef,
1121 EnumOLEVERB_Release,
1122 EnumOLEVERB_Next,
1123 EnumOLEVERB_Skip,
1124 EnumOLEVERB_Reset,
1125 EnumOLEVERB_Clone
1128 static HRESULT EnumOLEVERB_Construct(HKEY hkeyVerb, ULONG index, IEnumOLEVERB **ppenum)
1130 EnumOLEVERB *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1131 if (!This)
1133 RegCloseKey(hkeyVerb);
1134 return E_OUTOFMEMORY;
1136 This->IEnumOLEVERB_iface.lpVtbl = &EnumOLEVERB_VTable;
1137 This->ref = 1;
1138 This->index = index;
1139 This->hkeyVerb = hkeyVerb;
1140 *ppenum = &This->IEnumOLEVERB_iface;
1141 return S_OK;
1144 /***********************************************************************
1145 * OleRegEnumVerbs [OLE32.@]
1147 * Enumerates verbs associated with a class stored in the registry.
1149 * PARAMS
1150 * clsid [I] Class ID to enumerate the verbs for.
1151 * ppenum [O] Enumerator.
1153 * RETURNS
1154 * S_OK: Success.
1155 * REGDB_E_CLASSNOTREG: The specified class does not have a key in the registry.
1156 * REGDB_E_READREGDB: The class key could not be opened for some other reason.
1157 * OLE_E_REGDB_KEY: The Verb subkey for the class is not present.
1158 * OLEOBJ_E_NOVERBS: The Verb subkey for the class is empty.
1160 HRESULT WINAPI OleRegEnumVerbs (REFCLSID clsid, LPENUMOLEVERB* ppenum)
1162 LONG res;
1163 HKEY hkeyVerb;
1164 DWORD dwSubKeys;
1165 static const WCHAR wszVerb[] = {'V','e','r','b',0};
1167 TRACE("(%s, %p)\n", debugstr_guid(clsid), ppenum);
1169 res = COM_OpenKeyForCLSID(clsid, wszVerb, KEY_READ, &hkeyVerb);
1170 if (FAILED(res))
1172 if (res == REGDB_E_CLASSNOTREG)
1173 ERR("CLSID %s not registered\n", debugstr_guid(clsid));
1174 else if (res == REGDB_E_KEYMISSING)
1175 ERR("no Verbs key for class %s\n", debugstr_guid(clsid));
1176 else
1177 ERR("failed to open Verbs key for CLSID %s with error %d\n",
1178 debugstr_guid(clsid), res);
1179 return res;
1182 res = RegQueryInfoKeyW(hkeyVerb, NULL, NULL, NULL, &dwSubKeys, NULL,
1183 NULL, NULL, NULL, NULL, NULL, NULL);
1184 if (res != ERROR_SUCCESS)
1186 ERR("failed to get subkey count with error %d\n", GetLastError());
1187 return REGDB_E_READREGDB;
1190 if (!dwSubKeys)
1192 WARN("class %s has no verbs\n", debugstr_guid(clsid));
1193 RegCloseKey(hkeyVerb);
1194 return OLEOBJ_E_NOVERBS;
1197 return EnumOLEVERB_Construct(hkeyVerb, 0, ppenum);
1200 /******************************************************************************
1201 * OleSetContainedObject [OLE32.@]
1203 HRESULT WINAPI OleSetContainedObject(
1204 LPUNKNOWN pUnknown,
1205 BOOL fContained)
1207 IRunnableObject* runnable = NULL;
1208 HRESULT hres;
1210 TRACE("(%p,%x)\n", pUnknown, fContained);
1212 hres = IUnknown_QueryInterface(pUnknown,
1213 &IID_IRunnableObject,
1214 (void**)&runnable);
1216 if (SUCCEEDED(hres))
1218 hres = IRunnableObject_SetContainedObject(runnable, fContained);
1220 IRunnableObject_Release(runnable);
1222 return hres;
1225 return S_OK;
1228 /******************************************************************************
1229 * OleRun [OLE32.@]
1231 * Set the OLE object to the running state.
1233 * PARAMS
1234 * pUnknown [I] OLE object to run.
1236 * RETURNS
1237 * Success: S_OK.
1238 * Failure: Any HRESULT code.
1240 HRESULT WINAPI OleRun(LPUNKNOWN pUnknown)
1242 IRunnableObject *runable;
1243 HRESULT hres;
1245 TRACE("(%p)\n", pUnknown);
1247 hres = IUnknown_QueryInterface(pUnknown, &IID_IRunnableObject, (void**)&runable);
1248 if (FAILED(hres))
1249 return S_OK; /* Appears to return no error. */
1251 hres = IRunnableObject_Run(runable, NULL);
1252 IRunnableObject_Release(runable);
1253 return hres;
1256 /******************************************************************************
1257 * OleLoad [OLE32.@]
1259 HRESULT WINAPI OleLoad(
1260 LPSTORAGE pStg,
1261 REFIID riid,
1262 LPOLECLIENTSITE pClientSite,
1263 LPVOID* ppvObj)
1265 IPersistStorage* persistStorage = NULL;
1266 IUnknown* pUnk;
1267 IOleObject* pOleObject = NULL;
1268 STATSTG storageInfo;
1269 HRESULT hres;
1271 TRACE("(%p, %s, %p, %p)\n", pStg, debugstr_guid(riid), pClientSite, ppvObj);
1273 *ppvObj = NULL;
1276 * TODO, Conversion ... OleDoAutoConvert
1280 * Get the class ID for the object.
1282 hres = IStorage_Stat(pStg, &storageInfo, STATFLAG_NONAME);
1285 * Now, try and create the handler for the object
1287 hres = CoCreateInstance(&storageInfo.clsid,
1288 NULL,
1289 CLSCTX_INPROC_HANDLER|CLSCTX_INPROC_SERVER,
1290 riid,
1291 (void**)&pUnk);
1294 * If that fails, as it will most times, load the default
1295 * OLE handler.
1297 if (FAILED(hres))
1299 hres = OleCreateDefaultHandler(&storageInfo.clsid,
1300 NULL,
1301 riid,
1302 (void**)&pUnk);
1306 * If we couldn't find a handler... this is bad. Abort the whole thing.
1308 if (FAILED(hres))
1309 return hres;
1311 if (pClientSite)
1313 hres = IUnknown_QueryInterface(pUnk, &IID_IOleObject, (void **)&pOleObject);
1314 if (SUCCEEDED(hres))
1316 DWORD dwStatus;
1317 hres = IOleObject_GetMiscStatus(pOleObject, DVASPECT_CONTENT, &dwStatus);
1322 * Initialize the object with its IPersistStorage interface.
1324 hres = IUnknown_QueryInterface(pUnk, &IID_IPersistStorage, (void**)&persistStorage);
1325 if (SUCCEEDED(hres))
1327 hres = IPersistStorage_Load(persistStorage, pStg);
1329 IPersistStorage_Release(persistStorage);
1330 persistStorage = NULL;
1333 if (SUCCEEDED(hres) && pClientSite)
1335 * Inform the new object of its client site.
1337 hres = IOleObject_SetClientSite(pOleObject, pClientSite);
1340 * Cleanup interfaces used internally
1342 if (pOleObject)
1343 IOleObject_Release(pOleObject);
1345 if (SUCCEEDED(hres))
1347 IOleLink *pOleLink;
1348 HRESULT hres1;
1349 hres1 = IUnknown_QueryInterface(pUnk, &IID_IOleLink, (void **)&pOleLink);
1350 if (SUCCEEDED(hres1))
1352 FIXME("handle OLE link\n");
1353 IOleLink_Release(pOleLink);
1357 if (FAILED(hres))
1359 IUnknown_Release(pUnk);
1360 pUnk = NULL;
1363 *ppvObj = pUnk;
1365 return hres;
1368 /***********************************************************************
1369 * OleSave [OLE32.@]
1371 HRESULT WINAPI OleSave(
1372 LPPERSISTSTORAGE pPS,
1373 LPSTORAGE pStg,
1374 BOOL fSameAsLoad)
1376 HRESULT hres;
1377 CLSID objectClass;
1379 TRACE("(%p,%p,%x)\n", pPS, pStg, fSameAsLoad);
1382 * First, we transfer the class ID (if available)
1384 hres = IPersistStorage_GetClassID(pPS, &objectClass);
1386 if (SUCCEEDED(hres))
1388 WriteClassStg(pStg, &objectClass);
1392 * Then, we ask the object to save itself to the
1393 * storage. If it is successful, we commit the storage.
1395 hres = IPersistStorage_Save(pPS, pStg, fSameAsLoad);
1397 if (SUCCEEDED(hres))
1399 IStorage_Commit(pStg,
1400 STGC_DEFAULT);
1403 return hres;
1407 /******************************************************************************
1408 * OleLockRunning [OLE32.@]
1410 HRESULT WINAPI OleLockRunning(LPUNKNOWN pUnknown, BOOL fLock, BOOL fLastUnlockCloses)
1412 IRunnableObject* runnable = NULL;
1413 HRESULT hres;
1415 TRACE("(%p,%x,%x)\n", pUnknown, fLock, fLastUnlockCloses);
1417 hres = IUnknown_QueryInterface(pUnknown,
1418 &IID_IRunnableObject,
1419 (void**)&runnable);
1421 if (SUCCEEDED(hres))
1423 hres = IRunnableObject_LockRunning(runnable, fLock, fLastUnlockCloses);
1425 IRunnableObject_Release(runnable);
1427 return hres;
1430 return S_OK;
1434 /**************************************************************************
1435 * Internal methods to manage the shared OLE menu in response to the
1436 * OLE***MenuDescriptor API
1439 /***
1440 * OLEMenu_Initialize()
1442 * Initializes the OLEMENU data structures.
1444 static void OLEMenu_Initialize(void)
1448 /***
1449 * OLEMenu_UnInitialize()
1451 * Releases the OLEMENU data structures.
1453 static void OLEMenu_UnInitialize(void)
1457 /*************************************************************************
1458 * OLEMenu_InstallHooks
1459 * Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
1461 * RETURNS: TRUE if message hooks were successfully installed
1462 * FALSE on failure
1464 static BOOL OLEMenu_InstallHooks( DWORD tid )
1466 OleMenuHookItem *pHookItem;
1468 /* Create an entry for the hook table */
1469 if ( !(pHookItem = HeapAlloc(GetProcessHeap(), 0,
1470 sizeof(OleMenuHookItem)) ) )
1471 return FALSE;
1473 pHookItem->tid = tid;
1474 pHookItem->hHeap = GetProcessHeap();
1475 pHookItem->CallWndProc_hHook = NULL;
1477 /* Install a thread scope message hook for WH_GETMESSAGE */
1478 pHookItem->GetMsg_hHook = SetWindowsHookExW( WH_GETMESSAGE, OLEMenu_GetMsgProc,
1479 0, GetCurrentThreadId() );
1480 if ( !pHookItem->GetMsg_hHook )
1481 goto CLEANUP;
1483 /* Install a thread scope message hook for WH_CALLWNDPROC */
1484 pHookItem->CallWndProc_hHook = SetWindowsHookExW( WH_CALLWNDPROC, OLEMenu_CallWndProc,
1485 0, GetCurrentThreadId() );
1486 if ( !pHookItem->CallWndProc_hHook )
1487 goto CLEANUP;
1489 /* Insert the hook table entry */
1490 pHookItem->next = hook_list;
1491 hook_list = pHookItem;
1493 return TRUE;
1495 CLEANUP:
1496 /* Unhook any hooks */
1497 if ( pHookItem->GetMsg_hHook )
1498 UnhookWindowsHookEx( pHookItem->GetMsg_hHook );
1499 if ( pHookItem->CallWndProc_hHook )
1500 UnhookWindowsHookEx( pHookItem->CallWndProc_hHook );
1501 /* Release the hook table entry */
1502 HeapFree(pHookItem->hHeap, 0, pHookItem );
1504 return FALSE;
1507 /*************************************************************************
1508 * OLEMenu_UnInstallHooks
1509 * UnInstall thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
1511 * RETURNS: TRUE if message hooks were successfully installed
1512 * FALSE on failure
1514 static BOOL OLEMenu_UnInstallHooks( DWORD tid )
1516 OleMenuHookItem *pHookItem = NULL;
1517 OleMenuHookItem **ppHook = &hook_list;
1519 while (*ppHook)
1521 if ((*ppHook)->tid == tid)
1523 pHookItem = *ppHook;
1524 *ppHook = pHookItem->next;
1525 break;
1527 ppHook = &(*ppHook)->next;
1529 if (!pHookItem) return FALSE;
1531 /* Uninstall the hooks installed for this thread */
1532 if ( !UnhookWindowsHookEx( pHookItem->GetMsg_hHook ) )
1533 goto CLEANUP;
1534 if ( !UnhookWindowsHookEx( pHookItem->CallWndProc_hHook ) )
1535 goto CLEANUP;
1537 /* Release the hook table entry */
1538 HeapFree(pHookItem->hHeap, 0, pHookItem );
1540 return TRUE;
1542 CLEANUP:
1543 /* Release the hook table entry */
1544 HeapFree(pHookItem->hHeap, 0, pHookItem );
1546 return FALSE;
1549 /*************************************************************************
1550 * OLEMenu_IsHookInstalled
1551 * Tests if OLEMenu hooks have been installed for a thread
1553 * RETURNS: The pointer and index of the hook table entry for the tid
1554 * NULL and -1 for the index if no hooks were installed for this thread
1556 static OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid )
1558 OleMenuHookItem *pHookItem;
1560 /* Do a simple linear search for an entry whose tid matches ours.
1561 * We really need a map but efficiency is not a concern here. */
1562 for (pHookItem = hook_list; pHookItem; pHookItem = pHookItem->next)
1564 if ( tid == pHookItem->tid )
1565 return pHookItem;
1568 return NULL;
1571 /***********************************************************************
1572 * OLEMenu_FindMainMenuIndex
1574 * Used by OLEMenu API to find the top level group a menu item belongs to.
1575 * On success pnPos contains the index of the item in the top level menu group
1577 * RETURNS: TRUE if the ID was found, FALSE on failure
1579 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos )
1581 INT i, nItems;
1583 nItems = GetMenuItemCount( hMainMenu );
1585 for (i = 0; i < nItems; i++)
1587 HMENU hsubmenu;
1589 /* Is the current item a submenu? */
1590 if ( (hsubmenu = GetSubMenu(hMainMenu, i)) )
1592 /* If the handle is the same we're done */
1593 if ( hsubmenu == hPopupMenu )
1595 if (pnPos)
1596 *pnPos = i;
1597 return TRUE;
1599 /* Recursively search without updating pnPos */
1600 else if ( OLEMenu_FindMainMenuIndex( hsubmenu, hPopupMenu, NULL ) )
1602 if (pnPos)
1603 *pnPos = i;
1604 return TRUE;
1609 return FALSE;
1612 /***********************************************************************
1613 * OLEMenu_SetIsServerMenu
1615 * Checks whether a popup menu belongs to a shared menu group which is
1616 * owned by the server, and sets the menu descriptor state accordingly.
1617 * All menu messages from these groups should be routed to the server.
1619 * RETURNS: TRUE if the popup menu is part of a server owned group
1620 * FALSE if the popup menu is part of a container owned group
1622 static BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor )
1624 UINT nPos = 0, nWidth, i;
1626 pOleMenuDescriptor->bIsServerItem = FALSE;
1628 /* Don't bother searching if the popup is the combined menu itself */
1629 if ( hmenu == pOleMenuDescriptor->hmenuCombined )
1630 return FALSE;
1632 /* Find the menu item index in the shared OLE menu that this item belongs to */
1633 if ( !OLEMenu_FindMainMenuIndex( pOleMenuDescriptor->hmenuCombined, hmenu, &nPos ) )
1634 return FALSE;
1636 /* The group widths array has counts for the number of elements
1637 * in the groups File, Edit, Container, Object, Window, Help.
1638 * The Edit, Object & Help groups belong to the server object
1639 * and the other three belong to the container.
1640 * Loop through the group widths and locate the group we are a member of.
1642 for ( i = 0, nWidth = 0; i < 6; i++ )
1644 nWidth += pOleMenuDescriptor->mgw.width[i];
1645 if ( nPos < nWidth )
1647 /* Odd elements are server menu widths */
1648 pOleMenuDescriptor->bIsServerItem = i%2;
1649 break;
1653 return pOleMenuDescriptor->bIsServerItem;
1656 /*************************************************************************
1657 * OLEMenu_CallWndProc
1658 * Thread scope WH_CALLWNDPROC hook proc filter function (callback)
1659 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1661 static LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam)
1663 LPCWPSTRUCT pMsg;
1664 HOLEMENU hOleMenu = 0;
1665 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1666 OleMenuHookItem *pHookItem = NULL;
1667 WORD fuFlags;
1669 TRACE("%i, %04lx, %08lx\n", code, wParam, lParam );
1671 /* Check if we're being asked to process the message */
1672 if ( HC_ACTION != code )
1673 goto NEXTHOOK;
1675 /* Retrieve the current message being dispatched from lParam */
1676 pMsg = (LPCWPSTRUCT)lParam;
1678 /* Check if the message is destined for a window we are interested in:
1679 * If the window has an OLEMenu property we may need to dispatch
1680 * the menu message to its active objects window instead. */
1682 hOleMenu = GetPropW( pMsg->hwnd, prop_olemenuW );
1683 if ( !hOleMenu )
1684 goto NEXTHOOK;
1686 /* Get the menu descriptor */
1687 pOleMenuDescriptor = GlobalLock( hOleMenu );
1688 if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1689 goto NEXTHOOK;
1691 /* Process menu messages */
1692 switch( pMsg->message )
1694 case WM_INITMENU:
1696 /* Reset the menu descriptor state */
1697 pOleMenuDescriptor->bIsServerItem = FALSE;
1699 /* Send this message to the server as well */
1700 SendMessageW( pOleMenuDescriptor->hwndActiveObject,
1701 pMsg->message, pMsg->wParam, pMsg->lParam );
1702 goto NEXTHOOK;
1705 case WM_INITMENUPOPUP:
1707 /* Save the state for whether this is a server owned menu */
1708 OLEMenu_SetIsServerMenu( (HMENU)pMsg->wParam, pOleMenuDescriptor );
1709 break;
1712 case WM_MENUSELECT:
1714 fuFlags = HIWORD(pMsg->wParam); /* Get flags */
1715 if ( fuFlags & MF_SYSMENU )
1716 goto NEXTHOOK;
1718 /* Save the state for whether this is a server owned popup menu */
1719 else if ( fuFlags & MF_POPUP )
1720 OLEMenu_SetIsServerMenu( (HMENU)pMsg->lParam, pOleMenuDescriptor );
1722 break;
1725 case WM_DRAWITEM:
1727 LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT) pMsg->lParam;
1728 if ( pMsg->wParam != 0 || lpdis->CtlType != ODT_MENU )
1729 goto NEXTHOOK; /* Not a menu message */
1731 break;
1734 default:
1735 goto NEXTHOOK;
1738 /* If the message was for the server dispatch it accordingly */
1739 if ( pOleMenuDescriptor->bIsServerItem )
1741 SendMessageW( pOleMenuDescriptor->hwndActiveObject,
1742 pMsg->message, pMsg->wParam, pMsg->lParam );
1745 NEXTHOOK:
1746 if ( pOleMenuDescriptor )
1747 GlobalUnlock( hOleMenu );
1749 /* Lookup the hook item for the current thread */
1750 if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1752 /* This should never fail!! */
1753 WARN("could not retrieve hHook for current thread!\n" );
1754 return 0;
1757 /* Pass on the message to the next hooker */
1758 return CallNextHookEx( pHookItem->CallWndProc_hHook, code, wParam, lParam );
1761 /*************************************************************************
1762 * OLEMenu_GetMsgProc
1763 * Thread scope WH_GETMESSAGE hook proc filter function (callback)
1764 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1766 static LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam)
1768 LPMSG pMsg;
1769 HOLEMENU hOleMenu = 0;
1770 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1771 OleMenuHookItem *pHookItem = NULL;
1772 WORD wCode;
1774 TRACE("%i, %04lx, %08lx\n", code, wParam, lParam );
1776 /* Check if we're being asked to process a messages */
1777 if ( HC_ACTION != code )
1778 goto NEXTHOOK;
1780 /* Retrieve the current message being dispatched from lParam */
1781 pMsg = (LPMSG)lParam;
1783 /* Check if the message is destined for a window we are interested in:
1784 * If the window has an OLEMenu property we may need to dispatch
1785 * the menu message to its active objects window instead. */
1787 hOleMenu = GetPropW( pMsg->hwnd, prop_olemenuW );
1788 if ( !hOleMenu )
1789 goto NEXTHOOK;
1791 /* Process menu messages */
1792 switch( pMsg->message )
1794 case WM_COMMAND:
1796 wCode = HIWORD(pMsg->wParam); /* Get notification code */
1797 if ( wCode )
1798 goto NEXTHOOK; /* Not a menu message */
1799 break;
1801 default:
1802 goto NEXTHOOK;
1805 /* Get the menu descriptor */
1806 pOleMenuDescriptor = GlobalLock( hOleMenu );
1807 if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1808 goto NEXTHOOK;
1810 /* If the message was for the server dispatch it accordingly */
1811 if ( pOleMenuDescriptor->bIsServerItem )
1813 /* Change the hWnd in the message to the active objects hWnd.
1814 * The message loop which reads this message will automatically
1815 * dispatch it to the embedded objects window. */
1816 pMsg->hwnd = pOleMenuDescriptor->hwndActiveObject;
1819 NEXTHOOK:
1820 if ( pOleMenuDescriptor )
1821 GlobalUnlock( hOleMenu );
1823 /* Lookup the hook item for the current thread */
1824 if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1826 /* This should never fail!! */
1827 WARN("could not retrieve hHook for current thread!\n" );
1828 return FALSE;
1831 /* Pass on the message to the next hooker */
1832 return CallNextHookEx( pHookItem->GetMsg_hHook, code, wParam, lParam );
1835 /***********************************************************************
1836 * OleCreateMenuDescriptor [OLE32.@]
1837 * Creates an OLE menu descriptor for OLE to use when dispatching
1838 * menu messages and commands.
1840 * PARAMS:
1841 * hmenuCombined - Handle to the objects combined menu
1842 * lpMenuWidths - Pointer to array of 6 LONG's indicating menus per group
1845 HOLEMENU WINAPI OleCreateMenuDescriptor(
1846 HMENU hmenuCombined,
1847 LPOLEMENUGROUPWIDTHS lpMenuWidths)
1849 HOLEMENU hOleMenu;
1850 OleMenuDescriptor *pOleMenuDescriptor;
1851 int i;
1853 if ( !hmenuCombined || !lpMenuWidths )
1854 return 0;
1856 /* Create an OLE menu descriptor */
1857 if ( !(hOleMenu = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT,
1858 sizeof(OleMenuDescriptor) ) ) )
1859 return 0;
1861 pOleMenuDescriptor = GlobalLock( hOleMenu );
1862 if ( !pOleMenuDescriptor )
1863 return 0;
1865 /* Initialize menu group widths and hmenu */
1866 for ( i = 0; i < 6; i++ )
1867 pOleMenuDescriptor->mgw.width[i] = lpMenuWidths->width[i];
1869 pOleMenuDescriptor->hmenuCombined = hmenuCombined;
1870 pOleMenuDescriptor->bIsServerItem = FALSE;
1871 GlobalUnlock( hOleMenu );
1873 return hOleMenu;
1876 /***********************************************************************
1877 * OleDestroyMenuDescriptor [OLE32.@]
1878 * Destroy the shared menu descriptor
1880 HRESULT WINAPI OleDestroyMenuDescriptor(
1881 HOLEMENU hmenuDescriptor)
1883 if ( hmenuDescriptor )
1884 GlobalFree( hmenuDescriptor );
1885 return S_OK;
1888 /***********************************************************************
1889 * OleSetMenuDescriptor [OLE32.@]
1890 * Installs or removes OLE dispatching code for the containers frame window.
1892 * PARAMS
1893 * hOleMenu Handle to composite menu descriptor
1894 * hwndFrame Handle to containers frame window
1895 * hwndActiveObject Handle to objects in-place activation window
1896 * lpFrame Pointer to IOleInPlaceFrame on containers window
1897 * lpActiveObject Pointer to IOleInPlaceActiveObject on active in-place object
1899 * RETURNS
1900 * S_OK - menu installed correctly
1901 * E_FAIL, E_INVALIDARG, E_UNEXPECTED - failure
1903 * FIXME
1904 * The lpFrame and lpActiveObject parameters are currently ignored
1905 * OLE should install context sensitive help F1 filtering for the app when
1906 * these are non null.
1908 HRESULT WINAPI OleSetMenuDescriptor(
1909 HOLEMENU hOleMenu,
1910 HWND hwndFrame,
1911 HWND hwndActiveObject,
1912 LPOLEINPLACEFRAME lpFrame,
1913 LPOLEINPLACEACTIVEOBJECT lpActiveObject)
1915 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1917 /* Check args */
1918 if ( !hwndFrame || (hOleMenu && !hwndActiveObject) )
1919 return E_INVALIDARG;
1921 if ( lpFrame || lpActiveObject )
1923 FIXME("(%p, %p, %p, %p, %p), Context sensitive help filtering not implemented!\n",
1924 hOleMenu,
1925 hwndFrame,
1926 hwndActiveObject,
1927 lpFrame,
1928 lpActiveObject);
1931 /* Set up a message hook to intercept the containers frame window messages.
1932 * The message filter is responsible for dispatching menu messages from the
1933 * shared menu which are intended for the object.
1936 if ( hOleMenu ) /* Want to install dispatching code */
1938 /* If OLEMenu hooks are already installed for this thread, fail
1939 * Note: This effectively means that OleSetMenuDescriptor cannot
1940 * be called twice in succession on the same frame window
1941 * without first calling it with a null hOleMenu to uninstall
1943 if ( OLEMenu_IsHookInstalled( GetCurrentThreadId() ) )
1944 return E_FAIL;
1946 /* Get the menu descriptor */
1947 pOleMenuDescriptor = GlobalLock( hOleMenu );
1948 if ( !pOleMenuDescriptor )
1949 return E_UNEXPECTED;
1951 /* Update the menu descriptor */
1952 pOleMenuDescriptor->hwndFrame = hwndFrame;
1953 pOleMenuDescriptor->hwndActiveObject = hwndActiveObject;
1955 GlobalUnlock( hOleMenu );
1956 pOleMenuDescriptor = NULL;
1958 /* Add a menu descriptor windows property to the frame window */
1959 SetPropW( hwndFrame, prop_olemenuW, hOleMenu );
1961 /* Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC */
1962 if ( !OLEMenu_InstallHooks( GetCurrentThreadId() ) )
1963 return E_FAIL;
1965 else /* Want to uninstall dispatching code */
1967 /* Uninstall the hooks */
1968 if ( !OLEMenu_UnInstallHooks( GetCurrentThreadId() ) )
1969 return E_FAIL;
1971 /* Remove the menu descriptor property from the frame window */
1972 RemovePropW( hwndFrame, prop_olemenuW );
1975 return S_OK;
1978 /******************************************************************************
1979 * IsAccelerator [OLE32.@]
1980 * Mostly copied from controls/menu.c TranslateAccelerator implementation
1982 BOOL WINAPI IsAccelerator(HACCEL hAccel, int cAccelEntries, LPMSG lpMsg, WORD* lpwCmd)
1984 LPACCEL lpAccelTbl;
1985 int i;
1987 if(!lpMsg) return FALSE;
1988 if (!hAccel)
1990 WARN_(accel)("NULL accel handle\n");
1991 return FALSE;
1993 if((lpMsg->message != WM_KEYDOWN &&
1994 lpMsg->message != WM_SYSKEYDOWN &&
1995 lpMsg->message != WM_SYSCHAR &&
1996 lpMsg->message != WM_CHAR)) return FALSE;
1997 lpAccelTbl = HeapAlloc(GetProcessHeap(), 0, cAccelEntries * sizeof(ACCEL));
1998 if (NULL == lpAccelTbl)
2000 return FALSE;
2002 if (CopyAcceleratorTableW(hAccel, lpAccelTbl, cAccelEntries) != cAccelEntries)
2004 WARN_(accel)("CopyAcceleratorTableW failed\n");
2005 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
2006 return FALSE;
2009 TRACE_(accel)("hAccel=%p, cAccelEntries=%d,"
2010 "msg->hwnd=%p, msg->message=%04x, wParam=%08lx, lParam=%08lx\n",
2011 hAccel, cAccelEntries,
2012 lpMsg->hwnd, lpMsg->message, lpMsg->wParam, lpMsg->lParam);
2013 for(i = 0; i < cAccelEntries; i++)
2015 if(lpAccelTbl[i].key != lpMsg->wParam)
2016 continue;
2018 if(lpMsg->message == WM_CHAR)
2020 if(!(lpAccelTbl[i].fVirt & FALT) && !(lpAccelTbl[i].fVirt & FVIRTKEY))
2022 TRACE_(accel)("found accel for WM_CHAR: ('%c')\n", LOWORD(lpMsg->wParam) & 0xff);
2023 goto found;
2026 else
2028 if(lpAccelTbl[i].fVirt & FVIRTKEY)
2030 INT mask = 0;
2031 TRACE_(accel)("found accel for virt_key %04lx (scan %04x)\n",
2032 lpMsg->wParam, HIWORD(lpMsg->lParam) & 0xff);
2033 if(GetKeyState(VK_SHIFT) & 0x8000) mask |= FSHIFT;
2034 if(GetKeyState(VK_CONTROL) & 0x8000) mask |= FCONTROL;
2035 if(GetKeyState(VK_MENU) & 0x8000) mask |= FALT;
2036 if(mask == (lpAccelTbl[i].fVirt & (FSHIFT | FCONTROL | FALT))) goto found;
2037 TRACE_(accel)("incorrect SHIFT/CTRL/ALT-state\n");
2039 else
2041 if(!(lpMsg->lParam & 0x01000000)) /* no special_key */
2043 if((lpAccelTbl[i].fVirt & FALT) && (lpMsg->lParam & 0x20000000))
2044 { /* ^^ ALT pressed */
2045 TRACE_(accel)("found accel for Alt-%c\n", LOWORD(lpMsg->wParam) & 0xff);
2046 goto found;
2053 WARN_(accel)("couldn't translate accelerator key\n");
2054 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
2055 return FALSE;
2057 found:
2058 if(lpwCmd) *lpwCmd = lpAccelTbl[i].cmd;
2059 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
2060 return TRUE;
2063 /***********************************************************************
2064 * ReleaseStgMedium [OLE32.@]
2066 void WINAPI ReleaseStgMedium(
2067 STGMEDIUM* pmedium)
2069 switch (pmedium->tymed)
2071 case TYMED_HGLOBAL:
2073 if ( (pmedium->pUnkForRelease==0) &&
2074 (pmedium->u.hGlobal!=0) )
2075 GlobalFree(pmedium->u.hGlobal);
2076 break;
2078 case TYMED_FILE:
2080 if (pmedium->u.lpszFileName!=0)
2082 if (pmedium->pUnkForRelease==0)
2084 DeleteFileW(pmedium->u.lpszFileName);
2087 CoTaskMemFree(pmedium->u.lpszFileName);
2089 break;
2091 case TYMED_ISTREAM:
2093 if (pmedium->u.pstm!=0)
2095 IStream_Release(pmedium->u.pstm);
2097 break;
2099 case TYMED_ISTORAGE:
2101 if (pmedium->u.pstg!=0)
2103 IStorage_Release(pmedium->u.pstg);
2105 break;
2107 case TYMED_GDI:
2109 if ( (pmedium->pUnkForRelease==0) &&
2110 (pmedium->u.hBitmap!=0) )
2111 DeleteObject(pmedium->u.hBitmap);
2112 break;
2114 case TYMED_MFPICT:
2116 if ( (pmedium->pUnkForRelease==0) &&
2117 (pmedium->u.hMetaFilePict!=0) )
2119 LPMETAFILEPICT pMP = GlobalLock(pmedium->u.hMetaFilePict);
2120 DeleteMetaFile(pMP->hMF);
2121 GlobalUnlock(pmedium->u.hMetaFilePict);
2122 GlobalFree(pmedium->u.hMetaFilePict);
2124 break;
2126 case TYMED_ENHMF:
2128 if ( (pmedium->pUnkForRelease==0) &&
2129 (pmedium->u.hEnhMetaFile!=0) )
2131 DeleteEnhMetaFile(pmedium->u.hEnhMetaFile);
2133 break;
2135 case TYMED_NULL:
2136 default:
2137 break;
2139 pmedium->tymed=TYMED_NULL;
2142 * After cleaning up, the unknown is released
2144 if (pmedium->pUnkForRelease!=0)
2146 IUnknown_Release(pmedium->pUnkForRelease);
2147 pmedium->pUnkForRelease = 0;
2151 /***
2152 * OLEDD_Initialize()
2154 * Initializes the OLE drag and drop data structures.
2156 static void OLEDD_Initialize(void)
2158 WNDCLASSW wndClass;
2160 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
2161 wndClass.style = CS_GLOBALCLASS;
2162 wndClass.lpfnWndProc = OLEDD_DragTrackerWindowProc;
2163 wndClass.cbClsExtra = 0;
2164 wndClass.cbWndExtra = sizeof(TrackerWindowInfo*);
2165 wndClass.hCursor = 0;
2166 wndClass.hbrBackground = 0;
2167 wndClass.lpszClassName = OLEDD_DRAGTRACKERCLASS;
2169 RegisterClassW (&wndClass);
2172 /***
2173 * OLEDD_DragTrackerWindowProc()
2175 * This method is the WindowProcedure of the drag n drop tracking
2176 * window. During a drag n Drop operation, an invisible window is created
2177 * to receive the user input and act upon it. This procedure is in charge
2178 * of this behavior.
2181 #define DRAG_TIMER_ID 1
2183 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(
2184 HWND hwnd,
2185 UINT uMsg,
2186 WPARAM wParam,
2187 LPARAM lParam)
2189 switch (uMsg)
2191 case WM_CREATE:
2193 LPCREATESTRUCTA createStruct = (LPCREATESTRUCTA)lParam;
2195 SetWindowLongPtrW(hwnd, 0, (LONG_PTR)createStruct->lpCreateParams);
2196 SetTimer(hwnd, DRAG_TIMER_ID, 50, NULL);
2198 break;
2200 case WM_TIMER:
2201 case WM_MOUSEMOVE:
2203 OLEDD_TrackMouseMove((TrackerWindowInfo*)GetWindowLongPtrA(hwnd, 0));
2204 break;
2206 case WM_LBUTTONUP:
2207 case WM_MBUTTONUP:
2208 case WM_RBUTTONUP:
2209 case WM_LBUTTONDOWN:
2210 case WM_MBUTTONDOWN:
2211 case WM_RBUTTONDOWN:
2213 OLEDD_TrackStateChange((TrackerWindowInfo*)GetWindowLongPtrA(hwnd, 0));
2214 break;
2216 case WM_DESTROY:
2218 KillTimer(hwnd, DRAG_TIMER_ID);
2219 break;
2224 * This is a window proc after all. Let's call the default.
2226 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
2229 /***
2230 * OLEDD_TrackMouseMove()
2232 * This method is invoked while a drag and drop operation is in effect.
2233 * it will generate the appropriate callbacks in the drop source
2234 * and drop target. It will also provide the expected feedback to
2235 * the user.
2237 * params:
2238 * trackerInfo - Pointer to the structure identifying the
2239 * drag & drop operation that is currently
2240 * active.
2242 static void OLEDD_TrackMouseMove(TrackerWindowInfo* trackerInfo)
2244 HWND hwndNewTarget = 0;
2245 HRESULT hr = S_OK;
2246 POINT pt;
2249 * Get the handle of the window under the mouse
2251 pt.x = trackerInfo->curMousePos.x;
2252 pt.y = trackerInfo->curMousePos.y;
2253 hwndNewTarget = WindowFromPoint(pt);
2256 * Every time, we re-initialize the effects passed to the
2257 * IDropTarget to the effects allowed by the source.
2259 *trackerInfo->pdwEffect = trackerInfo->dwOKEffect;
2262 * If we are hovering over the same target as before, send the
2263 * DragOver notification
2265 if ( (trackerInfo->curDragTarget != 0) &&
2266 (trackerInfo->curTargetHWND == hwndNewTarget) )
2268 IDropTarget_DragOver(trackerInfo->curDragTarget,
2269 trackerInfo->dwKeyState,
2270 trackerInfo->curMousePos,
2271 trackerInfo->pdwEffect);
2273 else
2276 * If we changed window, we have to notify our old target and check for
2277 * the new one.
2279 if (trackerInfo->curDragTarget)
2280 IDropTarget_DragLeave(trackerInfo->curDragTarget);
2283 * Make sure we're hovering over a window.
2285 if (hwndNewTarget)
2288 * Find-out if there is a drag target under the mouse
2290 HWND next_target_wnd = hwndNewTarget;
2292 trackerInfo->curTargetHWND = hwndNewTarget;
2294 while (next_target_wnd && !is_droptarget(next_target_wnd))
2295 next_target_wnd = GetParent(next_target_wnd);
2297 if (next_target_wnd) hwndNewTarget = next_target_wnd;
2299 trackerInfo->curDragTargetHWND = hwndNewTarget;
2300 if(trackerInfo->curDragTarget) IDropTarget_Release(trackerInfo->curDragTarget);
2301 trackerInfo->curDragTarget = get_droptarget_pointer(hwndNewTarget);
2304 * If there is, notify it that we just dragged-in
2306 if (trackerInfo->curDragTarget)
2308 hr = IDropTarget_DragEnter(trackerInfo->curDragTarget,
2309 trackerInfo->dataObject,
2310 trackerInfo->dwKeyState,
2311 trackerInfo->curMousePos,
2312 trackerInfo->pdwEffect);
2314 /* failed DragEnter() means invalid target */
2315 if (hr != S_OK)
2317 trackerInfo->curDragTargetHWND = 0;
2318 trackerInfo->curTargetHWND = 0;
2319 IDropTarget_Release(trackerInfo->curDragTarget);
2320 trackerInfo->curDragTarget = 0;
2324 else
2327 * The mouse is not over a window so we don't track anything.
2329 trackerInfo->curDragTargetHWND = 0;
2330 trackerInfo->curTargetHWND = 0;
2331 if(trackerInfo->curDragTarget) IDropTarget_Release(trackerInfo->curDragTarget);
2332 trackerInfo->curDragTarget = 0;
2337 * Now that we have done that, we have to tell the source to give
2338 * us feedback on the work being done by the target. If we don't
2339 * have a target, simulate no effect.
2341 if (trackerInfo->curDragTarget==0)
2343 *trackerInfo->pdwEffect = DROPEFFECT_NONE;
2346 hr = IDropSource_GiveFeedback(trackerInfo->dropSource,
2347 *trackerInfo->pdwEffect);
2350 * When we ask for feedback from the drop source, sometimes it will
2351 * do all the necessary work and sometimes it will not handle it
2352 * when that's the case, we must display the standard drag and drop
2353 * cursors.
2355 if (hr == DRAGDROP_S_USEDEFAULTCURSORS)
2357 HCURSOR hCur;
2359 if (*trackerInfo->pdwEffect & DROPEFFECT_MOVE)
2361 hCur = LoadCursorW(hProxyDll, MAKEINTRESOURCEW(2));
2363 else if (*trackerInfo->pdwEffect & DROPEFFECT_COPY)
2365 hCur = LoadCursorW(hProxyDll, MAKEINTRESOURCEW(3));
2367 else if (*trackerInfo->pdwEffect & DROPEFFECT_LINK)
2369 hCur = LoadCursorW(hProxyDll, MAKEINTRESOURCEW(4));
2371 else
2373 hCur = LoadCursorW(hProxyDll, MAKEINTRESOURCEW(1));
2376 SetCursor(hCur);
2380 /***
2381 * OLEDD_TrackStateChange()
2383 * This method is invoked while a drag and drop operation is in effect.
2384 * It is used to notify the drop target/drop source callbacks when
2385 * the state of the keyboard or mouse button change.
2387 * params:
2388 * trackerInfo - Pointer to the structure identifying the
2389 * drag & drop operation that is currently
2390 * active.
2392 static void OLEDD_TrackStateChange(TrackerWindowInfo* trackerInfo)
2395 * Ask the drop source what to do with the operation.
2397 trackerInfo->returnValue = IDropSource_QueryContinueDrag(
2398 trackerInfo->dropSource,
2399 trackerInfo->escPressed,
2400 trackerInfo->dwKeyState);
2403 * All the return valued will stop the operation except the S_OK
2404 * return value.
2406 if (trackerInfo->returnValue!=S_OK)
2409 * Make sure the message loop in DoDragDrop stops
2411 trackerInfo->trackingDone = TRUE;
2414 * Release the mouse in case the drop target decides to show a popup
2415 * or a menu or something.
2417 ReleaseCapture();
2420 * If we end-up over a target, drop the object in the target or
2421 * inform the target that the operation was cancelled.
2423 if (trackerInfo->curDragTarget)
2425 switch (trackerInfo->returnValue)
2428 * If the source wants us to complete the operation, we tell
2429 * the drop target that we just dropped the object in it.
2431 case DRAGDROP_S_DROP:
2432 if (*trackerInfo->pdwEffect != DROPEFFECT_NONE)
2433 IDropTarget_Drop(trackerInfo->curDragTarget,
2434 trackerInfo->dataObject,
2435 trackerInfo->dwKeyState,
2436 trackerInfo->curMousePos,
2437 trackerInfo->pdwEffect);
2438 else
2439 IDropTarget_DragLeave(trackerInfo->curDragTarget);
2440 break;
2443 * If the source told us that we should cancel, fool the drop
2444 * target by telling it that the mouse left its window.
2445 * Also set the drop effect to "NONE" in case the application
2446 * ignores the result of DoDragDrop.
2448 case DRAGDROP_S_CANCEL:
2449 IDropTarget_DragLeave(trackerInfo->curDragTarget);
2450 *trackerInfo->pdwEffect = DROPEFFECT_NONE;
2451 break;
2457 /***
2458 * OLEDD_GetButtonState()
2460 * This method will use the current state of the keyboard to build
2461 * a button state mask equivalent to the one passed in the
2462 * WM_MOUSEMOVE wParam.
2464 static DWORD OLEDD_GetButtonState(void)
2466 BYTE keyboardState[256];
2467 DWORD keyMask = 0;
2469 GetKeyboardState(keyboardState);
2471 if ( (keyboardState[VK_SHIFT] & 0x80) !=0)
2472 keyMask |= MK_SHIFT;
2474 if ( (keyboardState[VK_CONTROL] & 0x80) !=0)
2475 keyMask |= MK_CONTROL;
2477 if ( (keyboardState[VK_LBUTTON] & 0x80) !=0)
2478 keyMask |= MK_LBUTTON;
2480 if ( (keyboardState[VK_RBUTTON] & 0x80) !=0)
2481 keyMask |= MK_RBUTTON;
2483 if ( (keyboardState[VK_MBUTTON] & 0x80) !=0)
2484 keyMask |= MK_MBUTTON;
2486 return keyMask;
2489 /***
2490 * OLEDD_GetButtonState()
2492 * This method will read the default value of the registry key in
2493 * parameter and extract a DWORD value from it. The registry key value
2494 * can be in a string key or a DWORD key.
2496 * params:
2497 * regKey - Key to read the default value from
2498 * pdwValue - Pointer to the location where the DWORD
2499 * value is returned. This value is not modified
2500 * if the value is not found.
2503 static void OLEUTL_ReadRegistryDWORDValue(
2504 HKEY regKey,
2505 DWORD* pdwValue)
2507 WCHAR buffer[20];
2508 DWORD cbData = sizeof(buffer);
2509 DWORD dwKeyType;
2510 LONG lres;
2512 lres = RegQueryValueExW(regKey,
2513 emptyW,
2514 NULL,
2515 &dwKeyType,
2516 (LPBYTE)buffer,
2517 &cbData);
2519 if (lres==ERROR_SUCCESS)
2521 switch (dwKeyType)
2523 case REG_DWORD:
2524 *pdwValue = *(DWORD*)buffer;
2525 break;
2526 case REG_EXPAND_SZ:
2527 case REG_MULTI_SZ:
2528 case REG_SZ:
2529 *pdwValue = (DWORD)strtoulW(buffer, NULL, 10);
2530 break;
2535 /******************************************************************************
2536 * OleDraw (OLE32.@)
2538 * The operation of this function is documented literally in the WinAPI
2539 * documentation to involve a QueryInterface for the IViewObject interface,
2540 * followed by a call to IViewObject::Draw.
2542 HRESULT WINAPI OleDraw(
2543 IUnknown *pUnk,
2544 DWORD dwAspect,
2545 HDC hdcDraw,
2546 LPCRECT rect)
2548 HRESULT hres;
2549 IViewObject *viewobject;
2551 if (!pUnk) return E_INVALIDARG;
2553 hres = IUnknown_QueryInterface(pUnk,
2554 &IID_IViewObject,
2555 (void**)&viewobject);
2556 if (SUCCEEDED(hres))
2558 hres = IViewObject_Draw(viewobject, dwAspect, -1, 0, 0, 0, hdcDraw, (RECTL*)rect, 0, 0, 0);
2559 IViewObject_Release(viewobject);
2560 return hres;
2562 else
2563 return DV_E_NOIVIEWOBJECT;
2566 /***********************************************************************
2567 * OleTranslateAccelerator [OLE32.@]
2569 HRESULT WINAPI OleTranslateAccelerator (LPOLEINPLACEFRAME lpFrame,
2570 LPOLEINPLACEFRAMEINFO lpFrameInfo, LPMSG lpmsg)
2572 WORD wID;
2574 TRACE("(%p,%p,%p)\n", lpFrame, lpFrameInfo, lpmsg);
2576 if (IsAccelerator(lpFrameInfo->haccel,lpFrameInfo->cAccelEntries,lpmsg,&wID))
2577 return IOleInPlaceFrame_TranslateAccelerator(lpFrame,lpmsg,wID);
2579 return S_FALSE;
2582 /******************************************************************************
2583 * OleCreate [OLE32.@]
2586 HRESULT WINAPI OleCreate(
2587 REFCLSID rclsid,
2588 REFIID riid,
2589 DWORD renderopt,
2590 LPFORMATETC pFormatEtc,
2591 LPOLECLIENTSITE pClientSite,
2592 LPSTORAGE pStg,
2593 LPVOID* ppvObj)
2595 HRESULT hres;
2596 IUnknown * pUnk = NULL;
2597 IOleObject *pOleObject = NULL;
2599 TRACE("(%s, %s, %d, %p, %p, %p, %p)\n", debugstr_guid(rclsid),
2600 debugstr_guid(riid), renderopt, pFormatEtc, pClientSite, pStg, ppvObj);
2602 hres = CoCreateInstance(rclsid, 0, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER, riid, (LPVOID*)&pUnk);
2604 if (SUCCEEDED(hres))
2605 hres = IStorage_SetClass(pStg, rclsid);
2607 if (pClientSite && SUCCEEDED(hres))
2609 hres = IUnknown_QueryInterface(pUnk, &IID_IOleObject, (LPVOID*)&pOleObject);
2610 if (SUCCEEDED(hres))
2612 DWORD dwStatus;
2613 hres = IOleObject_GetMiscStatus(pOleObject, DVASPECT_CONTENT, &dwStatus);
2617 if (SUCCEEDED(hres))
2619 IPersistStorage * pPS;
2620 if (SUCCEEDED((hres = IUnknown_QueryInterface(pUnk, &IID_IPersistStorage, (LPVOID*)&pPS))))
2622 TRACE("trying to set stg %p\n", pStg);
2623 hres = IPersistStorage_InitNew(pPS, pStg);
2624 TRACE("-- result 0x%08x\n", hres);
2625 IPersistStorage_Release(pPS);
2629 if (pClientSite && SUCCEEDED(hres))
2631 TRACE("trying to set clientsite %p\n", pClientSite);
2632 hres = IOleObject_SetClientSite(pOleObject, pClientSite);
2633 TRACE("-- result 0x%08x\n", hres);
2636 if (pOleObject)
2637 IOleObject_Release(pOleObject);
2639 if (((renderopt == OLERENDER_DRAW) || (renderopt == OLERENDER_FORMAT)) &&
2640 SUCCEEDED(hres))
2642 IRunnableObject *pRunnable;
2643 IOleCache *pOleCache;
2644 HRESULT hres2;
2646 hres2 = IUnknown_QueryInterface(pUnk, &IID_IRunnableObject, (void **)&pRunnable);
2647 if (SUCCEEDED(hres2))
2649 hres = IRunnableObject_Run(pRunnable, NULL);
2650 IRunnableObject_Release(pRunnable);
2653 if (SUCCEEDED(hres))
2655 hres2 = IUnknown_QueryInterface(pUnk, &IID_IOleCache, (void **)&pOleCache);
2656 if (SUCCEEDED(hres2))
2658 DWORD dwConnection;
2659 if (renderopt == OLERENDER_DRAW && !pFormatEtc) {
2660 FORMATETC pfe;
2661 pfe.cfFormat = 0;
2662 pfe.ptd = NULL;
2663 pfe.dwAspect = DVASPECT_CONTENT;
2664 pfe.lindex = -1;
2665 pfe.tymed = TYMED_NULL;
2666 hres = IOleCache_Cache(pOleCache, &pfe, ADVF_PRIMEFIRST, &dwConnection);
2668 else
2669 hres = IOleCache_Cache(pOleCache, pFormatEtc, ADVF_PRIMEFIRST, &dwConnection);
2670 IOleCache_Release(pOleCache);
2675 if (FAILED(hres) && pUnk)
2677 IUnknown_Release(pUnk);
2678 pUnk = NULL;
2681 *ppvObj = pUnk;
2683 TRACE("-- %p\n", pUnk);
2684 return hres;
2687 /******************************************************************************
2688 * OleGetAutoConvert [OLE32.@]
2690 HRESULT WINAPI OleGetAutoConvert(REFCLSID clsidOld, LPCLSID pClsidNew)
2692 static const WCHAR wszAutoConvertTo[] = {'A','u','t','o','C','o','n','v','e','r','t','T','o',0};
2693 HKEY hkey = NULL;
2694 WCHAR buf[CHARS_IN_GUID];
2695 LONG len;
2696 HRESULT res = S_OK;
2698 res = COM_OpenKeyForCLSID(clsidOld, wszAutoConvertTo, KEY_READ, &hkey);
2699 if (FAILED(res))
2700 goto done;
2702 len = sizeof(buf);
2703 if (RegQueryValueW(hkey, NULL, buf, &len))
2705 res = REGDB_E_KEYMISSING;
2706 goto done;
2708 res = CLSIDFromString(buf, pClsidNew);
2709 done:
2710 if (hkey) RegCloseKey(hkey);
2711 return res;
2714 /******************************************************************************
2715 * OleSetAutoConvert [OLE32.@]
2717 HRESULT WINAPI OleSetAutoConvert(REFCLSID clsidOld, REFCLSID clsidNew)
2719 static const WCHAR wszAutoConvertTo[] = {'A','u','t','o','C','o','n','v','e','r','t','T','o',0};
2720 HKEY hkey = NULL;
2721 WCHAR szClsidNew[CHARS_IN_GUID];
2722 HRESULT res = S_OK;
2724 TRACE("(%s,%s)\n", debugstr_guid(clsidOld), debugstr_guid(clsidNew));
2726 res = COM_OpenKeyForCLSID(clsidOld, NULL, KEY_READ | KEY_WRITE, &hkey);
2727 if (FAILED(res))
2728 goto done;
2729 StringFromGUID2(clsidNew, szClsidNew, CHARS_IN_GUID);
2730 if (RegSetValueW(hkey, wszAutoConvertTo, REG_SZ, szClsidNew, (strlenW(szClsidNew)+1) * sizeof(WCHAR)))
2732 res = REGDB_E_WRITEREGDB;
2733 goto done;
2736 done:
2737 if (hkey) RegCloseKey(hkey);
2738 return res;
2741 /******************************************************************************
2742 * OleDoAutoConvert [OLE32.@]
2744 HRESULT WINAPI OleDoAutoConvert(LPSTORAGE pStg, LPCLSID pClsidNew)
2746 FIXME("(%p,%p) : stub\n",pStg,pClsidNew);
2747 return E_NOTIMPL;
2750 /******************************************************************************
2751 * OleIsRunning [OLE32.@]
2753 BOOL WINAPI OleIsRunning(LPOLEOBJECT object)
2755 IRunnableObject *pRunnable;
2756 HRESULT hr;
2757 BOOL running;
2759 TRACE("(%p)\n", object);
2761 if (!object) return FALSE;
2763 hr = IOleObject_QueryInterface(object, &IID_IRunnableObject, (void **)&pRunnable);
2764 if (FAILED(hr))
2765 return TRUE;
2766 running = IRunnableObject_IsRunning(pRunnable);
2767 IRunnableObject_Release(pRunnable);
2768 return running;
2771 /***********************************************************************
2772 * OleNoteObjectVisible [OLE32.@]
2774 HRESULT WINAPI OleNoteObjectVisible(LPUNKNOWN pUnknown, BOOL bVisible)
2776 TRACE("(%p, %s)\n", pUnknown, bVisible ? "TRUE" : "FALSE");
2777 return CoLockObjectExternal(pUnknown, bVisible, TRUE);
2781 /***********************************************************************
2782 * OLE_FreeClipDataArray [internal]
2784 * NOTES:
2785 * frees the data associated with an array of CLIPDATAs
2787 static void OLE_FreeClipDataArray(ULONG count, CLIPDATA * pClipDataArray)
2789 ULONG i;
2790 for (i = 0; i < count; i++)
2791 if (pClipDataArray[i].pClipData)
2792 CoTaskMemFree(pClipDataArray[i].pClipData);
2795 /***********************************************************************
2796 * PropSysAllocString [OLE32.@]
2797 * NOTES
2798 * Forward to oleaut32.
2800 BSTR WINAPI PropSysAllocString(LPCOLESTR str)
2802 return SysAllocString(str);
2805 /***********************************************************************
2806 * PropSysFreeString [OLE32.@]
2807 * NOTES
2808 * Forward to oleaut32.
2810 void WINAPI PropSysFreeString(LPOLESTR str)
2812 SysFreeString(str);
2815 /******************************************************************************
2816 * Check if a PROPVARIANT's type is valid.
2818 static inline HRESULT PROPVARIANT_ValidateType(VARTYPE vt)
2820 switch (vt)
2822 case VT_EMPTY:
2823 case VT_NULL:
2824 case VT_I1:
2825 case VT_I2:
2826 case VT_I4:
2827 case VT_I8:
2828 case VT_R4:
2829 case VT_R8:
2830 case VT_CY:
2831 case VT_DATE:
2832 case VT_BSTR:
2833 case VT_ERROR:
2834 case VT_BOOL:
2835 case VT_DECIMAL:
2836 case VT_UI1:
2837 case VT_UI2:
2838 case VT_UI4:
2839 case VT_UI8:
2840 case VT_LPSTR:
2841 case VT_LPWSTR:
2842 case VT_FILETIME:
2843 case VT_BLOB:
2844 case VT_STREAM:
2845 case VT_STORAGE:
2846 case VT_STREAMED_OBJECT:
2847 case VT_STORED_OBJECT:
2848 case VT_BLOB_OBJECT:
2849 case VT_CF:
2850 case VT_CLSID:
2851 case VT_I1|VT_VECTOR:
2852 case VT_I2|VT_VECTOR:
2853 case VT_I4|VT_VECTOR:
2854 case VT_I8|VT_VECTOR:
2855 case VT_R4|VT_VECTOR:
2856 case VT_R8|VT_VECTOR:
2857 case VT_CY|VT_VECTOR:
2858 case VT_DATE|VT_VECTOR:
2859 case VT_BSTR|VT_VECTOR:
2860 case VT_ERROR|VT_VECTOR:
2861 case VT_BOOL|VT_VECTOR:
2862 case VT_VARIANT|VT_VECTOR:
2863 case VT_UI1|VT_VECTOR:
2864 case VT_UI2|VT_VECTOR:
2865 case VT_UI4|VT_VECTOR:
2866 case VT_UI8|VT_VECTOR:
2867 case VT_LPSTR|VT_VECTOR:
2868 case VT_LPWSTR|VT_VECTOR:
2869 case VT_FILETIME|VT_VECTOR:
2870 case VT_CF|VT_VECTOR:
2871 case VT_CLSID|VT_VECTOR:
2872 return S_OK;
2874 WARN("Bad type %d\n", vt);
2875 return STG_E_INVALIDPARAMETER;
2878 /***********************************************************************
2879 * PropVariantClear [OLE32.@]
2881 HRESULT WINAPI PropVariantClear(PROPVARIANT * pvar) /* [in/out] */
2883 HRESULT hr;
2885 TRACE("(%p)\n", pvar);
2887 if (!pvar)
2888 return S_OK;
2890 hr = PROPVARIANT_ValidateType(pvar->vt);
2891 if (FAILED(hr))
2892 return hr;
2894 switch(pvar->vt)
2896 case VT_EMPTY:
2897 case VT_NULL:
2898 case VT_I1:
2899 case VT_I2:
2900 case VT_I4:
2901 case VT_I8:
2902 case VT_R4:
2903 case VT_R8:
2904 case VT_CY:
2905 case VT_DATE:
2906 case VT_ERROR:
2907 case VT_BOOL:
2908 case VT_DECIMAL:
2909 case VT_UI1:
2910 case VT_UI2:
2911 case VT_UI4:
2912 case VT_UI8:
2913 case VT_FILETIME:
2914 break;
2915 case VT_STREAM:
2916 case VT_STREAMED_OBJECT:
2917 case VT_STORAGE:
2918 case VT_STORED_OBJECT:
2919 if (pvar->u.pStream)
2920 IStream_Release(pvar->u.pStream);
2921 break;
2922 case VT_CLSID:
2923 case VT_LPSTR:
2924 case VT_LPWSTR:
2925 /* pick an arbitrary typed pointer - we don't care about the type
2926 * as we are just freeing it */
2927 CoTaskMemFree(pvar->u.puuid);
2928 break;
2929 case VT_BLOB:
2930 case VT_BLOB_OBJECT:
2931 CoTaskMemFree(pvar->u.blob.pBlobData);
2932 break;
2933 case VT_BSTR:
2934 PropSysFreeString(pvar->u.bstrVal);
2935 break;
2936 case VT_CF:
2937 if (pvar->u.pclipdata)
2939 OLE_FreeClipDataArray(1, pvar->u.pclipdata);
2940 CoTaskMemFree(pvar->u.pclipdata);
2942 break;
2943 default:
2944 if (pvar->vt & VT_VECTOR)
2946 ULONG i;
2948 switch (pvar->vt & ~VT_VECTOR)
2950 case VT_VARIANT:
2951 FreePropVariantArray(pvar->u.capropvar.cElems, pvar->u.capropvar.pElems);
2952 break;
2953 case VT_CF:
2954 OLE_FreeClipDataArray(pvar->u.caclipdata.cElems, pvar->u.caclipdata.pElems);
2955 break;
2956 case VT_BSTR:
2957 for (i = 0; i < pvar->u.cabstr.cElems; i++)
2958 PropSysFreeString(pvar->u.cabstr.pElems[i]);
2959 break;
2960 case VT_LPSTR:
2961 for (i = 0; i < pvar->u.calpstr.cElems; i++)
2962 CoTaskMemFree(pvar->u.calpstr.pElems[i]);
2963 break;
2964 case VT_LPWSTR:
2965 for (i = 0; i < pvar->u.calpwstr.cElems; i++)
2966 CoTaskMemFree(pvar->u.calpwstr.pElems[i]);
2967 break;
2969 if (pvar->vt & ~VT_VECTOR)
2971 /* pick an arbitrary VT_VECTOR structure - they all have the same
2972 * memory layout */
2973 CoTaskMemFree(pvar->u.capropvar.pElems);
2976 else
2977 WARN("Invalid/unsupported type %d\n", pvar->vt);
2980 ZeroMemory(pvar, sizeof(*pvar));
2982 return S_OK;
2985 /***********************************************************************
2986 * PropVariantCopy [OLE32.@]
2988 HRESULT WINAPI PropVariantCopy(PROPVARIANT *pvarDest, /* [out] */
2989 const PROPVARIANT *pvarSrc) /* [in] */
2991 ULONG len;
2992 HRESULT hr;
2994 TRACE("(%p, %p vt %04x)\n", pvarDest, pvarSrc, pvarSrc->vt);
2996 hr = PROPVARIANT_ValidateType(pvarSrc->vt);
2997 if (FAILED(hr))
2998 return hr;
3000 /* this will deal with most cases */
3001 *pvarDest = *pvarSrc;
3003 switch(pvarSrc->vt)
3005 case VT_EMPTY:
3006 case VT_NULL:
3007 case VT_I1:
3008 case VT_UI1:
3009 case VT_I2:
3010 case VT_UI2:
3011 case VT_BOOL:
3012 case VT_DECIMAL:
3013 case VT_I4:
3014 case VT_UI4:
3015 case VT_R4:
3016 case VT_ERROR:
3017 case VT_I8:
3018 case VT_UI8:
3019 case VT_R8:
3020 case VT_CY:
3021 case VT_DATE:
3022 case VT_FILETIME:
3023 break;
3024 case VT_STREAM:
3025 case VT_STREAMED_OBJECT:
3026 case VT_STORAGE:
3027 case VT_STORED_OBJECT:
3028 IUnknown_AddRef((LPUNKNOWN)pvarDest->u.pStream);
3029 break;
3030 case VT_CLSID:
3031 pvarDest->u.puuid = CoTaskMemAlloc(sizeof(CLSID));
3032 *pvarDest->u.puuid = *pvarSrc->u.puuid;
3033 break;
3034 case VT_LPSTR:
3035 len = strlen(pvarSrc->u.pszVal);
3036 pvarDest->u.pszVal = CoTaskMemAlloc((len+1)*sizeof(CHAR));
3037 CopyMemory(pvarDest->u.pszVal, pvarSrc->u.pszVal, (len+1)*sizeof(CHAR));
3038 break;
3039 case VT_LPWSTR:
3040 len = lstrlenW(pvarSrc->u.pwszVal);
3041 pvarDest->u.pwszVal = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
3042 CopyMemory(pvarDest->u.pwszVal, pvarSrc->u.pwszVal, (len+1)*sizeof(WCHAR));
3043 break;
3044 case VT_BLOB:
3045 case VT_BLOB_OBJECT:
3046 if (pvarSrc->u.blob.pBlobData)
3048 len = pvarSrc->u.blob.cbSize;
3049 pvarDest->u.blob.pBlobData = CoTaskMemAlloc(len);
3050 CopyMemory(pvarDest->u.blob.pBlobData, pvarSrc->u.blob.pBlobData, len);
3052 break;
3053 case VT_BSTR:
3054 pvarDest->u.bstrVal = PropSysAllocString(pvarSrc->u.bstrVal);
3055 break;
3056 case VT_CF:
3057 if (pvarSrc->u.pclipdata)
3059 len = pvarSrc->u.pclipdata->cbSize - sizeof(pvarSrc->u.pclipdata->ulClipFmt);
3060 pvarDest->u.pclipdata = CoTaskMemAlloc(sizeof (CLIPDATA));
3061 pvarDest->u.pclipdata->cbSize = pvarSrc->u.pclipdata->cbSize;
3062 pvarDest->u.pclipdata->ulClipFmt = pvarSrc->u.pclipdata->ulClipFmt;
3063 pvarDest->u.pclipdata->pClipData = CoTaskMemAlloc(len);
3064 CopyMemory(pvarDest->u.pclipdata->pClipData, pvarSrc->u.pclipdata->pClipData, len);
3066 break;
3067 default:
3068 if (pvarSrc->vt & VT_VECTOR)
3070 int elemSize;
3071 ULONG i;
3073 switch(pvarSrc->vt & ~VT_VECTOR)
3075 case VT_I1: elemSize = sizeof(pvarSrc->u.cVal); break;
3076 case VT_UI1: elemSize = sizeof(pvarSrc->u.bVal); break;
3077 case VT_I2: elemSize = sizeof(pvarSrc->u.iVal); break;
3078 case VT_UI2: elemSize = sizeof(pvarSrc->u.uiVal); break;
3079 case VT_BOOL: elemSize = sizeof(pvarSrc->u.boolVal); break;
3080 case VT_I4: elemSize = sizeof(pvarSrc->u.lVal); break;
3081 case VT_UI4: elemSize = sizeof(pvarSrc->u.ulVal); break;
3082 case VT_R4: elemSize = sizeof(pvarSrc->u.fltVal); break;
3083 case VT_R8: elemSize = sizeof(pvarSrc->u.dblVal); break;
3084 case VT_ERROR: elemSize = sizeof(pvarSrc->u.scode); break;
3085 case VT_I8: elemSize = sizeof(pvarSrc->u.hVal); break;
3086 case VT_UI8: elemSize = sizeof(pvarSrc->u.uhVal); break;
3087 case VT_CY: elemSize = sizeof(pvarSrc->u.cyVal); break;
3088 case VT_DATE: elemSize = sizeof(pvarSrc->u.date); break;
3089 case VT_FILETIME: elemSize = sizeof(pvarSrc->u.filetime); break;
3090 case VT_CLSID: elemSize = sizeof(*pvarSrc->u.puuid); break;
3091 case VT_CF: elemSize = sizeof(*pvarSrc->u.pclipdata); break;
3092 case VT_BSTR: elemSize = sizeof(pvarSrc->u.bstrVal); break;
3093 case VT_LPSTR: elemSize = sizeof(pvarSrc->u.pszVal); break;
3094 case VT_LPWSTR: elemSize = sizeof(pvarSrc->u.pwszVal); break;
3095 case VT_VARIANT: elemSize = sizeof(*pvarSrc->u.pvarVal); break;
3097 default:
3098 FIXME("Invalid element type: %ul\n", pvarSrc->vt & ~VT_VECTOR);
3099 return E_INVALIDARG;
3101 len = pvarSrc->u.capropvar.cElems;
3102 pvarDest->u.capropvar.pElems = CoTaskMemAlloc(len * elemSize);
3103 if (pvarSrc->vt == (VT_VECTOR | VT_VARIANT))
3105 for (i = 0; i < len; i++)
3106 PropVariantCopy(&pvarDest->u.capropvar.pElems[i], &pvarSrc->u.capropvar.pElems[i]);
3108 else if (pvarSrc->vt == (VT_VECTOR | VT_CF))
3110 FIXME("Copy clipformats\n");
3112 else if (pvarSrc->vt == (VT_VECTOR | VT_BSTR))
3114 for (i = 0; i < len; i++)
3115 pvarDest->u.cabstr.pElems[i] = PropSysAllocString(pvarSrc->u.cabstr.pElems[i]);
3117 else if (pvarSrc->vt == (VT_VECTOR | VT_LPSTR))
3119 size_t strLen;
3120 for (i = 0; i < len; i++)
3122 strLen = lstrlenA(pvarSrc->u.calpstr.pElems[i]) + 1;
3123 pvarDest->u.calpstr.pElems[i] = CoTaskMemAlloc(strLen);
3124 memcpy(pvarDest->u.calpstr.pElems[i],
3125 pvarSrc->u.calpstr.pElems[i], strLen);
3128 else if (pvarSrc->vt == (VT_VECTOR | VT_LPWSTR))
3130 size_t strLen;
3131 for (i = 0; i < len; i++)
3133 strLen = (lstrlenW(pvarSrc->u.calpwstr.pElems[i]) + 1) *
3134 sizeof(WCHAR);
3135 pvarDest->u.calpstr.pElems[i] = CoTaskMemAlloc(strLen);
3136 memcpy(pvarDest->u.calpstr.pElems[i],
3137 pvarSrc->u.calpstr.pElems[i], strLen);
3140 else
3141 CopyMemory(pvarDest->u.capropvar.pElems, pvarSrc->u.capropvar.pElems, len * elemSize);
3143 else
3144 WARN("Invalid/unsupported type %d\n", pvarSrc->vt);
3147 return S_OK;
3150 /***********************************************************************
3151 * FreePropVariantArray [OLE32.@]
3153 HRESULT WINAPI FreePropVariantArray(ULONG cVariants, /* [in] */
3154 PROPVARIANT *rgvars) /* [in/out] */
3156 ULONG i;
3158 TRACE("(%u, %p)\n", cVariants, rgvars);
3160 if (!rgvars)
3161 return E_INVALIDARG;
3163 for(i = 0; i < cVariants; i++)
3164 PropVariantClear(&rgvars[i]);
3166 return S_OK;
3169 /******************************************************************************
3170 * DllDebugObjectRPCHook (OLE32.@)
3171 * turns on and off internal debugging, pointer is only used on macintosh
3174 BOOL WINAPI DllDebugObjectRPCHook(BOOL b, void *dummy)
3176 FIXME("stub\n");
3177 return TRUE;