Added descriptions for a few more machine types.
[wine/multimedia.git] / dlls / ole32 / ole2.c
blob6192ef2a2ed2bb4d225b2c56ef2ce229903a4275
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
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "config.h"
27 #include <assert.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <string.h>
33 #define COBJMACROS
34 #define NONAMELESSUNION
35 #define NONAMELESSSTRUCT
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 "commctrl.h"
45 #include "ole2.h"
46 #include "ole2ver.h"
47 #include "wownt32.h"
49 #include "wine/unicode.h"
50 #include "wine/winbase16.h"
51 #include "wine/wingdi16.h"
52 #include "wine/winuser16.h"
53 #include "compobj_private.h"
55 #include "wine/debug.h"
57 WINE_DEFAULT_DEBUG_CHANNEL(ole);
58 WINE_DECLARE_DEBUG_CHANNEL(accel);
60 #define HICON_16(h32) (LOWORD(h32))
61 #define HICON_32(h16) ((HICON)(ULONG_PTR)(h16))
62 #define HINSTANCE_32(h16) ((HINSTANCE)(ULONG_PTR)(h16))
64 /******************************************************************************
65 * These are static/global variables and internal data structures that the
66 * OLE module uses to maintain it's state.
68 typedef struct tagDropTargetNode
70 HWND hwndTarget;
71 IDropTarget* dropTarget;
72 struct tagDropTargetNode* prevDropTarget;
73 struct tagDropTargetNode* nextDropTarget;
74 } DropTargetNode;
76 typedef struct tagTrackerWindowInfo
78 IDataObject* dataObject;
79 IDropSource* dropSource;
80 DWORD dwOKEffect;
81 DWORD* pdwEffect;
82 BOOL trackingDone;
83 HRESULT returnValue;
85 BOOL escPressed;
86 HWND curTargetHWND; /* window the mouse is hovering over */
87 HWND curDragTargetHWND; /* might be a ancestor of curTargetHWND */
88 IDropTarget* curDragTarget;
89 } TrackerWindowInfo;
91 typedef struct tagOleMenuDescriptor /* OleMenuDescriptor */
93 HWND hwndFrame; /* The containers frame window */
94 HWND hwndActiveObject; /* The active objects window */
95 OLEMENUGROUPWIDTHS mgw; /* OLE menu group widths for the shared menu */
96 HMENU hmenuCombined; /* The combined menu */
97 BOOL bIsServerItem; /* True if the currently open popup belongs to the server */
98 } OleMenuDescriptor;
100 typedef struct tagOleMenuHookItem /* OleMenu hook item in per thread hook list */
102 DWORD tid; /* Thread Id */
103 HANDLE hHeap; /* Heap this is allocated from */
104 HHOOK GetMsg_hHook; /* message hook for WH_GETMESSAGE */
105 HHOOK CallWndProc_hHook; /* message hook for WH_CALLWNDPROC */
106 struct tagOleMenuHookItem *next;
107 } OleMenuHookItem;
109 static OleMenuHookItem *hook_list;
112 * This is the lock count on the OLE library. It is controlled by the
113 * OLEInitialize/OLEUninitialize methods.
115 static ULONG OLE_moduleLockCount = 0;
118 * Name of our registered window class.
120 static const char OLEDD_DRAGTRACKERCLASS[] = "WineDragDropTracker32";
123 * This is the head of the Drop target container.
125 static DropTargetNode* targetListHead = NULL;
127 /******************************************************************************
128 * These are the prototypes of miscelaneous utility methods
130 static void OLEUTL_ReadRegistryDWORDValue(HKEY regKey, DWORD* pdwValue);
132 /******************************************************************************
133 * These are the prototypes of the utility methods used to manage a shared menu
135 static void OLEMenu_Initialize(void);
136 static void OLEMenu_UnInitialize(void);
137 BOOL OLEMenu_InstallHooks( DWORD tid );
138 BOOL OLEMenu_UnInstallHooks( DWORD tid );
139 OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid );
140 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos );
141 BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor );
142 LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam);
143 LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam);
145 /******************************************************************************
146 * These are the prototypes of the OLE Clipboard initialization methods (in clipboard.c)
148 extern void OLEClipbrd_UnInitialize(void);
149 extern void OLEClipbrd_Initialize(void);
151 /******************************************************************************
152 * These are the prototypes of the utility methods used for OLE Drag n Drop
154 static void OLEDD_Initialize(void);
155 static void OLEDD_UnInitialize(void);
156 static void OLEDD_InsertDropTarget(
157 DropTargetNode* nodeToAdd);
158 static DropTargetNode* OLEDD_ExtractDropTarget(
159 HWND hwndOfTarget);
160 static DropTargetNode* OLEDD_FindDropTarget(
161 HWND hwndOfTarget);
162 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(
163 HWND hwnd,
164 UINT uMsg,
165 WPARAM wParam,
166 LPARAM lParam);
167 static void OLEDD_TrackMouseMove(
168 TrackerWindowInfo* trackerInfo,
169 POINT mousePos,
170 DWORD keyState);
171 static void OLEDD_TrackStateChange(
172 TrackerWindowInfo* trackerInfo,
173 POINT mousePos,
174 DWORD keyState);
175 static DWORD OLEDD_GetButtonState(void);
178 /******************************************************************************
179 * OleBuildVersion [OLE2.1]
180 * OleBuildVersion [OLE32.@]
182 DWORD WINAPI OleBuildVersion(void)
184 TRACE("Returning version %d, build %d.\n", rmm, rup);
185 return (rmm<<16)+rup;
188 /***********************************************************************
189 * OleInitialize (OLE2.2)
190 * OleInitialize (OLE32.@)
192 HRESULT WINAPI OleInitialize(LPVOID reserved)
194 HRESULT hr;
196 TRACE("(%p)\n", reserved);
199 * The first duty of the OleInitialize is to initialize the COM libraries.
201 hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
204 * If the CoInitializeEx call failed, the OLE libraries can't be
205 * initialized.
207 if (FAILED(hr))
208 return hr;
211 * Then, it has to initialize the OLE specific modules.
212 * This includes:
213 * Clipboard
214 * Drag and Drop
215 * Object linking and Embedding
216 * In-place activation
218 if (OLE_moduleLockCount==0)
221 * Initialize the libraries.
223 TRACE("() - Initializing the OLE libraries\n");
226 * OLE Clipboard
228 OLEClipbrd_Initialize();
231 * Drag and Drop
233 OLEDD_Initialize();
236 * OLE shared menu
238 OLEMenu_Initialize();
242 * Then, we increase the lock count on the OLE module.
244 OLE_moduleLockCount++;
246 return hr;
249 /******************************************************************************
250 * OleUninitialize [OLE2.3]
251 * OleUninitialize [OLE32.@]
253 void WINAPI OleUninitialize(void)
255 TRACE("()\n");
258 * Decrease the lock count on the OLE module.
260 OLE_moduleLockCount--;
263 * If we hit the bottom of the lock stack, free the libraries.
265 if (OLE_moduleLockCount==0)
268 * Actually free the libraries.
270 TRACE("() - Freeing the last reference count\n");
273 * OLE Clipboard
275 OLEClipbrd_UnInitialize();
278 * Drag and Drop
280 OLEDD_UnInitialize();
283 * OLE shared menu
285 OLEMenu_UnInitialize();
289 * Then, uninitialize the COM libraries.
291 CoUninitialize();
294 /******************************************************************************
295 * OleInitializeWOW [OLE32.@]
297 HRESULT WINAPI OleInitializeWOW(DWORD x, DWORD y) {
298 FIXME("(0x%08lx, 0x%08lx),stub!\n",x, y);
299 return 0;
302 /***********************************************************************
303 * RegisterDragDrop (OLE32.@)
305 HRESULT WINAPI RegisterDragDrop(
306 HWND hwnd,
307 LPDROPTARGET pDropTarget)
309 DropTargetNode* dropTargetInfo;
311 TRACE("(%p,%p)\n", hwnd, pDropTarget);
313 if (!pDropTarget)
314 return E_INVALIDARG;
317 * First, check if the window is already registered.
319 dropTargetInfo = OLEDD_FindDropTarget(hwnd);
321 if (dropTargetInfo!=NULL)
322 return DRAGDROP_E_ALREADYREGISTERED;
325 * If it's not there, we can add it. We first create a node for it.
327 dropTargetInfo = HeapAlloc(GetProcessHeap(), 0, sizeof(DropTargetNode));
329 if (dropTargetInfo==NULL)
330 return E_OUTOFMEMORY;
332 dropTargetInfo->hwndTarget = hwnd;
333 dropTargetInfo->prevDropTarget = NULL;
334 dropTargetInfo->nextDropTarget = NULL;
337 * Don't forget that this is an interface pointer, need to nail it down since
338 * we keep a copy of it.
340 dropTargetInfo->dropTarget = pDropTarget;
341 IDropTarget_AddRef(dropTargetInfo->dropTarget);
343 OLEDD_InsertDropTarget(dropTargetInfo);
345 return S_OK;
348 /***********************************************************************
349 * RevokeDragDrop (OLE32.@)
351 HRESULT WINAPI RevokeDragDrop(
352 HWND hwnd)
354 DropTargetNode* dropTargetInfo;
356 TRACE("(%p)\n", hwnd);
359 * First, check if the window is already registered.
361 dropTargetInfo = OLEDD_ExtractDropTarget(hwnd);
364 * If it ain't in there, it's an error.
366 if (dropTargetInfo==NULL)
367 return DRAGDROP_E_NOTREGISTERED;
370 * If it's in there, clean-up it's used memory and
371 * references
373 IDropTarget_Release(dropTargetInfo->dropTarget);
374 HeapFree(GetProcessHeap(), 0, dropTargetInfo);
376 return S_OK;
379 /***********************************************************************
380 * OleRegGetUserType (OLE32.@)
382 * This implementation of OleRegGetUserType ignores the dwFormOfType
383 * parameter and always returns the full name of the object. This is
384 * not too bad since this is the case for many objects because of the
385 * way they are registered.
387 HRESULT WINAPI OleRegGetUserType(
388 REFCLSID clsid,
389 DWORD dwFormOfType,
390 LPOLESTR* pszUserType)
392 char keyName[60];
393 DWORD dwKeyType;
394 DWORD cbData;
395 HKEY clsidKey;
396 LONG hres;
397 LPSTR buffer;
398 HRESULT retVal;
400 * Initialize the out parameter.
402 *pszUserType = NULL;
405 * Build the key name we're looking for
407 sprintf( keyName, "CLSID\\{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\",
408 clsid->Data1, clsid->Data2, clsid->Data3,
409 clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
410 clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );
412 TRACE("(%s, %ld, %p)\n", keyName, dwFormOfType, pszUserType);
415 * Open the class id Key
417 hres = RegOpenKeyA(HKEY_CLASSES_ROOT,
418 keyName,
419 &clsidKey);
421 if (hres != ERROR_SUCCESS)
422 return REGDB_E_CLASSNOTREG;
425 * Retrieve the size of the name string.
427 cbData = 0;
429 hres = RegQueryValueExA(clsidKey,
431 NULL,
432 &dwKeyType,
433 NULL,
434 &cbData);
436 if (hres!=ERROR_SUCCESS)
438 RegCloseKey(clsidKey);
439 return REGDB_E_READREGDB;
443 * Allocate a buffer for the registry value.
445 *pszUserType = CoTaskMemAlloc(cbData*2);
447 if (*pszUserType==NULL)
449 RegCloseKey(clsidKey);
450 return E_OUTOFMEMORY;
453 buffer = HeapAlloc(GetProcessHeap(), 0, cbData);
455 if (buffer == NULL)
457 RegCloseKey(clsidKey);
458 CoTaskMemFree(*pszUserType);
459 *pszUserType=NULL;
460 return E_OUTOFMEMORY;
463 hres = RegQueryValueExA(clsidKey,
465 NULL,
466 &dwKeyType,
467 (LPBYTE) buffer,
468 &cbData);
470 RegCloseKey(clsidKey);
473 if (hres!=ERROR_SUCCESS)
475 CoTaskMemFree(*pszUserType);
476 *pszUserType=NULL;
478 retVal = REGDB_E_READREGDB;
480 else
482 MultiByteToWideChar( CP_ACP, 0, buffer, -1, *pszUserType, cbData /*FIXME*/ );
483 retVal = S_OK;
485 HeapFree(GetProcessHeap(), 0, buffer);
487 return retVal;
490 /***********************************************************************
491 * DoDragDrop [OLE32.@]
493 HRESULT WINAPI DoDragDrop (
494 IDataObject *pDataObject, /* [in] ptr to the data obj */
495 IDropSource* pDropSource, /* [in] ptr to the source obj */
496 DWORD dwOKEffect, /* [in] effects allowed by the source */
497 DWORD *pdwEffect) /* [out] ptr to effects of the source */
499 TrackerWindowInfo trackerInfo;
500 HWND hwndTrackWindow;
501 MSG msg;
503 TRACE("(DataObject %p, DropSource %p)\n", pDataObject, pDropSource);
506 * Setup the drag n drop tracking window.
508 if (!IsValidInterface((LPUNKNOWN)pDropSource))
509 return E_INVALIDARG;
511 trackerInfo.dataObject = pDataObject;
512 trackerInfo.dropSource = pDropSource;
513 trackerInfo.dwOKEffect = dwOKEffect;
514 trackerInfo.pdwEffect = pdwEffect;
515 trackerInfo.trackingDone = FALSE;
516 trackerInfo.escPressed = FALSE;
517 trackerInfo.curDragTargetHWND = 0;
518 trackerInfo.curTargetHWND = 0;
519 trackerInfo.curDragTarget = 0;
521 hwndTrackWindow = CreateWindowA(OLEDD_DRAGTRACKERCLASS,
522 "TrackerWindow",
523 WS_POPUP,
524 CW_USEDEFAULT, CW_USEDEFAULT,
525 CW_USEDEFAULT, CW_USEDEFAULT,
529 (LPVOID)&trackerInfo);
531 if (hwndTrackWindow!=0)
534 * Capture the mouse input
536 SetCapture(hwndTrackWindow);
539 * Pump messages. All mouse input should go the the capture window.
541 while (!trackerInfo.trackingDone && GetMessageA(&msg, 0, 0, 0) )
543 if ( (msg.message >= WM_KEYFIRST) &&
544 (msg.message <= WM_KEYLAST) )
547 * When keyboard messages are sent to windows on this thread, we
548 * want to ignore notify the drop source that the state changed.
549 * in the case of the Escape key, we also notify the drop source
550 * we give it a special meaning.
552 if ( (msg.message==WM_KEYDOWN) &&
553 (msg.wParam==VK_ESCAPE) )
555 trackerInfo.escPressed = TRUE;
559 * Notify the drop source.
561 OLEDD_TrackStateChange(&trackerInfo,
562 msg.pt,
563 OLEDD_GetButtonState());
565 else
568 * Dispatch the messages only when it's not a keyboard message.
570 DispatchMessageA(&msg);
575 * Destroy the temporary window.
577 DestroyWindow(hwndTrackWindow);
579 return trackerInfo.returnValue;
582 return E_FAIL;
585 /***********************************************************************
586 * OleQueryLinkFromData [OLE32.@]
588 HRESULT WINAPI OleQueryLinkFromData(
589 IDataObject* pSrcDataObject)
591 FIXME("(%p),stub!\n", pSrcDataObject);
592 return S_OK;
595 /***********************************************************************
596 * OleRegGetMiscStatus [OLE32.@]
598 HRESULT WINAPI OleRegGetMiscStatus(
599 REFCLSID clsid,
600 DWORD dwAspect,
601 DWORD* pdwStatus)
603 char keyName[60];
604 HKEY clsidKey;
605 HKEY miscStatusKey;
606 HKEY aspectKey;
607 LONG result;
610 * Initialize the out parameter.
612 *pdwStatus = 0;
615 * Build the key name we're looking for
617 sprintf( keyName, "CLSID\\{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\",
618 clsid->Data1, clsid->Data2, clsid->Data3,
619 clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
620 clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );
622 TRACE("(%s, %ld, %p)\n", keyName, dwAspect, pdwStatus);
625 * Open the class id Key
627 result = RegOpenKeyA(HKEY_CLASSES_ROOT,
628 keyName,
629 &clsidKey);
631 if (result != ERROR_SUCCESS)
632 return REGDB_E_CLASSNOTREG;
635 * Get the MiscStatus
637 result = RegOpenKeyA(clsidKey,
638 "MiscStatus",
639 &miscStatusKey);
642 if (result != ERROR_SUCCESS)
644 RegCloseKey(clsidKey);
645 return REGDB_E_READREGDB;
649 * Read the default value
651 OLEUTL_ReadRegistryDWORDValue(miscStatusKey, pdwStatus);
654 * Open the key specific to the requested aspect.
656 sprintf(keyName, "%ld", dwAspect);
658 result = RegOpenKeyA(miscStatusKey,
659 keyName,
660 &aspectKey);
662 if (result == ERROR_SUCCESS)
664 OLEUTL_ReadRegistryDWORDValue(aspectKey, pdwStatus);
665 RegCloseKey(aspectKey);
669 * Cleanup
671 RegCloseKey(miscStatusKey);
672 RegCloseKey(clsidKey);
674 return S_OK;
677 /******************************************************************************
678 * OleSetContainedObject [OLE32.@]
680 HRESULT WINAPI OleSetContainedObject(
681 LPUNKNOWN pUnknown,
682 BOOL fContained)
684 IRunnableObject* runnable = NULL;
685 HRESULT hres;
687 TRACE("(%p,%x), stub!\n", pUnknown, fContained);
689 hres = IUnknown_QueryInterface(pUnknown,
690 &IID_IRunnableObject,
691 (void**)&runnable);
693 if (SUCCEEDED(hres))
695 hres = IRunnableObject_SetContainedObject(runnable, fContained);
697 IRunnableObject_Release(runnable);
699 return hres;
702 return S_OK;
705 /******************************************************************************
706 * OleLoad [OLE32.@]
708 HRESULT WINAPI OleLoad(
709 LPSTORAGE pStg,
710 REFIID riid,
711 LPOLECLIENTSITE pClientSite,
712 LPVOID* ppvObj)
714 IPersistStorage* persistStorage = NULL;
715 IOleObject* oleObject = NULL;
716 STATSTG storageInfo;
717 HRESULT hres;
719 TRACE("(%p,%p,%p,%p)\n", pStg, riid, pClientSite, ppvObj);
722 * TODO, Conversion ... OleDoAutoConvert
726 * Get the class ID for the object.
728 hres = IStorage_Stat(pStg, &storageInfo, STATFLAG_NONAME);
731 * Now, try and create the handler for the object
733 hres = CoCreateInstance(&storageInfo.clsid,
734 NULL,
735 CLSCTX_INPROC_HANDLER,
736 &IID_IOleObject,
737 (void**)&oleObject);
740 * If that fails, as it will most times, load the default
741 * OLE handler.
743 if (FAILED(hres))
745 hres = OleCreateDefaultHandler(&storageInfo.clsid,
746 NULL,
747 &IID_IOleObject,
748 (void**)&oleObject);
752 * If we couldn't find a handler... this is bad. Abort the whole thing.
754 if (FAILED(hres))
755 return hres;
758 * Inform the new object of it's client site.
760 hres = IOleObject_SetClientSite(oleObject, pClientSite);
763 * Initialize the object with it's IPersistStorage interface.
765 hres = IOleObject_QueryInterface(oleObject,
766 &IID_IPersistStorage,
767 (void**)&persistStorage);
769 if (SUCCEEDED(hres))
771 IPersistStorage_Load(persistStorage, pStg);
773 IPersistStorage_Release(persistStorage);
774 persistStorage = NULL;
778 * Return the requested interface to the caller.
780 hres = IOleObject_QueryInterface(oleObject, riid, ppvObj);
783 * Cleanup interfaces used internally
785 IOleObject_Release(oleObject);
787 return hres;
790 /***********************************************************************
791 * OleSave [OLE32.@]
793 HRESULT WINAPI OleSave(
794 LPPERSISTSTORAGE pPS,
795 LPSTORAGE pStg,
796 BOOL fSameAsLoad)
798 HRESULT hres;
799 CLSID objectClass;
801 TRACE("(%p,%p,%x)\n", pPS, pStg, fSameAsLoad);
804 * First, we transfer the class ID (if available)
806 hres = IPersistStorage_GetClassID(pPS, &objectClass);
808 if (SUCCEEDED(hres))
810 WriteClassStg(pStg, &objectClass);
814 * Then, we ask the object to save itself to the
815 * storage. If it is successful, we commit the storage.
817 hres = IPersistStorage_Save(pPS, pStg, fSameAsLoad);
819 if (SUCCEEDED(hres))
821 IStorage_Commit(pStg,
822 STGC_DEFAULT);
825 return hres;
829 /******************************************************************************
830 * OleLockRunning [OLE32.@]
832 HRESULT WINAPI OleLockRunning(LPUNKNOWN pUnknown, BOOL fLock, BOOL fLastUnlockCloses)
834 IRunnableObject* runnable = NULL;
835 HRESULT hres;
837 TRACE("(%p,%x,%x)\n", pUnknown, fLock, fLastUnlockCloses);
839 hres = IUnknown_QueryInterface(pUnknown,
840 &IID_IRunnableObject,
841 (void**)&runnable);
843 if (SUCCEEDED(hres))
845 hres = IRunnableObject_LockRunning(runnable, fLock, fLastUnlockCloses);
847 IRunnableObject_Release(runnable);
849 return hres;
851 else
852 return E_INVALIDARG;
856 /**************************************************************************
857 * Internal methods to manage the shared OLE menu in response to the
858 * OLE***MenuDescriptor API
861 /***
862 * OLEMenu_Initialize()
864 * Initializes the OLEMENU data structures.
866 static void OLEMenu_Initialize()
870 /***
871 * OLEMenu_UnInitialize()
873 * Releases the OLEMENU data structures.
875 static void OLEMenu_UnInitialize()
879 /*************************************************************************
880 * OLEMenu_InstallHooks
881 * Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
883 * RETURNS: TRUE if message hooks were successfully installed
884 * FALSE on failure
886 BOOL OLEMenu_InstallHooks( DWORD tid )
888 OleMenuHookItem *pHookItem = NULL;
890 /* Create an entry for the hook table */
891 if ( !(pHookItem = HeapAlloc(GetProcessHeap(), 0,
892 sizeof(OleMenuHookItem)) ) )
893 return FALSE;
895 pHookItem->tid = tid;
896 pHookItem->hHeap = GetProcessHeap();
898 /* Install a thread scope message hook for WH_GETMESSAGE */
899 pHookItem->GetMsg_hHook = SetWindowsHookExA( WH_GETMESSAGE, OLEMenu_GetMsgProc,
900 0, GetCurrentThreadId() );
901 if ( !pHookItem->GetMsg_hHook )
902 goto CLEANUP;
904 /* Install a thread scope message hook for WH_CALLWNDPROC */
905 pHookItem->CallWndProc_hHook = SetWindowsHookExA( WH_CALLWNDPROC, OLEMenu_CallWndProc,
906 0, GetCurrentThreadId() );
907 if ( !pHookItem->CallWndProc_hHook )
908 goto CLEANUP;
910 /* Insert the hook table entry */
911 pHookItem->next = hook_list;
912 hook_list = pHookItem;
914 return TRUE;
916 CLEANUP:
917 /* Unhook any hooks */
918 if ( pHookItem->GetMsg_hHook )
919 UnhookWindowsHookEx( pHookItem->GetMsg_hHook );
920 if ( pHookItem->CallWndProc_hHook )
921 UnhookWindowsHookEx( pHookItem->CallWndProc_hHook );
922 /* Release the hook table entry */
923 HeapFree(pHookItem->hHeap, 0, pHookItem );
925 return FALSE;
928 /*************************************************************************
929 * OLEMenu_UnInstallHooks
930 * UnInstall thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
932 * RETURNS: TRUE if message hooks were successfully installed
933 * FALSE on failure
935 BOOL OLEMenu_UnInstallHooks( DWORD tid )
937 OleMenuHookItem *pHookItem = NULL;
938 OleMenuHookItem **ppHook = &hook_list;
940 while (*ppHook)
942 if ((*ppHook)->tid == tid)
944 pHookItem = *ppHook;
945 *ppHook = pHookItem->next;
946 break;
948 ppHook = &(*ppHook)->next;
950 if (!pHookItem) return FALSE;
952 /* Uninstall the hooks installed for this thread */
953 if ( !UnhookWindowsHookEx( pHookItem->GetMsg_hHook ) )
954 goto CLEANUP;
955 if ( !UnhookWindowsHookEx( pHookItem->CallWndProc_hHook ) )
956 goto CLEANUP;
958 /* Release the hook table entry */
959 HeapFree(pHookItem->hHeap, 0, pHookItem );
961 return TRUE;
963 CLEANUP:
964 /* Release the hook table entry */
965 HeapFree(pHookItem->hHeap, 0, pHookItem );
967 return FALSE;
970 /*************************************************************************
971 * OLEMenu_IsHookInstalled
972 * Tests if OLEMenu hooks have been installed for a thread
974 * RETURNS: The pointer and index of the hook table entry for the tid
975 * NULL and -1 for the index if no hooks were installed for this thread
977 OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid )
979 OleMenuHookItem *pHookItem = NULL;
981 /* Do a simple linear search for an entry whose tid matches ours.
982 * We really need a map but efficiency is not a concern here. */
983 for (pHookItem = hook_list; pHookItem; pHookItem = pHookItem->next)
985 if ( tid == pHookItem->tid )
986 return pHookItem;
989 return NULL;
992 /***********************************************************************
993 * OLEMenu_FindMainMenuIndex
995 * Used by OLEMenu API to find the top level group a menu item belongs to.
996 * On success pnPos contains the index of the item in the top level menu group
998 * RETURNS: TRUE if the ID was found, FALSE on failure
1000 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos )
1002 UINT i, nItems;
1004 nItems = GetMenuItemCount( hMainMenu );
1006 for (i = 0; i < nItems; i++)
1008 HMENU hsubmenu;
1010 /* Is the current item a submenu? */
1011 if ( (hsubmenu = GetSubMenu(hMainMenu, i)) )
1013 /* If the handle is the same we're done */
1014 if ( hsubmenu == hPopupMenu )
1016 if (pnPos)
1017 *pnPos = i;
1018 return TRUE;
1020 /* Recursively search without updating pnPos */
1021 else if ( OLEMenu_FindMainMenuIndex( hsubmenu, hPopupMenu, NULL ) )
1023 if (pnPos)
1024 *pnPos = i;
1025 return TRUE;
1030 return FALSE;
1033 /***********************************************************************
1034 * OLEMenu_SetIsServerMenu
1036 * Checks whether a popup menu belongs to a shared menu group which is
1037 * owned by the server, and sets the menu descriptor state accordingly.
1038 * All menu messages from these groups should be routed to the server.
1040 * RETURNS: TRUE if the popup menu is part of a server owned group
1041 * FALSE if the popup menu is part of a container owned group
1043 BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor )
1045 UINT nPos = 0, nWidth, i;
1047 pOleMenuDescriptor->bIsServerItem = FALSE;
1049 /* Don't bother searching if the popup is the combined menu itself */
1050 if ( hmenu == pOleMenuDescriptor->hmenuCombined )
1051 return FALSE;
1053 /* Find the menu item index in the shared OLE menu that this item belongs to */
1054 if ( !OLEMenu_FindMainMenuIndex( pOleMenuDescriptor->hmenuCombined, hmenu, &nPos ) )
1055 return FALSE;
1057 /* The group widths array has counts for the number of elements
1058 * in the groups File, Edit, Container, Object, Window, Help.
1059 * The Edit, Object & Help groups belong to the server object
1060 * and the other three belong to the container.
1061 * Loop through the group widths and locate the group we are a member of.
1063 for ( i = 0, nWidth = 0; i < 6; i++ )
1065 nWidth += pOleMenuDescriptor->mgw.width[i];
1066 if ( nPos < nWidth )
1068 /* Odd elements are server menu widths */
1069 pOleMenuDescriptor->bIsServerItem = (i%2) ? TRUE : FALSE;
1070 break;
1074 return pOleMenuDescriptor->bIsServerItem;
1077 /*************************************************************************
1078 * OLEMenu_CallWndProc
1079 * Thread scope WH_CALLWNDPROC hook proc filter function (callback)
1080 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1082 LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam)
1084 LPCWPSTRUCT pMsg = NULL;
1085 HOLEMENU hOleMenu = 0;
1086 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1087 OleMenuHookItem *pHookItem = NULL;
1088 WORD fuFlags;
1090 TRACE("%i, %04x, %08x\n", code, wParam, (unsigned)lParam );
1092 /* Check if we're being asked to process the message */
1093 if ( HC_ACTION != code )
1094 goto NEXTHOOK;
1096 /* Retrieve the current message being dispatched from lParam */
1097 pMsg = (LPCWPSTRUCT)lParam;
1099 /* Check if the message is destined for a window we are interested in:
1100 * If the window has an OLEMenu property we may need to dispatch
1101 * the menu message to its active objects window instead. */
1103 hOleMenu = (HOLEMENU)GetPropA( pMsg->hwnd, "PROP_OLEMenuDescriptor" );
1104 if ( !hOleMenu )
1105 goto NEXTHOOK;
1107 /* Get the menu descriptor */
1108 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1109 if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1110 goto NEXTHOOK;
1112 /* Process menu messages */
1113 switch( pMsg->message )
1115 case WM_INITMENU:
1117 /* Reset the menu descriptor state */
1118 pOleMenuDescriptor->bIsServerItem = FALSE;
1120 /* Send this message to the server as well */
1121 SendMessageA( pOleMenuDescriptor->hwndActiveObject,
1122 pMsg->message, pMsg->wParam, pMsg->lParam );
1123 goto NEXTHOOK;
1126 case WM_INITMENUPOPUP:
1128 /* Save the state for whether this is a server owned menu */
1129 OLEMenu_SetIsServerMenu( (HMENU)pMsg->wParam, pOleMenuDescriptor );
1130 break;
1133 case WM_MENUSELECT:
1135 fuFlags = HIWORD(pMsg->wParam); /* Get flags */
1136 if ( fuFlags & MF_SYSMENU )
1137 goto NEXTHOOK;
1139 /* Save the state for whether this is a server owned popup menu */
1140 else if ( fuFlags & MF_POPUP )
1141 OLEMenu_SetIsServerMenu( (HMENU)pMsg->lParam, pOleMenuDescriptor );
1143 break;
1146 case WM_DRAWITEM:
1148 LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT) pMsg->lParam;
1149 if ( pMsg->wParam != 0 || lpdis->CtlType != ODT_MENU )
1150 goto NEXTHOOK; /* Not a menu message */
1152 break;
1155 default:
1156 goto NEXTHOOK;
1159 /* If the message was for the server dispatch it accordingly */
1160 if ( pOleMenuDescriptor->bIsServerItem )
1162 SendMessageA( pOleMenuDescriptor->hwndActiveObject,
1163 pMsg->message, pMsg->wParam, pMsg->lParam );
1166 NEXTHOOK:
1167 if ( pOleMenuDescriptor )
1168 GlobalUnlock( hOleMenu );
1170 /* Lookup the hook item for the current thread */
1171 if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1173 /* This should never fail!! */
1174 WARN("could not retrieve hHook for current thread!\n" );
1175 return 0;
1178 /* Pass on the message to the next hooker */
1179 return CallNextHookEx( pHookItem->CallWndProc_hHook, code, wParam, lParam );
1182 /*************************************************************************
1183 * OLEMenu_GetMsgProc
1184 * Thread scope WH_GETMESSAGE hook proc filter function (callback)
1185 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1187 LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam)
1189 LPMSG pMsg = NULL;
1190 HOLEMENU hOleMenu = 0;
1191 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1192 OleMenuHookItem *pHookItem = NULL;
1193 WORD wCode;
1195 TRACE("%i, %04x, %08x\n", code, wParam, (unsigned)lParam );
1197 /* Check if we're being asked to process a messages */
1198 if ( HC_ACTION != code )
1199 goto NEXTHOOK;
1201 /* Retrieve the current message being dispatched from lParam */
1202 pMsg = (LPMSG)lParam;
1204 /* Check if the message is destined for a window we are interested in:
1205 * If the window has an OLEMenu property we may need to dispatch
1206 * the menu message to its active objects window instead. */
1208 hOleMenu = (HOLEMENU)GetPropA( pMsg->hwnd, "PROP_OLEMenuDescriptor" );
1209 if ( !hOleMenu )
1210 goto NEXTHOOK;
1212 /* Process menu messages */
1213 switch( pMsg->message )
1215 case WM_COMMAND:
1217 wCode = HIWORD(pMsg->wParam); /* Get notification code */
1218 if ( wCode )
1219 goto NEXTHOOK; /* Not a menu message */
1220 break;
1222 default:
1223 goto NEXTHOOK;
1226 /* Get the menu descriptor */
1227 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1228 if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1229 goto NEXTHOOK;
1231 /* If the message was for the server dispatch it accordingly */
1232 if ( pOleMenuDescriptor->bIsServerItem )
1234 /* Change the hWnd in the message to the active objects hWnd.
1235 * The message loop which reads this message will automatically
1236 * dispatch it to the embedded objects window. */
1237 pMsg->hwnd = pOleMenuDescriptor->hwndActiveObject;
1240 NEXTHOOK:
1241 if ( pOleMenuDescriptor )
1242 GlobalUnlock( hOleMenu );
1244 /* Lookup the hook item for the current thread */
1245 if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1247 /* This should never fail!! */
1248 WARN("could not retrieve hHook for current thread!\n" );
1249 return FALSE;
1252 /* Pass on the message to the next hooker */
1253 return CallNextHookEx( pHookItem->GetMsg_hHook, code, wParam, lParam );
1256 /***********************************************************************
1257 * OleCreateMenuDescriptor [OLE32.@]
1258 * Creates an OLE menu descriptor for OLE to use when dispatching
1259 * menu messages and commands.
1261 * PARAMS:
1262 * hmenuCombined - Handle to the objects combined menu
1263 * lpMenuWidths - Pointer to array of 6 LONG's indicating menus per group
1266 HOLEMENU WINAPI OleCreateMenuDescriptor(
1267 HMENU hmenuCombined,
1268 LPOLEMENUGROUPWIDTHS lpMenuWidths)
1270 HOLEMENU hOleMenu;
1271 OleMenuDescriptor *pOleMenuDescriptor;
1272 int i;
1274 if ( !hmenuCombined || !lpMenuWidths )
1275 return 0;
1277 /* Create an OLE menu descriptor */
1278 if ( !(hOleMenu = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT,
1279 sizeof(OleMenuDescriptor) ) ) )
1280 return 0;
1282 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1283 if ( !pOleMenuDescriptor )
1284 return 0;
1286 /* Initialize menu group widths and hmenu */
1287 for ( i = 0; i < 6; i++ )
1288 pOleMenuDescriptor->mgw.width[i] = lpMenuWidths->width[i];
1290 pOleMenuDescriptor->hmenuCombined = hmenuCombined;
1291 pOleMenuDescriptor->bIsServerItem = FALSE;
1292 GlobalUnlock( hOleMenu );
1294 return hOleMenu;
1297 /***********************************************************************
1298 * OleDestroyMenuDescriptor [OLE32.@]
1299 * Destroy the shared menu descriptor
1301 HRESULT WINAPI OleDestroyMenuDescriptor(
1302 HOLEMENU hmenuDescriptor)
1304 if ( hmenuDescriptor )
1305 GlobalFree( hmenuDescriptor );
1306 return S_OK;
1309 /***********************************************************************
1310 * OleSetMenuDescriptor [OLE32.@]
1311 * Installs or removes OLE dispatching code for the containers frame window
1312 * FIXME: The lpFrame and lpActiveObject parameters are currently ignored
1313 * OLE should install context sensitive help F1 filtering for the app when
1314 * these are non null.
1316 * PARAMS:
1317 * hOleMenu Handle to composite menu descriptor
1318 * hwndFrame Handle to containers frame window
1319 * hwndActiveObject Handle to objects in-place activation window
1320 * lpFrame Pointer to IOleInPlaceFrame on containers window
1321 * lpActiveObject Pointer to IOleInPlaceActiveObject on active in-place object
1323 * RETURNS:
1324 * S_OK - menu installed correctly
1325 * E_FAIL, E_INVALIDARG, E_UNEXPECTED - failure
1327 HRESULT WINAPI OleSetMenuDescriptor(
1328 HOLEMENU hOleMenu,
1329 HWND hwndFrame,
1330 HWND hwndActiveObject,
1331 LPOLEINPLACEFRAME lpFrame,
1332 LPOLEINPLACEACTIVEOBJECT lpActiveObject)
1334 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1336 /* Check args */
1337 if ( !hwndFrame || (hOleMenu && !hwndActiveObject) )
1338 return E_INVALIDARG;
1340 if ( lpFrame || lpActiveObject )
1342 FIXME("(%x, %p, %p, %p, %p), Context sensitive help filtering not implemented!\n",
1343 (unsigned int)hOleMenu,
1344 hwndFrame,
1345 hwndActiveObject,
1346 lpFrame,
1347 lpActiveObject);
1350 /* Set up a message hook to intercept the containers frame window messages.
1351 * The message filter is responsible for dispatching menu messages from the
1352 * shared menu which are intended for the object.
1355 if ( hOleMenu ) /* Want to install dispatching code */
1357 /* If OLEMenu hooks are already installed for this thread, fail
1358 * Note: This effectively means that OleSetMenuDescriptor cannot
1359 * be called twice in succession on the same frame window
1360 * without first calling it with a null hOleMenu to uninstall */
1361 if ( OLEMenu_IsHookInstalled( GetCurrentThreadId() ) )
1362 return E_FAIL;
1364 /* Get the menu descriptor */
1365 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1366 if ( !pOleMenuDescriptor )
1367 return E_UNEXPECTED;
1369 /* Update the menu descriptor */
1370 pOleMenuDescriptor->hwndFrame = hwndFrame;
1371 pOleMenuDescriptor->hwndActiveObject = hwndActiveObject;
1373 GlobalUnlock( hOleMenu );
1374 pOleMenuDescriptor = NULL;
1376 /* Add a menu descriptor windows property to the frame window */
1377 SetPropA( hwndFrame, "PROP_OLEMenuDescriptor", hOleMenu );
1379 /* Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC */
1380 if ( !OLEMenu_InstallHooks( GetCurrentThreadId() ) )
1381 return E_FAIL;
1383 else /* Want to uninstall dispatching code */
1385 /* Uninstall the hooks */
1386 if ( !OLEMenu_UnInstallHooks( GetCurrentThreadId() ) )
1387 return E_FAIL;
1389 /* Remove the menu descriptor property from the frame window */
1390 RemovePropA( hwndFrame, "PROP_OLEMenuDescriptor" );
1393 return S_OK;
1396 /******************************************************************************
1397 * IsAccelerator [OLE32.@]
1398 * Mostly copied from controls/menu.c TranslateAccelerator implementation
1400 BOOL WINAPI IsAccelerator(HACCEL hAccel, int cAccelEntries, LPMSG lpMsg, WORD* lpwCmd)
1402 LPACCEL lpAccelTbl;
1403 int i;
1405 if(!lpMsg) return FALSE;
1406 if (!hAccel)
1408 WARN_(accel)("NULL accel handle\n");
1409 return FALSE;
1411 if((lpMsg->message != WM_KEYDOWN &&
1412 lpMsg->message != WM_KEYUP &&
1413 lpMsg->message != WM_SYSKEYDOWN &&
1414 lpMsg->message != WM_SYSKEYUP &&
1415 lpMsg->message != WM_CHAR)) return FALSE;
1416 lpAccelTbl = HeapAlloc(GetProcessHeap(), 0, cAccelEntries * sizeof(ACCEL));
1417 if (NULL == lpAccelTbl)
1419 return FALSE;
1421 if (CopyAcceleratorTableW(hAccel, lpAccelTbl, cAccelEntries) != cAccelEntries)
1423 WARN_(accel)("CopyAcceleratorTableW failed\n");
1424 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
1425 return FALSE;
1428 TRACE_(accel)("hAccel=%p, cAccelEntries=%d,"
1429 "msg->hwnd=%p, msg->message=%04x, wParam=%08x, lParam=%08lx\n",
1430 hAccel, cAccelEntries,
1431 lpMsg->hwnd, lpMsg->message, lpMsg->wParam, lpMsg->lParam);
1432 for(i = 0; i < cAccelEntries; i++)
1434 if(lpAccelTbl[i].key != lpMsg->wParam)
1435 continue;
1437 if(lpMsg->message == WM_CHAR)
1439 if(!(lpAccelTbl[i].fVirt & FALT) && !(lpAccelTbl[i].fVirt & FVIRTKEY))
1441 TRACE_(accel)("found accel for WM_CHAR: ('%c')\n", lpMsg->wParam & 0xff);
1442 goto found;
1445 else
1447 if(lpAccelTbl[i].fVirt & FVIRTKEY)
1449 INT mask = 0;
1450 TRACE_(accel)("found accel for virt_key %04x (scan %04x)\n",
1451 lpMsg->wParam, HIWORD(lpMsg->lParam) & 0xff);
1452 if(GetKeyState(VK_SHIFT) & 0x8000) mask |= FSHIFT;
1453 if(GetKeyState(VK_CONTROL) & 0x8000) mask |= FCONTROL;
1454 if(GetKeyState(VK_MENU) & 0x8000) mask |= FALT;
1455 if(mask == (lpAccelTbl[i].fVirt & (FSHIFT | FCONTROL | FALT))) goto found;
1456 TRACE_(accel)("incorrect SHIFT/CTRL/ALT-state\n");
1458 else
1460 if(!(lpMsg->lParam & 0x01000000)) /* no special_key */
1462 if((lpAccelTbl[i].fVirt & FALT) && (lpMsg->lParam & 0x20000000))
1463 { /* ^^ ALT pressed */
1464 TRACE_(accel)("found accel for Alt-%c\n", lpMsg->wParam & 0xff);
1465 goto found;
1472 WARN_(accel)("couldn't translate accelerator key\n");
1473 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
1474 return FALSE;
1476 found:
1477 if(lpwCmd) *lpwCmd = lpAccelTbl[i].cmd;
1478 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
1479 return TRUE;
1482 /***********************************************************************
1483 * ReleaseStgMedium [OLE32.@]
1485 void WINAPI ReleaseStgMedium(
1486 STGMEDIUM* pmedium)
1488 switch (pmedium->tymed)
1490 case TYMED_HGLOBAL:
1492 if ( (pmedium->pUnkForRelease==0) &&
1493 (pmedium->u.hGlobal!=0) )
1494 GlobalFree(pmedium->u.hGlobal);
1495 break;
1497 case TYMED_FILE:
1499 if (pmedium->u.lpszFileName!=0)
1501 if (pmedium->pUnkForRelease==0)
1503 DeleteFileW(pmedium->u.lpszFileName);
1506 CoTaskMemFree(pmedium->u.lpszFileName);
1508 break;
1510 case TYMED_ISTREAM:
1512 if (pmedium->u.pstm!=0)
1514 IStream_Release(pmedium->u.pstm);
1516 break;
1518 case TYMED_ISTORAGE:
1520 if (pmedium->u.pstg!=0)
1522 IStorage_Release(pmedium->u.pstg);
1524 break;
1526 case TYMED_GDI:
1528 if ( (pmedium->pUnkForRelease==0) &&
1529 (pmedium->u.hBitmap!=0) )
1530 DeleteObject(pmedium->u.hBitmap);
1531 break;
1533 case TYMED_MFPICT:
1535 if ( (pmedium->pUnkForRelease==0) &&
1536 (pmedium->u.hMetaFilePict!=0) )
1538 LPMETAFILEPICT pMP = GlobalLock(pmedium->u.hMetaFilePict);
1539 DeleteMetaFile(pMP->hMF);
1540 GlobalUnlock(pmedium->u.hMetaFilePict);
1541 GlobalFree(pmedium->u.hMetaFilePict);
1543 break;
1545 case TYMED_ENHMF:
1547 if ( (pmedium->pUnkForRelease==0) &&
1548 (pmedium->u.hEnhMetaFile!=0) )
1550 DeleteEnhMetaFile(pmedium->u.hEnhMetaFile);
1552 break;
1554 case TYMED_NULL:
1555 default:
1556 break;
1558 pmedium->tymed=TYMED_NULL;
1561 * After cleaning up, the unknown is released
1563 if (pmedium->pUnkForRelease!=0)
1565 IUnknown_Release(pmedium->pUnkForRelease);
1566 pmedium->pUnkForRelease = 0;
1570 /***
1571 * OLEDD_Initialize()
1573 * Initializes the OLE drag and drop data structures.
1575 static void OLEDD_Initialize()
1577 WNDCLASSA wndClass;
1579 ZeroMemory (&wndClass, sizeof(WNDCLASSA));
1580 wndClass.style = CS_GLOBALCLASS;
1581 wndClass.lpfnWndProc = OLEDD_DragTrackerWindowProc;
1582 wndClass.cbClsExtra = 0;
1583 wndClass.cbWndExtra = sizeof(TrackerWindowInfo*);
1584 wndClass.hCursor = 0;
1585 wndClass.hbrBackground = 0;
1586 wndClass.lpszClassName = OLEDD_DRAGTRACKERCLASS;
1588 RegisterClassA (&wndClass);
1591 /***
1592 * OLEDD_UnInitialize()
1594 * Releases the OLE drag and drop data structures.
1596 static void OLEDD_UnInitialize()
1599 * Simply empty the list.
1601 while (targetListHead!=NULL)
1603 RevokeDragDrop(targetListHead->hwndTarget);
1607 /***
1608 * OLEDD_InsertDropTarget()
1610 * Insert the target node in the tree.
1612 static void OLEDD_InsertDropTarget(DropTargetNode* nodeToAdd)
1614 DropTargetNode* curNode;
1615 DropTargetNode** parentNodeLink;
1618 * Iterate the tree to find the insertion point.
1620 curNode = targetListHead;
1621 parentNodeLink = &targetListHead;
1623 while (curNode!=NULL)
1625 if (nodeToAdd->hwndTarget<curNode->hwndTarget)
1628 * If the node we want to add has a smaller HWND, go left
1630 parentNodeLink = &curNode->prevDropTarget;
1631 curNode = curNode->prevDropTarget;
1633 else if (nodeToAdd->hwndTarget>curNode->hwndTarget)
1636 * If the node we want to add has a larger HWND, go right
1638 parentNodeLink = &curNode->nextDropTarget;
1639 curNode = curNode->nextDropTarget;
1641 else
1644 * The item was found in the list. It shouldn't have been there
1646 assert(FALSE);
1647 return;
1652 * If we get here, we have found a spot for our item. The parentNodeLink
1653 * pointer points to the pointer that we have to modify.
1654 * The curNode should be NULL. We just have to establish the link and Voila!
1656 assert(curNode==NULL);
1657 assert(parentNodeLink!=NULL);
1658 assert(*parentNodeLink==NULL);
1660 *parentNodeLink=nodeToAdd;
1663 /***
1664 * OLEDD_ExtractDropTarget()
1666 * Removes the target node from the tree.
1668 static DropTargetNode* OLEDD_ExtractDropTarget(HWND hwndOfTarget)
1670 DropTargetNode* curNode;
1671 DropTargetNode** parentNodeLink;
1674 * Iterate the tree to find the insertion point.
1676 curNode = targetListHead;
1677 parentNodeLink = &targetListHead;
1679 while (curNode!=NULL)
1681 if (hwndOfTarget<curNode->hwndTarget)
1684 * If the node we want to add has a smaller HWND, go left
1686 parentNodeLink = &curNode->prevDropTarget;
1687 curNode = curNode->prevDropTarget;
1689 else if (hwndOfTarget>curNode->hwndTarget)
1692 * If the node we want to add has a larger HWND, go right
1694 parentNodeLink = &curNode->nextDropTarget;
1695 curNode = curNode->nextDropTarget;
1697 else
1700 * The item was found in the list. Detach it from it's parent and
1701 * re-insert it's kids in the tree.
1703 assert(parentNodeLink!=NULL);
1704 assert(*parentNodeLink==curNode);
1707 * We arbitrately re-attach the left sub-tree to the parent.
1709 *parentNodeLink = curNode->prevDropTarget;
1712 * And we re-insert the right subtree
1714 if (curNode->nextDropTarget!=NULL)
1716 OLEDD_InsertDropTarget(curNode->nextDropTarget);
1720 * The node we found is still a valid node once we complete
1721 * the unlinking of the kids.
1723 curNode->nextDropTarget=NULL;
1724 curNode->prevDropTarget=NULL;
1726 return curNode;
1731 * If we get here, the node is not in the tree
1733 return NULL;
1736 /***
1737 * OLEDD_FindDropTarget()
1739 * Finds information about the drop target.
1741 static DropTargetNode* OLEDD_FindDropTarget(HWND hwndOfTarget)
1743 DropTargetNode* curNode;
1746 * Iterate the tree to find the HWND value.
1748 curNode = targetListHead;
1750 while (curNode!=NULL)
1752 if (hwndOfTarget<curNode->hwndTarget)
1755 * If the node we want to add has a smaller HWND, go left
1757 curNode = curNode->prevDropTarget;
1759 else if (hwndOfTarget>curNode->hwndTarget)
1762 * If the node we want to add has a larger HWND, go right
1764 curNode = curNode->nextDropTarget;
1766 else
1769 * The item was found in the list.
1771 return curNode;
1776 * If we get here, the item is not in the list
1778 return NULL;
1781 /***
1782 * OLEDD_DragTrackerWindowProc()
1784 * This method is the WindowProcedure of the drag n drop tracking
1785 * window. During a drag n Drop operation, an invisible window is created
1786 * to receive the user input and act upon it. This procedure is in charge
1787 * of this behavior.
1789 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(
1790 HWND hwnd,
1791 UINT uMsg,
1792 WPARAM wParam,
1793 LPARAM lParam)
1795 switch (uMsg)
1797 case WM_CREATE:
1799 LPCREATESTRUCTA createStruct = (LPCREATESTRUCTA)lParam;
1801 SetWindowLongA(hwnd, 0, (LONG)createStruct->lpCreateParams);
1804 break;
1806 case WM_MOUSEMOVE:
1808 TrackerWindowInfo* trackerInfo = (TrackerWindowInfo*)GetWindowLongA(hwnd, 0);
1809 POINT mousePos;
1812 * Get the current mouse position in screen coordinates.
1814 mousePos.x = LOWORD(lParam);
1815 mousePos.y = HIWORD(lParam);
1816 ClientToScreen(hwnd, &mousePos);
1819 * Track the movement of the mouse.
1821 OLEDD_TrackMouseMove(trackerInfo, mousePos, wParam);
1823 break;
1825 case WM_LBUTTONUP:
1826 case WM_MBUTTONUP:
1827 case WM_RBUTTONUP:
1828 case WM_LBUTTONDOWN:
1829 case WM_MBUTTONDOWN:
1830 case WM_RBUTTONDOWN:
1832 TrackerWindowInfo* trackerInfo = (TrackerWindowInfo*)GetWindowLongA(hwnd, 0);
1833 POINT mousePos;
1836 * Get the current mouse position in screen coordinates.
1838 mousePos.x = LOWORD(lParam);
1839 mousePos.y = HIWORD(lParam);
1840 ClientToScreen(hwnd, &mousePos);
1843 * Notify everyone that the button state changed
1844 * TODO: Check if the "escape" key was pressed.
1846 OLEDD_TrackStateChange(trackerInfo, mousePos, wParam);
1848 break;
1853 * This is a window proc after all. Let's call the default.
1855 return DefWindowProcA (hwnd, uMsg, wParam, lParam);
1858 /***
1859 * OLEDD_TrackMouseMove()
1861 * This method is invoked while a drag and drop operation is in effect.
1862 * it will generate the appropriate callbacks in the drop source
1863 * and drop target. It will also provide the expected feedback to
1864 * the user.
1866 * params:
1867 * trackerInfo - Pointer to the structure identifying the
1868 * drag & drop operation that is currently
1869 * active.
1870 * mousePos - Current position of the mouse in screen
1871 * coordinates.
1872 * keyState - Contains the state of the shift keys and the
1873 * mouse buttons (MK_LBUTTON and the like)
1875 static void OLEDD_TrackMouseMove(
1876 TrackerWindowInfo* trackerInfo,
1877 POINT mousePos,
1878 DWORD keyState)
1880 HWND hwndNewTarget = 0;
1881 HRESULT hr = S_OK;
1884 * Get the handle of the window under the mouse
1886 hwndNewTarget = WindowFromPoint(mousePos);
1889 * Every time, we re-initialize the effects passed to the
1890 * IDropTarget to the effects allowed by the source.
1892 *trackerInfo->pdwEffect = trackerInfo->dwOKEffect;
1895 * If we are hovering over the same target as before, send the
1896 * DragOver notification
1898 if ( (trackerInfo->curDragTarget != 0) &&
1899 (trackerInfo->curTargetHWND == hwndNewTarget) )
1901 POINTL mousePosParam;
1904 * The documentation tells me that the coordinate should be in the target
1905 * window's coordinate space. However, the tests I made tell me the
1906 * coordinates should be in screen coordinates.
1908 mousePosParam.x = mousePos.x;
1909 mousePosParam.y = mousePos.y;
1911 IDropTarget_DragOver(trackerInfo->curDragTarget,
1912 keyState,
1913 mousePosParam,
1914 trackerInfo->pdwEffect);
1916 else
1918 DropTargetNode* newDropTargetNode = 0;
1921 * If we changed window, we have to notify our old target and check for
1922 * the new one.
1924 if (trackerInfo->curDragTarget!=0)
1926 IDropTarget_DragLeave(trackerInfo->curDragTarget);
1930 * Make sure we're hovering over a window.
1932 if (hwndNewTarget!=0)
1935 * Find-out if there is a drag target under the mouse
1937 HWND nexttar = hwndNewTarget;
1938 trackerInfo->curTargetHWND = hwndNewTarget;
1940 do {
1941 newDropTargetNode = OLEDD_FindDropTarget(nexttar);
1942 } while (!newDropTargetNode && (nexttar = GetParent(nexttar)) != 0);
1943 if(nexttar) hwndNewTarget = nexttar;
1945 trackerInfo->curDragTargetHWND = hwndNewTarget;
1946 trackerInfo->curDragTarget = newDropTargetNode ? newDropTargetNode->dropTarget : 0;
1949 * If there is, notify it that we just dragged-in
1951 if (trackerInfo->curDragTarget!=0)
1953 POINTL mousePosParam;
1956 * The documentation tells me that the coordinate should be in the target
1957 * window's coordinate space. However, the tests I made tell me the
1958 * coordinates should be in screen coordinates.
1960 mousePosParam.x = mousePos.x;
1961 mousePosParam.y = mousePos.y;
1963 IDropTarget_DragEnter(trackerInfo->curDragTarget,
1964 trackerInfo->dataObject,
1965 keyState,
1966 mousePosParam,
1967 trackerInfo->pdwEffect);
1970 else
1973 * The mouse is not over a window so we don't track anything.
1975 trackerInfo->curDragTargetHWND = 0;
1976 trackerInfo->curTargetHWND = 0;
1977 trackerInfo->curDragTarget = 0;
1982 * Now that we have done that, we have to tell the source to give
1983 * us feedback on the work being done by the target. If we don't
1984 * have a target, simulate no effect.
1986 if (trackerInfo->curDragTarget==0)
1988 *trackerInfo->pdwEffect = DROPEFFECT_NONE;
1991 hr = IDropSource_GiveFeedback(trackerInfo->dropSource,
1992 *trackerInfo->pdwEffect);
1995 * When we ask for feedback from the drop source, sometimes it will
1996 * do all the necessary work and sometimes it will not handle it
1997 * when that's the case, we must display the standard drag and drop
1998 * cursors.
2000 if (hr==DRAGDROP_S_USEDEFAULTCURSORS)
2002 if (*trackerInfo->pdwEffect & DROPEFFECT_MOVE)
2004 SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(1)));
2006 else if (*trackerInfo->pdwEffect & DROPEFFECT_COPY)
2008 SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(2)));
2010 else if (*trackerInfo->pdwEffect & DROPEFFECT_LINK)
2012 SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(3)));
2014 else
2016 SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(0)));
2021 /***
2022 * OLEDD_TrackStateChange()
2024 * This method is invoked while a drag and drop operation is in effect.
2025 * It is used to notify the drop target/drop source callbacks when
2026 * the state of the keyboard or mouse button change.
2028 * params:
2029 * trackerInfo - Pointer to the structure identifying the
2030 * drag & drop operation that is currently
2031 * active.
2032 * mousePos - Current position of the mouse in screen
2033 * coordinates.
2034 * keyState - Contains the state of the shift keys and the
2035 * mouse buttons (MK_LBUTTON and the like)
2037 static void OLEDD_TrackStateChange(
2038 TrackerWindowInfo* trackerInfo,
2039 POINT mousePos,
2040 DWORD keyState)
2043 * Ask the drop source what to do with the operation.
2045 trackerInfo->returnValue = IDropSource_QueryContinueDrag(
2046 trackerInfo->dropSource,
2047 trackerInfo->escPressed,
2048 keyState);
2051 * All the return valued will stop the operation except the S_OK
2052 * return value.
2054 if (trackerInfo->returnValue!=S_OK)
2057 * Make sure the message loop in DoDragDrop stops
2059 trackerInfo->trackingDone = TRUE;
2062 * Release the mouse in case the drop target decides to show a popup
2063 * or a menu or something.
2065 ReleaseCapture();
2068 * If we end-up over a target, drop the object in the target or
2069 * inform the target that the operation was cancelled.
2071 if (trackerInfo->curDragTarget!=0)
2073 switch (trackerInfo->returnValue)
2076 * If the source wants us to complete the operation, we tell
2077 * the drop target that we just dropped the object in it.
2079 case DRAGDROP_S_DROP:
2081 POINTL mousePosParam;
2084 * The documentation tells me that the coordinate should be
2085 * in the target window's coordinate space. However, the tests
2086 * I made tell me the coordinates should be in screen coordinates.
2088 mousePosParam.x = mousePos.x;
2089 mousePosParam.y = mousePos.y;
2091 IDropTarget_Drop(trackerInfo->curDragTarget,
2092 trackerInfo->dataObject,
2093 keyState,
2094 mousePosParam,
2095 trackerInfo->pdwEffect);
2096 break;
2099 * If the source told us that we should cancel, fool the drop
2100 * target by telling it that the mouse left it's window.
2101 * Also set the drop effect to "NONE" in case the application
2102 * ignores the result of DoDragDrop.
2104 case DRAGDROP_S_CANCEL:
2105 IDropTarget_DragLeave(trackerInfo->curDragTarget);
2106 *trackerInfo->pdwEffect = DROPEFFECT_NONE;
2107 break;
2113 /***
2114 * OLEDD_GetButtonState()
2116 * This method will use the current state of the keyboard to build
2117 * a button state mask equivalent to the one passed in the
2118 * WM_MOUSEMOVE wParam.
2120 static DWORD OLEDD_GetButtonState()
2122 BYTE keyboardState[256];
2123 DWORD keyMask = 0;
2125 GetKeyboardState(keyboardState);
2127 if ( (keyboardState[VK_SHIFT] & 0x80) !=0)
2128 keyMask |= MK_SHIFT;
2130 if ( (keyboardState[VK_CONTROL] & 0x80) !=0)
2131 keyMask |= MK_CONTROL;
2133 if ( (keyboardState[VK_LBUTTON] & 0x80) !=0)
2134 keyMask |= MK_LBUTTON;
2136 if ( (keyboardState[VK_RBUTTON] & 0x80) !=0)
2137 keyMask |= MK_RBUTTON;
2139 if ( (keyboardState[VK_MBUTTON] & 0x80) !=0)
2140 keyMask |= MK_MBUTTON;
2142 return keyMask;
2145 /***
2146 * OLEDD_GetButtonState()
2148 * This method will read the default value of the registry key in
2149 * parameter and extract a DWORD value from it. The registry key value
2150 * can be in a string key or a DWORD key.
2152 * params:
2153 * regKey - Key to read the default value from
2154 * pdwValue - Pointer to the location where the DWORD
2155 * value is returned. This value is not modified
2156 * if the value is not found.
2159 static void OLEUTL_ReadRegistryDWORDValue(
2160 HKEY regKey,
2161 DWORD* pdwValue)
2163 char buffer[20];
2164 DWORD dwKeyType;
2165 DWORD cbData = 20;
2166 LONG lres;
2168 lres = RegQueryValueExA(regKey,
2170 NULL,
2171 &dwKeyType,
2172 (LPBYTE)buffer,
2173 &cbData);
2175 if (lres==ERROR_SUCCESS)
2177 switch (dwKeyType)
2179 case REG_DWORD:
2180 *pdwValue = *(DWORD*)buffer;
2181 break;
2182 case REG_EXPAND_SZ:
2183 case REG_MULTI_SZ:
2184 case REG_SZ:
2185 *pdwValue = (DWORD)strtoul(buffer, NULL, 10);
2186 break;
2191 /******************************************************************************
2192 * OleDraw (OLE32.@)
2194 * The operation of this function is documented literally in the WinAPI
2195 * documentation to involve a QueryInterface for the IViewObject interface,
2196 * followed by a call to IViewObject::Draw.
2198 HRESULT WINAPI OleDraw(
2199 IUnknown *pUnk,
2200 DWORD dwAspect,
2201 HDC hdcDraw,
2202 LPCRECT lprcBounds)
2204 HRESULT hres;
2205 IViewObject *viewobject;
2207 hres = IUnknown_QueryInterface(pUnk,
2208 &IID_IViewObject,
2209 (void**)&viewobject);
2211 if (SUCCEEDED(hres))
2213 RECTL rectl;
2215 rectl.left = lprcBounds->left;
2216 rectl.right = lprcBounds->right;
2217 rectl.top = lprcBounds->top;
2218 rectl.bottom = lprcBounds->bottom;
2219 hres = IViewObject_Draw(viewobject, dwAspect, -1, 0, 0, 0, hdcDraw, &rectl, 0, 0, 0);
2221 IViewObject_Release(viewobject);
2222 return hres;
2224 else
2226 return DV_E_NOIVIEWOBJECT;
2230 /***********************************************************************
2231 * OleTranslateAccelerator [OLE32.@]
2233 HRESULT WINAPI OleTranslateAccelerator (LPOLEINPLACEFRAME lpFrame,
2234 LPOLEINPLACEFRAMEINFO lpFrameInfo, LPMSG lpmsg)
2236 WORD wID;
2238 TRACE("(%p,%p,%p)\n", lpFrame, lpFrameInfo, lpmsg);
2240 if (IsAccelerator(lpFrameInfo->haccel,lpFrameInfo->cAccelEntries,lpmsg,&wID))
2241 return IOleInPlaceFrame_TranslateAccelerator(lpFrame,lpmsg,wID);
2243 return S_FALSE;
2246 /******************************************************************************
2247 * OleCreate [OLE32.@]
2250 HRESULT WINAPI OleCreate(
2251 REFCLSID rclsid,
2252 REFIID riid,
2253 DWORD renderopt,
2254 LPFORMATETC pFormatEtc,
2255 LPOLECLIENTSITE pClientSite,
2256 LPSTORAGE pStg,
2257 LPVOID* ppvObj)
2259 HRESULT hres, hres1;
2260 IUnknown * pUnk = NULL;
2262 FIXME("\n\t%s\n\t%s semi-stub!\n", debugstr_guid(rclsid), debugstr_guid(riid));
2264 if (SUCCEEDED((hres = CoCreateInstance(rclsid, 0, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER|CLSCTX_LOCAL_SERVER , riid, (LPVOID*)&pUnk))))
2266 if (pClientSite)
2268 IOleObject * pOE;
2269 IPersistStorage * pPS;
2270 if (SUCCEEDED((hres = IUnknown_QueryInterface( pUnk, &IID_IOleObject, (LPVOID*)&pOE))))
2272 TRACE("trying to set clientsite %p\n", pClientSite);
2273 hres1 = IOleObject_SetClientSite(pOE, pClientSite);
2274 TRACE("-- result 0x%08lx\n", hres1);
2275 IOleObject_Release(pOE);
2277 if (SUCCEEDED((hres = IUnknown_QueryInterface( pUnk, &IID_IPersistStorage, (LPVOID*)&pPS))))
2279 TRACE("trying to set stg %p\n", pStg);
2280 hres1 = IPersistStorage_InitNew(pPS, pStg);
2281 TRACE("-- result 0x%08lx\n", hres1);
2282 IPersistStorage_Release(pPS);
2287 *ppvObj = pUnk;
2289 TRACE("-- %p \n", pUnk);
2290 return hres;
2293 /******************************************************************************
2294 * OleSetAutoConvert [OLE32.@]
2296 HRESULT WINAPI OleSetAutoConvert(REFCLSID clsidOld, REFCLSID clsidNew)
2298 static const WCHAR wszAutoConvertTo[] = {'A','u','t','o','C','o','n','v','e','r','t','T','o',0};
2299 HKEY hkey = NULL;
2300 WCHAR szClsidNew[CHARS_IN_GUID];
2301 HRESULT res = S_OK;
2303 TRACE("(%s,%s)\n", debugstr_guid(clsidOld), debugstr_guid(clsidNew));
2305 if (COM_OpenKeyForCLSID(clsidOld, KEY_READ | KEY_WRITE, &hkey))
2307 res = REGDB_E_CLASSNOTREG;
2308 goto done;
2310 StringFromGUID2(clsidNew, szClsidNew, CHARS_IN_GUID);
2311 if (RegSetValueW(hkey, wszAutoConvertTo, REG_SZ, szClsidNew, (strlenW(szClsidNew)+1) * sizeof(WCHAR)))
2313 res = REGDB_E_WRITEREGDB;
2314 goto done;
2317 done:
2318 if (hkey) RegCloseKey(hkey);
2319 return res;
2322 /******************************************************************************
2323 * OleDoAutoConvert [OLE32.@]
2325 HRESULT WINAPI OleDoAutoConvert(LPSTORAGE pStg, LPCLSID pClsidNew)
2327 FIXME("(%p,%p) : stub\n",pStg,pClsidNew);
2328 return E_NOTIMPL;
2331 /******************************************************************************
2332 * OleDoAutoConvert [OLE2.79]
2334 HRESULT WINAPI OleDoAutoConvert16(LPSTORAGE pStg, LPCLSID pClsidNew)
2336 FIXME("(%p,%p) : stub\n",pStg,pClsidNew);
2337 return E_NOTIMPL;
2340 /***********************************************************************
2341 * OLE_FreeClipDataArray [internal]
2343 * NOTES:
2344 * frees the data associated with an array of CLIPDATAs
2346 static void OLE_FreeClipDataArray(ULONG count, CLIPDATA * pClipDataArray)
2348 ULONG i;
2349 for (i = 0; i < count; i++)
2350 if (pClipDataArray[i].pClipData)
2351 CoTaskMemFree(pClipDataArray[i].pClipData);
2354 /***********************************************************************
2355 * PropSysAllocString [OLE32.@]
2356 * NOTES:
2357 * Basically a copy of SysAllocStringLen.
2359 BSTR WINAPI PropSysAllocString(LPCOLESTR str)
2361 DWORD bufferSize;
2362 DWORD* newBuffer;
2363 WCHAR* stringBuffer;
2364 int len;
2366 if (!str) return 0;
2368 len = lstrlenW(str);
2370 * Find the length of the buffer passed-in in bytes.
2372 bufferSize = len * sizeof (WCHAR);
2375 * Allocate a new buffer to hold the string.
2376 * don't forget to keep an empty spot at the beginning of the
2377 * buffer for the character count and an extra character at the
2378 * end for the NULL.
2380 newBuffer = HeapAlloc(GetProcessHeap(), 0,
2381 bufferSize + sizeof(WCHAR) + sizeof(DWORD));
2384 * If the memory allocation failed, return a null pointer.
2386 if (newBuffer==0)
2387 return 0;
2390 * Copy the length of the string in the placeholder.
2392 *newBuffer = bufferSize;
2395 * Skip the byte count.
2397 newBuffer++;
2400 * Copy the information in the buffer.
2401 * Since it is valid to pass a NULL pointer here, we'll initialize the
2402 * buffer to nul if it is the case.
2404 if (str != 0)
2405 memcpy(newBuffer, str, bufferSize);
2406 else
2407 memset(newBuffer, 0, bufferSize);
2410 * Make sure that there is a nul character at the end of the
2411 * string.
2413 stringBuffer = (WCHAR*)newBuffer;
2414 stringBuffer[len] = L'\0';
2416 return (LPWSTR)stringBuffer;
2419 /***********************************************************************
2420 * PropSysFreeString [OLE32.@]
2421 * NOTES
2422 * Copy of SysFreeString.
2424 void WINAPI PropSysFreeString(LPOLESTR str)
2426 DWORD* bufferPointer;
2428 /* NULL is a valid parameter */
2429 if(!str) return;
2432 * We have to be careful when we free a BSTR pointer, it points to
2433 * the beginning of the string but it skips the byte count contained
2434 * before the string.
2436 bufferPointer = (DWORD*)str;
2438 bufferPointer--;
2441 * Free the memory from its "real" origin.
2443 HeapFree(GetProcessHeap(), 0, bufferPointer);
2446 /******************************************************************************
2447 * Check if a PROPVARIANT's type is valid.
2449 static inline HRESULT PROPVARIANT_ValidateType(VARTYPE vt)
2451 switch (vt)
2453 case VT_EMPTY:
2454 case VT_NULL:
2455 case VT_I2:
2456 case VT_I4:
2457 case VT_R4:
2458 case VT_R8:
2459 case VT_CY:
2460 case VT_DATE:
2461 case VT_BSTR:
2462 case VT_ERROR:
2463 case VT_BOOL:
2464 case VT_UI1:
2465 case VT_UI2:
2466 case VT_UI4:
2467 case VT_I8:
2468 case VT_UI8:
2469 case VT_LPSTR:
2470 case VT_LPWSTR:
2471 case VT_FILETIME:
2472 case VT_BLOB:
2473 case VT_STREAM:
2474 case VT_STORAGE:
2475 case VT_STREAMED_OBJECT:
2476 case VT_STORED_OBJECT:
2477 case VT_BLOB_OBJECT:
2478 case VT_CF:
2479 case VT_CLSID:
2480 case VT_I2|VT_VECTOR:
2481 case VT_I4|VT_VECTOR:
2482 case VT_R4|VT_VECTOR:
2483 case VT_R8|VT_VECTOR:
2484 case VT_CY|VT_VECTOR:
2485 case VT_DATE|VT_VECTOR:
2486 case VT_BSTR|VT_VECTOR:
2487 case VT_ERROR|VT_VECTOR:
2488 case VT_BOOL|VT_VECTOR:
2489 case VT_VARIANT|VT_VECTOR:
2490 case VT_UI1|VT_VECTOR:
2491 case VT_UI2|VT_VECTOR:
2492 case VT_UI4|VT_VECTOR:
2493 case VT_I8|VT_VECTOR:
2494 case VT_UI8|VT_VECTOR:
2495 case VT_LPSTR|VT_VECTOR:
2496 case VT_LPWSTR|VT_VECTOR:
2497 case VT_FILETIME|VT_VECTOR:
2498 case VT_CF|VT_VECTOR:
2499 case VT_CLSID|VT_VECTOR:
2500 return S_OK;
2502 WARN("Bad type %d\n", vt);
2503 return STG_E_INVALIDPARAMETER;
2506 /***********************************************************************
2507 * PropVariantClear [OLE32.@]
2509 HRESULT WINAPI PropVariantClear(PROPVARIANT * pvar) /* [in/out] */
2511 HRESULT hr;
2513 TRACE("(%p)\n", pvar);
2515 if (!pvar)
2516 return S_OK;
2518 hr = PROPVARIANT_ValidateType(pvar->vt);
2519 if (FAILED(hr))
2520 return hr;
2522 switch(pvar->vt)
2524 case VT_STREAM:
2525 case VT_STREAMED_OBJECT:
2526 case VT_STORAGE:
2527 case VT_STORED_OBJECT:
2528 if (pvar->u.pStream)
2529 IUnknown_Release(pvar->u.pStream);
2530 break;
2531 case VT_CLSID:
2532 case VT_LPSTR:
2533 case VT_LPWSTR:
2534 /* pick an arbitary typed pointer - we don't care about the type
2535 * as we are just freeing it */
2536 CoTaskMemFree(pvar->u.puuid);
2537 break;
2538 case VT_BLOB:
2539 case VT_BLOB_OBJECT:
2540 CoTaskMemFree(pvar->u.blob.pBlobData);
2541 break;
2542 case VT_BSTR:
2543 if (pvar->u.bstrVal)
2544 PropSysFreeString(pvar->u.bstrVal);
2545 break;
2546 case VT_CF:
2547 if (pvar->u.pclipdata)
2549 OLE_FreeClipDataArray(1, pvar->u.pclipdata);
2550 CoTaskMemFree(pvar->u.pclipdata);
2552 break;
2553 default:
2554 if (pvar->vt & VT_VECTOR)
2556 ULONG i;
2558 switch (pvar->vt & ~VT_VECTOR)
2560 case VT_VARIANT:
2561 FreePropVariantArray(pvar->u.capropvar.cElems, pvar->u.capropvar.pElems);
2562 break;
2563 case VT_CF:
2564 OLE_FreeClipDataArray(pvar->u.caclipdata.cElems, pvar->u.caclipdata.pElems);
2565 break;
2566 case VT_BSTR:
2567 for (i = 0; i < pvar->u.cabstr.cElems; i++)
2568 PropSysFreeString(pvar->u.cabstr.pElems[i]);
2569 break;
2570 case VT_LPSTR:
2571 for (i = 0; i < pvar->u.calpstr.cElems; i++)
2572 CoTaskMemFree(pvar->u.calpstr.pElems[i]);
2573 break;
2574 case VT_LPWSTR:
2575 for (i = 0; i < pvar->u.calpwstr.cElems; i++)
2576 CoTaskMemFree(pvar->u.calpwstr.pElems[i]);
2577 break;
2579 if (pvar->vt & ~VT_VECTOR)
2581 /* pick an arbitary VT_VECTOR structure - they all have the same
2582 * memory layout */
2583 CoTaskMemFree(pvar->u.capropvar.pElems);
2586 else
2587 WARN("Invalid/unsupported type %d\n", pvar->vt);
2590 ZeroMemory(pvar, sizeof(*pvar));
2592 return S_OK;
2595 /***********************************************************************
2596 * PropVariantCopy [OLE32.@]
2598 HRESULT WINAPI PropVariantCopy(PROPVARIANT *pvarDest, /* [out] */
2599 const PROPVARIANT *pvarSrc) /* [in] */
2601 ULONG len;
2602 HRESULT hr;
2604 TRACE("(%p, %p)\n", pvarDest, pvarSrc);
2606 hr = PROPVARIANT_ValidateType(pvarSrc->vt);
2607 if (FAILED(hr))
2608 return hr;
2610 /* this will deal with most cases */
2611 CopyMemory(pvarDest, pvarSrc, sizeof(*pvarDest));
2613 switch(pvarSrc->vt)
2615 case VT_STREAM:
2616 case VT_STREAMED_OBJECT:
2617 case VT_STORAGE:
2618 case VT_STORED_OBJECT:
2619 IUnknown_AddRef((LPUNKNOWN)pvarDest->u.pStream);
2620 break;
2621 case VT_CLSID:
2622 pvarDest->u.puuid = CoTaskMemAlloc(sizeof(CLSID));
2623 CopyMemory(pvarDest->u.puuid, pvarSrc->u.puuid, sizeof(CLSID));
2624 break;
2625 case VT_LPSTR:
2626 len = strlen(pvarSrc->u.pszVal);
2627 pvarDest->u.pszVal = CoTaskMemAlloc((len+1)*sizeof(CHAR));
2628 CopyMemory(pvarDest->u.pszVal, pvarSrc->u.pszVal, (len+1)*sizeof(CHAR));
2629 break;
2630 case VT_LPWSTR:
2631 len = lstrlenW(pvarSrc->u.pwszVal);
2632 pvarDest->u.pwszVal = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
2633 CopyMemory(pvarDest->u.pwszVal, pvarSrc->u.pwszVal, (len+1)*sizeof(WCHAR));
2634 break;
2635 case VT_BLOB:
2636 case VT_BLOB_OBJECT:
2637 if (pvarSrc->u.blob.pBlobData)
2639 len = pvarSrc->u.blob.cbSize;
2640 pvarDest->u.blob.pBlobData = CoTaskMemAlloc(len);
2641 CopyMemory(pvarDest->u.blob.pBlobData, pvarSrc->u.blob.pBlobData, len);
2643 break;
2644 case VT_BSTR:
2645 pvarDest->u.bstrVal = PropSysAllocString(pvarSrc->u.bstrVal);
2646 break;
2647 case VT_CF:
2648 if (pvarSrc->u.pclipdata)
2650 len = pvarSrc->u.pclipdata->cbSize - sizeof(pvarSrc->u.pclipdata->ulClipFmt);
2651 CoTaskMemAlloc(len);
2652 CopyMemory(pvarDest->u.pclipdata->pClipData, pvarSrc->u.pclipdata->pClipData, len);
2654 break;
2655 default:
2656 if (pvarSrc->vt & VT_VECTOR)
2658 int elemSize;
2659 ULONG i;
2661 switch(pvarSrc->vt & ~VT_VECTOR)
2663 case VT_I1: elemSize = sizeof(pvarSrc->u.cVal); break;
2664 case VT_UI1: elemSize = sizeof(pvarSrc->u.bVal); break;
2665 case VT_I2: elemSize = sizeof(pvarSrc->u.iVal); break;
2666 case VT_UI2: elemSize = sizeof(pvarSrc->u.uiVal); break;
2667 case VT_BOOL: elemSize = sizeof(pvarSrc->u.boolVal); break;
2668 case VT_I4: elemSize = sizeof(pvarSrc->u.lVal); break;
2669 case VT_UI4: elemSize = sizeof(pvarSrc->u.ulVal); break;
2670 case VT_R4: elemSize = sizeof(pvarSrc->u.fltVal); break;
2671 case VT_R8: elemSize = sizeof(pvarSrc->u.dblVal); break;
2672 case VT_ERROR: elemSize = sizeof(pvarSrc->u.scode); break;
2673 case VT_I8: elemSize = sizeof(pvarSrc->u.hVal); break;
2674 case VT_UI8: elemSize = sizeof(pvarSrc->u.uhVal); break;
2675 case VT_CY: elemSize = sizeof(pvarSrc->u.cyVal); break;
2676 case VT_DATE: elemSize = sizeof(pvarSrc->u.date); break;
2677 case VT_FILETIME: elemSize = sizeof(pvarSrc->u.filetime); break;
2678 case VT_CLSID: elemSize = sizeof(*pvarSrc->u.puuid); break;
2679 case VT_CF: elemSize = sizeof(*pvarSrc->u.pclipdata); break;
2680 case VT_BSTR: elemSize = sizeof(*pvarSrc->u.bstrVal); break;
2681 case VT_LPSTR: elemSize = sizeof(*pvarSrc->u.pszVal); break;
2682 case VT_LPWSTR: elemSize = sizeof(*pvarSrc->u.pwszVal); break;
2684 case VT_VARIANT:
2685 default:
2686 FIXME("Invalid element type: %ul\n", pvarSrc->vt & ~VT_VECTOR);
2687 return E_INVALIDARG;
2689 len = pvarSrc->u.capropvar.cElems;
2690 pvarDest->u.capropvar.pElems = CoTaskMemAlloc(len * elemSize);
2691 if (pvarSrc->vt == (VT_VECTOR | VT_VARIANT))
2693 for (i = 0; i < len; i++)
2694 PropVariantCopy(&pvarDest->u.capropvar.pElems[i], &pvarSrc->u.capropvar.pElems[i]);
2696 else if (pvarSrc->vt == (VT_VECTOR | VT_CF))
2698 FIXME("Copy clipformats\n");
2700 else if (pvarSrc->vt == (VT_VECTOR | VT_BSTR))
2702 for (i = 0; i < len; i++)
2703 pvarDest->u.cabstr.pElems[i] = PropSysAllocString(pvarSrc->u.cabstr.pElems[i]);
2705 else if (pvarSrc->vt == (VT_VECTOR | VT_LPSTR))
2707 size_t strLen;
2708 for (i = 0; i < len; i++)
2710 strLen = lstrlenA(pvarSrc->u.calpstr.pElems[i]) + 1;
2711 pvarDest->u.calpstr.pElems[i] = CoTaskMemAlloc(strLen);
2712 memcpy(pvarDest->u.calpstr.pElems[i],
2713 pvarSrc->u.calpstr.pElems[i], strLen);
2716 else if (pvarSrc->vt == (VT_VECTOR | VT_LPWSTR))
2718 size_t strLen;
2719 for (i = 0; i < len; i++)
2721 strLen = (lstrlenW(pvarSrc->u.calpwstr.pElems[i]) + 1) *
2722 sizeof(WCHAR);
2723 pvarDest->u.calpstr.pElems[i] = CoTaskMemAlloc(strLen);
2724 memcpy(pvarDest->u.calpstr.pElems[i],
2725 pvarSrc->u.calpstr.pElems[i], strLen);
2728 else
2729 CopyMemory(pvarDest->u.capropvar.pElems, pvarSrc->u.capropvar.pElems, len * elemSize);
2731 else
2732 WARN("Invalid/unsupported type %d\n", pvarSrc->vt);
2735 return S_OK;
2738 /***********************************************************************
2739 * FreePropVariantArray [OLE32.@]
2741 HRESULT WINAPI FreePropVariantArray(ULONG cVariants, /* [in] */
2742 PROPVARIANT *rgvars) /* [in/out] */
2744 ULONG i;
2746 TRACE("(%lu, %p)\n", cVariants, rgvars);
2748 for(i = 0; i < cVariants; i++)
2749 PropVariantClear(&rgvars[i]);
2751 return S_OK;
2754 /******************************************************************************
2755 * DllDebugObjectRPCHook (OLE32.@)
2756 * turns on and off internal debugging, pointer is only used on macintosh
2759 BOOL WINAPI DllDebugObjectRPCHook(BOOL b, void *dummy)
2761 FIXME("stub\n");
2762 return TRUE;