wined3d: Avoid the deprecated syntax for SM3 varyings on core profile.
[wine/multimedia.git] / dlls / ole32 / ole2.c
blob09cbccf4fc8385056df98087eeb947013332d403
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
37 #include "windef.h"
38 #include "winbase.h"
39 #include "winerror.h"
40 #include "wingdi.h"
41 #include "winuser.h"
42 #include "winnls.h"
43 #include "winreg.h"
44 #include "ole2.h"
45 #include "ole2ver.h"
47 #include "wine/unicode.h"
48 #include "compobj_private.h"
49 #include "wine/list.h"
51 #include "wine/debug.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(ole);
54 WINE_DECLARE_DEBUG_CHANNEL(accel);
56 /******************************************************************************
57 * These are static/global variables and internal data structures that the
58 * OLE module uses to maintain its state.
60 typedef struct tagTrackerWindowInfo
62 IDataObject* dataObject;
63 IDropSource* dropSource;
64 DWORD dwOKEffect;
65 DWORD* pdwEffect;
66 BOOL trackingDone;
67 HRESULT returnValue;
69 BOOL escPressed;
70 HWND curTargetHWND; /* window the mouse is hovering over */
71 HWND curDragTargetHWND; /* might be an ancestor of curTargetHWND */
72 IDropTarget* curDragTarget;
73 POINTL curMousePos; /* current position of the mouse in screen coordinates */
74 DWORD dwKeyState; /* current state of the shift and ctrl keys and the mouse buttons */
75 } TrackerWindowInfo;
77 typedef struct tagOleMenuDescriptor /* OleMenuDescriptor */
79 HWND hwndFrame; /* The containers frame window */
80 HWND hwndActiveObject; /* The active objects window */
81 OLEMENUGROUPWIDTHS mgw; /* OLE menu group widths for the shared menu */
82 HMENU hmenuCombined; /* The combined menu */
83 BOOL bIsServerItem; /* True if the currently open popup belongs to the server */
84 } OleMenuDescriptor;
86 typedef struct tagOleMenuHookItem /* OleMenu hook item in per thread hook list */
88 DWORD tid; /* Thread Id */
89 HANDLE hHeap; /* Heap this is allocated from */
90 HHOOK GetMsg_hHook; /* message hook for WH_GETMESSAGE */
91 HHOOK CallWndProc_hHook; /* message hook for WH_CALLWNDPROC */
92 struct tagOleMenuHookItem *next;
93 } OleMenuHookItem;
95 static OleMenuHookItem *hook_list;
98 * This is the lock count on the OLE library. It is controlled by the
99 * OLEInitialize/OLEUninitialize methods.
101 static LONG OLE_moduleLockCount = 0;
104 * Name of our registered window class.
106 static const WCHAR OLEDD_DRAGTRACKERCLASS[] =
107 {'W','i','n','e','D','r','a','g','D','r','o','p','T','r','a','c','k','e','r','3','2',0};
110 * Name of menu descriptor property.
112 static const WCHAR prop_olemenuW[] =
113 {'P','R','O','P','_','O','L','E','M','e','n','u','D','e','s','c','r','i','p','t','o','r',0};
115 /* property to store IDropTarget pointer */
116 static const WCHAR prop_oledroptarget[] =
117 {'O','l','e','D','r','o','p','T','a','r','g','e','t','I','n','t','e','r','f','a','c','e',0};
119 /* property to store Marshalled IDropTarget pointer */
120 static const WCHAR prop_marshalleddroptarget[] =
121 {'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};
123 static const WCHAR clsidfmtW[] =
124 {'C','L','S','I','D','\\','{','%','0','8','x','-','%','0','4','x','-','%','0','4','x','-',
125 '%','0','2','x','%','0','2','x','-','%','0','2','x','%','0','2','x','%','0','2','x','%','0','2','x',
126 '%','0','2','x','%','0','2','x','}','\\',0};
128 static const WCHAR emptyW[] = { 0 };
130 /******************************************************************************
131 * These are the prototypes of miscellaneous utility methods
133 static void OLEUTL_ReadRegistryDWORDValue(HKEY regKey, DWORD* pdwValue);
135 /******************************************************************************
136 * These are the prototypes of the utility methods used to manage a shared menu
138 static void OLEMenu_Initialize(void);
139 static void OLEMenu_UnInitialize(void);
140 static BOOL OLEMenu_InstallHooks( DWORD tid );
141 static BOOL OLEMenu_UnInstallHooks( DWORD tid );
142 static OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid );
143 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos );
144 static BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor );
145 static LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam);
146 static LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam);
148 /******************************************************************************
149 * These are the prototypes of the OLE Clipboard initialization methods (in clipboard.c)
151 extern void OLEClipbrd_UnInitialize(void);
152 extern void OLEClipbrd_Initialize(void);
154 /******************************************************************************
155 * These are the prototypes of the utility methods used for OLE Drag n Drop
157 static void OLEDD_Initialize(void);
158 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
159 static void OLEDD_TrackStateChange(TrackerWindowInfo* trackerInfo);
160 static DWORD OLEDD_GetButtonState(void);
162 /******************************************************************************
163 * OleBuildVersion [OLE32.@]
165 DWORD WINAPI OleBuildVersion(void)
167 TRACE("Returning version %d, build %d.\n", rmm, rup);
168 return (rmm<<16)+rup;
171 /***********************************************************************
172 * OleInitialize (OLE32.@)
174 HRESULT WINAPI OleInitialize(LPVOID reserved)
176 HRESULT hr;
178 TRACE("(%p)\n", reserved);
181 * The first duty of the OleInitialize is to initialize the COM libraries.
183 hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
186 * If the CoInitializeEx call failed, the OLE libraries can't be
187 * initialized.
189 if (FAILED(hr))
190 return hr;
192 if (!COM_CurrentInfo()->ole_inits)
193 hr = S_OK;
196 * Then, it has to initialize the OLE specific modules.
197 * This includes:
198 * Clipboard
199 * Drag and Drop
200 * Object linking and Embedding
201 * In-place activation
203 if (!COM_CurrentInfo()->ole_inits++ &&
204 InterlockedIncrement(&OLE_moduleLockCount) == 1)
207 * Initialize the libraries.
209 TRACE("() - Initializing the OLE libraries\n");
212 * OLE Clipboard
214 OLEClipbrd_Initialize();
217 * Drag and Drop
219 OLEDD_Initialize();
222 * OLE shared menu
224 OLEMenu_Initialize();
227 return hr;
230 /******************************************************************************
231 * OleUninitialize [OLE32.@]
233 void WINAPI DECLSPEC_HOTPATCH OleUninitialize(void)
235 TRACE("()\n");
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 * This implementation of OleRegGetUserType ignores the dwFormOfType
671 * parameter and always returns the full name of the object. This is
672 * not too bad since this is the case for many objects because of the
673 * way they are registered.
675 HRESULT WINAPI OleRegGetUserType(
676 REFCLSID clsid,
677 DWORD dwFormOfType,
678 LPOLESTR* pszUserType)
680 WCHAR keyName[60];
681 DWORD dwKeyType;
682 DWORD cbData;
683 HKEY clsidKey;
684 LONG hres;
687 * Initialize the out parameter.
689 *pszUserType = NULL;
692 * Build the key name we're looking for
694 sprintfW( keyName, clsidfmtW,
695 clsid->Data1, clsid->Data2, clsid->Data3,
696 clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
697 clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );
699 TRACE("(%s, %d, %p)\n", debugstr_w(keyName), dwFormOfType, pszUserType);
702 * Open the class id Key
704 hres = open_classes_key(HKEY_CLASSES_ROOT, keyName, MAXIMUM_ALLOWED, &clsidKey);
705 if (hres != ERROR_SUCCESS)
706 return REGDB_E_CLASSNOTREG;
709 * Retrieve the size of the name string.
711 cbData = 0;
713 hres = RegQueryValueExW(clsidKey,
714 emptyW,
715 NULL,
716 &dwKeyType,
717 NULL,
718 &cbData);
720 if (hres!=ERROR_SUCCESS)
722 RegCloseKey(clsidKey);
723 return REGDB_E_READREGDB;
727 * Allocate a buffer for the registry value.
729 *pszUserType = CoTaskMemAlloc(cbData);
731 if (*pszUserType==NULL)
733 RegCloseKey(clsidKey);
734 return E_OUTOFMEMORY;
737 hres = RegQueryValueExW(clsidKey,
738 emptyW,
739 NULL,
740 &dwKeyType,
741 (LPBYTE) *pszUserType,
742 &cbData);
744 RegCloseKey(clsidKey);
746 if (hres != ERROR_SUCCESS)
748 CoTaskMemFree(*pszUserType);
749 *pszUserType = NULL;
751 return REGDB_E_READREGDB;
754 return S_OK;
757 /***********************************************************************
758 * DoDragDrop [OLE32.@]
760 HRESULT WINAPI DoDragDrop (
761 IDataObject *pDataObject, /* [in] ptr to the data obj */
762 IDropSource* pDropSource, /* [in] ptr to the source obj */
763 DWORD dwOKEffect, /* [in] effects allowed by the source */
764 DWORD *pdwEffect) /* [out] ptr to effects of the source */
766 static const WCHAR trackerW[] = {'T','r','a','c','k','e','r','W','i','n','d','o','w',0};
767 TrackerWindowInfo trackerInfo;
768 HWND hwndTrackWindow;
769 MSG msg;
771 TRACE("(%p, %p, %08x, %p)\n", pDataObject, pDropSource, dwOKEffect, pdwEffect);
773 if (!pDataObject || !pDropSource || !pdwEffect)
774 return E_INVALIDARG;
777 * Setup the drag n drop tracking window.
780 trackerInfo.dataObject = pDataObject;
781 trackerInfo.dropSource = pDropSource;
782 trackerInfo.dwOKEffect = dwOKEffect;
783 trackerInfo.pdwEffect = pdwEffect;
784 trackerInfo.trackingDone = FALSE;
785 trackerInfo.escPressed = FALSE;
786 trackerInfo.curDragTargetHWND = 0;
787 trackerInfo.curTargetHWND = 0;
788 trackerInfo.curDragTarget = 0;
790 hwndTrackWindow = CreateWindowW(OLEDD_DRAGTRACKERCLASS, trackerW,
791 WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT,
792 CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, 0,
793 &trackerInfo);
795 if (hwndTrackWindow)
798 * Capture the mouse input
800 SetCapture(hwndTrackWindow);
802 msg.message = 0;
805 * Pump messages. All mouse input should go to the capture window.
807 while (!trackerInfo.trackingDone && GetMessageW(&msg, 0, 0, 0) )
809 trackerInfo.curMousePos.x = msg.pt.x;
810 trackerInfo.curMousePos.y = msg.pt.y;
811 trackerInfo.dwKeyState = OLEDD_GetButtonState();
813 if ( (msg.message >= WM_KEYFIRST) &&
814 (msg.message <= WM_KEYLAST) )
817 * When keyboard messages are sent to windows on this thread, we
818 * want to ignore notify the drop source that the state changed.
819 * in the case of the Escape key, we also notify the drop source
820 * we give it a special meaning.
822 if ( (msg.message==WM_KEYDOWN) &&
823 (msg.wParam==VK_ESCAPE) )
825 trackerInfo.escPressed = TRUE;
829 * Notify the drop source.
831 OLEDD_TrackStateChange(&trackerInfo);
833 else
836 * Dispatch the messages only when it's not a keyboard message.
838 DispatchMessageW(&msg);
842 /* re-post the quit message to outer message loop */
843 if (msg.message == WM_QUIT)
844 PostQuitMessage(msg.wParam);
846 * Destroy the temporary window.
848 DestroyWindow(hwndTrackWindow);
850 return trackerInfo.returnValue;
853 return E_FAIL;
856 /***********************************************************************
857 * OleQueryLinkFromData [OLE32.@]
859 HRESULT WINAPI OleQueryLinkFromData(
860 IDataObject* pSrcDataObject)
862 FIXME("(%p),stub!\n", pSrcDataObject);
863 return S_FALSE;
866 /***********************************************************************
867 * OleRegGetMiscStatus [OLE32.@]
869 HRESULT WINAPI OleRegGetMiscStatus(
870 REFCLSID clsid,
871 DWORD dwAspect,
872 DWORD* pdwStatus)
874 static const WCHAR miscstatusW[] = {'M','i','s','c','S','t','a','t','u','s',0};
875 static const WCHAR dfmtW[] = {'%','d',0};
876 WCHAR keyName[60];
877 HKEY clsidKey;
878 HKEY miscStatusKey;
879 HKEY aspectKey;
880 LONG result;
883 * Build the key name we're looking for
885 sprintfW( keyName, clsidfmtW,
886 clsid->Data1, clsid->Data2, clsid->Data3,
887 clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
888 clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );
890 TRACE("(%s, %d, %p)\n", debugstr_w(keyName), dwAspect, pdwStatus);
892 if (!pdwStatus) return E_INVALIDARG;
894 *pdwStatus = 0;
896 if (actctx_get_miscstatus(clsid, dwAspect, pdwStatus)) return S_OK;
899 * Open the class id Key
901 result = open_classes_key(HKEY_CLASSES_ROOT, keyName, MAXIMUM_ALLOWED, &clsidKey);
902 if (result != ERROR_SUCCESS)
903 return REGDB_E_CLASSNOTREG;
906 * Get the MiscStatus
908 result = open_classes_key(clsidKey, miscstatusW, MAXIMUM_ALLOWED, &miscStatusKey);
909 if (result != ERROR_SUCCESS)
911 RegCloseKey(clsidKey);
912 return S_OK;
916 * Read the default value
918 OLEUTL_ReadRegistryDWORDValue(miscStatusKey, pdwStatus);
921 * Open the key specific to the requested aspect.
923 sprintfW(keyName, dfmtW, dwAspect);
925 result = open_classes_key(miscStatusKey, keyName, MAXIMUM_ALLOWED, &aspectKey);
926 if (result == ERROR_SUCCESS)
928 OLEUTL_ReadRegistryDWORDValue(aspectKey, pdwStatus);
929 RegCloseKey(aspectKey);
933 * Cleanup
935 RegCloseKey(miscStatusKey);
936 RegCloseKey(clsidKey);
938 return S_OK;
941 static HRESULT EnumOLEVERB_Construct(HKEY hkeyVerb, ULONG index, IEnumOLEVERB **ppenum);
943 typedef struct
945 IEnumOLEVERB IEnumOLEVERB_iface;
946 LONG ref;
948 HKEY hkeyVerb;
949 ULONG index;
950 } EnumOLEVERB;
952 static inline EnumOLEVERB *impl_from_IEnumOLEVERB(IEnumOLEVERB *iface)
954 return CONTAINING_RECORD(iface, EnumOLEVERB, IEnumOLEVERB_iface);
957 static HRESULT WINAPI EnumOLEVERB_QueryInterface(
958 IEnumOLEVERB *iface, REFIID riid, void **ppv)
960 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
961 if (IsEqualIID(riid, &IID_IUnknown) ||
962 IsEqualIID(riid, &IID_IEnumOLEVERB))
964 IEnumOLEVERB_AddRef(iface);
965 *ppv = iface;
966 return S_OK;
968 return E_NOINTERFACE;
971 static ULONG WINAPI EnumOLEVERB_AddRef(
972 IEnumOLEVERB *iface)
974 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
975 TRACE("()\n");
976 return InterlockedIncrement(&This->ref);
979 static ULONG WINAPI EnumOLEVERB_Release(
980 IEnumOLEVERB *iface)
982 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
983 LONG refs = InterlockedDecrement(&This->ref);
984 TRACE("()\n");
985 if (!refs)
987 RegCloseKey(This->hkeyVerb);
988 HeapFree(GetProcessHeap(), 0, This);
990 return refs;
993 static HRESULT WINAPI EnumOLEVERB_Next(
994 IEnumOLEVERB *iface, ULONG celt, LPOLEVERB rgelt,
995 ULONG *pceltFetched)
997 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
998 HRESULT hr = S_OK;
1000 TRACE("(%d, %p, %p)\n", celt, rgelt, pceltFetched);
1002 if (pceltFetched)
1003 *pceltFetched = 0;
1005 for (; celt; celt--, rgelt++)
1007 WCHAR wszSubKey[20];
1008 LONG cbData;
1009 LPWSTR pwszOLEVERB;
1010 LPWSTR pwszMenuFlags;
1011 LPWSTR pwszAttribs;
1012 LONG res = RegEnumKeyW(This->hkeyVerb, This->index, wszSubKey, sizeof(wszSubKey)/sizeof(wszSubKey[0]));
1013 if (res == ERROR_NO_MORE_ITEMS)
1015 hr = S_FALSE;
1016 break;
1018 else if (res != ERROR_SUCCESS)
1020 ERR("RegEnumKeyW failed with error %d\n", res);
1021 hr = REGDB_E_READREGDB;
1022 break;
1024 res = RegQueryValueW(This->hkeyVerb, wszSubKey, NULL, &cbData);
1025 if (res != ERROR_SUCCESS)
1027 ERR("RegQueryValueW failed with error %d\n", res);
1028 hr = REGDB_E_READREGDB;
1029 break;
1031 pwszOLEVERB = CoTaskMemAlloc(cbData);
1032 if (!pwszOLEVERB)
1034 hr = E_OUTOFMEMORY;
1035 break;
1037 res = RegQueryValueW(This->hkeyVerb, wszSubKey, pwszOLEVERB, &cbData);
1038 if (res != ERROR_SUCCESS)
1040 ERR("RegQueryValueW failed with error %d\n", res);
1041 hr = REGDB_E_READREGDB;
1042 CoTaskMemFree(pwszOLEVERB);
1043 break;
1046 TRACE("verb string: %s\n", debugstr_w(pwszOLEVERB));
1047 pwszMenuFlags = strchrW(pwszOLEVERB, ',');
1048 if (!pwszMenuFlags)
1050 hr = OLEOBJ_E_INVALIDVERB;
1051 CoTaskMemFree(pwszOLEVERB);
1052 break;
1054 /* nul terminate the name string and advance to first character */
1055 *pwszMenuFlags = '\0';
1056 pwszMenuFlags++;
1057 pwszAttribs = strchrW(pwszMenuFlags, ',');
1058 if (!pwszAttribs)
1060 hr = OLEOBJ_E_INVALIDVERB;
1061 CoTaskMemFree(pwszOLEVERB);
1062 break;
1064 /* nul terminate the menu string and advance to first character */
1065 *pwszAttribs = '\0';
1066 pwszAttribs++;
1068 /* fill out structure for this verb */
1069 rgelt->lVerb = atolW(wszSubKey);
1070 rgelt->lpszVerbName = pwszOLEVERB; /* user should free */
1071 rgelt->fuFlags = atolW(pwszMenuFlags);
1072 rgelt->grfAttribs = atolW(pwszAttribs);
1074 if (pceltFetched)
1075 (*pceltFetched)++;
1076 This->index++;
1078 return hr;
1081 static HRESULT WINAPI EnumOLEVERB_Skip(
1082 IEnumOLEVERB *iface, ULONG celt)
1084 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
1086 TRACE("(%d)\n", celt);
1088 This->index += celt;
1089 return S_OK;
1092 static HRESULT WINAPI EnumOLEVERB_Reset(
1093 IEnumOLEVERB *iface)
1095 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
1097 TRACE("()\n");
1099 This->index = 0;
1100 return S_OK;
1103 static HRESULT WINAPI EnumOLEVERB_Clone(
1104 IEnumOLEVERB *iface,
1105 IEnumOLEVERB **ppenum)
1107 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
1108 HKEY hkeyVerb;
1109 TRACE("(%p)\n", ppenum);
1110 if (!DuplicateHandle(GetCurrentProcess(), This->hkeyVerb, GetCurrentProcess(), (HANDLE *)&hkeyVerb, 0, FALSE, DUPLICATE_SAME_ACCESS))
1111 return HRESULT_FROM_WIN32(GetLastError());
1112 return EnumOLEVERB_Construct(hkeyVerb, This->index, ppenum);
1115 static const IEnumOLEVERBVtbl EnumOLEVERB_VTable =
1117 EnumOLEVERB_QueryInterface,
1118 EnumOLEVERB_AddRef,
1119 EnumOLEVERB_Release,
1120 EnumOLEVERB_Next,
1121 EnumOLEVERB_Skip,
1122 EnumOLEVERB_Reset,
1123 EnumOLEVERB_Clone
1126 static HRESULT EnumOLEVERB_Construct(HKEY hkeyVerb, ULONG index, IEnumOLEVERB **ppenum)
1128 EnumOLEVERB *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1129 if (!This)
1131 RegCloseKey(hkeyVerb);
1132 return E_OUTOFMEMORY;
1134 This->IEnumOLEVERB_iface.lpVtbl = &EnumOLEVERB_VTable;
1135 This->ref = 1;
1136 This->index = index;
1137 This->hkeyVerb = hkeyVerb;
1138 *ppenum = &This->IEnumOLEVERB_iface;
1139 return S_OK;
1142 /***********************************************************************
1143 * OleRegEnumVerbs [OLE32.@]
1145 * Enumerates verbs associated with a class stored in the registry.
1147 * PARAMS
1148 * clsid [I] Class ID to enumerate the verbs for.
1149 * ppenum [O] Enumerator.
1151 * RETURNS
1152 * S_OK: Success.
1153 * REGDB_E_CLASSNOTREG: The specified class does not have a key in the registry.
1154 * REGDB_E_READREGDB: The class key could not be opened for some other reason.
1155 * OLE_E_REGDB_KEY: The Verb subkey for the class is not present.
1156 * OLEOBJ_E_NOVERBS: The Verb subkey for the class is empty.
1158 HRESULT WINAPI OleRegEnumVerbs (REFCLSID clsid, LPENUMOLEVERB* ppenum)
1160 LONG res;
1161 HKEY hkeyVerb;
1162 DWORD dwSubKeys;
1163 static const WCHAR wszVerb[] = {'V','e','r','b',0};
1165 TRACE("(%s, %p)\n", debugstr_guid(clsid), ppenum);
1167 res = COM_OpenKeyForCLSID(clsid, wszVerb, KEY_READ, &hkeyVerb);
1168 if (FAILED(res))
1170 if (res == REGDB_E_CLASSNOTREG)
1171 ERR("CLSID %s not registered\n", debugstr_guid(clsid));
1172 else if (res == REGDB_E_KEYMISSING)
1173 ERR("no Verbs key for class %s\n", debugstr_guid(clsid));
1174 else
1175 ERR("failed to open Verbs key for CLSID %s with error %d\n",
1176 debugstr_guid(clsid), res);
1177 return res;
1180 res = RegQueryInfoKeyW(hkeyVerb, NULL, NULL, NULL, &dwSubKeys, NULL,
1181 NULL, NULL, NULL, NULL, NULL, NULL);
1182 if (res != ERROR_SUCCESS)
1184 ERR("failed to get subkey count with error %d\n", GetLastError());
1185 return REGDB_E_READREGDB;
1188 if (!dwSubKeys)
1190 WARN("class %s has no verbs\n", debugstr_guid(clsid));
1191 RegCloseKey(hkeyVerb);
1192 return OLEOBJ_E_NOVERBS;
1195 return EnumOLEVERB_Construct(hkeyVerb, 0, ppenum);
1198 /******************************************************************************
1199 * OleSetContainedObject [OLE32.@]
1201 HRESULT WINAPI OleSetContainedObject(
1202 LPUNKNOWN pUnknown,
1203 BOOL fContained)
1205 IRunnableObject* runnable = NULL;
1206 HRESULT hres;
1208 TRACE("(%p,%x)\n", pUnknown, fContained);
1210 hres = IUnknown_QueryInterface(pUnknown,
1211 &IID_IRunnableObject,
1212 (void**)&runnable);
1214 if (SUCCEEDED(hres))
1216 hres = IRunnableObject_SetContainedObject(runnable, fContained);
1218 IRunnableObject_Release(runnable);
1220 return hres;
1223 return S_OK;
1226 /******************************************************************************
1227 * OleRun [OLE32.@]
1229 * Set the OLE object to the running state.
1231 * PARAMS
1232 * pUnknown [I] OLE object to run.
1234 * RETURNS
1235 * Success: S_OK.
1236 * Failure: Any HRESULT code.
1238 HRESULT WINAPI OleRun(LPUNKNOWN pUnknown)
1240 IRunnableObject *runable;
1241 HRESULT hres;
1243 TRACE("(%p)\n", pUnknown);
1245 hres = IUnknown_QueryInterface(pUnknown, &IID_IRunnableObject, (void**)&runable);
1246 if (FAILED(hres))
1247 return S_OK; /* Appears to return no error. */
1249 hres = IRunnableObject_Run(runable, NULL);
1250 IRunnableObject_Release(runable);
1251 return hres;
1254 /******************************************************************************
1255 * OleLoad [OLE32.@]
1257 HRESULT WINAPI OleLoad(
1258 LPSTORAGE pStg,
1259 REFIID riid,
1260 LPOLECLIENTSITE pClientSite,
1261 LPVOID* ppvObj)
1263 IPersistStorage* persistStorage = NULL;
1264 IUnknown* pUnk;
1265 IOleObject* pOleObject = NULL;
1266 STATSTG storageInfo;
1267 HRESULT hres;
1269 TRACE("(%p, %s, %p, %p)\n", pStg, debugstr_guid(riid), pClientSite, ppvObj);
1271 *ppvObj = NULL;
1274 * TODO, Conversion ... OleDoAutoConvert
1278 * Get the class ID for the object.
1280 hres = IStorage_Stat(pStg, &storageInfo, STATFLAG_NONAME);
1281 if (FAILED(hres))
1282 return hres;
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:
2202 case WM_LBUTTONUP:
2203 case WM_MBUTTONUP:
2204 case WM_RBUTTONUP:
2205 case WM_LBUTTONDOWN:
2206 case WM_MBUTTONDOWN:
2207 case WM_RBUTTONDOWN:
2209 TrackerWindowInfo *trackerInfo = (TrackerWindowInfo*)GetWindowLongPtrA(hwnd, 0);
2210 if (trackerInfo->trackingDone) break;
2211 OLEDD_TrackStateChange(trackerInfo);
2212 break;
2214 case WM_DESTROY:
2216 KillTimer(hwnd, DRAG_TIMER_ID);
2217 break;
2222 * This is a window proc after all. Let's call the default.
2224 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
2227 /***
2228 * OLEDD_TrackStateChange()
2230 * This method is invoked while a drag and drop operation is in effect.
2232 * params:
2233 * trackerInfo - Pointer to the structure identifying the
2234 * drag & drop operation that is currently
2235 * active.
2237 static void OLEDD_TrackStateChange(TrackerWindowInfo* trackerInfo)
2239 HWND hwndNewTarget = 0;
2240 HRESULT hr = S_OK;
2241 POINT pt;
2244 * Get the handle of the window under the mouse
2246 pt.x = trackerInfo->curMousePos.x;
2247 pt.y = trackerInfo->curMousePos.y;
2248 hwndNewTarget = WindowFromPoint(pt);
2250 trackerInfo->returnValue = IDropSource_QueryContinueDrag(trackerInfo->dropSource,
2251 trackerInfo->escPressed,
2252 trackerInfo->dwKeyState);
2255 * Every time, we re-initialize the effects passed to the
2256 * IDropTarget to the effects allowed by the source.
2258 *trackerInfo->pdwEffect = trackerInfo->dwOKEffect;
2261 * If we are hovering over the same target as before, send the
2262 * DragOver notification
2264 if ( (trackerInfo->curDragTarget != 0) &&
2265 (trackerInfo->curTargetHWND == hwndNewTarget) )
2267 IDropTarget_DragOver(trackerInfo->curDragTarget,
2268 trackerInfo->dwKeyState,
2269 trackerInfo->curMousePos,
2270 trackerInfo->pdwEffect);
2271 *trackerInfo->pdwEffect &= trackerInfo->dwOKEffect;
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);
2313 *trackerInfo->pdwEffect &= trackerInfo->dwOKEffect;
2315 /* failed DragEnter() means invalid target */
2316 if (hr != S_OK)
2318 trackerInfo->curDragTargetHWND = 0;
2319 trackerInfo->curTargetHWND = 0;
2320 IDropTarget_Release(trackerInfo->curDragTarget);
2321 trackerInfo->curDragTarget = 0;
2325 else
2328 * The mouse is not over a window so we don't track anything.
2330 trackerInfo->curDragTargetHWND = 0;
2331 trackerInfo->curTargetHWND = 0;
2332 if(trackerInfo->curDragTarget) IDropTarget_Release(trackerInfo->curDragTarget);
2333 trackerInfo->curDragTarget = 0;
2338 * Now that we have done that, we have to tell the source to give
2339 * us feedback on the work being done by the target. If we don't
2340 * have a target, simulate no effect.
2342 if (trackerInfo->curDragTarget==0)
2344 *trackerInfo->pdwEffect = DROPEFFECT_NONE;
2347 hr = IDropSource_GiveFeedback(trackerInfo->dropSource,
2348 *trackerInfo->pdwEffect);
2351 * When we ask for feedback from the drop source, sometimes it will
2352 * do all the necessary work and sometimes it will not handle it
2353 * when that's the case, we must display the standard drag and drop
2354 * cursors.
2356 if (hr == DRAGDROP_S_USEDEFAULTCURSORS)
2358 HCURSOR hCur;
2360 if (*trackerInfo->pdwEffect & DROPEFFECT_MOVE)
2362 hCur = LoadCursorW(hProxyDll, MAKEINTRESOURCEW(2));
2364 else if (*trackerInfo->pdwEffect & DROPEFFECT_COPY)
2366 hCur = LoadCursorW(hProxyDll, MAKEINTRESOURCEW(3));
2368 else if (*trackerInfo->pdwEffect & DROPEFFECT_LINK)
2370 hCur = LoadCursorW(hProxyDll, MAKEINTRESOURCEW(4));
2372 else
2374 hCur = LoadCursorW(hProxyDll, MAKEINTRESOURCEW(1));
2377 SetCursor(hCur);
2381 * All the return valued will stop the operation except the S_OK
2382 * return value.
2384 if (trackerInfo->returnValue!=S_OK)
2387 * Make sure the message loop in DoDragDrop stops
2389 trackerInfo->trackingDone = TRUE;
2392 * Release the mouse in case the drop target decides to show a popup
2393 * or a menu or something.
2395 ReleaseCapture();
2398 * If we end-up over a target, drop the object in the target or
2399 * inform the target that the operation was cancelled.
2401 if (trackerInfo->curDragTarget)
2403 switch (trackerInfo->returnValue)
2406 * If the source wants us to complete the operation, we tell
2407 * the drop target that we just dropped the object in it.
2409 case DRAGDROP_S_DROP:
2410 if (*trackerInfo->pdwEffect != DROPEFFECT_NONE)
2412 hr = IDropTarget_Drop(trackerInfo->curDragTarget, trackerInfo->dataObject,
2413 trackerInfo->dwKeyState, trackerInfo->curMousePos, trackerInfo->pdwEffect);
2414 if (FAILED(hr))
2415 trackerInfo->returnValue = hr;
2417 else
2418 IDropTarget_DragLeave(trackerInfo->curDragTarget);
2419 break;
2422 * If the source told us that we should cancel, fool the drop
2423 * target by telling it that the mouse left its window.
2424 * Also set the drop effect to "NONE" in case the application
2425 * ignores the result of DoDragDrop.
2427 case DRAGDROP_S_CANCEL:
2428 IDropTarget_DragLeave(trackerInfo->curDragTarget);
2429 *trackerInfo->pdwEffect = DROPEFFECT_NONE;
2430 break;
2436 /***
2437 * OLEDD_GetButtonState()
2439 * This method will use the current state of the keyboard to build
2440 * a button state mask equivalent to the one passed in the
2441 * WM_MOUSEMOVE wParam.
2443 static DWORD OLEDD_GetButtonState(void)
2445 BYTE keyboardState[256];
2446 DWORD keyMask = 0;
2448 GetKeyboardState(keyboardState);
2450 if ( (keyboardState[VK_SHIFT] & 0x80) !=0)
2451 keyMask |= MK_SHIFT;
2453 if ( (keyboardState[VK_CONTROL] & 0x80) !=0)
2454 keyMask |= MK_CONTROL;
2456 if ( (keyboardState[VK_MENU] & 0x80) !=0)
2457 keyMask |= MK_ALT;
2459 if ( (keyboardState[VK_LBUTTON] & 0x80) !=0)
2460 keyMask |= MK_LBUTTON;
2462 if ( (keyboardState[VK_RBUTTON] & 0x80) !=0)
2463 keyMask |= MK_RBUTTON;
2465 if ( (keyboardState[VK_MBUTTON] & 0x80) !=0)
2466 keyMask |= MK_MBUTTON;
2468 return keyMask;
2471 /***
2472 * OLEDD_GetButtonState()
2474 * This method will read the default value of the registry key in
2475 * parameter and extract a DWORD value from it. The registry key value
2476 * can be in a string key or a DWORD key.
2478 * params:
2479 * regKey - Key to read the default value from
2480 * pdwValue - Pointer to the location where the DWORD
2481 * value is returned. This value is not modified
2482 * if the value is not found.
2485 static void OLEUTL_ReadRegistryDWORDValue(
2486 HKEY regKey,
2487 DWORD* pdwValue)
2489 WCHAR buffer[20];
2490 DWORD cbData = sizeof(buffer);
2491 DWORD dwKeyType;
2492 LONG lres;
2494 lres = RegQueryValueExW(regKey,
2495 emptyW,
2496 NULL,
2497 &dwKeyType,
2498 (LPBYTE)buffer,
2499 &cbData);
2501 if (lres==ERROR_SUCCESS)
2503 switch (dwKeyType)
2505 case REG_DWORD:
2506 *pdwValue = *(DWORD*)buffer;
2507 break;
2508 case REG_EXPAND_SZ:
2509 case REG_MULTI_SZ:
2510 case REG_SZ:
2511 *pdwValue = (DWORD)strtoulW(buffer, NULL, 10);
2512 break;
2517 /******************************************************************************
2518 * OleDraw (OLE32.@)
2520 * The operation of this function is documented literally in the WinAPI
2521 * documentation to involve a QueryInterface for the IViewObject interface,
2522 * followed by a call to IViewObject::Draw.
2524 HRESULT WINAPI OleDraw(
2525 IUnknown *pUnk,
2526 DWORD dwAspect,
2527 HDC hdcDraw,
2528 LPCRECT rect)
2530 HRESULT hres;
2531 IViewObject *viewobject;
2533 if (!pUnk) return E_INVALIDARG;
2535 hres = IUnknown_QueryInterface(pUnk,
2536 &IID_IViewObject,
2537 (void**)&viewobject);
2538 if (SUCCEEDED(hres))
2540 hres = IViewObject_Draw(viewobject, dwAspect, -1, 0, 0, 0, hdcDraw, (RECTL*)rect, 0, 0, 0);
2541 IViewObject_Release(viewobject);
2542 return hres;
2544 else
2545 return DV_E_NOIVIEWOBJECT;
2548 /***********************************************************************
2549 * OleTranslateAccelerator [OLE32.@]
2551 HRESULT WINAPI OleTranslateAccelerator (LPOLEINPLACEFRAME lpFrame,
2552 LPOLEINPLACEFRAMEINFO lpFrameInfo, LPMSG lpmsg)
2554 WORD wID;
2556 TRACE("(%p,%p,%p)\n", lpFrame, lpFrameInfo, lpmsg);
2558 if (IsAccelerator(lpFrameInfo->haccel,lpFrameInfo->cAccelEntries,lpmsg,&wID))
2559 return IOleInPlaceFrame_TranslateAccelerator(lpFrame,lpmsg,wID);
2561 return S_FALSE;
2564 /******************************************************************************
2565 * OleCreate [OLE32.@]
2568 HRESULT WINAPI OleCreate(
2569 REFCLSID rclsid,
2570 REFIID riid,
2571 DWORD renderopt,
2572 LPFORMATETC pFormatEtc,
2573 LPOLECLIENTSITE pClientSite,
2574 LPSTORAGE pStg,
2575 LPVOID* ppvObj)
2577 HRESULT hres;
2578 IUnknown * pUnk = NULL;
2579 IOleObject *pOleObject = NULL;
2581 TRACE("(%s, %s, %d, %p, %p, %p, %p)\n", debugstr_guid(rclsid),
2582 debugstr_guid(riid), renderopt, pFormatEtc, pClientSite, pStg, ppvObj);
2584 hres = CoCreateInstance(rclsid, 0, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER, riid, (LPVOID*)&pUnk);
2586 if (SUCCEEDED(hres))
2587 hres = IStorage_SetClass(pStg, rclsid);
2589 if (pClientSite && SUCCEEDED(hres))
2591 hres = IUnknown_QueryInterface(pUnk, &IID_IOleObject, (LPVOID*)&pOleObject);
2592 if (SUCCEEDED(hres))
2594 DWORD dwStatus;
2595 hres = IOleObject_GetMiscStatus(pOleObject, DVASPECT_CONTENT, &dwStatus);
2599 if (SUCCEEDED(hres))
2601 IPersistStorage * pPS;
2602 if (SUCCEEDED((hres = IUnknown_QueryInterface(pUnk, &IID_IPersistStorage, (LPVOID*)&pPS))))
2604 TRACE("trying to set stg %p\n", pStg);
2605 hres = IPersistStorage_InitNew(pPS, pStg);
2606 TRACE("-- result 0x%08x\n", hres);
2607 IPersistStorage_Release(pPS);
2611 if (pClientSite && SUCCEEDED(hres))
2613 TRACE("trying to set clientsite %p\n", pClientSite);
2614 hres = IOleObject_SetClientSite(pOleObject, pClientSite);
2615 TRACE("-- result 0x%08x\n", hres);
2618 if (pOleObject)
2619 IOleObject_Release(pOleObject);
2621 if (((renderopt == OLERENDER_DRAW) || (renderopt == OLERENDER_FORMAT)) &&
2622 SUCCEEDED(hres))
2624 IRunnableObject *pRunnable;
2625 IOleCache *pOleCache;
2626 HRESULT hres2;
2628 hres2 = IUnknown_QueryInterface(pUnk, &IID_IRunnableObject, (void **)&pRunnable);
2629 if (SUCCEEDED(hres2))
2631 hres = IRunnableObject_Run(pRunnable, NULL);
2632 IRunnableObject_Release(pRunnable);
2635 if (SUCCEEDED(hres))
2637 hres2 = IUnknown_QueryInterface(pUnk, &IID_IOleCache, (void **)&pOleCache);
2638 if (SUCCEEDED(hres2))
2640 DWORD dwConnection;
2641 if (renderopt == OLERENDER_DRAW && !pFormatEtc) {
2642 FORMATETC pfe;
2643 pfe.cfFormat = 0;
2644 pfe.ptd = NULL;
2645 pfe.dwAspect = DVASPECT_CONTENT;
2646 pfe.lindex = -1;
2647 pfe.tymed = TYMED_NULL;
2648 hres = IOleCache_Cache(pOleCache, &pfe, ADVF_PRIMEFIRST, &dwConnection);
2650 else
2651 hres = IOleCache_Cache(pOleCache, pFormatEtc, ADVF_PRIMEFIRST, &dwConnection);
2652 IOleCache_Release(pOleCache);
2657 if (FAILED(hres) && pUnk)
2659 IUnknown_Release(pUnk);
2660 pUnk = NULL;
2663 *ppvObj = pUnk;
2665 TRACE("-- %p\n", pUnk);
2666 return hres;
2669 /******************************************************************************
2670 * OleGetAutoConvert [OLE32.@]
2672 HRESULT WINAPI OleGetAutoConvert(REFCLSID clsidOld, LPCLSID pClsidNew)
2674 static const WCHAR wszAutoConvertTo[] = {'A','u','t','o','C','o','n','v','e','r','t','T','o',0};
2675 HKEY hkey = NULL;
2676 WCHAR buf[CHARS_IN_GUID];
2677 LONG len;
2678 HRESULT res = S_OK;
2680 res = COM_OpenKeyForCLSID(clsidOld, wszAutoConvertTo, KEY_READ, &hkey);
2681 if (FAILED(res))
2682 goto done;
2684 len = sizeof(buf);
2685 if (RegQueryValueW(hkey, NULL, buf, &len))
2687 res = REGDB_E_KEYMISSING;
2688 goto done;
2690 res = CLSIDFromString(buf, pClsidNew);
2691 done:
2692 if (hkey) RegCloseKey(hkey);
2693 return res;
2696 /******************************************************************************
2697 * OleSetAutoConvert [OLE32.@]
2699 HRESULT WINAPI OleSetAutoConvert(REFCLSID clsidOld, REFCLSID clsidNew)
2701 static const WCHAR wszAutoConvertTo[] = {'A','u','t','o','C','o','n','v','e','r','t','T','o',0};
2702 HKEY hkey = NULL;
2703 WCHAR szClsidNew[CHARS_IN_GUID];
2704 HRESULT res = S_OK;
2706 TRACE("(%s,%s)\n", debugstr_guid(clsidOld), debugstr_guid(clsidNew));
2708 res = COM_OpenKeyForCLSID(clsidOld, NULL, KEY_READ | KEY_WRITE, &hkey);
2709 if (FAILED(res))
2710 goto done;
2711 StringFromGUID2(clsidNew, szClsidNew, CHARS_IN_GUID);
2712 if (RegSetValueW(hkey, wszAutoConvertTo, REG_SZ, szClsidNew, (strlenW(szClsidNew)+1) * sizeof(WCHAR)))
2714 res = REGDB_E_WRITEREGDB;
2715 goto done;
2718 done:
2719 if (hkey) RegCloseKey(hkey);
2720 return res;
2723 /******************************************************************************
2724 * OleDoAutoConvert [OLE32.@]
2726 HRESULT WINAPI OleDoAutoConvert(LPSTORAGE pStg, LPCLSID pClsidNew)
2728 WCHAR *user_type_old, *user_type_new;
2729 CLIPFORMAT cf;
2730 STATSTG stat;
2731 CLSID clsid;
2732 HRESULT hr;
2734 TRACE("(%p, %p)\n", pStg, pClsidNew);
2736 *pClsidNew = CLSID_NULL;
2737 if(!pStg)
2738 return E_INVALIDARG;
2739 hr = IStorage_Stat(pStg, &stat, STATFLAG_NONAME);
2740 if(FAILED(hr))
2741 return hr;
2743 *pClsidNew = stat.clsid;
2744 hr = OleGetAutoConvert(&stat.clsid, &clsid);
2745 if(FAILED(hr))
2746 return hr;
2748 hr = IStorage_SetClass(pStg, &clsid);
2749 if(FAILED(hr))
2750 return hr;
2752 hr = ReadFmtUserTypeStg(pStg, &cf, &user_type_old);
2753 if(FAILED(hr)) {
2754 cf = 0;
2755 user_type_new = NULL;
2758 hr = OleRegGetUserType(&clsid, USERCLASSTYPE_FULL, &user_type_new);
2759 if(FAILED(hr))
2760 user_type_new = NULL;
2762 hr = WriteFmtUserTypeStg(pStg, cf, user_type_new);
2763 CoTaskMemFree(user_type_new);
2764 if(FAILED(hr))
2766 CoTaskMemFree(user_type_old);
2767 IStorage_SetClass(pStg, &stat.clsid);
2768 return hr;
2771 hr = SetConvertStg(pStg, TRUE);
2772 if(FAILED(hr))
2774 WriteFmtUserTypeStg(pStg, cf, user_type_old);
2775 IStorage_SetClass(pStg, &stat.clsid);
2777 else
2778 *pClsidNew = clsid;
2779 CoTaskMemFree(user_type_old);
2780 return hr;
2783 /******************************************************************************
2784 * OleIsRunning [OLE32.@]
2786 BOOL WINAPI OleIsRunning(LPOLEOBJECT object)
2788 IRunnableObject *pRunnable;
2789 HRESULT hr;
2790 BOOL running;
2792 TRACE("(%p)\n", object);
2794 if (!object) return FALSE;
2796 hr = IOleObject_QueryInterface(object, &IID_IRunnableObject, (void **)&pRunnable);
2797 if (FAILED(hr))
2798 return TRUE;
2799 running = IRunnableObject_IsRunning(pRunnable);
2800 IRunnableObject_Release(pRunnable);
2801 return running;
2804 /***********************************************************************
2805 * OleNoteObjectVisible [OLE32.@]
2807 HRESULT WINAPI OleNoteObjectVisible(LPUNKNOWN pUnknown, BOOL bVisible)
2809 TRACE("(%p, %s)\n", pUnknown, bVisible ? "TRUE" : "FALSE");
2810 return CoLockObjectExternal(pUnknown, bVisible, TRUE);
2814 /***********************************************************************
2815 * OLE_FreeClipDataArray [internal]
2817 * NOTES:
2818 * frees the data associated with an array of CLIPDATAs
2820 static void OLE_FreeClipDataArray(ULONG count, CLIPDATA * pClipDataArray)
2822 ULONG i;
2823 for (i = 0; i < count; i++)
2824 if (pClipDataArray[i].pClipData)
2825 CoTaskMemFree(pClipDataArray[i].pClipData);
2828 /***********************************************************************
2829 * PropSysAllocString [OLE32.@]
2830 * NOTES
2831 * Forward to oleaut32.
2833 BSTR WINAPI PropSysAllocString(LPCOLESTR str)
2835 return SysAllocString(str);
2838 /***********************************************************************
2839 * PropSysFreeString [OLE32.@]
2840 * NOTES
2841 * Forward to oleaut32.
2843 void WINAPI PropSysFreeString(LPOLESTR str)
2845 SysFreeString(str);
2848 /******************************************************************************
2849 * Check if a PROPVARIANT's type is valid.
2851 static inline HRESULT PROPVARIANT_ValidateType(VARTYPE vt)
2853 switch (vt)
2855 case VT_EMPTY:
2856 case VT_NULL:
2857 case VT_I1:
2858 case VT_I2:
2859 case VT_I4:
2860 case VT_I8:
2861 case VT_R4:
2862 case VT_R8:
2863 case VT_CY:
2864 case VT_DATE:
2865 case VT_BSTR:
2866 case VT_ERROR:
2867 case VT_BOOL:
2868 case VT_DECIMAL:
2869 case VT_UI1:
2870 case VT_UI2:
2871 case VT_UI4:
2872 case VT_UI8:
2873 case VT_INT:
2874 case VT_UINT:
2875 case VT_LPSTR:
2876 case VT_LPWSTR:
2877 case VT_FILETIME:
2878 case VT_BLOB:
2879 case VT_DISPATCH:
2880 case VT_UNKNOWN:
2881 case VT_STREAM:
2882 case VT_STORAGE:
2883 case VT_STREAMED_OBJECT:
2884 case VT_STORED_OBJECT:
2885 case VT_BLOB_OBJECT:
2886 case VT_CF:
2887 case VT_CLSID:
2888 case VT_I1|VT_VECTOR:
2889 case VT_I2|VT_VECTOR:
2890 case VT_I4|VT_VECTOR:
2891 case VT_I8|VT_VECTOR:
2892 case VT_R4|VT_VECTOR:
2893 case VT_R8|VT_VECTOR:
2894 case VT_CY|VT_VECTOR:
2895 case VT_DATE|VT_VECTOR:
2896 case VT_BSTR|VT_VECTOR:
2897 case VT_ERROR|VT_VECTOR:
2898 case VT_BOOL|VT_VECTOR:
2899 case VT_VARIANT|VT_VECTOR:
2900 case VT_UI1|VT_VECTOR:
2901 case VT_UI2|VT_VECTOR:
2902 case VT_UI4|VT_VECTOR:
2903 case VT_UI8|VT_VECTOR:
2904 case VT_LPSTR|VT_VECTOR:
2905 case VT_LPWSTR|VT_VECTOR:
2906 case VT_FILETIME|VT_VECTOR:
2907 case VT_CF|VT_VECTOR:
2908 case VT_CLSID|VT_VECTOR:
2909 return S_OK;
2911 WARN("Bad type %d\n", vt);
2912 return STG_E_INVALIDPARAMETER;
2915 /***********************************************************************
2916 * PropVariantClear [OLE32.@]
2918 HRESULT WINAPI PropVariantClear(PROPVARIANT * pvar) /* [in/out] */
2920 HRESULT hr;
2922 TRACE("(%p)\n", pvar);
2924 if (!pvar)
2925 return S_OK;
2927 hr = PROPVARIANT_ValidateType(pvar->vt);
2928 if (FAILED(hr))
2930 memset(pvar, 0, sizeof(*pvar));
2931 return hr;
2934 switch(pvar->vt)
2936 case VT_EMPTY:
2937 case VT_NULL:
2938 case VT_I1:
2939 case VT_I2:
2940 case VT_I4:
2941 case VT_I8:
2942 case VT_R4:
2943 case VT_R8:
2944 case VT_CY:
2945 case VT_DATE:
2946 case VT_ERROR:
2947 case VT_BOOL:
2948 case VT_DECIMAL:
2949 case VT_UI1:
2950 case VT_UI2:
2951 case VT_UI4:
2952 case VT_UI8:
2953 case VT_INT:
2954 case VT_UINT:
2955 case VT_FILETIME:
2956 break;
2957 case VT_DISPATCH:
2958 case VT_UNKNOWN:
2959 case VT_STREAM:
2960 case VT_STREAMED_OBJECT:
2961 case VT_STORAGE:
2962 case VT_STORED_OBJECT:
2963 if (pvar->u.pStream)
2964 IStream_Release(pvar->u.pStream);
2965 break;
2966 case VT_CLSID:
2967 case VT_LPSTR:
2968 case VT_LPWSTR:
2969 /* pick an arbitrary typed pointer - we don't care about the type
2970 * as we are just freeing it */
2971 CoTaskMemFree(pvar->u.puuid);
2972 break;
2973 case VT_BLOB:
2974 case VT_BLOB_OBJECT:
2975 CoTaskMemFree(pvar->u.blob.pBlobData);
2976 break;
2977 case VT_BSTR:
2978 PropSysFreeString(pvar->u.bstrVal);
2979 break;
2980 case VT_CF:
2981 if (pvar->u.pclipdata)
2983 OLE_FreeClipDataArray(1, pvar->u.pclipdata);
2984 CoTaskMemFree(pvar->u.pclipdata);
2986 break;
2987 default:
2988 if (pvar->vt & VT_VECTOR)
2990 ULONG i;
2992 switch (pvar->vt & ~VT_VECTOR)
2994 case VT_VARIANT:
2995 FreePropVariantArray(pvar->u.capropvar.cElems, pvar->u.capropvar.pElems);
2996 break;
2997 case VT_CF:
2998 OLE_FreeClipDataArray(pvar->u.caclipdata.cElems, pvar->u.caclipdata.pElems);
2999 break;
3000 case VT_BSTR:
3001 for (i = 0; i < pvar->u.cabstr.cElems; i++)
3002 PropSysFreeString(pvar->u.cabstr.pElems[i]);
3003 break;
3004 case VT_LPSTR:
3005 for (i = 0; i < pvar->u.calpstr.cElems; i++)
3006 CoTaskMemFree(pvar->u.calpstr.pElems[i]);
3007 break;
3008 case VT_LPWSTR:
3009 for (i = 0; i < pvar->u.calpwstr.cElems; i++)
3010 CoTaskMemFree(pvar->u.calpwstr.pElems[i]);
3011 break;
3013 if (pvar->vt & ~VT_VECTOR)
3015 /* pick an arbitrary VT_VECTOR structure - they all have the same
3016 * memory layout */
3017 CoTaskMemFree(pvar->u.capropvar.pElems);
3020 else
3022 WARN("Invalid/unsupported type %d\n", pvar->vt);
3023 hr = STG_E_INVALIDPARAMETER;
3027 memset(pvar, 0, sizeof(*pvar));
3028 return hr;
3031 /***********************************************************************
3032 * PropVariantCopy [OLE32.@]
3034 HRESULT WINAPI PropVariantCopy(PROPVARIANT *pvarDest, /* [out] */
3035 const PROPVARIANT *pvarSrc) /* [in] */
3037 ULONG len;
3038 HRESULT hr;
3040 TRACE("(%p, %p vt %04x)\n", pvarDest, pvarSrc, pvarSrc->vt);
3042 hr = PROPVARIANT_ValidateType(pvarSrc->vt);
3043 if (FAILED(hr))
3044 return hr;
3046 /* this will deal with most cases */
3047 *pvarDest = *pvarSrc;
3049 switch(pvarSrc->vt)
3051 case VT_EMPTY:
3052 case VT_NULL:
3053 case VT_I1:
3054 case VT_UI1:
3055 case VT_I2:
3056 case VT_UI2:
3057 case VT_BOOL:
3058 case VT_DECIMAL:
3059 case VT_I4:
3060 case VT_UI4:
3061 case VT_R4:
3062 case VT_ERROR:
3063 case VT_I8:
3064 case VT_UI8:
3065 case VT_INT:
3066 case VT_UINT:
3067 case VT_R8:
3068 case VT_CY:
3069 case VT_DATE:
3070 case VT_FILETIME:
3071 break;
3072 case VT_DISPATCH:
3073 case VT_UNKNOWN:
3074 case VT_STREAM:
3075 case VT_STREAMED_OBJECT:
3076 case VT_STORAGE:
3077 case VT_STORED_OBJECT:
3078 if (pvarDest->u.pStream)
3079 IStream_AddRef(pvarDest->u.pStream);
3080 break;
3081 case VT_CLSID:
3082 pvarDest->u.puuid = CoTaskMemAlloc(sizeof(CLSID));
3083 *pvarDest->u.puuid = *pvarSrc->u.puuid;
3084 break;
3085 case VT_LPSTR:
3086 if (pvarSrc->u.pszVal)
3088 len = strlen(pvarSrc->u.pszVal);
3089 pvarDest->u.pszVal = CoTaskMemAlloc((len+1)*sizeof(CHAR));
3090 CopyMemory(pvarDest->u.pszVal, pvarSrc->u.pszVal, (len+1)*sizeof(CHAR));
3092 break;
3093 case VT_LPWSTR:
3094 if (pvarSrc->u.pwszVal)
3096 len = lstrlenW(pvarSrc->u.pwszVal);
3097 pvarDest->u.pwszVal = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
3098 CopyMemory(pvarDest->u.pwszVal, pvarSrc->u.pwszVal, (len+1)*sizeof(WCHAR));
3100 break;
3101 case VT_BLOB:
3102 case VT_BLOB_OBJECT:
3103 if (pvarSrc->u.blob.pBlobData)
3105 len = pvarSrc->u.blob.cbSize;
3106 pvarDest->u.blob.pBlobData = CoTaskMemAlloc(len);
3107 CopyMemory(pvarDest->u.blob.pBlobData, pvarSrc->u.blob.pBlobData, len);
3109 break;
3110 case VT_BSTR:
3111 pvarDest->u.bstrVal = PropSysAllocString(pvarSrc->u.bstrVal);
3112 break;
3113 case VT_CF:
3114 if (pvarSrc->u.pclipdata)
3116 len = pvarSrc->u.pclipdata->cbSize - sizeof(pvarSrc->u.pclipdata->ulClipFmt);
3117 pvarDest->u.pclipdata = CoTaskMemAlloc(sizeof (CLIPDATA));
3118 pvarDest->u.pclipdata->cbSize = pvarSrc->u.pclipdata->cbSize;
3119 pvarDest->u.pclipdata->ulClipFmt = pvarSrc->u.pclipdata->ulClipFmt;
3120 pvarDest->u.pclipdata->pClipData = CoTaskMemAlloc(len);
3121 CopyMemory(pvarDest->u.pclipdata->pClipData, pvarSrc->u.pclipdata->pClipData, len);
3123 break;
3124 default:
3125 if (pvarSrc->vt & VT_VECTOR)
3127 int elemSize;
3128 ULONG i;
3130 switch(pvarSrc->vt & ~VT_VECTOR)
3132 case VT_I1: elemSize = sizeof(pvarSrc->u.cVal); break;
3133 case VT_UI1: elemSize = sizeof(pvarSrc->u.bVal); break;
3134 case VT_I2: elemSize = sizeof(pvarSrc->u.iVal); break;
3135 case VT_UI2: elemSize = sizeof(pvarSrc->u.uiVal); break;
3136 case VT_BOOL: elemSize = sizeof(pvarSrc->u.boolVal); break;
3137 case VT_I4: elemSize = sizeof(pvarSrc->u.lVal); break;
3138 case VT_UI4: elemSize = sizeof(pvarSrc->u.ulVal); break;
3139 case VT_R4: elemSize = sizeof(pvarSrc->u.fltVal); break;
3140 case VT_R8: elemSize = sizeof(pvarSrc->u.dblVal); break;
3141 case VT_ERROR: elemSize = sizeof(pvarSrc->u.scode); break;
3142 case VT_I8: elemSize = sizeof(pvarSrc->u.hVal); break;
3143 case VT_UI8: elemSize = sizeof(pvarSrc->u.uhVal); break;
3144 case VT_CY: elemSize = sizeof(pvarSrc->u.cyVal); break;
3145 case VT_DATE: elemSize = sizeof(pvarSrc->u.date); break;
3146 case VT_FILETIME: elemSize = sizeof(pvarSrc->u.filetime); break;
3147 case VT_CLSID: elemSize = sizeof(*pvarSrc->u.puuid); break;
3148 case VT_CF: elemSize = sizeof(*pvarSrc->u.pclipdata); break;
3149 case VT_BSTR: elemSize = sizeof(pvarSrc->u.bstrVal); break;
3150 case VT_LPSTR: elemSize = sizeof(pvarSrc->u.pszVal); break;
3151 case VT_LPWSTR: elemSize = sizeof(pvarSrc->u.pwszVal); break;
3152 case VT_VARIANT: elemSize = sizeof(*pvarSrc->u.pvarVal); break;
3154 default:
3155 FIXME("Invalid element type: %ul\n", pvarSrc->vt & ~VT_VECTOR);
3156 return E_INVALIDARG;
3158 len = pvarSrc->u.capropvar.cElems;
3159 pvarDest->u.capropvar.pElems = len ? CoTaskMemAlloc(len * elemSize) : NULL;
3160 if (pvarSrc->vt == (VT_VECTOR | VT_VARIANT))
3162 for (i = 0; i < len; i++)
3163 PropVariantCopy(&pvarDest->u.capropvar.pElems[i], &pvarSrc->u.capropvar.pElems[i]);
3165 else if (pvarSrc->vt == (VT_VECTOR | VT_CF))
3167 FIXME("Copy clipformats\n");
3169 else if (pvarSrc->vt == (VT_VECTOR | VT_BSTR))
3171 for (i = 0; i < len; i++)
3172 pvarDest->u.cabstr.pElems[i] = PropSysAllocString(pvarSrc->u.cabstr.pElems[i]);
3174 else if (pvarSrc->vt == (VT_VECTOR | VT_LPSTR))
3176 size_t strLen;
3177 for (i = 0; i < len; i++)
3179 strLen = lstrlenA(pvarSrc->u.calpstr.pElems[i]) + 1;
3180 pvarDest->u.calpstr.pElems[i] = CoTaskMemAlloc(strLen);
3181 memcpy(pvarDest->u.calpstr.pElems[i],
3182 pvarSrc->u.calpstr.pElems[i], strLen);
3185 else if (pvarSrc->vt == (VT_VECTOR | VT_LPWSTR))
3187 size_t strLen;
3188 for (i = 0; i < len; i++)
3190 strLen = (lstrlenW(pvarSrc->u.calpwstr.pElems[i]) + 1) *
3191 sizeof(WCHAR);
3192 pvarDest->u.calpstr.pElems[i] = CoTaskMemAlloc(strLen);
3193 memcpy(pvarDest->u.calpstr.pElems[i],
3194 pvarSrc->u.calpstr.pElems[i], strLen);
3197 else
3198 CopyMemory(pvarDest->u.capropvar.pElems, pvarSrc->u.capropvar.pElems, len * elemSize);
3200 else
3201 WARN("Invalid/unsupported type %d\n", pvarSrc->vt);
3204 return S_OK;
3207 /***********************************************************************
3208 * FreePropVariantArray [OLE32.@]
3210 HRESULT WINAPI FreePropVariantArray(ULONG cVariants, /* [in] */
3211 PROPVARIANT *rgvars) /* [in/out] */
3213 ULONG i;
3215 TRACE("(%u, %p)\n", cVariants, rgvars);
3217 if (!rgvars)
3218 return E_INVALIDARG;
3220 for(i = 0; i < cVariants; i++)
3221 PropVariantClear(&rgvars[i]);
3223 return S_OK;
3226 /******************************************************************************
3227 * DllDebugObjectRPCHook (OLE32.@)
3228 * turns on and off internal debugging, pointer is only used on macintosh
3231 BOOL WINAPI DllDebugObjectRPCHook(BOOL b, void *dummy)
3233 FIXME("stub\n");
3234 return TRUE;