msvcrt: Add __ExceptionPtr* functions implementation.
[wine.git] / dlls / ole32 / ole2.c
bloba017c1b84ac808ef174c7cbd3d90291708acef73
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_TrackStateChange(TrackerWindowInfo* trackerInfo);
161 static DWORD OLEDD_GetButtonState(void);
163 /******************************************************************************
164 * OleBuildVersion [OLE32.@]
166 DWORD WINAPI OleBuildVersion(void)
168 TRACE("Returning version %d, build %d.\n", rmm, rup);
169 return (rmm<<16)+rup;
172 /***********************************************************************
173 * OleInitialize (OLE32.@)
175 HRESULT WINAPI OleInitialize(LPVOID reserved)
177 HRESULT hr;
179 TRACE("(%p)\n", reserved);
182 * The first duty of the OleInitialize is to initialize the COM libraries.
184 hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
187 * If the CoInitializeEx call failed, the OLE libraries can't be
188 * initialized.
190 if (FAILED(hr))
191 return hr;
193 if (!COM_CurrentInfo()->ole_inits)
194 hr = S_OK;
197 * Then, it has to initialize the OLE specific modules.
198 * This includes:
199 * Clipboard
200 * Drag and Drop
201 * Object linking and Embedding
202 * In-place activation
204 if (!COM_CurrentInfo()->ole_inits++ &&
205 InterlockedIncrement(&OLE_moduleLockCount) == 1)
208 * Initialize the libraries.
210 TRACE("() - Initializing the OLE libraries\n");
213 * OLE Clipboard
215 OLEClipbrd_Initialize();
218 * Drag and Drop
220 OLEDD_Initialize();
223 * OLE shared menu
225 OLEMenu_Initialize();
228 return hr;
231 /******************************************************************************
232 * OleUninitialize [OLE32.@]
234 void WINAPI OleUninitialize(void)
236 TRACE("()\n");
239 * If we hit the bottom of the lock stack, free the libraries.
241 if (!--COM_CurrentInfo()->ole_inits && !InterlockedDecrement(&OLE_moduleLockCount))
244 * Actually free the libraries.
246 TRACE("() - Freeing the last reference count\n");
249 * OLE Clipboard
251 OLEClipbrd_UnInitialize();
254 * OLE shared menu
256 OLEMenu_UnInitialize();
260 * Then, uninitialize the COM libraries.
262 CoUninitialize();
265 /******************************************************************************
266 * OleInitializeWOW [OLE32.@]
268 HRESULT WINAPI OleInitializeWOW(DWORD x, DWORD y) {
269 FIXME("(0x%08x, 0x%08x),stub!\n",x, y);
270 return 0;
273 /*************************************************************
274 * get_droptarget_handle
276 * Retrieve a handle to the map containing the marshalled IDropTarget.
277 * This handle belongs to the process that called RegisterDragDrop.
278 * See get_droptarget_local_handle().
280 static inline HANDLE get_droptarget_handle(HWND hwnd)
282 return GetPropW(hwnd, prop_marshalleddroptarget);
285 /*************************************************************
286 * is_droptarget
288 * Is the window a droptarget.
290 static inline BOOL is_droptarget(HWND hwnd)
292 return get_droptarget_handle(hwnd) != 0;
295 /*************************************************************
296 * get_droptarget_local_handle
298 * Retrieve a handle to the map containing the marshalled IDropTarget.
299 * The handle should be closed when finished with.
301 static HANDLE get_droptarget_local_handle(HWND hwnd)
303 HANDLE handle, local_handle = 0;
305 handle = get_droptarget_handle(hwnd);
307 if(handle)
309 DWORD pid;
310 HANDLE process;
312 GetWindowThreadProcessId(hwnd, &pid);
313 process = OpenProcess(PROCESS_DUP_HANDLE, FALSE, pid);
314 if(process)
316 DuplicateHandle(process, handle, GetCurrentProcess(), &local_handle, 0, FALSE, DUPLICATE_SAME_ACCESS);
317 CloseHandle(process);
320 return local_handle;
323 /***********************************************************************
324 * create_map_from_stream
326 * Helper for RegisterDragDrop. Creates a file mapping object
327 * with the contents of the provided stream. The stream must
328 * be a global memory backed stream.
330 static HRESULT create_map_from_stream(IStream *stream, HANDLE *map)
332 HGLOBAL hmem;
333 DWORD size;
334 HRESULT hr;
335 void *data;
337 hr = GetHGlobalFromStream(stream, &hmem);
338 if(FAILED(hr)) return hr;
340 size = GlobalSize(hmem);
341 *map = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, size, NULL);
342 if(!*map) return E_OUTOFMEMORY;
344 data = MapViewOfFile(*map, FILE_MAP_WRITE, 0, 0, size);
345 memcpy(data, GlobalLock(hmem), size);
346 GlobalUnlock(hmem);
347 UnmapViewOfFile(data);
348 return S_OK;
351 /***********************************************************************
352 * create_stream_from_map
354 * Creates a stream from the provided map.
356 static HRESULT create_stream_from_map(HANDLE map, IStream **stream)
358 HRESULT hr = E_OUTOFMEMORY;
359 HGLOBAL hmem;
360 void *data;
361 MEMORY_BASIC_INFORMATION info;
363 data = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
364 if(!data) return hr;
366 VirtualQuery(data, &info, sizeof(info));
367 TRACE("size %d\n", (int)info.RegionSize);
369 hmem = GlobalAlloc(GMEM_MOVEABLE, info.RegionSize);
370 if(hmem)
372 memcpy(GlobalLock(hmem), data, info.RegionSize);
373 GlobalUnlock(hmem);
374 hr = CreateStreamOnHGlobal(hmem, TRUE, stream);
376 UnmapViewOfFile(data);
377 return hr;
380 /* This is to work around apps which break COM rules by not implementing
381 * IDropTarget::QueryInterface(). Windows doesn't expose this because it
382 * doesn't call CoMarshallInterface() in RegisterDragDrop().
383 * The wrapper is only used internally, and only exists for the life of
384 * the marshal. We don't want to hold a ref on the app provided target
385 * as some apps destroy this prior to CoUninitialize without calling
386 * RevokeDragDrop. The only (long-term) ref is held by the window prop. */
387 typedef struct {
388 IDropTarget IDropTarget_iface;
389 HWND hwnd;
390 LONG refs;
391 } DropTargetWrapper;
393 static inline DropTargetWrapper* impl_from_IDropTarget(IDropTarget* iface)
395 return CONTAINING_RECORD(iface, DropTargetWrapper, IDropTarget_iface);
398 static HRESULT WINAPI DropTargetWrapper_QueryInterface(IDropTarget* iface,
399 REFIID riid,
400 void** ppvObject)
402 DropTargetWrapper* This = impl_from_IDropTarget(iface);
403 if (IsEqualIID(riid, &IID_IUnknown) ||
404 IsEqualIID(riid, &IID_IDropTarget))
406 IDropTarget_AddRef(&This->IDropTarget_iface);
407 *ppvObject = &This->IDropTarget_iface;
408 return S_OK;
410 *ppvObject = NULL;
411 return E_NOINTERFACE;
414 static ULONG WINAPI DropTargetWrapper_AddRef(IDropTarget* iface)
416 DropTargetWrapper* This = impl_from_IDropTarget(iface);
417 return InterlockedIncrement(&This->refs);
420 static ULONG WINAPI DropTargetWrapper_Release(IDropTarget* iface)
422 DropTargetWrapper* This = impl_from_IDropTarget(iface);
423 ULONG refs = InterlockedDecrement(&This->refs);
424 if (!refs) HeapFree(GetProcessHeap(), 0, This);
425 return refs;
428 static inline HRESULT get_target_from_wrapper( IDropTarget *wrapper, IDropTarget **target )
430 DropTargetWrapper* This = impl_from_IDropTarget( wrapper );
431 *target = GetPropW( This->hwnd, prop_oledroptarget );
432 if (!*target) return DRAGDROP_E_NOTREGISTERED;
433 IDropTarget_AddRef( *target );
434 return S_OK;
437 static HRESULT WINAPI DropTargetWrapper_DragEnter(IDropTarget* iface,
438 IDataObject* pDataObj,
439 DWORD grfKeyState,
440 POINTL pt,
441 DWORD* pdwEffect)
443 IDropTarget *target;
444 HRESULT r = get_target_from_wrapper( iface, &target );
446 if (SUCCEEDED( r ))
448 r = IDropTarget_DragEnter( target, pDataObj, grfKeyState, pt, pdwEffect );
449 IDropTarget_Release( target );
451 return r;
454 static HRESULT WINAPI DropTargetWrapper_DragOver(IDropTarget* iface,
455 DWORD grfKeyState,
456 POINTL pt,
457 DWORD* pdwEffect)
459 IDropTarget *target;
460 HRESULT r = get_target_from_wrapper( iface, &target );
462 if (SUCCEEDED( r ))
464 r = IDropTarget_DragOver( target, grfKeyState, pt, pdwEffect );
465 IDropTarget_Release( target );
467 return r;
470 static HRESULT WINAPI DropTargetWrapper_DragLeave(IDropTarget* iface)
472 IDropTarget *target;
473 HRESULT r = get_target_from_wrapper( iface, &target );
475 if (SUCCEEDED( r ))
477 r = IDropTarget_DragLeave( target );
478 IDropTarget_Release( target );
480 return r;
483 static HRESULT WINAPI DropTargetWrapper_Drop(IDropTarget* iface,
484 IDataObject* pDataObj,
485 DWORD grfKeyState,
486 POINTL pt,
487 DWORD* pdwEffect)
489 IDropTarget *target;
490 HRESULT r = get_target_from_wrapper( iface, &target );
492 if (SUCCEEDED( r ))
494 r = IDropTarget_Drop( target, pDataObj, grfKeyState, pt, pdwEffect );
495 IDropTarget_Release( target );
497 return r;
500 static const IDropTargetVtbl DropTargetWrapperVTbl =
502 DropTargetWrapper_QueryInterface,
503 DropTargetWrapper_AddRef,
504 DropTargetWrapper_Release,
505 DropTargetWrapper_DragEnter,
506 DropTargetWrapper_DragOver,
507 DropTargetWrapper_DragLeave,
508 DropTargetWrapper_Drop
511 static IDropTarget* WrapDropTarget( HWND hwnd )
513 DropTargetWrapper* This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
515 if (This)
517 This->IDropTarget_iface.lpVtbl = &DropTargetWrapperVTbl;
518 This->hwnd = hwnd;
519 This->refs = 1;
521 return &This->IDropTarget_iface;
524 /***********************************************************************
525 * get_droptarget_pointer
527 * Retrieves the marshalled IDropTarget from the window.
529 static IDropTarget* get_droptarget_pointer(HWND hwnd)
531 IDropTarget *droptarget = NULL;
532 HANDLE map;
533 IStream *stream;
535 map = get_droptarget_local_handle(hwnd);
536 if(!map) return NULL;
538 if(SUCCEEDED(create_stream_from_map(map, &stream)))
540 CoUnmarshalInterface(stream, &IID_IDropTarget, (void**)&droptarget);
541 IStream_Release(stream);
543 CloseHandle(map);
544 return droptarget;
547 /***********************************************************************
548 * RegisterDragDrop (OLE32.@)
550 HRESULT WINAPI RegisterDragDrop(HWND hwnd, LPDROPTARGET pDropTarget)
552 DWORD pid = 0;
553 HRESULT hr;
554 IStream *stream;
555 HANDLE map;
556 IDropTarget *wrapper;
558 TRACE("(%p,%p)\n", hwnd, pDropTarget);
560 if (!COM_CurrentApt())
562 ERR("COM not initialized\n");
563 return E_OUTOFMEMORY;
566 if (!pDropTarget)
567 return E_INVALIDARG;
569 if (!IsWindow(hwnd))
571 ERR("invalid hwnd %p\n", hwnd);
572 return DRAGDROP_E_INVALIDHWND;
575 /* block register for other processes windows */
576 GetWindowThreadProcessId(hwnd, &pid);
577 if (pid != GetCurrentProcessId())
579 FIXME("register for another process windows is disabled\n");
580 return DRAGDROP_E_INVALIDHWND;
583 /* check if the window is already registered */
584 if (is_droptarget(hwnd))
585 return DRAGDROP_E_ALREADYREGISTERED;
588 * Marshal the drop target pointer into a shared memory map and
589 * store the map's handle in a Wine specific window prop. We also
590 * store the drop target pointer itself in the
591 * "OleDropTargetInterface" prop for compatibility with Windows.
594 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
595 if(FAILED(hr)) return hr;
597 /* IDropTarget::QueryInterface() shouldn't be called, some (broken) apps depend on this. */
598 wrapper = WrapDropTarget( hwnd );
599 if(!wrapper)
601 IStream_Release(stream);
602 return E_OUTOFMEMORY;
604 hr = CoMarshalInterface(stream, &IID_IDropTarget, (IUnknown*)wrapper, MSHCTX_LOCAL, NULL, MSHLFLAGS_TABLESTRONG);
605 IDropTarget_Release(wrapper);
607 if(SUCCEEDED(hr))
609 hr = create_map_from_stream(stream, &map);
610 if(SUCCEEDED(hr))
612 IDropTarget_AddRef(pDropTarget);
613 SetPropW(hwnd, prop_oledroptarget, pDropTarget);
614 SetPropW(hwnd, prop_marshalleddroptarget, map);
616 else
618 LARGE_INTEGER zero;
619 zero.QuadPart = 0;
620 IStream_Seek(stream, zero, STREAM_SEEK_SET, NULL);
621 CoReleaseMarshalData(stream);
624 IStream_Release(stream);
626 return hr;
629 /***********************************************************************
630 * RevokeDragDrop (OLE32.@)
632 HRESULT WINAPI RevokeDragDrop(HWND hwnd)
634 HANDLE map;
635 IStream *stream;
636 IDropTarget *drop_target;
637 HRESULT hr;
639 TRACE("(%p)\n", hwnd);
641 if (!IsWindow(hwnd))
643 ERR("invalid hwnd %p\n", hwnd);
644 return DRAGDROP_E_INVALIDHWND;
647 /* no registration data */
648 if (!(map = get_droptarget_handle(hwnd)))
649 return DRAGDROP_E_NOTREGISTERED;
651 drop_target = GetPropW(hwnd, prop_oledroptarget);
652 if(drop_target) IDropTarget_Release(drop_target);
654 RemovePropW(hwnd, prop_oledroptarget);
655 RemovePropW(hwnd, prop_marshalleddroptarget);
657 hr = create_stream_from_map(map, &stream);
658 if(SUCCEEDED(hr))
660 CoReleaseMarshalData(stream);
661 IStream_Release(stream);
663 CloseHandle(map);
665 return hr;
668 /***********************************************************************
669 * OleRegGetUserType (OLE32.@)
671 * This implementation of OleRegGetUserType ignores the dwFormOfType
672 * parameter and always returns the full name of the object. This is
673 * not too bad since this is the case for many objects because of the
674 * way they are registered.
676 HRESULT WINAPI OleRegGetUserType(
677 REFCLSID clsid,
678 DWORD dwFormOfType,
679 LPOLESTR* pszUserType)
681 WCHAR keyName[60];
682 DWORD dwKeyType;
683 DWORD cbData;
684 HKEY clsidKey;
685 LONG hres;
688 * Initialize the out parameter.
690 *pszUserType = NULL;
693 * Build the key name we're looking for
695 sprintfW( keyName, clsidfmtW,
696 clsid->Data1, clsid->Data2, clsid->Data3,
697 clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
698 clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );
700 TRACE("(%s, %d, %p)\n", debugstr_w(keyName), dwFormOfType, pszUserType);
703 * Open the class id Key
705 hres = open_classes_key(HKEY_CLASSES_ROOT, keyName, MAXIMUM_ALLOWED, &clsidKey);
706 if (hres != ERROR_SUCCESS)
707 return REGDB_E_CLASSNOTREG;
710 * Retrieve the size of the name string.
712 cbData = 0;
714 hres = RegQueryValueExW(clsidKey,
715 emptyW,
716 NULL,
717 &dwKeyType,
718 NULL,
719 &cbData);
721 if (hres!=ERROR_SUCCESS)
723 RegCloseKey(clsidKey);
724 return REGDB_E_READREGDB;
728 * Allocate a buffer for the registry value.
730 *pszUserType = CoTaskMemAlloc(cbData);
732 if (*pszUserType==NULL)
734 RegCloseKey(clsidKey);
735 return E_OUTOFMEMORY;
738 hres = RegQueryValueExW(clsidKey,
739 emptyW,
740 NULL,
741 &dwKeyType,
742 (LPBYTE) *pszUserType,
743 &cbData);
745 RegCloseKey(clsidKey);
747 if (hres != ERROR_SUCCESS)
749 CoTaskMemFree(*pszUserType);
750 *pszUserType = NULL;
752 return REGDB_E_READREGDB;
755 return S_OK;
758 /***********************************************************************
759 * DoDragDrop [OLE32.@]
761 HRESULT WINAPI DoDragDrop (
762 IDataObject *pDataObject, /* [in] ptr to the data obj */
763 IDropSource* pDropSource, /* [in] ptr to the source obj */
764 DWORD dwOKEffect, /* [in] effects allowed by the source */
765 DWORD *pdwEffect) /* [out] ptr to effects of the source */
767 static const WCHAR trackerW[] = {'T','r','a','c','k','e','r','W','i','n','d','o','w',0};
768 TrackerWindowInfo trackerInfo;
769 HWND hwndTrackWindow;
770 MSG msg;
772 TRACE("(%p, %p, %08x, %p)\n", pDataObject, pDropSource, dwOKEffect, pdwEffect);
774 if (!pDataObject || !pDropSource || !pdwEffect)
775 return E_INVALIDARG;
778 * Setup the drag n drop tracking window.
781 trackerInfo.dataObject = pDataObject;
782 trackerInfo.dropSource = pDropSource;
783 trackerInfo.dwOKEffect = dwOKEffect;
784 trackerInfo.pdwEffect = pdwEffect;
785 trackerInfo.trackingDone = FALSE;
786 trackerInfo.escPressed = FALSE;
787 trackerInfo.curDragTargetHWND = 0;
788 trackerInfo.curTargetHWND = 0;
789 trackerInfo.curDragTarget = 0;
791 hwndTrackWindow = CreateWindowW(OLEDD_DRAGTRACKERCLASS, trackerW,
792 WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT,
793 CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, 0,
794 &trackerInfo);
796 if (hwndTrackWindow)
799 * Capture the mouse input
801 SetCapture(hwndTrackWindow);
803 msg.message = 0;
806 * Pump messages. All mouse input should go to the capture window.
808 while (!trackerInfo.trackingDone && GetMessageW(&msg, 0, 0, 0) )
810 trackerInfo.curMousePos.x = msg.pt.x;
811 trackerInfo.curMousePos.y = msg.pt.y;
812 trackerInfo.dwKeyState = OLEDD_GetButtonState();
814 if ( (msg.message >= WM_KEYFIRST) &&
815 (msg.message <= WM_KEYLAST) )
818 * When keyboard messages are sent to windows on this thread, we
819 * want to ignore notify the drop source that the state changed.
820 * in the case of the Escape key, we also notify the drop source
821 * we give it a special meaning.
823 if ( (msg.message==WM_KEYDOWN) &&
824 (msg.wParam==VK_ESCAPE) )
826 trackerInfo.escPressed = TRUE;
830 * Notify the drop source.
832 OLEDD_TrackStateChange(&trackerInfo);
834 else
837 * Dispatch the messages only when it's not a keyboard message.
839 DispatchMessageW(&msg);
843 /* re-post the quit message to outer message loop */
844 if (msg.message == WM_QUIT)
845 PostQuitMessage(msg.wParam);
847 * Destroy the temporary window.
849 DestroyWindow(hwndTrackWindow);
851 return trackerInfo.returnValue;
854 return E_FAIL;
857 /***********************************************************************
858 * OleQueryLinkFromData [OLE32.@]
860 HRESULT WINAPI OleQueryLinkFromData(
861 IDataObject* pSrcDataObject)
863 FIXME("(%p),stub!\n", pSrcDataObject);
864 return S_FALSE;
867 /***********************************************************************
868 * OleRegGetMiscStatus [OLE32.@]
870 HRESULT WINAPI OleRegGetMiscStatus(
871 REFCLSID clsid,
872 DWORD dwAspect,
873 DWORD* pdwStatus)
875 static const WCHAR miscstatusW[] = {'M','i','s','c','S','t','a','t','u','s',0};
876 static const WCHAR dfmtW[] = {'%','d',0};
877 WCHAR keyName[60];
878 HKEY clsidKey;
879 HKEY miscStatusKey;
880 HKEY aspectKey;
881 LONG result;
884 * Build the key name we're looking for
886 sprintfW( keyName, clsidfmtW,
887 clsid->Data1, clsid->Data2, clsid->Data3,
888 clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
889 clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );
891 TRACE("(%s, %d, %p)\n", debugstr_w(keyName), dwAspect, pdwStatus);
893 if (!pdwStatus) return E_INVALIDARG;
895 *pdwStatus = 0;
897 if (actctx_get_miscstatus(clsid, dwAspect, pdwStatus)) return S_OK;
900 * Open the class id Key
902 result = open_classes_key(HKEY_CLASSES_ROOT, keyName, MAXIMUM_ALLOWED, &clsidKey);
903 if (result != ERROR_SUCCESS)
904 return REGDB_E_CLASSNOTREG;
907 * Get the MiscStatus
909 result = open_classes_key(clsidKey, miscstatusW, MAXIMUM_ALLOWED, &miscStatusKey);
910 if (result != ERROR_SUCCESS)
912 RegCloseKey(clsidKey);
913 return S_OK;
917 * Read the default value
919 OLEUTL_ReadRegistryDWORDValue(miscStatusKey, pdwStatus);
922 * Open the key specific to the requested aspect.
924 sprintfW(keyName, dfmtW, dwAspect);
926 result = open_classes_key(miscStatusKey, keyName, MAXIMUM_ALLOWED, &aspectKey);
927 if (result == ERROR_SUCCESS)
929 OLEUTL_ReadRegistryDWORDValue(aspectKey, pdwStatus);
930 RegCloseKey(aspectKey);
934 * Cleanup
936 RegCloseKey(miscStatusKey);
937 RegCloseKey(clsidKey);
939 return S_OK;
942 static HRESULT EnumOLEVERB_Construct(HKEY hkeyVerb, ULONG index, IEnumOLEVERB **ppenum);
944 typedef struct
946 IEnumOLEVERB IEnumOLEVERB_iface;
947 LONG ref;
949 HKEY hkeyVerb;
950 ULONG index;
951 } EnumOLEVERB;
953 static inline EnumOLEVERB *impl_from_IEnumOLEVERB(IEnumOLEVERB *iface)
955 return CONTAINING_RECORD(iface, EnumOLEVERB, IEnumOLEVERB_iface);
958 static HRESULT WINAPI EnumOLEVERB_QueryInterface(
959 IEnumOLEVERB *iface, REFIID riid, void **ppv)
961 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
962 if (IsEqualIID(riid, &IID_IUnknown) ||
963 IsEqualIID(riid, &IID_IEnumOLEVERB))
965 IEnumOLEVERB_AddRef(iface);
966 *ppv = iface;
967 return S_OK;
969 return E_NOINTERFACE;
972 static ULONG WINAPI EnumOLEVERB_AddRef(
973 IEnumOLEVERB *iface)
975 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
976 TRACE("()\n");
977 return InterlockedIncrement(&This->ref);
980 static ULONG WINAPI EnumOLEVERB_Release(
981 IEnumOLEVERB *iface)
983 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
984 LONG refs = InterlockedDecrement(&This->ref);
985 TRACE("()\n");
986 if (!refs)
988 RegCloseKey(This->hkeyVerb);
989 HeapFree(GetProcessHeap(), 0, This);
991 return refs;
994 static HRESULT WINAPI EnumOLEVERB_Next(
995 IEnumOLEVERB *iface, ULONG celt, LPOLEVERB rgelt,
996 ULONG *pceltFetched)
998 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
999 HRESULT hr = S_OK;
1001 TRACE("(%d, %p, %p)\n", celt, rgelt, pceltFetched);
1003 if (pceltFetched)
1004 *pceltFetched = 0;
1006 for (; celt; celt--, rgelt++)
1008 WCHAR wszSubKey[20];
1009 LONG cbData;
1010 LPWSTR pwszOLEVERB;
1011 LPWSTR pwszMenuFlags;
1012 LPWSTR pwszAttribs;
1013 LONG res = RegEnumKeyW(This->hkeyVerb, This->index, wszSubKey, sizeof(wszSubKey)/sizeof(wszSubKey[0]));
1014 if (res == ERROR_NO_MORE_ITEMS)
1016 hr = S_FALSE;
1017 break;
1019 else if (res != ERROR_SUCCESS)
1021 ERR("RegEnumKeyW failed with error %d\n", res);
1022 hr = REGDB_E_READREGDB;
1023 break;
1025 res = RegQueryValueW(This->hkeyVerb, wszSubKey, NULL, &cbData);
1026 if (res != ERROR_SUCCESS)
1028 ERR("RegQueryValueW failed with error %d\n", res);
1029 hr = REGDB_E_READREGDB;
1030 break;
1032 pwszOLEVERB = CoTaskMemAlloc(cbData);
1033 if (!pwszOLEVERB)
1035 hr = E_OUTOFMEMORY;
1036 break;
1038 res = RegQueryValueW(This->hkeyVerb, wszSubKey, pwszOLEVERB, &cbData);
1039 if (res != ERROR_SUCCESS)
1041 ERR("RegQueryValueW failed with error %d\n", res);
1042 hr = REGDB_E_READREGDB;
1043 CoTaskMemFree(pwszOLEVERB);
1044 break;
1047 TRACE("verb string: %s\n", debugstr_w(pwszOLEVERB));
1048 pwszMenuFlags = strchrW(pwszOLEVERB, ',');
1049 if (!pwszMenuFlags)
1051 hr = OLEOBJ_E_INVALIDVERB;
1052 CoTaskMemFree(pwszOLEVERB);
1053 break;
1055 /* nul terminate the name string and advance to first character */
1056 *pwszMenuFlags = '\0';
1057 pwszMenuFlags++;
1058 pwszAttribs = strchrW(pwszMenuFlags, ',');
1059 if (!pwszAttribs)
1061 hr = OLEOBJ_E_INVALIDVERB;
1062 CoTaskMemFree(pwszOLEVERB);
1063 break;
1065 /* nul terminate the menu string and advance to first character */
1066 *pwszAttribs = '\0';
1067 pwszAttribs++;
1069 /* fill out structure for this verb */
1070 rgelt->lVerb = atolW(wszSubKey);
1071 rgelt->lpszVerbName = pwszOLEVERB; /* user should free */
1072 rgelt->fuFlags = atolW(pwszMenuFlags);
1073 rgelt->grfAttribs = atolW(pwszAttribs);
1075 if (pceltFetched)
1076 (*pceltFetched)++;
1077 This->index++;
1079 return hr;
1082 static HRESULT WINAPI EnumOLEVERB_Skip(
1083 IEnumOLEVERB *iface, ULONG celt)
1085 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
1087 TRACE("(%d)\n", celt);
1089 This->index += celt;
1090 return S_OK;
1093 static HRESULT WINAPI EnumOLEVERB_Reset(
1094 IEnumOLEVERB *iface)
1096 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
1098 TRACE("()\n");
1100 This->index = 0;
1101 return S_OK;
1104 static HRESULT WINAPI EnumOLEVERB_Clone(
1105 IEnumOLEVERB *iface,
1106 IEnumOLEVERB **ppenum)
1108 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
1109 HKEY hkeyVerb;
1110 TRACE("(%p)\n", ppenum);
1111 if (!DuplicateHandle(GetCurrentProcess(), This->hkeyVerb, GetCurrentProcess(), (HANDLE *)&hkeyVerb, 0, FALSE, DUPLICATE_SAME_ACCESS))
1112 return HRESULT_FROM_WIN32(GetLastError());
1113 return EnumOLEVERB_Construct(hkeyVerb, This->index, ppenum);
1116 static const IEnumOLEVERBVtbl EnumOLEVERB_VTable =
1118 EnumOLEVERB_QueryInterface,
1119 EnumOLEVERB_AddRef,
1120 EnumOLEVERB_Release,
1121 EnumOLEVERB_Next,
1122 EnumOLEVERB_Skip,
1123 EnumOLEVERB_Reset,
1124 EnumOLEVERB_Clone
1127 static HRESULT EnumOLEVERB_Construct(HKEY hkeyVerb, ULONG index, IEnumOLEVERB **ppenum)
1129 EnumOLEVERB *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1130 if (!This)
1132 RegCloseKey(hkeyVerb);
1133 return E_OUTOFMEMORY;
1135 This->IEnumOLEVERB_iface.lpVtbl = &EnumOLEVERB_VTable;
1136 This->ref = 1;
1137 This->index = index;
1138 This->hkeyVerb = hkeyVerb;
1139 *ppenum = &This->IEnumOLEVERB_iface;
1140 return S_OK;
1143 /***********************************************************************
1144 * OleRegEnumVerbs [OLE32.@]
1146 * Enumerates verbs associated with a class stored in the registry.
1148 * PARAMS
1149 * clsid [I] Class ID to enumerate the verbs for.
1150 * ppenum [O] Enumerator.
1152 * RETURNS
1153 * S_OK: Success.
1154 * REGDB_E_CLASSNOTREG: The specified class does not have a key in the registry.
1155 * REGDB_E_READREGDB: The class key could not be opened for some other reason.
1156 * OLE_E_REGDB_KEY: The Verb subkey for the class is not present.
1157 * OLEOBJ_E_NOVERBS: The Verb subkey for the class is empty.
1159 HRESULT WINAPI OleRegEnumVerbs (REFCLSID clsid, LPENUMOLEVERB* ppenum)
1161 LONG res;
1162 HKEY hkeyVerb;
1163 DWORD dwSubKeys;
1164 static const WCHAR wszVerb[] = {'V','e','r','b',0};
1166 TRACE("(%s, %p)\n", debugstr_guid(clsid), ppenum);
1168 res = COM_OpenKeyForCLSID(clsid, wszVerb, KEY_READ, &hkeyVerb);
1169 if (FAILED(res))
1171 if (res == REGDB_E_CLASSNOTREG)
1172 ERR("CLSID %s not registered\n", debugstr_guid(clsid));
1173 else if (res == REGDB_E_KEYMISSING)
1174 ERR("no Verbs key for class %s\n", debugstr_guid(clsid));
1175 else
1176 ERR("failed to open Verbs key for CLSID %s with error %d\n",
1177 debugstr_guid(clsid), res);
1178 return res;
1181 res = RegQueryInfoKeyW(hkeyVerb, NULL, NULL, NULL, &dwSubKeys, NULL,
1182 NULL, NULL, NULL, NULL, NULL, NULL);
1183 if (res != ERROR_SUCCESS)
1185 ERR("failed to get subkey count with error %d\n", GetLastError());
1186 return REGDB_E_READREGDB;
1189 if (!dwSubKeys)
1191 WARN("class %s has no verbs\n", debugstr_guid(clsid));
1192 RegCloseKey(hkeyVerb);
1193 return OLEOBJ_E_NOVERBS;
1196 return EnumOLEVERB_Construct(hkeyVerb, 0, ppenum);
1199 /******************************************************************************
1200 * OleSetContainedObject [OLE32.@]
1202 HRESULT WINAPI OleSetContainedObject(
1203 LPUNKNOWN pUnknown,
1204 BOOL fContained)
1206 IRunnableObject* runnable = NULL;
1207 HRESULT hres;
1209 TRACE("(%p,%x)\n", pUnknown, fContained);
1211 hres = IUnknown_QueryInterface(pUnknown,
1212 &IID_IRunnableObject,
1213 (void**)&runnable);
1215 if (SUCCEEDED(hres))
1217 hres = IRunnableObject_SetContainedObject(runnable, fContained);
1219 IRunnableObject_Release(runnable);
1221 return hres;
1224 return S_OK;
1227 /******************************************************************************
1228 * OleRun [OLE32.@]
1230 * Set the OLE object to the running state.
1232 * PARAMS
1233 * pUnknown [I] OLE object to run.
1235 * RETURNS
1236 * Success: S_OK.
1237 * Failure: Any HRESULT code.
1239 HRESULT WINAPI OleRun(LPUNKNOWN pUnknown)
1241 IRunnableObject *runable;
1242 HRESULT hres;
1244 TRACE("(%p)\n", pUnknown);
1246 hres = IUnknown_QueryInterface(pUnknown, &IID_IRunnableObject, (void**)&runable);
1247 if (FAILED(hres))
1248 return S_OK; /* Appears to return no error. */
1250 hres = IRunnableObject_Run(runable, NULL);
1251 IRunnableObject_Release(runable);
1252 return hres;
1255 /******************************************************************************
1256 * OleLoad [OLE32.@]
1258 HRESULT WINAPI OleLoad(
1259 LPSTORAGE pStg,
1260 REFIID riid,
1261 LPOLECLIENTSITE pClientSite,
1262 LPVOID* ppvObj)
1264 IPersistStorage* persistStorage = NULL;
1265 IUnknown* pUnk;
1266 IOleObject* pOleObject = NULL;
1267 STATSTG storageInfo;
1268 HRESULT hres;
1270 TRACE("(%p, %s, %p, %p)\n", pStg, debugstr_guid(riid), pClientSite, ppvObj);
1272 *ppvObj = NULL;
1275 * TODO, Conversion ... OleDoAutoConvert
1279 * Get the class ID for the object.
1281 hres = IStorage_Stat(pStg, &storageInfo, STATFLAG_NONAME);
1284 * Now, try and create the handler for the object
1286 hres = CoCreateInstance(&storageInfo.clsid,
1287 NULL,
1288 CLSCTX_INPROC_HANDLER|CLSCTX_INPROC_SERVER,
1289 riid,
1290 (void**)&pUnk);
1293 * If that fails, as it will most times, load the default
1294 * OLE handler.
1296 if (FAILED(hres))
1298 hres = OleCreateDefaultHandler(&storageInfo.clsid,
1299 NULL,
1300 riid,
1301 (void**)&pUnk);
1305 * If we couldn't find a handler... this is bad. Abort the whole thing.
1307 if (FAILED(hres))
1308 return hres;
1310 if (pClientSite)
1312 hres = IUnknown_QueryInterface(pUnk, &IID_IOleObject, (void **)&pOleObject);
1313 if (SUCCEEDED(hres))
1315 DWORD dwStatus;
1316 hres = IOleObject_GetMiscStatus(pOleObject, DVASPECT_CONTENT, &dwStatus);
1321 * Initialize the object with its IPersistStorage interface.
1323 hres = IUnknown_QueryInterface(pUnk, &IID_IPersistStorage, (void**)&persistStorage);
1324 if (SUCCEEDED(hres))
1326 hres = IPersistStorage_Load(persistStorage, pStg);
1328 IPersistStorage_Release(persistStorage);
1329 persistStorage = NULL;
1332 if (SUCCEEDED(hres) && pClientSite)
1334 * Inform the new object of its client site.
1336 hres = IOleObject_SetClientSite(pOleObject, pClientSite);
1339 * Cleanup interfaces used internally
1341 if (pOleObject)
1342 IOleObject_Release(pOleObject);
1344 if (SUCCEEDED(hres))
1346 IOleLink *pOleLink;
1347 HRESULT hres1;
1348 hres1 = IUnknown_QueryInterface(pUnk, &IID_IOleLink, (void **)&pOleLink);
1349 if (SUCCEEDED(hres1))
1351 FIXME("handle OLE link\n");
1352 IOleLink_Release(pOleLink);
1356 if (FAILED(hres))
1358 IUnknown_Release(pUnk);
1359 pUnk = NULL;
1362 *ppvObj = pUnk;
1364 return hres;
1367 /***********************************************************************
1368 * OleSave [OLE32.@]
1370 HRESULT WINAPI OleSave(
1371 LPPERSISTSTORAGE pPS,
1372 LPSTORAGE pStg,
1373 BOOL fSameAsLoad)
1375 HRESULT hres;
1376 CLSID objectClass;
1378 TRACE("(%p,%p,%x)\n", pPS, pStg, fSameAsLoad);
1381 * First, we transfer the class ID (if available)
1383 hres = IPersistStorage_GetClassID(pPS, &objectClass);
1385 if (SUCCEEDED(hres))
1387 WriteClassStg(pStg, &objectClass);
1391 * Then, we ask the object to save itself to the
1392 * storage. If it is successful, we commit the storage.
1394 hres = IPersistStorage_Save(pPS, pStg, fSameAsLoad);
1396 if (SUCCEEDED(hres))
1398 IStorage_Commit(pStg,
1399 STGC_DEFAULT);
1402 return hres;
1406 /******************************************************************************
1407 * OleLockRunning [OLE32.@]
1409 HRESULT WINAPI OleLockRunning(LPUNKNOWN pUnknown, BOOL fLock, BOOL fLastUnlockCloses)
1411 IRunnableObject* runnable = NULL;
1412 HRESULT hres;
1414 TRACE("(%p,%x,%x)\n", pUnknown, fLock, fLastUnlockCloses);
1416 hres = IUnknown_QueryInterface(pUnknown,
1417 &IID_IRunnableObject,
1418 (void**)&runnable);
1420 if (SUCCEEDED(hres))
1422 hres = IRunnableObject_LockRunning(runnable, fLock, fLastUnlockCloses);
1424 IRunnableObject_Release(runnable);
1426 return hres;
1429 return S_OK;
1433 /**************************************************************************
1434 * Internal methods to manage the shared OLE menu in response to the
1435 * OLE***MenuDescriptor API
1438 /***
1439 * OLEMenu_Initialize()
1441 * Initializes the OLEMENU data structures.
1443 static void OLEMenu_Initialize(void)
1447 /***
1448 * OLEMenu_UnInitialize()
1450 * Releases the OLEMENU data structures.
1452 static void OLEMenu_UnInitialize(void)
1456 /*************************************************************************
1457 * OLEMenu_InstallHooks
1458 * Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
1460 * RETURNS: TRUE if message hooks were successfully installed
1461 * FALSE on failure
1463 static BOOL OLEMenu_InstallHooks( DWORD tid )
1465 OleMenuHookItem *pHookItem;
1467 /* Create an entry for the hook table */
1468 if ( !(pHookItem = HeapAlloc(GetProcessHeap(), 0,
1469 sizeof(OleMenuHookItem)) ) )
1470 return FALSE;
1472 pHookItem->tid = tid;
1473 pHookItem->hHeap = GetProcessHeap();
1474 pHookItem->CallWndProc_hHook = NULL;
1476 /* Install a thread scope message hook for WH_GETMESSAGE */
1477 pHookItem->GetMsg_hHook = SetWindowsHookExW( WH_GETMESSAGE, OLEMenu_GetMsgProc,
1478 0, GetCurrentThreadId() );
1479 if ( !pHookItem->GetMsg_hHook )
1480 goto CLEANUP;
1482 /* Install a thread scope message hook for WH_CALLWNDPROC */
1483 pHookItem->CallWndProc_hHook = SetWindowsHookExW( WH_CALLWNDPROC, OLEMenu_CallWndProc,
1484 0, GetCurrentThreadId() );
1485 if ( !pHookItem->CallWndProc_hHook )
1486 goto CLEANUP;
1488 /* Insert the hook table entry */
1489 pHookItem->next = hook_list;
1490 hook_list = pHookItem;
1492 return TRUE;
1494 CLEANUP:
1495 /* Unhook any hooks */
1496 if ( pHookItem->GetMsg_hHook )
1497 UnhookWindowsHookEx( pHookItem->GetMsg_hHook );
1498 if ( pHookItem->CallWndProc_hHook )
1499 UnhookWindowsHookEx( pHookItem->CallWndProc_hHook );
1500 /* Release the hook table entry */
1501 HeapFree(pHookItem->hHeap, 0, pHookItem );
1503 return FALSE;
1506 /*************************************************************************
1507 * OLEMenu_UnInstallHooks
1508 * UnInstall thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
1510 * RETURNS: TRUE if message hooks were successfully installed
1511 * FALSE on failure
1513 static BOOL OLEMenu_UnInstallHooks( DWORD tid )
1515 OleMenuHookItem *pHookItem = NULL;
1516 OleMenuHookItem **ppHook = &hook_list;
1518 while (*ppHook)
1520 if ((*ppHook)->tid == tid)
1522 pHookItem = *ppHook;
1523 *ppHook = pHookItem->next;
1524 break;
1526 ppHook = &(*ppHook)->next;
1528 if (!pHookItem) return FALSE;
1530 /* Uninstall the hooks installed for this thread */
1531 if ( !UnhookWindowsHookEx( pHookItem->GetMsg_hHook ) )
1532 goto CLEANUP;
1533 if ( !UnhookWindowsHookEx( pHookItem->CallWndProc_hHook ) )
1534 goto CLEANUP;
1536 /* Release the hook table entry */
1537 HeapFree(pHookItem->hHeap, 0, pHookItem );
1539 return TRUE;
1541 CLEANUP:
1542 /* Release the hook table entry */
1543 HeapFree(pHookItem->hHeap, 0, pHookItem );
1545 return FALSE;
1548 /*************************************************************************
1549 * OLEMenu_IsHookInstalled
1550 * Tests if OLEMenu hooks have been installed for a thread
1552 * RETURNS: The pointer and index of the hook table entry for the tid
1553 * NULL and -1 for the index if no hooks were installed for this thread
1555 static OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid )
1557 OleMenuHookItem *pHookItem;
1559 /* Do a simple linear search for an entry whose tid matches ours.
1560 * We really need a map but efficiency is not a concern here. */
1561 for (pHookItem = hook_list; pHookItem; pHookItem = pHookItem->next)
1563 if ( tid == pHookItem->tid )
1564 return pHookItem;
1567 return NULL;
1570 /***********************************************************************
1571 * OLEMenu_FindMainMenuIndex
1573 * Used by OLEMenu API to find the top level group a menu item belongs to.
1574 * On success pnPos contains the index of the item in the top level menu group
1576 * RETURNS: TRUE if the ID was found, FALSE on failure
1578 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos )
1580 INT i, nItems;
1582 nItems = GetMenuItemCount( hMainMenu );
1584 for (i = 0; i < nItems; i++)
1586 HMENU hsubmenu;
1588 /* Is the current item a submenu? */
1589 if ( (hsubmenu = GetSubMenu(hMainMenu, i)) )
1591 /* If the handle is the same we're done */
1592 if ( hsubmenu == hPopupMenu )
1594 if (pnPos)
1595 *pnPos = i;
1596 return TRUE;
1598 /* Recursively search without updating pnPos */
1599 else if ( OLEMenu_FindMainMenuIndex( hsubmenu, hPopupMenu, NULL ) )
1601 if (pnPos)
1602 *pnPos = i;
1603 return TRUE;
1608 return FALSE;
1611 /***********************************************************************
1612 * OLEMenu_SetIsServerMenu
1614 * Checks whether a popup menu belongs to a shared menu group which is
1615 * owned by the server, and sets the menu descriptor state accordingly.
1616 * All menu messages from these groups should be routed to the server.
1618 * RETURNS: TRUE if the popup menu is part of a server owned group
1619 * FALSE if the popup menu is part of a container owned group
1621 static BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor )
1623 UINT nPos = 0, nWidth, i;
1625 pOleMenuDescriptor->bIsServerItem = FALSE;
1627 /* Don't bother searching if the popup is the combined menu itself */
1628 if ( hmenu == pOleMenuDescriptor->hmenuCombined )
1629 return FALSE;
1631 /* Find the menu item index in the shared OLE menu that this item belongs to */
1632 if ( !OLEMenu_FindMainMenuIndex( pOleMenuDescriptor->hmenuCombined, hmenu, &nPos ) )
1633 return FALSE;
1635 /* The group widths array has counts for the number of elements
1636 * in the groups File, Edit, Container, Object, Window, Help.
1637 * The Edit, Object & Help groups belong to the server object
1638 * and the other three belong to the container.
1639 * Loop through the group widths and locate the group we are a member of.
1641 for ( i = 0, nWidth = 0; i < 6; i++ )
1643 nWidth += pOleMenuDescriptor->mgw.width[i];
1644 if ( nPos < nWidth )
1646 /* Odd elements are server menu widths */
1647 pOleMenuDescriptor->bIsServerItem = i%2;
1648 break;
1652 return pOleMenuDescriptor->bIsServerItem;
1655 /*************************************************************************
1656 * OLEMenu_CallWndProc
1657 * Thread scope WH_CALLWNDPROC hook proc filter function (callback)
1658 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1660 static LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam)
1662 LPCWPSTRUCT pMsg;
1663 HOLEMENU hOleMenu = 0;
1664 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1665 OleMenuHookItem *pHookItem = NULL;
1666 WORD fuFlags;
1668 TRACE("%i, %04lx, %08lx\n", code, wParam, lParam );
1670 /* Check if we're being asked to process the message */
1671 if ( HC_ACTION != code )
1672 goto NEXTHOOK;
1674 /* Retrieve the current message being dispatched from lParam */
1675 pMsg = (LPCWPSTRUCT)lParam;
1677 /* Check if the message is destined for a window we are interested in:
1678 * If the window has an OLEMenu property we may need to dispatch
1679 * the menu message to its active objects window instead. */
1681 hOleMenu = GetPropW( pMsg->hwnd, prop_olemenuW );
1682 if ( !hOleMenu )
1683 goto NEXTHOOK;
1685 /* Get the menu descriptor */
1686 pOleMenuDescriptor = GlobalLock( hOleMenu );
1687 if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1688 goto NEXTHOOK;
1690 /* Process menu messages */
1691 switch( pMsg->message )
1693 case WM_INITMENU:
1695 /* Reset the menu descriptor state */
1696 pOleMenuDescriptor->bIsServerItem = FALSE;
1698 /* Send this message to the server as well */
1699 SendMessageW( pOleMenuDescriptor->hwndActiveObject,
1700 pMsg->message, pMsg->wParam, pMsg->lParam );
1701 goto NEXTHOOK;
1704 case WM_INITMENUPOPUP:
1706 /* Save the state for whether this is a server owned menu */
1707 OLEMenu_SetIsServerMenu( (HMENU)pMsg->wParam, pOleMenuDescriptor );
1708 break;
1711 case WM_MENUSELECT:
1713 fuFlags = HIWORD(pMsg->wParam); /* Get flags */
1714 if ( fuFlags & MF_SYSMENU )
1715 goto NEXTHOOK;
1717 /* Save the state for whether this is a server owned popup menu */
1718 else if ( fuFlags & MF_POPUP )
1719 OLEMenu_SetIsServerMenu( (HMENU)pMsg->lParam, pOleMenuDescriptor );
1721 break;
1724 case WM_DRAWITEM:
1726 LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT) pMsg->lParam;
1727 if ( pMsg->wParam != 0 || lpdis->CtlType != ODT_MENU )
1728 goto NEXTHOOK; /* Not a menu message */
1730 break;
1733 default:
1734 goto NEXTHOOK;
1737 /* If the message was for the server dispatch it accordingly */
1738 if ( pOleMenuDescriptor->bIsServerItem )
1740 SendMessageW( pOleMenuDescriptor->hwndActiveObject,
1741 pMsg->message, pMsg->wParam, pMsg->lParam );
1744 NEXTHOOK:
1745 if ( pOleMenuDescriptor )
1746 GlobalUnlock( hOleMenu );
1748 /* Lookup the hook item for the current thread */
1749 if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1751 /* This should never fail!! */
1752 WARN("could not retrieve hHook for current thread!\n" );
1753 return 0;
1756 /* Pass on the message to the next hooker */
1757 return CallNextHookEx( pHookItem->CallWndProc_hHook, code, wParam, lParam );
1760 /*************************************************************************
1761 * OLEMenu_GetMsgProc
1762 * Thread scope WH_GETMESSAGE hook proc filter function (callback)
1763 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1765 static LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam)
1767 LPMSG pMsg;
1768 HOLEMENU hOleMenu = 0;
1769 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1770 OleMenuHookItem *pHookItem = NULL;
1771 WORD wCode;
1773 TRACE("%i, %04lx, %08lx\n", code, wParam, lParam );
1775 /* Check if we're being asked to process a messages */
1776 if ( HC_ACTION != code )
1777 goto NEXTHOOK;
1779 /* Retrieve the current message being dispatched from lParam */
1780 pMsg = (LPMSG)lParam;
1782 /* Check if the message is destined for a window we are interested in:
1783 * If the window has an OLEMenu property we may need to dispatch
1784 * the menu message to its active objects window instead. */
1786 hOleMenu = GetPropW( pMsg->hwnd, prop_olemenuW );
1787 if ( !hOleMenu )
1788 goto NEXTHOOK;
1790 /* Process menu messages */
1791 switch( pMsg->message )
1793 case WM_COMMAND:
1795 wCode = HIWORD(pMsg->wParam); /* Get notification code */
1796 if ( wCode )
1797 goto NEXTHOOK; /* Not a menu message */
1798 break;
1800 default:
1801 goto NEXTHOOK;
1804 /* Get the menu descriptor */
1805 pOleMenuDescriptor = GlobalLock( hOleMenu );
1806 if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1807 goto NEXTHOOK;
1809 /* If the message was for the server dispatch it accordingly */
1810 if ( pOleMenuDescriptor->bIsServerItem )
1812 /* Change the hWnd in the message to the active objects hWnd.
1813 * The message loop which reads this message will automatically
1814 * dispatch it to the embedded objects window. */
1815 pMsg->hwnd = pOleMenuDescriptor->hwndActiveObject;
1818 NEXTHOOK:
1819 if ( pOleMenuDescriptor )
1820 GlobalUnlock( hOleMenu );
1822 /* Lookup the hook item for the current thread */
1823 if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1825 /* This should never fail!! */
1826 WARN("could not retrieve hHook for current thread!\n" );
1827 return FALSE;
1830 /* Pass on the message to the next hooker */
1831 return CallNextHookEx( pHookItem->GetMsg_hHook, code, wParam, lParam );
1834 /***********************************************************************
1835 * OleCreateMenuDescriptor [OLE32.@]
1836 * Creates an OLE menu descriptor for OLE to use when dispatching
1837 * menu messages and commands.
1839 * PARAMS:
1840 * hmenuCombined - Handle to the objects combined menu
1841 * lpMenuWidths - Pointer to array of 6 LONG's indicating menus per group
1844 HOLEMENU WINAPI OleCreateMenuDescriptor(
1845 HMENU hmenuCombined,
1846 LPOLEMENUGROUPWIDTHS lpMenuWidths)
1848 HOLEMENU hOleMenu;
1849 OleMenuDescriptor *pOleMenuDescriptor;
1850 int i;
1852 if ( !hmenuCombined || !lpMenuWidths )
1853 return 0;
1855 /* Create an OLE menu descriptor */
1856 if ( !(hOleMenu = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT,
1857 sizeof(OleMenuDescriptor) ) ) )
1858 return 0;
1860 pOleMenuDescriptor = GlobalLock( hOleMenu );
1861 if ( !pOleMenuDescriptor )
1862 return 0;
1864 /* Initialize menu group widths and hmenu */
1865 for ( i = 0; i < 6; i++ )
1866 pOleMenuDescriptor->mgw.width[i] = lpMenuWidths->width[i];
1868 pOleMenuDescriptor->hmenuCombined = hmenuCombined;
1869 pOleMenuDescriptor->bIsServerItem = FALSE;
1870 GlobalUnlock( hOleMenu );
1872 return hOleMenu;
1875 /***********************************************************************
1876 * OleDestroyMenuDescriptor [OLE32.@]
1877 * Destroy the shared menu descriptor
1879 HRESULT WINAPI OleDestroyMenuDescriptor(
1880 HOLEMENU hmenuDescriptor)
1882 if ( hmenuDescriptor )
1883 GlobalFree( hmenuDescriptor );
1884 return S_OK;
1887 /***********************************************************************
1888 * OleSetMenuDescriptor [OLE32.@]
1889 * Installs or removes OLE dispatching code for the containers frame window.
1891 * PARAMS
1892 * hOleMenu Handle to composite menu descriptor
1893 * hwndFrame Handle to containers frame window
1894 * hwndActiveObject Handle to objects in-place activation window
1895 * lpFrame Pointer to IOleInPlaceFrame on containers window
1896 * lpActiveObject Pointer to IOleInPlaceActiveObject on active in-place object
1898 * RETURNS
1899 * S_OK - menu installed correctly
1900 * E_FAIL, E_INVALIDARG, E_UNEXPECTED - failure
1902 * FIXME
1903 * The lpFrame and lpActiveObject parameters are currently ignored
1904 * OLE should install context sensitive help F1 filtering for the app when
1905 * these are non null.
1907 HRESULT WINAPI OleSetMenuDescriptor(
1908 HOLEMENU hOleMenu,
1909 HWND hwndFrame,
1910 HWND hwndActiveObject,
1911 LPOLEINPLACEFRAME lpFrame,
1912 LPOLEINPLACEACTIVEOBJECT lpActiveObject)
1914 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1916 /* Check args */
1917 if ( !hwndFrame || (hOleMenu && !hwndActiveObject) )
1918 return E_INVALIDARG;
1920 if ( lpFrame || lpActiveObject )
1922 FIXME("(%p, %p, %p, %p, %p), Context sensitive help filtering not implemented!\n",
1923 hOleMenu,
1924 hwndFrame,
1925 hwndActiveObject,
1926 lpFrame,
1927 lpActiveObject);
1930 /* Set up a message hook to intercept the containers frame window messages.
1931 * The message filter is responsible for dispatching menu messages from the
1932 * shared menu which are intended for the object.
1935 if ( hOleMenu ) /* Want to install dispatching code */
1937 /* If OLEMenu hooks are already installed for this thread, fail
1938 * Note: This effectively means that OleSetMenuDescriptor cannot
1939 * be called twice in succession on the same frame window
1940 * without first calling it with a null hOleMenu to uninstall
1942 if ( OLEMenu_IsHookInstalled( GetCurrentThreadId() ) )
1943 return E_FAIL;
1945 /* Get the menu descriptor */
1946 pOleMenuDescriptor = GlobalLock( hOleMenu );
1947 if ( !pOleMenuDescriptor )
1948 return E_UNEXPECTED;
1950 /* Update the menu descriptor */
1951 pOleMenuDescriptor->hwndFrame = hwndFrame;
1952 pOleMenuDescriptor->hwndActiveObject = hwndActiveObject;
1954 GlobalUnlock( hOleMenu );
1955 pOleMenuDescriptor = NULL;
1957 /* Add a menu descriptor windows property to the frame window */
1958 SetPropW( hwndFrame, prop_olemenuW, hOleMenu );
1960 /* Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC */
1961 if ( !OLEMenu_InstallHooks( GetCurrentThreadId() ) )
1962 return E_FAIL;
1964 else /* Want to uninstall dispatching code */
1966 /* Uninstall the hooks */
1967 if ( !OLEMenu_UnInstallHooks( GetCurrentThreadId() ) )
1968 return E_FAIL;
1970 /* Remove the menu descriptor property from the frame window */
1971 RemovePropW( hwndFrame, prop_olemenuW );
1974 return S_OK;
1977 /******************************************************************************
1978 * IsAccelerator [OLE32.@]
1979 * Mostly copied from controls/menu.c TranslateAccelerator implementation
1981 BOOL WINAPI IsAccelerator(HACCEL hAccel, int cAccelEntries, LPMSG lpMsg, WORD* lpwCmd)
1983 LPACCEL lpAccelTbl;
1984 int i;
1986 if(!lpMsg) return FALSE;
1987 if (!hAccel)
1989 WARN_(accel)("NULL accel handle\n");
1990 return FALSE;
1992 if((lpMsg->message != WM_KEYDOWN &&
1993 lpMsg->message != WM_SYSKEYDOWN &&
1994 lpMsg->message != WM_SYSCHAR &&
1995 lpMsg->message != WM_CHAR)) return FALSE;
1996 lpAccelTbl = HeapAlloc(GetProcessHeap(), 0, cAccelEntries * sizeof(ACCEL));
1997 if (NULL == lpAccelTbl)
1999 return FALSE;
2001 if (CopyAcceleratorTableW(hAccel, lpAccelTbl, cAccelEntries) != cAccelEntries)
2003 WARN_(accel)("CopyAcceleratorTableW failed\n");
2004 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
2005 return FALSE;
2008 TRACE_(accel)("hAccel=%p, cAccelEntries=%d,"
2009 "msg->hwnd=%p, msg->message=%04x, wParam=%08lx, lParam=%08lx\n",
2010 hAccel, cAccelEntries,
2011 lpMsg->hwnd, lpMsg->message, lpMsg->wParam, lpMsg->lParam);
2012 for(i = 0; i < cAccelEntries; i++)
2014 if(lpAccelTbl[i].key != lpMsg->wParam)
2015 continue;
2017 if(lpMsg->message == WM_CHAR)
2019 if(!(lpAccelTbl[i].fVirt & FALT) && !(lpAccelTbl[i].fVirt & FVIRTKEY))
2021 TRACE_(accel)("found accel for WM_CHAR: ('%c')\n", LOWORD(lpMsg->wParam) & 0xff);
2022 goto found;
2025 else
2027 if(lpAccelTbl[i].fVirt & FVIRTKEY)
2029 INT mask = 0;
2030 TRACE_(accel)("found accel for virt_key %04lx (scan %04x)\n",
2031 lpMsg->wParam, HIWORD(lpMsg->lParam) & 0xff);
2032 if(GetKeyState(VK_SHIFT) & 0x8000) mask |= FSHIFT;
2033 if(GetKeyState(VK_CONTROL) & 0x8000) mask |= FCONTROL;
2034 if(GetKeyState(VK_MENU) & 0x8000) mask |= FALT;
2035 if(mask == (lpAccelTbl[i].fVirt & (FSHIFT | FCONTROL | FALT))) goto found;
2036 TRACE_(accel)("incorrect SHIFT/CTRL/ALT-state\n");
2038 else
2040 if(!(lpMsg->lParam & 0x01000000)) /* no special_key */
2042 if((lpAccelTbl[i].fVirt & FALT) && (lpMsg->lParam & 0x20000000))
2043 { /* ^^ ALT pressed */
2044 TRACE_(accel)("found accel for Alt-%c\n", LOWORD(lpMsg->wParam) & 0xff);
2045 goto found;
2052 WARN_(accel)("couldn't translate accelerator key\n");
2053 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
2054 return FALSE;
2056 found:
2057 if(lpwCmd) *lpwCmd = lpAccelTbl[i].cmd;
2058 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
2059 return TRUE;
2062 /***********************************************************************
2063 * ReleaseStgMedium [OLE32.@]
2065 void WINAPI ReleaseStgMedium(
2066 STGMEDIUM* pmedium)
2068 switch (pmedium->tymed)
2070 case TYMED_HGLOBAL:
2072 if ( (pmedium->pUnkForRelease==0) &&
2073 (pmedium->u.hGlobal!=0) )
2074 GlobalFree(pmedium->u.hGlobal);
2075 break;
2077 case TYMED_FILE:
2079 if (pmedium->u.lpszFileName!=0)
2081 if (pmedium->pUnkForRelease==0)
2083 DeleteFileW(pmedium->u.lpszFileName);
2086 CoTaskMemFree(pmedium->u.lpszFileName);
2088 break;
2090 case TYMED_ISTREAM:
2092 if (pmedium->u.pstm!=0)
2094 IStream_Release(pmedium->u.pstm);
2096 break;
2098 case TYMED_ISTORAGE:
2100 if (pmedium->u.pstg!=0)
2102 IStorage_Release(pmedium->u.pstg);
2104 break;
2106 case TYMED_GDI:
2108 if ( (pmedium->pUnkForRelease==0) &&
2109 (pmedium->u.hBitmap!=0) )
2110 DeleteObject(pmedium->u.hBitmap);
2111 break;
2113 case TYMED_MFPICT:
2115 if ( (pmedium->pUnkForRelease==0) &&
2116 (pmedium->u.hMetaFilePict!=0) )
2118 LPMETAFILEPICT pMP = GlobalLock(pmedium->u.hMetaFilePict);
2119 DeleteMetaFile(pMP->hMF);
2120 GlobalUnlock(pmedium->u.hMetaFilePict);
2121 GlobalFree(pmedium->u.hMetaFilePict);
2123 break;
2125 case TYMED_ENHMF:
2127 if ( (pmedium->pUnkForRelease==0) &&
2128 (pmedium->u.hEnhMetaFile!=0) )
2130 DeleteEnhMetaFile(pmedium->u.hEnhMetaFile);
2132 break;
2134 case TYMED_NULL:
2135 default:
2136 break;
2138 pmedium->tymed=TYMED_NULL;
2141 * After cleaning up, the unknown is released
2143 if (pmedium->pUnkForRelease!=0)
2145 IUnknown_Release(pmedium->pUnkForRelease);
2146 pmedium->pUnkForRelease = 0;
2150 /***
2151 * OLEDD_Initialize()
2153 * Initializes the OLE drag and drop data structures.
2155 static void OLEDD_Initialize(void)
2157 WNDCLASSW wndClass;
2159 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
2160 wndClass.style = CS_GLOBALCLASS;
2161 wndClass.lpfnWndProc = OLEDD_DragTrackerWindowProc;
2162 wndClass.cbClsExtra = 0;
2163 wndClass.cbWndExtra = sizeof(TrackerWindowInfo*);
2164 wndClass.hCursor = 0;
2165 wndClass.hbrBackground = 0;
2166 wndClass.lpszClassName = OLEDD_DRAGTRACKERCLASS;
2168 RegisterClassW (&wndClass);
2171 /***
2172 * OLEDD_DragTrackerWindowProc()
2174 * This method is the WindowProcedure of the drag n drop tracking
2175 * window. During a drag n Drop operation, an invisible window is created
2176 * to receive the user input and act upon it. This procedure is in charge
2177 * of this behavior.
2180 #define DRAG_TIMER_ID 1
2182 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(
2183 HWND hwnd,
2184 UINT uMsg,
2185 WPARAM wParam,
2186 LPARAM lParam)
2188 switch (uMsg)
2190 case WM_CREATE:
2192 LPCREATESTRUCTA createStruct = (LPCREATESTRUCTA)lParam;
2194 SetWindowLongPtrW(hwnd, 0, (LONG_PTR)createStruct->lpCreateParams);
2195 SetTimer(hwnd, DRAG_TIMER_ID, 50, NULL);
2197 break;
2199 case WM_TIMER:
2200 case WM_MOUSEMOVE:
2201 case WM_LBUTTONUP:
2202 case WM_MBUTTONUP:
2203 case WM_RBUTTONUP:
2204 case WM_LBUTTONDOWN:
2205 case WM_MBUTTONDOWN:
2206 case WM_RBUTTONDOWN:
2208 OLEDD_TrackStateChange((TrackerWindowInfo*)GetWindowLongPtrA(hwnd, 0));
2209 break;
2211 case WM_DESTROY:
2213 KillTimer(hwnd, DRAG_TIMER_ID);
2214 break;
2219 * This is a window proc after all. Let's call the default.
2221 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
2224 /***
2225 * OLEDD_TrackStateChange()
2227 * This method is invoked while a drag and drop operation is in effect.
2229 * params:
2230 * trackerInfo - Pointer to the structure identifying the
2231 * drag & drop operation that is currently
2232 * active.
2234 static void OLEDD_TrackStateChange(TrackerWindowInfo* trackerInfo)
2236 HWND hwndNewTarget = 0;
2237 HRESULT hr = S_OK;
2238 POINT pt;
2241 * Get the handle of the window under the mouse
2243 pt.x = trackerInfo->curMousePos.x;
2244 pt.y = trackerInfo->curMousePos.y;
2245 hwndNewTarget = WindowFromPoint(pt);
2247 trackerInfo->returnValue = IDropSource_QueryContinueDrag(trackerInfo->dropSource,
2248 trackerInfo->escPressed,
2249 trackerInfo->dwKeyState);
2252 * Every time, we re-initialize the effects passed to the
2253 * IDropTarget to the effects allowed by the source.
2255 *trackerInfo->pdwEffect = trackerInfo->dwOKEffect;
2258 * If we are hovering over the same target as before, send the
2259 * DragOver notification
2261 if ( (trackerInfo->curDragTarget != 0) &&
2262 (trackerInfo->curTargetHWND == hwndNewTarget) )
2264 IDropTarget_DragOver(trackerInfo->curDragTarget,
2265 trackerInfo->dwKeyState,
2266 trackerInfo->curMousePos,
2267 trackerInfo->pdwEffect);
2268 *trackerInfo->pdwEffect &= trackerInfo->dwOKEffect;
2270 else
2273 * If we changed window, we have to notify our old target and check for
2274 * the new one.
2276 if (trackerInfo->curDragTarget)
2277 IDropTarget_DragLeave(trackerInfo->curDragTarget);
2280 * Make sure we're hovering over a window.
2282 if (hwndNewTarget)
2285 * Find-out if there is a drag target under the mouse
2287 HWND next_target_wnd = hwndNewTarget;
2289 trackerInfo->curTargetHWND = hwndNewTarget;
2291 while (next_target_wnd && !is_droptarget(next_target_wnd))
2292 next_target_wnd = GetParent(next_target_wnd);
2294 if (next_target_wnd) hwndNewTarget = next_target_wnd;
2296 trackerInfo->curDragTargetHWND = hwndNewTarget;
2297 if(trackerInfo->curDragTarget) IDropTarget_Release(trackerInfo->curDragTarget);
2298 trackerInfo->curDragTarget = get_droptarget_pointer(hwndNewTarget);
2301 * If there is, notify it that we just dragged-in
2303 if (trackerInfo->curDragTarget)
2305 hr = IDropTarget_DragEnter(trackerInfo->curDragTarget,
2306 trackerInfo->dataObject,
2307 trackerInfo->dwKeyState,
2308 trackerInfo->curMousePos,
2309 trackerInfo->pdwEffect);
2310 *trackerInfo->pdwEffect &= trackerInfo->dwOKEffect;
2312 /* failed DragEnter() means invalid target */
2313 if (hr != S_OK)
2315 trackerInfo->curDragTargetHWND = 0;
2316 trackerInfo->curTargetHWND = 0;
2317 IDropTarget_Release(trackerInfo->curDragTarget);
2318 trackerInfo->curDragTarget = 0;
2322 else
2325 * The mouse is not over a window so we don't track anything.
2327 trackerInfo->curDragTargetHWND = 0;
2328 trackerInfo->curTargetHWND = 0;
2329 if(trackerInfo->curDragTarget) IDropTarget_Release(trackerInfo->curDragTarget);
2330 trackerInfo->curDragTarget = 0;
2335 * Now that we have done that, we have to tell the source to give
2336 * us feedback on the work being done by the target. If we don't
2337 * have a target, simulate no effect.
2339 if (trackerInfo->curDragTarget==0)
2341 *trackerInfo->pdwEffect = DROPEFFECT_NONE;
2344 hr = IDropSource_GiveFeedback(trackerInfo->dropSource,
2345 *trackerInfo->pdwEffect);
2348 * When we ask for feedback from the drop source, sometimes it will
2349 * do all the necessary work and sometimes it will not handle it
2350 * when that's the case, we must display the standard drag and drop
2351 * cursors.
2353 if (hr == DRAGDROP_S_USEDEFAULTCURSORS)
2355 HCURSOR hCur;
2357 if (*trackerInfo->pdwEffect & DROPEFFECT_MOVE)
2359 hCur = LoadCursorW(hProxyDll, MAKEINTRESOURCEW(2));
2361 else if (*trackerInfo->pdwEffect & DROPEFFECT_COPY)
2363 hCur = LoadCursorW(hProxyDll, MAKEINTRESOURCEW(3));
2365 else if (*trackerInfo->pdwEffect & DROPEFFECT_LINK)
2367 hCur = LoadCursorW(hProxyDll, MAKEINTRESOURCEW(4));
2369 else
2371 hCur = LoadCursorW(hProxyDll, MAKEINTRESOURCEW(1));
2374 SetCursor(hCur);
2378 * All the return valued will stop the operation except the S_OK
2379 * return value.
2381 if (trackerInfo->returnValue!=S_OK)
2384 * Make sure the message loop in DoDragDrop stops
2386 trackerInfo->trackingDone = TRUE;
2389 * Release the mouse in case the drop target decides to show a popup
2390 * or a menu or something.
2392 ReleaseCapture();
2395 * If we end-up over a target, drop the object in the target or
2396 * inform the target that the operation was cancelled.
2398 if (trackerInfo->curDragTarget)
2400 switch (trackerInfo->returnValue)
2403 * If the source wants us to complete the operation, we tell
2404 * the drop target that we just dropped the object in it.
2406 case DRAGDROP_S_DROP:
2407 if (*trackerInfo->pdwEffect != DROPEFFECT_NONE)
2408 trackerInfo->returnValue = IDropTarget_Drop(trackerInfo->curDragTarget,
2409 trackerInfo->dataObject,
2410 trackerInfo->dwKeyState,
2411 trackerInfo->curMousePos,
2412 trackerInfo->pdwEffect);
2413 else
2414 IDropTarget_DragLeave(trackerInfo->curDragTarget);
2415 break;
2418 * If the source told us that we should cancel, fool the drop
2419 * target by telling it that the mouse left its window.
2420 * Also set the drop effect to "NONE" in case the application
2421 * ignores the result of DoDragDrop.
2423 case DRAGDROP_S_CANCEL:
2424 IDropTarget_DragLeave(trackerInfo->curDragTarget);
2425 *trackerInfo->pdwEffect = DROPEFFECT_NONE;
2426 break;
2432 /***
2433 * OLEDD_GetButtonState()
2435 * This method will use the current state of the keyboard to build
2436 * a button state mask equivalent to the one passed in the
2437 * WM_MOUSEMOVE wParam.
2439 static DWORD OLEDD_GetButtonState(void)
2441 BYTE keyboardState[256];
2442 DWORD keyMask = 0;
2444 GetKeyboardState(keyboardState);
2446 if ( (keyboardState[VK_SHIFT] & 0x80) !=0)
2447 keyMask |= MK_SHIFT;
2449 if ( (keyboardState[VK_CONTROL] & 0x80) !=0)
2450 keyMask |= MK_CONTROL;
2452 if ( (keyboardState[VK_LBUTTON] & 0x80) !=0)
2453 keyMask |= MK_LBUTTON;
2455 if ( (keyboardState[VK_RBUTTON] & 0x80) !=0)
2456 keyMask |= MK_RBUTTON;
2458 if ( (keyboardState[VK_MBUTTON] & 0x80) !=0)
2459 keyMask |= MK_MBUTTON;
2461 return keyMask;
2464 /***
2465 * OLEDD_GetButtonState()
2467 * This method will read the default value of the registry key in
2468 * parameter and extract a DWORD value from it. The registry key value
2469 * can be in a string key or a DWORD key.
2471 * params:
2472 * regKey - Key to read the default value from
2473 * pdwValue - Pointer to the location where the DWORD
2474 * value is returned. This value is not modified
2475 * if the value is not found.
2478 static void OLEUTL_ReadRegistryDWORDValue(
2479 HKEY regKey,
2480 DWORD* pdwValue)
2482 WCHAR buffer[20];
2483 DWORD cbData = sizeof(buffer);
2484 DWORD dwKeyType;
2485 LONG lres;
2487 lres = RegQueryValueExW(regKey,
2488 emptyW,
2489 NULL,
2490 &dwKeyType,
2491 (LPBYTE)buffer,
2492 &cbData);
2494 if (lres==ERROR_SUCCESS)
2496 switch (dwKeyType)
2498 case REG_DWORD:
2499 *pdwValue = *(DWORD*)buffer;
2500 break;
2501 case REG_EXPAND_SZ:
2502 case REG_MULTI_SZ:
2503 case REG_SZ:
2504 *pdwValue = (DWORD)strtoulW(buffer, NULL, 10);
2505 break;
2510 /******************************************************************************
2511 * OleDraw (OLE32.@)
2513 * The operation of this function is documented literally in the WinAPI
2514 * documentation to involve a QueryInterface for the IViewObject interface,
2515 * followed by a call to IViewObject::Draw.
2517 HRESULT WINAPI OleDraw(
2518 IUnknown *pUnk,
2519 DWORD dwAspect,
2520 HDC hdcDraw,
2521 LPCRECT rect)
2523 HRESULT hres;
2524 IViewObject *viewobject;
2526 if (!pUnk) return E_INVALIDARG;
2528 hres = IUnknown_QueryInterface(pUnk,
2529 &IID_IViewObject,
2530 (void**)&viewobject);
2531 if (SUCCEEDED(hres))
2533 hres = IViewObject_Draw(viewobject, dwAspect, -1, 0, 0, 0, hdcDraw, (RECTL*)rect, 0, 0, 0);
2534 IViewObject_Release(viewobject);
2535 return hres;
2537 else
2538 return DV_E_NOIVIEWOBJECT;
2541 /***********************************************************************
2542 * OleTranslateAccelerator [OLE32.@]
2544 HRESULT WINAPI OleTranslateAccelerator (LPOLEINPLACEFRAME lpFrame,
2545 LPOLEINPLACEFRAMEINFO lpFrameInfo, LPMSG lpmsg)
2547 WORD wID;
2549 TRACE("(%p,%p,%p)\n", lpFrame, lpFrameInfo, lpmsg);
2551 if (IsAccelerator(lpFrameInfo->haccel,lpFrameInfo->cAccelEntries,lpmsg,&wID))
2552 return IOleInPlaceFrame_TranslateAccelerator(lpFrame,lpmsg,wID);
2554 return S_FALSE;
2557 /******************************************************************************
2558 * OleCreate [OLE32.@]
2561 HRESULT WINAPI OleCreate(
2562 REFCLSID rclsid,
2563 REFIID riid,
2564 DWORD renderopt,
2565 LPFORMATETC pFormatEtc,
2566 LPOLECLIENTSITE pClientSite,
2567 LPSTORAGE pStg,
2568 LPVOID* ppvObj)
2570 HRESULT hres;
2571 IUnknown * pUnk = NULL;
2572 IOleObject *pOleObject = NULL;
2574 TRACE("(%s, %s, %d, %p, %p, %p, %p)\n", debugstr_guid(rclsid),
2575 debugstr_guid(riid), renderopt, pFormatEtc, pClientSite, pStg, ppvObj);
2577 hres = CoCreateInstance(rclsid, 0, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER, riid, (LPVOID*)&pUnk);
2579 if (SUCCEEDED(hres))
2580 hres = IStorage_SetClass(pStg, rclsid);
2582 if (pClientSite && SUCCEEDED(hres))
2584 hres = IUnknown_QueryInterface(pUnk, &IID_IOleObject, (LPVOID*)&pOleObject);
2585 if (SUCCEEDED(hres))
2587 DWORD dwStatus;
2588 hres = IOleObject_GetMiscStatus(pOleObject, DVASPECT_CONTENT, &dwStatus);
2592 if (SUCCEEDED(hres))
2594 IPersistStorage * pPS;
2595 if (SUCCEEDED((hres = IUnknown_QueryInterface(pUnk, &IID_IPersistStorage, (LPVOID*)&pPS))))
2597 TRACE("trying to set stg %p\n", pStg);
2598 hres = IPersistStorage_InitNew(pPS, pStg);
2599 TRACE("-- result 0x%08x\n", hres);
2600 IPersistStorage_Release(pPS);
2604 if (pClientSite && SUCCEEDED(hres))
2606 TRACE("trying to set clientsite %p\n", pClientSite);
2607 hres = IOleObject_SetClientSite(pOleObject, pClientSite);
2608 TRACE("-- result 0x%08x\n", hres);
2611 if (pOleObject)
2612 IOleObject_Release(pOleObject);
2614 if (((renderopt == OLERENDER_DRAW) || (renderopt == OLERENDER_FORMAT)) &&
2615 SUCCEEDED(hres))
2617 IRunnableObject *pRunnable;
2618 IOleCache *pOleCache;
2619 HRESULT hres2;
2621 hres2 = IUnknown_QueryInterface(pUnk, &IID_IRunnableObject, (void **)&pRunnable);
2622 if (SUCCEEDED(hres2))
2624 hres = IRunnableObject_Run(pRunnable, NULL);
2625 IRunnableObject_Release(pRunnable);
2628 if (SUCCEEDED(hres))
2630 hres2 = IUnknown_QueryInterface(pUnk, &IID_IOleCache, (void **)&pOleCache);
2631 if (SUCCEEDED(hres2))
2633 DWORD dwConnection;
2634 if (renderopt == OLERENDER_DRAW && !pFormatEtc) {
2635 FORMATETC pfe;
2636 pfe.cfFormat = 0;
2637 pfe.ptd = NULL;
2638 pfe.dwAspect = DVASPECT_CONTENT;
2639 pfe.lindex = -1;
2640 pfe.tymed = TYMED_NULL;
2641 hres = IOleCache_Cache(pOleCache, &pfe, ADVF_PRIMEFIRST, &dwConnection);
2643 else
2644 hres = IOleCache_Cache(pOleCache, pFormatEtc, ADVF_PRIMEFIRST, &dwConnection);
2645 IOleCache_Release(pOleCache);
2650 if (FAILED(hres) && pUnk)
2652 IUnknown_Release(pUnk);
2653 pUnk = NULL;
2656 *ppvObj = pUnk;
2658 TRACE("-- %p\n", pUnk);
2659 return hres;
2662 /******************************************************************************
2663 * OleGetAutoConvert [OLE32.@]
2665 HRESULT WINAPI OleGetAutoConvert(REFCLSID clsidOld, LPCLSID pClsidNew)
2667 static const WCHAR wszAutoConvertTo[] = {'A','u','t','o','C','o','n','v','e','r','t','T','o',0};
2668 HKEY hkey = NULL;
2669 WCHAR buf[CHARS_IN_GUID];
2670 LONG len;
2671 HRESULT res = S_OK;
2673 res = COM_OpenKeyForCLSID(clsidOld, wszAutoConvertTo, KEY_READ, &hkey);
2674 if (FAILED(res))
2675 goto done;
2677 len = sizeof(buf);
2678 if (RegQueryValueW(hkey, NULL, buf, &len))
2680 res = REGDB_E_KEYMISSING;
2681 goto done;
2683 res = CLSIDFromString(buf, pClsidNew);
2684 done:
2685 if (hkey) RegCloseKey(hkey);
2686 return res;
2689 /******************************************************************************
2690 * OleSetAutoConvert [OLE32.@]
2692 HRESULT WINAPI OleSetAutoConvert(REFCLSID clsidOld, REFCLSID clsidNew)
2694 static const WCHAR wszAutoConvertTo[] = {'A','u','t','o','C','o','n','v','e','r','t','T','o',0};
2695 HKEY hkey = NULL;
2696 WCHAR szClsidNew[CHARS_IN_GUID];
2697 HRESULT res = S_OK;
2699 TRACE("(%s,%s)\n", debugstr_guid(clsidOld), debugstr_guid(clsidNew));
2701 res = COM_OpenKeyForCLSID(clsidOld, NULL, KEY_READ | KEY_WRITE, &hkey);
2702 if (FAILED(res))
2703 goto done;
2704 StringFromGUID2(clsidNew, szClsidNew, CHARS_IN_GUID);
2705 if (RegSetValueW(hkey, wszAutoConvertTo, REG_SZ, szClsidNew, (strlenW(szClsidNew)+1) * sizeof(WCHAR)))
2707 res = REGDB_E_WRITEREGDB;
2708 goto done;
2711 done:
2712 if (hkey) RegCloseKey(hkey);
2713 return res;
2716 /******************************************************************************
2717 * OleDoAutoConvert [OLE32.@]
2719 HRESULT WINAPI OleDoAutoConvert(LPSTORAGE pStg, LPCLSID pClsidNew)
2721 WCHAR *user_type_old, *user_type_new;
2722 CLIPFORMAT cf;
2723 STATSTG stat;
2724 CLSID clsid;
2725 HRESULT hr;
2727 TRACE("(%p, %p)\n", pStg, pClsidNew);
2729 *pClsidNew = CLSID_NULL;
2730 if(!pStg)
2731 return E_INVALIDARG;
2732 hr = IStorage_Stat(pStg, &stat, STATFLAG_NONAME);
2733 if(FAILED(hr))
2734 return hr;
2736 *pClsidNew = stat.clsid;
2737 hr = OleGetAutoConvert(&stat.clsid, &clsid);
2738 if(FAILED(hr))
2739 return hr;
2741 hr = IStorage_SetClass(pStg, &clsid);
2742 if(FAILED(hr))
2743 return hr;
2745 hr = ReadFmtUserTypeStg(pStg, &cf, &user_type_old);
2746 if(FAILED(hr)) {
2747 cf = 0;
2748 user_type_new = NULL;
2751 hr = OleRegGetUserType(&clsid, USERCLASSTYPE_FULL, &user_type_new);
2752 if(FAILED(hr))
2753 user_type_new = NULL;
2755 hr = WriteFmtUserTypeStg(pStg, cf, user_type_new);
2756 CoTaskMemFree(user_type_new);
2757 if(FAILED(hr))
2759 CoTaskMemFree(user_type_old);
2760 IStorage_SetClass(pStg, &stat.clsid);
2761 return hr;
2764 hr = SetConvertStg(pStg, TRUE);
2765 if(FAILED(hr))
2767 WriteFmtUserTypeStg(pStg, cf, user_type_old);
2768 IStorage_SetClass(pStg, &stat.clsid);
2770 else
2771 *pClsidNew = clsid;
2772 CoTaskMemFree(user_type_old);
2773 return hr;
2776 /******************************************************************************
2777 * OleIsRunning [OLE32.@]
2779 BOOL WINAPI OleIsRunning(LPOLEOBJECT object)
2781 IRunnableObject *pRunnable;
2782 HRESULT hr;
2783 BOOL running;
2785 TRACE("(%p)\n", object);
2787 if (!object) return FALSE;
2789 hr = IOleObject_QueryInterface(object, &IID_IRunnableObject, (void **)&pRunnable);
2790 if (FAILED(hr))
2791 return TRUE;
2792 running = IRunnableObject_IsRunning(pRunnable);
2793 IRunnableObject_Release(pRunnable);
2794 return running;
2797 /***********************************************************************
2798 * OleNoteObjectVisible [OLE32.@]
2800 HRESULT WINAPI OleNoteObjectVisible(LPUNKNOWN pUnknown, BOOL bVisible)
2802 TRACE("(%p, %s)\n", pUnknown, bVisible ? "TRUE" : "FALSE");
2803 return CoLockObjectExternal(pUnknown, bVisible, TRUE);
2807 /***********************************************************************
2808 * OLE_FreeClipDataArray [internal]
2810 * NOTES:
2811 * frees the data associated with an array of CLIPDATAs
2813 static void OLE_FreeClipDataArray(ULONG count, CLIPDATA * pClipDataArray)
2815 ULONG i;
2816 for (i = 0; i < count; i++)
2817 if (pClipDataArray[i].pClipData)
2818 CoTaskMemFree(pClipDataArray[i].pClipData);
2821 /***********************************************************************
2822 * PropSysAllocString [OLE32.@]
2823 * NOTES
2824 * Forward to oleaut32.
2826 BSTR WINAPI PropSysAllocString(LPCOLESTR str)
2828 return SysAllocString(str);
2831 /***********************************************************************
2832 * PropSysFreeString [OLE32.@]
2833 * NOTES
2834 * Forward to oleaut32.
2836 void WINAPI PropSysFreeString(LPOLESTR str)
2838 SysFreeString(str);
2841 /******************************************************************************
2842 * Check if a PROPVARIANT's type is valid.
2844 static inline HRESULT PROPVARIANT_ValidateType(VARTYPE vt)
2846 switch (vt)
2848 case VT_EMPTY:
2849 case VT_NULL:
2850 case VT_I1:
2851 case VT_I2:
2852 case VT_I4:
2853 case VT_I8:
2854 case VT_R4:
2855 case VT_R8:
2856 case VT_CY:
2857 case VT_DATE:
2858 case VT_BSTR:
2859 case VT_ERROR:
2860 case VT_BOOL:
2861 case VT_DECIMAL:
2862 case VT_UI1:
2863 case VT_UI2:
2864 case VT_UI4:
2865 case VT_UI8:
2866 case VT_INT:
2867 case VT_UINT:
2868 case VT_LPSTR:
2869 case VT_LPWSTR:
2870 case VT_FILETIME:
2871 case VT_BLOB:
2872 case VT_DISPATCH:
2873 case VT_UNKNOWN:
2874 case VT_STREAM:
2875 case VT_STORAGE:
2876 case VT_STREAMED_OBJECT:
2877 case VT_STORED_OBJECT:
2878 case VT_BLOB_OBJECT:
2879 case VT_CF:
2880 case VT_CLSID:
2881 case VT_I1|VT_VECTOR:
2882 case VT_I2|VT_VECTOR:
2883 case VT_I4|VT_VECTOR:
2884 case VT_I8|VT_VECTOR:
2885 case VT_R4|VT_VECTOR:
2886 case VT_R8|VT_VECTOR:
2887 case VT_CY|VT_VECTOR:
2888 case VT_DATE|VT_VECTOR:
2889 case VT_BSTR|VT_VECTOR:
2890 case VT_ERROR|VT_VECTOR:
2891 case VT_BOOL|VT_VECTOR:
2892 case VT_VARIANT|VT_VECTOR:
2893 case VT_UI1|VT_VECTOR:
2894 case VT_UI2|VT_VECTOR:
2895 case VT_UI4|VT_VECTOR:
2896 case VT_UI8|VT_VECTOR:
2897 case VT_LPSTR|VT_VECTOR:
2898 case VT_LPWSTR|VT_VECTOR:
2899 case VT_FILETIME|VT_VECTOR:
2900 case VT_CF|VT_VECTOR:
2901 case VT_CLSID|VT_VECTOR:
2902 return S_OK;
2904 WARN("Bad type %d\n", vt);
2905 return STG_E_INVALIDPARAMETER;
2908 /***********************************************************************
2909 * PropVariantClear [OLE32.@]
2911 HRESULT WINAPI PropVariantClear(PROPVARIANT * pvar) /* [in/out] */
2913 HRESULT hr;
2915 TRACE("(%p)\n", pvar);
2917 if (!pvar)
2918 return S_OK;
2920 hr = PROPVARIANT_ValidateType(pvar->vt);
2921 if (FAILED(hr))
2923 memset(pvar, 0, sizeof(*pvar));
2924 return hr;
2927 switch(pvar->vt)
2929 case VT_EMPTY:
2930 case VT_NULL:
2931 case VT_I1:
2932 case VT_I2:
2933 case VT_I4:
2934 case VT_I8:
2935 case VT_R4:
2936 case VT_R8:
2937 case VT_CY:
2938 case VT_DATE:
2939 case VT_ERROR:
2940 case VT_BOOL:
2941 case VT_DECIMAL:
2942 case VT_UI1:
2943 case VT_UI2:
2944 case VT_UI4:
2945 case VT_UI8:
2946 case VT_INT:
2947 case VT_UINT:
2948 case VT_FILETIME:
2949 break;
2950 case VT_DISPATCH:
2951 case VT_UNKNOWN:
2952 case VT_STREAM:
2953 case VT_STREAMED_OBJECT:
2954 case VT_STORAGE:
2955 case VT_STORED_OBJECT:
2956 if (pvar->u.pStream)
2957 IStream_Release(pvar->u.pStream);
2958 break;
2959 case VT_CLSID:
2960 case VT_LPSTR:
2961 case VT_LPWSTR:
2962 /* pick an arbitrary typed pointer - we don't care about the type
2963 * as we are just freeing it */
2964 CoTaskMemFree(pvar->u.puuid);
2965 break;
2966 case VT_BLOB:
2967 case VT_BLOB_OBJECT:
2968 CoTaskMemFree(pvar->u.blob.pBlobData);
2969 break;
2970 case VT_BSTR:
2971 PropSysFreeString(pvar->u.bstrVal);
2972 break;
2973 case VT_CF:
2974 if (pvar->u.pclipdata)
2976 OLE_FreeClipDataArray(1, pvar->u.pclipdata);
2977 CoTaskMemFree(pvar->u.pclipdata);
2979 break;
2980 default:
2981 if (pvar->vt & VT_VECTOR)
2983 ULONG i;
2985 switch (pvar->vt & ~VT_VECTOR)
2987 case VT_VARIANT:
2988 FreePropVariantArray(pvar->u.capropvar.cElems, pvar->u.capropvar.pElems);
2989 break;
2990 case VT_CF:
2991 OLE_FreeClipDataArray(pvar->u.caclipdata.cElems, pvar->u.caclipdata.pElems);
2992 break;
2993 case VT_BSTR:
2994 for (i = 0; i < pvar->u.cabstr.cElems; i++)
2995 PropSysFreeString(pvar->u.cabstr.pElems[i]);
2996 break;
2997 case VT_LPSTR:
2998 for (i = 0; i < pvar->u.calpstr.cElems; i++)
2999 CoTaskMemFree(pvar->u.calpstr.pElems[i]);
3000 break;
3001 case VT_LPWSTR:
3002 for (i = 0; i < pvar->u.calpwstr.cElems; i++)
3003 CoTaskMemFree(pvar->u.calpwstr.pElems[i]);
3004 break;
3006 if (pvar->vt & ~VT_VECTOR)
3008 /* pick an arbitrary VT_VECTOR structure - they all have the same
3009 * memory layout */
3010 CoTaskMemFree(pvar->u.capropvar.pElems);
3013 else
3015 WARN("Invalid/unsupported type %d\n", pvar->vt);
3016 hr = STG_E_INVALIDPARAMETER;
3020 memset(pvar, 0, sizeof(*pvar));
3021 return hr;
3024 /***********************************************************************
3025 * PropVariantCopy [OLE32.@]
3027 HRESULT WINAPI PropVariantCopy(PROPVARIANT *pvarDest, /* [out] */
3028 const PROPVARIANT *pvarSrc) /* [in] */
3030 ULONG len;
3031 HRESULT hr;
3033 TRACE("(%p, %p vt %04x)\n", pvarDest, pvarSrc, pvarSrc->vt);
3035 hr = PROPVARIANT_ValidateType(pvarSrc->vt);
3036 if (FAILED(hr))
3037 return hr;
3039 /* this will deal with most cases */
3040 *pvarDest = *pvarSrc;
3042 switch(pvarSrc->vt)
3044 case VT_EMPTY:
3045 case VT_NULL:
3046 case VT_I1:
3047 case VT_UI1:
3048 case VT_I2:
3049 case VT_UI2:
3050 case VT_BOOL:
3051 case VT_DECIMAL:
3052 case VT_I4:
3053 case VT_UI4:
3054 case VT_R4:
3055 case VT_ERROR:
3056 case VT_I8:
3057 case VT_UI8:
3058 case VT_INT:
3059 case VT_UINT:
3060 case VT_R8:
3061 case VT_CY:
3062 case VT_DATE:
3063 case VT_FILETIME:
3064 break;
3065 case VT_DISPATCH:
3066 case VT_UNKNOWN:
3067 case VT_STREAM:
3068 case VT_STREAMED_OBJECT:
3069 case VT_STORAGE:
3070 case VT_STORED_OBJECT:
3071 if (pvarDest->u.pStream)
3072 IStream_AddRef(pvarDest->u.pStream);
3073 break;
3074 case VT_CLSID:
3075 pvarDest->u.puuid = CoTaskMemAlloc(sizeof(CLSID));
3076 *pvarDest->u.puuid = *pvarSrc->u.puuid;
3077 break;
3078 case VT_LPSTR:
3079 if (pvarSrc->u.pszVal)
3081 len = strlen(pvarSrc->u.pszVal);
3082 pvarDest->u.pszVal = CoTaskMemAlloc((len+1)*sizeof(CHAR));
3083 CopyMemory(pvarDest->u.pszVal, pvarSrc->u.pszVal, (len+1)*sizeof(CHAR));
3085 break;
3086 case VT_LPWSTR:
3087 if (pvarSrc->u.pwszVal)
3089 len = lstrlenW(pvarSrc->u.pwszVal);
3090 pvarDest->u.pwszVal = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
3091 CopyMemory(pvarDest->u.pwszVal, pvarSrc->u.pwszVal, (len+1)*sizeof(WCHAR));
3093 break;
3094 case VT_BLOB:
3095 case VT_BLOB_OBJECT:
3096 if (pvarSrc->u.blob.pBlobData)
3098 len = pvarSrc->u.blob.cbSize;
3099 pvarDest->u.blob.pBlobData = CoTaskMemAlloc(len);
3100 CopyMemory(pvarDest->u.blob.pBlobData, pvarSrc->u.blob.pBlobData, len);
3102 break;
3103 case VT_BSTR:
3104 pvarDest->u.bstrVal = PropSysAllocString(pvarSrc->u.bstrVal);
3105 break;
3106 case VT_CF:
3107 if (pvarSrc->u.pclipdata)
3109 len = pvarSrc->u.pclipdata->cbSize - sizeof(pvarSrc->u.pclipdata->ulClipFmt);
3110 pvarDest->u.pclipdata = CoTaskMemAlloc(sizeof (CLIPDATA));
3111 pvarDest->u.pclipdata->cbSize = pvarSrc->u.pclipdata->cbSize;
3112 pvarDest->u.pclipdata->ulClipFmt = pvarSrc->u.pclipdata->ulClipFmt;
3113 pvarDest->u.pclipdata->pClipData = CoTaskMemAlloc(len);
3114 CopyMemory(pvarDest->u.pclipdata->pClipData, pvarSrc->u.pclipdata->pClipData, len);
3116 break;
3117 default:
3118 if (pvarSrc->vt & VT_VECTOR)
3120 int elemSize;
3121 ULONG i;
3123 switch(pvarSrc->vt & ~VT_VECTOR)
3125 case VT_I1: elemSize = sizeof(pvarSrc->u.cVal); break;
3126 case VT_UI1: elemSize = sizeof(pvarSrc->u.bVal); break;
3127 case VT_I2: elemSize = sizeof(pvarSrc->u.iVal); break;
3128 case VT_UI2: elemSize = sizeof(pvarSrc->u.uiVal); break;
3129 case VT_BOOL: elemSize = sizeof(pvarSrc->u.boolVal); break;
3130 case VT_I4: elemSize = sizeof(pvarSrc->u.lVal); break;
3131 case VT_UI4: elemSize = sizeof(pvarSrc->u.ulVal); break;
3132 case VT_R4: elemSize = sizeof(pvarSrc->u.fltVal); break;
3133 case VT_R8: elemSize = sizeof(pvarSrc->u.dblVal); break;
3134 case VT_ERROR: elemSize = sizeof(pvarSrc->u.scode); break;
3135 case VT_I8: elemSize = sizeof(pvarSrc->u.hVal); break;
3136 case VT_UI8: elemSize = sizeof(pvarSrc->u.uhVal); break;
3137 case VT_CY: elemSize = sizeof(pvarSrc->u.cyVal); break;
3138 case VT_DATE: elemSize = sizeof(pvarSrc->u.date); break;
3139 case VT_FILETIME: elemSize = sizeof(pvarSrc->u.filetime); break;
3140 case VT_CLSID: elemSize = sizeof(*pvarSrc->u.puuid); break;
3141 case VT_CF: elemSize = sizeof(*pvarSrc->u.pclipdata); break;
3142 case VT_BSTR: elemSize = sizeof(pvarSrc->u.bstrVal); break;
3143 case VT_LPSTR: elemSize = sizeof(pvarSrc->u.pszVal); break;
3144 case VT_LPWSTR: elemSize = sizeof(pvarSrc->u.pwszVal); break;
3145 case VT_VARIANT: elemSize = sizeof(*pvarSrc->u.pvarVal); break;
3147 default:
3148 FIXME("Invalid element type: %ul\n", pvarSrc->vt & ~VT_VECTOR);
3149 return E_INVALIDARG;
3151 len = pvarSrc->u.capropvar.cElems;
3152 pvarDest->u.capropvar.pElems = len ? CoTaskMemAlloc(len * elemSize) : NULL;
3153 if (pvarSrc->vt == (VT_VECTOR | VT_VARIANT))
3155 for (i = 0; i < len; i++)
3156 PropVariantCopy(&pvarDest->u.capropvar.pElems[i], &pvarSrc->u.capropvar.pElems[i]);
3158 else if (pvarSrc->vt == (VT_VECTOR | VT_CF))
3160 FIXME("Copy clipformats\n");
3162 else if (pvarSrc->vt == (VT_VECTOR | VT_BSTR))
3164 for (i = 0; i < len; i++)
3165 pvarDest->u.cabstr.pElems[i] = PropSysAllocString(pvarSrc->u.cabstr.pElems[i]);
3167 else if (pvarSrc->vt == (VT_VECTOR | VT_LPSTR))
3169 size_t strLen;
3170 for (i = 0; i < len; i++)
3172 strLen = lstrlenA(pvarSrc->u.calpstr.pElems[i]) + 1;
3173 pvarDest->u.calpstr.pElems[i] = CoTaskMemAlloc(strLen);
3174 memcpy(pvarDest->u.calpstr.pElems[i],
3175 pvarSrc->u.calpstr.pElems[i], strLen);
3178 else if (pvarSrc->vt == (VT_VECTOR | VT_LPWSTR))
3180 size_t strLen;
3181 for (i = 0; i < len; i++)
3183 strLen = (lstrlenW(pvarSrc->u.calpwstr.pElems[i]) + 1) *
3184 sizeof(WCHAR);
3185 pvarDest->u.calpstr.pElems[i] = CoTaskMemAlloc(strLen);
3186 memcpy(pvarDest->u.calpstr.pElems[i],
3187 pvarSrc->u.calpstr.pElems[i], strLen);
3190 else
3191 CopyMemory(pvarDest->u.capropvar.pElems, pvarSrc->u.capropvar.pElems, len * elemSize);
3193 else
3194 WARN("Invalid/unsupported type %d\n", pvarSrc->vt);
3197 return S_OK;
3200 /***********************************************************************
3201 * FreePropVariantArray [OLE32.@]
3203 HRESULT WINAPI FreePropVariantArray(ULONG cVariants, /* [in] */
3204 PROPVARIANT *rgvars) /* [in/out] */
3206 ULONG i;
3208 TRACE("(%u, %p)\n", cVariants, rgvars);
3210 if (!rgvars)
3211 return E_INVALIDARG;
3213 for(i = 0; i < cVariants; i++)
3214 PropVariantClear(&rgvars[i]);
3216 return S_OK;
3219 /******************************************************************************
3220 * DllDebugObjectRPCHook (OLE32.@)
3221 * turns on and off internal debugging, pointer is only used on macintosh
3224 BOOL WINAPI DllDebugObjectRPCHook(BOOL b, void *dummy)
3226 FIXME("stub\n");
3227 return TRUE;