Changes in crossover-wine-src-6.1.0 except for configure
[wine/hacks.git] / dlls / ole32 / ole2.c
blobd9f782ec40051e0da642539ac03272023986a4be
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 "compobj_private.h"
51 #include "wine/list.h"
53 #include "wine/debug.h"
55 WINE_DEFAULT_DEBUG_CHANNEL(ole);
56 WINE_DECLARE_DEBUG_CHANNEL(accel);
58 /******************************************************************************
59 * These are static/global variables and internal data structures that the
60 * OLE module uses to maintain it's state.
62 typedef struct tagDropTargetNode
64 HWND hwndTarget;
65 IDropTarget* dropTarget;
66 struct list entry;
67 } DropTargetNode;
69 typedef struct tagTrackerWindowInfo
71 IDataObject* dataObject;
72 IDropSource* dropSource;
73 DWORD dwOKEffect;
74 DWORD* pdwEffect;
75 BOOL trackingDone;
76 HRESULT returnValue;
78 BOOL escPressed;
79 HWND curTargetHWND; /* window the mouse is hovering over */
80 HWND curDragTargetHWND; /* might be a ancestor of curTargetHWND */
81 IDropTarget* curDragTarget;
82 POINTL curMousePos; /* current position of the mouse in screen coordinates */
83 DWORD dwKeyState; /* current state of the shift and ctrl keys and the mouse buttons */
84 } TrackerWindowInfo;
86 typedef struct tagOleMenuDescriptor /* OleMenuDescriptor */
88 HWND hwndFrame; /* The containers frame window */
89 HWND hwndActiveObject; /* The active objects window */
90 OLEMENUGROUPWIDTHS mgw; /* OLE menu group widths for the shared menu */
91 HMENU hmenuCombined; /* The combined menu */
92 BOOL bIsServerItem; /* True if the currently open popup belongs to the server */
93 } OleMenuDescriptor;
95 typedef struct tagOleMenuHookItem /* OleMenu hook item in per thread hook list */
97 DWORD tid; /* Thread Id */
98 HANDLE hHeap; /* Heap this is allocated from */
99 HHOOK GetMsg_hHook; /* message hook for WH_GETMESSAGE */
100 HHOOK CallWndProc_hHook; /* message hook for WH_CALLWNDPROC */
101 struct tagOleMenuHookItem *next;
102 } OleMenuHookItem;
104 static OleMenuHookItem *hook_list;
107 * This is the lock count on the OLE library. It is controlled by the
108 * OLEInitialize/OLEUninitialize methods.
110 static LONG OLE_moduleLockCount = 0;
113 * Name of our registered window class.
115 static const char OLEDD_DRAGTRACKERCLASS[] = "WineDragDropTracker32";
118 * This is the head of the Drop target container.
120 static struct list targetListHead = LIST_INIT(targetListHead);
122 /******************************************************************************
123 * These are the prototypes of miscelaneous utility methods
125 static void OLEUTL_ReadRegistryDWORDValue(HKEY regKey, DWORD* pdwValue);
127 /******************************************************************************
128 * These are the prototypes of the utility methods used to manage a shared menu
130 static void OLEMenu_Initialize(void);
131 static void OLEMenu_UnInitialize(void);
132 static BOOL OLEMenu_InstallHooks( DWORD tid );
133 static BOOL OLEMenu_UnInstallHooks( DWORD tid );
134 static OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid );
135 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos );
136 static BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor );
137 static LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam);
138 static LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam);
140 /******************************************************************************
141 * These are the prototypes of the OLE Clipboard initialization methods (in clipboard.c)
143 extern void OLEClipbrd_UnInitialize(void);
144 extern void OLEClipbrd_Initialize(void);
146 /******************************************************************************
147 * These are the prototypes of the utility methods used for OLE Drag n Drop
149 static void OLEDD_Initialize(void);
150 static void OLEDD_UnInitialize(void);
151 static DropTargetNode* OLEDD_FindDropTarget(
152 HWND hwndOfTarget);
153 static void OLEDD_FreeDropTarget(DropTargetNode*);
154 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(
155 HWND hwnd,
156 UINT uMsg,
157 WPARAM wParam,
158 LPARAM lParam);
159 static void OLEDD_TrackMouseMove(
160 TrackerWindowInfo* trackerInfo);
161 static void OLEDD_TrackStateChange(
162 TrackerWindowInfo* trackerInfo);
163 static DWORD OLEDD_GetButtonState(void);
166 /******************************************************************************
167 * OleBuildVersion [OLE2.1]
168 * OleBuildVersion [OLE32.@]
170 DWORD WINAPI OleBuildVersion(void)
172 TRACE("Returning version %d, build %d.\n", rmm, rup);
173 return (rmm<<16)+rup;
176 /***********************************************************************
177 * OleInitialize (OLE2.2)
178 * OleInitialize (OLE32.@)
180 HRESULT WINAPI OleInitialize(LPVOID reserved)
182 HRESULT hr;
184 TRACE("(%p)\n", reserved);
187 * The first duty of the OleInitialize is to initialize the COM libraries.
189 hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
192 * If the CoInitializeEx call failed, the OLE libraries can't be
193 * initialized.
195 if (FAILED(hr))
196 return hr;
199 * Then, it has to initialize the OLE specific modules.
200 * This includes:
201 * Clipboard
202 * Drag and Drop
203 * Object linking and Embedding
204 * In-place activation
206 if (!COM_CurrentInfo()->ole_inits++ &&
207 InterlockedIncrement(&OLE_moduleLockCount) == 1)
210 * Initialize the libraries.
212 TRACE("() - Initializing the OLE libraries\n");
215 * OLE Clipboard
217 OLEClipbrd_Initialize();
220 * Drag and Drop
222 OLEDD_Initialize();
225 * OLE shared menu
227 OLEMenu_Initialize();
230 return hr;
233 /******************************************************************************
234 * OleUninitialize [OLE2.3]
235 * OleUninitialize [OLE32.@]
237 void WINAPI OleUninitialize(void)
239 TRACE("()\n");
242 * If we hit the bottom of the lock stack, free the libraries.
244 if (!--COM_CurrentInfo()->ole_inits && !InterlockedDecrement(&OLE_moduleLockCount))
247 * Actually free the libraries.
249 TRACE("() - Freeing the last reference count\n");
252 * OLE Clipboard
254 OLEClipbrd_UnInitialize();
257 * Drag and Drop
259 OLEDD_UnInitialize();
262 * OLE shared menu
264 OLEMenu_UnInitialize();
268 * Then, uninitialize the COM libraries.
270 CoUninitialize();
273 /******************************************************************************
274 * OleInitializeWOW [OLE32.@]
276 HRESULT WINAPI OleInitializeWOW(DWORD x, DWORD y) {
277 FIXME("(0x%08x, 0x%08x),stub!\n",x, y);
278 return 0;
281 /***********************************************************************
282 * RegisterDragDrop (OLE32.@)
284 HRESULT WINAPI RegisterDragDrop(
285 HWND hwnd,
286 LPDROPTARGET pDropTarget)
288 DropTargetNode* dropTargetInfo;
290 TRACE("(%p,%p)\n", hwnd, pDropTarget);
292 if (!pDropTarget)
293 return E_INVALIDARG;
296 * First, check if the window is already registered.
298 dropTargetInfo = OLEDD_FindDropTarget(hwnd);
300 if (dropTargetInfo!=NULL)
301 return DRAGDROP_E_ALREADYREGISTERED;
304 * If it's not there, we can add it. We first create a node for it.
306 dropTargetInfo = HeapAlloc(GetProcessHeap(), 0, sizeof(DropTargetNode));
308 if (dropTargetInfo==NULL)
309 return E_OUTOFMEMORY;
311 dropTargetInfo->hwndTarget = hwnd;
314 * Don't forget that this is an interface pointer, need to nail it down since
315 * we keep a copy of it.
317 IDropTarget_AddRef(pDropTarget);
318 dropTargetInfo->dropTarget = pDropTarget;
320 list_add_tail(&targetListHead, &dropTargetInfo->entry);
322 return S_OK;
325 /***********************************************************************
326 * RevokeDragDrop (OLE32.@)
328 HRESULT WINAPI RevokeDragDrop(
329 HWND hwnd)
331 DropTargetNode* dropTargetInfo;
333 TRACE("(%p)\n", hwnd);
336 * First, check if the window is already registered.
338 dropTargetInfo = OLEDD_FindDropTarget(hwnd);
341 * If it ain't in there, it's an error.
343 if (dropTargetInfo==NULL)
344 return DRAGDROP_E_NOTREGISTERED;
346 OLEDD_FreeDropTarget(dropTargetInfo);
348 return S_OK;
351 /***********************************************************************
352 * OleRegGetUserType (OLE32.@)
354 * This implementation of OleRegGetUserType ignores the dwFormOfType
355 * parameter and always returns the full name of the object. This is
356 * not too bad since this is the case for many objects because of the
357 * way they are registered.
359 HRESULT WINAPI OleRegGetUserType(
360 REFCLSID clsid,
361 DWORD dwFormOfType,
362 LPOLESTR* pszUserType)
364 char keyName[60];
365 DWORD dwKeyType;
366 DWORD cbData;
367 HKEY clsidKey;
368 LONG hres;
369 LPSTR buffer;
370 HRESULT retVal;
372 * Initialize the out parameter.
374 *pszUserType = NULL;
377 * Build the key name we're looking for
379 sprintf( keyName, "CLSID\\{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\",
380 clsid->Data1, clsid->Data2, clsid->Data3,
381 clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
382 clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );
384 TRACE("(%s, %d, %p)\n", keyName, dwFormOfType, pszUserType);
387 * Open the class id Key
389 hres = RegOpenKeyA(HKEY_CLASSES_ROOT,
390 keyName,
391 &clsidKey);
393 if (hres != ERROR_SUCCESS)
394 return REGDB_E_CLASSNOTREG;
397 * Retrieve the size of the name string.
399 cbData = 0;
401 hres = RegQueryValueExA(clsidKey,
403 NULL,
404 &dwKeyType,
405 NULL,
406 &cbData);
408 if (hres!=ERROR_SUCCESS)
410 RegCloseKey(clsidKey);
411 return REGDB_E_READREGDB;
415 * Allocate a buffer for the registry value.
417 *pszUserType = CoTaskMemAlloc(cbData*2);
419 if (*pszUserType==NULL)
421 RegCloseKey(clsidKey);
422 return E_OUTOFMEMORY;
425 buffer = HeapAlloc(GetProcessHeap(), 0, cbData);
427 if (buffer == NULL)
429 RegCloseKey(clsidKey);
430 CoTaskMemFree(*pszUserType);
431 *pszUserType=NULL;
432 return E_OUTOFMEMORY;
435 hres = RegQueryValueExA(clsidKey,
437 NULL,
438 &dwKeyType,
439 (LPBYTE) buffer,
440 &cbData);
442 RegCloseKey(clsidKey);
445 if (hres!=ERROR_SUCCESS)
447 CoTaskMemFree(*pszUserType);
448 *pszUserType=NULL;
450 retVal = REGDB_E_READREGDB;
452 else
454 MultiByteToWideChar( CP_ACP, 0, buffer, -1, *pszUserType, cbData /*FIXME*/ );
455 retVal = S_OK;
457 HeapFree(GetProcessHeap(), 0, buffer);
459 return retVal;
462 /***********************************************************************
463 * DoDragDrop [OLE32.@]
465 HRESULT WINAPI DoDragDrop (
466 IDataObject *pDataObject, /* [in] ptr to the data obj */
467 IDropSource* pDropSource, /* [in] ptr to the source obj */
468 DWORD dwOKEffect, /* [in] effects allowed by the source */
469 DWORD *pdwEffect) /* [out] ptr to effects of the source */
471 TrackerWindowInfo trackerInfo;
472 HWND hwndTrackWindow;
473 MSG msg;
475 TRACE("(DataObject %p, DropSource %p)\n", pDataObject, pDropSource);
478 * Setup the drag n drop tracking window.
480 if (!IsValidInterface((LPUNKNOWN)pDropSource))
481 return E_INVALIDARG;
483 trackerInfo.dataObject = pDataObject;
484 trackerInfo.dropSource = pDropSource;
485 trackerInfo.dwOKEffect = dwOKEffect;
486 trackerInfo.pdwEffect = pdwEffect;
487 trackerInfo.trackingDone = FALSE;
488 trackerInfo.escPressed = FALSE;
489 trackerInfo.curDragTargetHWND = 0;
490 trackerInfo.curTargetHWND = 0;
491 trackerInfo.curDragTarget = 0;
493 hwndTrackWindow = CreateWindowA(OLEDD_DRAGTRACKERCLASS,
494 "TrackerWindow",
495 WS_POPUP,
496 CW_USEDEFAULT, CW_USEDEFAULT,
497 CW_USEDEFAULT, CW_USEDEFAULT,
501 (LPVOID)&trackerInfo);
503 if (hwndTrackWindow!=0)
506 * Capture the mouse input
508 SetCapture(hwndTrackWindow);
510 msg.message = 0;
513 * Pump messages. All mouse input should go the the capture window.
515 while (!trackerInfo.trackingDone && GetMessageA(&msg, 0, 0, 0) )
517 trackerInfo.curMousePos.x = msg.pt.x;
518 trackerInfo.curMousePos.y = msg.pt.y;
519 trackerInfo.dwKeyState = OLEDD_GetButtonState();
521 if ( (msg.message >= WM_KEYFIRST) &&
522 (msg.message <= WM_KEYLAST) )
525 * When keyboard messages are sent to windows on this thread, we
526 * want to ignore notify the drop source that the state changed.
527 * in the case of the Escape key, we also notify the drop source
528 * we give it a special meaning.
530 if ( (msg.message==WM_KEYDOWN) &&
531 (msg.wParam==VK_ESCAPE) )
533 trackerInfo.escPressed = TRUE;
537 * Notify the drop source.
539 OLEDD_TrackStateChange(&trackerInfo);
541 else
544 * Dispatch the messages only when it's not a keyboard message.
546 DispatchMessageA(&msg);
550 /* re-post the quit message to outer message loop */
551 if (msg.message == WM_QUIT)
552 PostQuitMessage(msg.wParam);
554 * Destroy the temporary window.
556 DestroyWindow(hwndTrackWindow);
558 return trackerInfo.returnValue;
561 return E_FAIL;
564 /***********************************************************************
565 * OleQueryLinkFromData [OLE32.@]
567 HRESULT WINAPI OleQueryLinkFromData(
568 IDataObject* pSrcDataObject)
570 FIXME("(%p),stub!\n", pSrcDataObject);
571 return S_OK;
574 /***********************************************************************
575 * OleRegGetMiscStatus [OLE32.@]
577 HRESULT WINAPI OleRegGetMiscStatus(
578 REFCLSID clsid,
579 DWORD dwAspect,
580 DWORD* pdwStatus)
582 char keyName[60];
583 HKEY clsidKey;
584 HKEY miscStatusKey;
585 HKEY aspectKey;
586 LONG result;
589 * Initialize the out parameter.
591 *pdwStatus = 0;
594 * Build the key name we're looking for
596 sprintf( keyName, "CLSID\\{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\",
597 clsid->Data1, clsid->Data2, clsid->Data3,
598 clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
599 clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );
601 TRACE("(%s, %d, %p)\n", keyName, dwAspect, pdwStatus);
604 * Open the class id Key
606 result = RegOpenKeyA(HKEY_CLASSES_ROOT,
607 keyName,
608 &clsidKey);
610 if (result != ERROR_SUCCESS)
611 return REGDB_E_CLASSNOTREG;
614 * Get the MiscStatus
616 result = RegOpenKeyA(clsidKey,
617 "MiscStatus",
618 &miscStatusKey);
621 if (result != ERROR_SUCCESS)
623 RegCloseKey(clsidKey);
624 return REGDB_E_READREGDB;
628 * Read the default value
630 OLEUTL_ReadRegistryDWORDValue(miscStatusKey, pdwStatus);
633 * Open the key specific to the requested aspect.
635 sprintf(keyName, "%d", dwAspect);
637 result = RegOpenKeyA(miscStatusKey,
638 keyName,
639 &aspectKey);
641 if (result == ERROR_SUCCESS)
643 OLEUTL_ReadRegistryDWORDValue(aspectKey, pdwStatus);
644 RegCloseKey(aspectKey);
648 * Cleanup
650 RegCloseKey(miscStatusKey);
651 RegCloseKey(clsidKey);
653 return S_OK;
656 static HRESULT EnumOLEVERB_Construct(HKEY hkeyVerb, ULONG index, IEnumOLEVERB **ppenum);
658 typedef struct
660 const IEnumOLEVERBVtbl *lpvtbl;
661 LONG ref;
663 HKEY hkeyVerb;
664 ULONG index;
665 } EnumOLEVERB;
667 static HRESULT WINAPI EnumOLEVERB_QueryInterface(
668 IEnumOLEVERB *iface, REFIID riid, void **ppv)
670 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
671 if (IsEqualIID(riid, &IID_IUnknown) ||
672 IsEqualIID(riid, &IID_IEnumOLEVERB))
674 IUnknown_AddRef(iface);
675 *ppv = iface;
676 return S_OK;
678 return E_NOINTERFACE;
681 static ULONG WINAPI EnumOLEVERB_AddRef(
682 IEnumOLEVERB *iface)
684 EnumOLEVERB *This = (EnumOLEVERB *)iface;
685 TRACE("()\n");
686 return InterlockedIncrement(&This->ref);
689 static ULONG WINAPI EnumOLEVERB_Release(
690 IEnumOLEVERB *iface)
692 EnumOLEVERB *This = (EnumOLEVERB *)iface;
693 LONG refs = InterlockedDecrement(&This->ref);
694 TRACE("()\n");
695 if (!refs)
697 RegCloseKey(This->hkeyVerb);
698 HeapFree(GetProcessHeap(), 0, This);
700 return refs;
703 static HRESULT WINAPI EnumOLEVERB_Next(
704 IEnumOLEVERB *iface, ULONG celt, LPOLEVERB rgelt,
705 ULONG *pceltFetched)
707 EnumOLEVERB *This = (EnumOLEVERB *)iface;
708 HRESULT hr = S_OK;
710 TRACE("(%d, %p, %p)\n", celt, rgelt, pceltFetched);
712 if (pceltFetched)
713 *pceltFetched = 0;
715 for (; celt; celt--, rgelt++)
717 WCHAR wszSubKey[20];
718 LONG cbData;
719 LPWSTR pwszOLEVERB;
720 LPWSTR pwszMenuFlags;
721 LPWSTR pwszAttribs;
722 LONG res = RegEnumKeyW(This->hkeyVerb, This->index, wszSubKey, sizeof(wszSubKey)/sizeof(wszSubKey[0]));
723 if (res == ERROR_NO_MORE_ITEMS)
725 hr = S_FALSE;
726 break;
728 else if (res != ERROR_SUCCESS)
730 ERR("RegEnumKeyW failed with error %d\n", res);
731 hr = REGDB_E_READREGDB;
732 break;
734 res = RegQueryValueW(This->hkeyVerb, wszSubKey, NULL, &cbData);
735 if (res != ERROR_SUCCESS)
737 ERR("RegQueryValueW failed with error %d\n", res);
738 hr = REGDB_E_READREGDB;
739 break;
741 pwszOLEVERB = CoTaskMemAlloc(cbData);
742 if (!pwszOLEVERB)
744 hr = E_OUTOFMEMORY;
745 break;
747 res = RegQueryValueW(This->hkeyVerb, wszSubKey, pwszOLEVERB, &cbData);
748 if (res != ERROR_SUCCESS)
750 ERR("RegQueryValueW failed with error %d\n", res);
751 hr = REGDB_E_READREGDB;
752 CoTaskMemFree(pwszOLEVERB);
753 break;
756 TRACE("verb string: %s\n", debugstr_w(pwszOLEVERB));
757 pwszMenuFlags = strchrW(pwszOLEVERB, ',');
758 if (!pwszMenuFlags)
760 hr = OLEOBJ_E_INVALIDVERB;
761 CoTaskMemFree(pwszOLEVERB);
762 break;
764 /* nul terminate the name string and advance to first character */
765 *pwszMenuFlags = '\0';
766 pwszMenuFlags++;
767 pwszAttribs = strchrW(pwszMenuFlags, ',');
768 if (!pwszAttribs)
770 hr = OLEOBJ_E_INVALIDVERB;
771 CoTaskMemFree(pwszOLEVERB);
772 break;
774 /* nul terminate the menu string and advance to first character */
775 *pwszAttribs = '\0';
776 pwszAttribs++;
778 /* fill out structure for this verb */
779 rgelt->lVerb = atolW(wszSubKey);
780 rgelt->lpszVerbName = pwszOLEVERB; /* user should free */
781 rgelt->fuFlags = atolW(pwszMenuFlags);
782 rgelt->grfAttribs = atolW(pwszAttribs);
784 if (pceltFetched)
785 (*pceltFetched)++;
786 This->index++;
788 return hr;
791 static HRESULT WINAPI EnumOLEVERB_Skip(
792 IEnumOLEVERB *iface, ULONG celt)
794 EnumOLEVERB *This = (EnumOLEVERB *)iface;
796 TRACE("(%d)\n", celt);
798 This->index += celt;
799 return S_OK;
802 static HRESULT WINAPI EnumOLEVERB_Reset(
803 IEnumOLEVERB *iface)
805 EnumOLEVERB *This = (EnumOLEVERB *)iface;
807 TRACE("()\n");
809 This->index = 0;
810 return S_OK;
813 static HRESULT WINAPI EnumOLEVERB_Clone(
814 IEnumOLEVERB *iface,
815 IEnumOLEVERB **ppenum)
817 EnumOLEVERB *This = (EnumOLEVERB *)iface;
818 HKEY hkeyVerb;
819 TRACE("(%p)\n", ppenum);
820 if (!DuplicateHandle(GetCurrentProcess(), This->hkeyVerb, GetCurrentProcess(), (HANDLE *)&hkeyVerb, 0, FALSE, DUPLICATE_SAME_ACCESS))
821 return HRESULT_FROM_WIN32(GetLastError());
822 return EnumOLEVERB_Construct(hkeyVerb, This->index, ppenum);
825 static const IEnumOLEVERBVtbl EnumOLEVERB_VTable =
827 EnumOLEVERB_QueryInterface,
828 EnumOLEVERB_AddRef,
829 EnumOLEVERB_Release,
830 EnumOLEVERB_Next,
831 EnumOLEVERB_Skip,
832 EnumOLEVERB_Reset,
833 EnumOLEVERB_Clone
836 static HRESULT EnumOLEVERB_Construct(HKEY hkeyVerb, ULONG index, IEnumOLEVERB **ppenum)
838 EnumOLEVERB *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
839 if (!This)
841 RegCloseKey(hkeyVerb);
842 return E_OUTOFMEMORY;
844 This->lpvtbl = &EnumOLEVERB_VTable;
845 This->ref = 1;
846 This->index = index;
847 This->hkeyVerb = hkeyVerb;
848 *ppenum = (IEnumOLEVERB *)&This->lpvtbl;
849 return S_OK;
852 /***********************************************************************
853 * OleRegEnumVerbs [OLE32.@]
855 * Enumerates verbs associated with a class stored in the registry.
857 * PARAMS
858 * clsid [I] Class ID to enumerate the verbs for.
859 * ppenum [O] Enumerator.
861 * RETURNS
862 * S_OK: Success.
863 * REGDB_E_CLASSNOTREG: The specified class does not have a key in the registry.
864 * REGDB_E_READREGDB: The class key could not be opened for some other reason.
865 * OLE_E_REGDB_KEY: The Verb subkey for the class is not present.
866 * OLEOBJ_E_NOVERBS: The Verb subkey for the class is empty.
868 HRESULT WINAPI OleRegEnumVerbs (REFCLSID clsid, LPENUMOLEVERB* ppenum)
870 LONG res;
871 HKEY hkeyVerb;
872 DWORD dwSubKeys;
873 static const WCHAR wszVerb[] = {'V','e','r','b',0};
875 TRACE("(%s, %p)\n", debugstr_guid(clsid), ppenum);
877 res = COM_OpenKeyForCLSID(clsid, wszVerb, KEY_READ, &hkeyVerb);
878 if (FAILED(res))
880 if (res == REGDB_E_CLASSNOTREG)
881 ERR("CLSID %s not registered\n", debugstr_guid(clsid));
882 else if (res == REGDB_E_KEYMISSING)
883 ERR("no Verbs key for class %s\n", debugstr_guid(clsid));
884 else
885 ERR("failed to open Verbs key for CLSID %s with error %d\n",
886 debugstr_guid(clsid), res);
887 return res;
890 res = RegQueryInfoKeyW(hkeyVerb, NULL, NULL, NULL, &dwSubKeys, NULL,
891 NULL, NULL, NULL, NULL, NULL, NULL);
892 if (res != ERROR_SUCCESS)
894 ERR("failed to get subkey count with error %d\n", GetLastError());
895 return REGDB_E_READREGDB;
898 if (!dwSubKeys)
900 WARN("class %s has no verbs\n", debugstr_guid(clsid));
901 RegCloseKey(hkeyVerb);
902 return OLEOBJ_E_NOVERBS;
905 return EnumOLEVERB_Construct(hkeyVerb, 0, ppenum);
908 /******************************************************************************
909 * OleSetContainedObject [OLE32.@]
911 HRESULT WINAPI OleSetContainedObject(
912 LPUNKNOWN pUnknown,
913 BOOL fContained)
915 IRunnableObject* runnable = NULL;
916 HRESULT hres;
918 TRACE("(%p,%x)\n", pUnknown, fContained);
920 hres = IUnknown_QueryInterface(pUnknown,
921 &IID_IRunnableObject,
922 (void**)&runnable);
924 if (SUCCEEDED(hres))
926 hres = IRunnableObject_SetContainedObject(runnable, fContained);
928 IRunnableObject_Release(runnable);
930 return hres;
933 return S_OK;
936 /******************************************************************************
937 * OleRun [OLE32.@]
939 * Set the OLE object to the running state.
941 * PARAMS
942 * pUnknown [I] OLE object to run.
944 * RETURNS
945 * Success: S_OK.
946 * Failure: Any HRESULT code.
948 HRESULT WINAPI OleRun(LPUNKNOWN pUnknown)
950 IRunnableObject *runable;
951 HRESULT hres;
953 TRACE("(%p)\n", pUnknown);
955 hres = IUnknown_QueryInterface(pUnknown, &IID_IRunnableObject, (void**)&runable);
956 if (FAILED(hres))
957 return S_OK; /* Appears to return no error. */
959 hres = IRunnableObject_Run(runable, NULL);
960 IRunnableObject_Release(runable);
961 return hres;
964 /******************************************************************************
965 * OleLoad [OLE32.@]
967 HRESULT WINAPI OleLoad(
968 LPSTORAGE pStg,
969 REFIID riid,
970 LPOLECLIENTSITE pClientSite,
971 LPVOID* ppvObj)
973 IPersistStorage* persistStorage = NULL;
974 IUnknown* pUnk;
975 IOleObject* pOleObject = NULL;
976 STATSTG storageInfo;
977 HRESULT hres;
979 TRACE("(%p, %s, %p, %p)\n", pStg, debugstr_guid(riid), pClientSite, ppvObj);
981 *ppvObj = NULL;
984 * TODO, Conversion ... OleDoAutoConvert
988 * Get the class ID for the object.
990 hres = IStorage_Stat(pStg, &storageInfo, STATFLAG_NONAME);
993 * Now, try and create the handler for the object
995 hres = CoCreateInstance(&storageInfo.clsid,
996 NULL,
997 CLSCTX_INPROC_HANDLER|CLSCTX_INPROC_SERVER,
998 riid,
999 (void**)&pUnk);
1002 * If that fails, as it will most times, load the default
1003 * OLE handler.
1005 if (FAILED(hres))
1007 hres = OleCreateDefaultHandler(&storageInfo.clsid,
1008 NULL,
1009 riid,
1010 (void**)&pUnk);
1014 * If we couldn't find a handler... this is bad. Abort the whole thing.
1016 if (FAILED(hres))
1017 return hres;
1019 if (pClientSite)
1021 hres = IUnknown_QueryInterface(pUnk, &IID_IOleObject, (void **)&pOleObject);
1022 if (SUCCEEDED(hres))
1024 DWORD dwStatus;
1025 hres = IOleObject_GetMiscStatus(pOleObject, DVASPECT_CONTENT, &dwStatus);
1029 if (SUCCEEDED(hres))
1031 * Initialize the object with it's IPersistStorage interface.
1033 hres = IOleObject_QueryInterface(pUnk,
1034 &IID_IPersistStorage,
1035 (void**)&persistStorage);
1037 if (SUCCEEDED(hres))
1039 hres = IPersistStorage_Load(persistStorage, pStg);
1041 IPersistStorage_Release(persistStorage);
1042 persistStorage = NULL;
1045 if (SUCCEEDED(hres) && pClientSite)
1047 * Inform the new object of it's client site.
1049 hres = IOleObject_SetClientSite(pOleObject, pClientSite);
1052 * Cleanup interfaces used internally
1054 if (pOleObject)
1055 IOleObject_Release(pOleObject);
1057 if (SUCCEEDED(hres))
1059 IOleLink *pOleLink;
1060 HRESULT hres1;
1061 hres1 = IUnknown_QueryInterface(pUnk, &IID_IOleLink, (void **)&pOleLink);
1062 if (SUCCEEDED(hres1))
1064 FIXME("handle OLE link\n");
1065 IOleLink_Release(pOleLink);
1069 if (FAILED(hres))
1071 IUnknown_Release(pUnk);
1072 pUnk = NULL;
1075 *ppvObj = pUnk;
1077 return hres;
1080 /***********************************************************************
1081 * OleSave [OLE32.@]
1083 HRESULT WINAPI OleSave(
1084 LPPERSISTSTORAGE pPS,
1085 LPSTORAGE pStg,
1086 BOOL fSameAsLoad)
1088 HRESULT hres;
1089 CLSID objectClass;
1091 TRACE("(%p,%p,%x)\n", pPS, pStg, fSameAsLoad);
1094 * First, we transfer the class ID (if available)
1096 hres = IPersistStorage_GetClassID(pPS, &objectClass);
1098 if (SUCCEEDED(hres))
1100 WriteClassStg(pStg, &objectClass);
1104 * Then, we ask the object to save itself to the
1105 * storage. If it is successful, we commit the storage.
1107 hres = IPersistStorage_Save(pPS, pStg, fSameAsLoad);
1109 if (SUCCEEDED(hres))
1111 IStorage_Commit(pStg,
1112 STGC_DEFAULT);
1115 return hres;
1119 /******************************************************************************
1120 * OleLockRunning [OLE32.@]
1122 HRESULT WINAPI OleLockRunning(LPUNKNOWN pUnknown, BOOL fLock, BOOL fLastUnlockCloses)
1124 IRunnableObject* runnable = NULL;
1125 HRESULT hres;
1127 TRACE("(%p,%x,%x)\n", pUnknown, fLock, fLastUnlockCloses);
1129 hres = IUnknown_QueryInterface(pUnknown,
1130 &IID_IRunnableObject,
1131 (void**)&runnable);
1133 if (SUCCEEDED(hres))
1135 hres = IRunnableObject_LockRunning(runnable, fLock, fLastUnlockCloses);
1137 IRunnableObject_Release(runnable);
1139 return hres;
1141 else
1142 return E_INVALIDARG;
1146 /**************************************************************************
1147 * Internal methods to manage the shared OLE menu in response to the
1148 * OLE***MenuDescriptor API
1151 /***
1152 * OLEMenu_Initialize()
1154 * Initializes the OLEMENU data structures.
1156 static void OLEMenu_Initialize(void)
1160 /***
1161 * OLEMenu_UnInitialize()
1163 * Releases the OLEMENU data structures.
1165 static void OLEMenu_UnInitialize(void)
1169 /*************************************************************************
1170 * OLEMenu_InstallHooks
1171 * Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
1173 * RETURNS: TRUE if message hooks were successfully installed
1174 * FALSE on failure
1176 static BOOL OLEMenu_InstallHooks( DWORD tid )
1178 OleMenuHookItem *pHookItem = NULL;
1180 /* Create an entry for the hook table */
1181 if ( !(pHookItem = HeapAlloc(GetProcessHeap(), 0,
1182 sizeof(OleMenuHookItem)) ) )
1183 return FALSE;
1185 pHookItem->tid = tid;
1186 pHookItem->hHeap = GetProcessHeap();
1188 /* Install a thread scope message hook for WH_GETMESSAGE */
1189 pHookItem->GetMsg_hHook = SetWindowsHookExA( WH_GETMESSAGE, OLEMenu_GetMsgProc,
1190 0, GetCurrentThreadId() );
1191 if ( !pHookItem->GetMsg_hHook )
1192 goto CLEANUP;
1194 /* Install a thread scope message hook for WH_CALLWNDPROC */
1195 pHookItem->CallWndProc_hHook = SetWindowsHookExA( WH_CALLWNDPROC, OLEMenu_CallWndProc,
1196 0, GetCurrentThreadId() );
1197 if ( !pHookItem->CallWndProc_hHook )
1198 goto CLEANUP;
1200 /* Insert the hook table entry */
1201 pHookItem->next = hook_list;
1202 hook_list = pHookItem;
1204 return TRUE;
1206 CLEANUP:
1207 /* Unhook any hooks */
1208 if ( pHookItem->GetMsg_hHook )
1209 UnhookWindowsHookEx( pHookItem->GetMsg_hHook );
1210 if ( pHookItem->CallWndProc_hHook )
1211 UnhookWindowsHookEx( pHookItem->CallWndProc_hHook );
1212 /* Release the hook table entry */
1213 HeapFree(pHookItem->hHeap, 0, pHookItem );
1215 return FALSE;
1218 /*************************************************************************
1219 * OLEMenu_UnInstallHooks
1220 * UnInstall thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
1222 * RETURNS: TRUE if message hooks were successfully installed
1223 * FALSE on failure
1225 static BOOL OLEMenu_UnInstallHooks( DWORD tid )
1227 OleMenuHookItem *pHookItem = NULL;
1228 OleMenuHookItem **ppHook = &hook_list;
1230 while (*ppHook)
1232 if ((*ppHook)->tid == tid)
1234 pHookItem = *ppHook;
1235 *ppHook = pHookItem->next;
1236 break;
1238 ppHook = &(*ppHook)->next;
1240 if (!pHookItem) return FALSE;
1242 /* Uninstall the hooks installed for this thread */
1243 if ( !UnhookWindowsHookEx( pHookItem->GetMsg_hHook ) )
1244 goto CLEANUP;
1245 if ( !UnhookWindowsHookEx( pHookItem->CallWndProc_hHook ) )
1246 goto CLEANUP;
1248 /* Release the hook table entry */
1249 HeapFree(pHookItem->hHeap, 0, pHookItem );
1251 return TRUE;
1253 CLEANUP:
1254 /* Release the hook table entry */
1255 HeapFree(pHookItem->hHeap, 0, pHookItem );
1257 return FALSE;
1260 /*************************************************************************
1261 * OLEMenu_IsHookInstalled
1262 * Tests if OLEMenu hooks have been installed for a thread
1264 * RETURNS: The pointer and index of the hook table entry for the tid
1265 * NULL and -1 for the index if no hooks were installed for this thread
1267 static OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid )
1269 OleMenuHookItem *pHookItem = NULL;
1271 /* Do a simple linear search for an entry whose tid matches ours.
1272 * We really need a map but efficiency is not a concern here. */
1273 for (pHookItem = hook_list; pHookItem; pHookItem = pHookItem->next)
1275 if ( tid == pHookItem->tid )
1276 return pHookItem;
1279 return NULL;
1282 /***********************************************************************
1283 * OLEMenu_FindMainMenuIndex
1285 * Used by OLEMenu API to find the top level group a menu item belongs to.
1286 * On success pnPos contains the index of the item in the top level menu group
1288 * RETURNS: TRUE if the ID was found, FALSE on failure
1290 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos )
1292 UINT i, nItems;
1294 nItems = GetMenuItemCount( hMainMenu );
1296 for (i = 0; i < nItems; i++)
1298 HMENU hsubmenu;
1300 /* Is the current item a submenu? */
1301 if ( (hsubmenu = GetSubMenu(hMainMenu, i)) )
1303 /* If the handle is the same we're done */
1304 if ( hsubmenu == hPopupMenu )
1306 if (pnPos)
1307 *pnPos = i;
1308 return TRUE;
1310 /* Recursively search without updating pnPos */
1311 else if ( OLEMenu_FindMainMenuIndex( hsubmenu, hPopupMenu, NULL ) )
1313 if (pnPos)
1314 *pnPos = i;
1315 return TRUE;
1320 return FALSE;
1323 /***********************************************************************
1324 * OLEMenu_SetIsServerMenu
1326 * Checks whether a popup menu belongs to a shared menu group which is
1327 * owned by the server, and sets the menu descriptor state accordingly.
1328 * All menu messages from these groups should be routed to the server.
1330 * RETURNS: TRUE if the popup menu is part of a server owned group
1331 * FALSE if the popup menu is part of a container owned group
1333 static BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor )
1335 UINT nPos = 0, nWidth, i;
1337 pOleMenuDescriptor->bIsServerItem = FALSE;
1339 /* Don't bother searching if the popup is the combined menu itself */
1340 if ( hmenu == pOleMenuDescriptor->hmenuCombined )
1341 return FALSE;
1343 /* Find the menu item index in the shared OLE menu that this item belongs to */
1344 if ( !OLEMenu_FindMainMenuIndex( pOleMenuDescriptor->hmenuCombined, hmenu, &nPos ) )
1345 return FALSE;
1347 /* The group widths array has counts for the number of elements
1348 * in the groups File, Edit, Container, Object, Window, Help.
1349 * The Edit, Object & Help groups belong to the server object
1350 * and the other three belong to the container.
1351 * Loop through the group widths and locate the group we are a member of.
1353 for ( i = 0, nWidth = 0; i < 6; i++ )
1355 nWidth += pOleMenuDescriptor->mgw.width[i];
1356 if ( nPos < nWidth )
1358 /* Odd elements are server menu widths */
1359 pOleMenuDescriptor->bIsServerItem = (i%2) ? TRUE : FALSE;
1360 break;
1364 return pOleMenuDescriptor->bIsServerItem;
1367 /*************************************************************************
1368 * OLEMenu_CallWndProc
1369 * Thread scope WH_CALLWNDPROC hook proc filter function (callback)
1370 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1372 static LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam)
1374 LPCWPSTRUCT pMsg = NULL;
1375 HOLEMENU hOleMenu = 0;
1376 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1377 OleMenuHookItem *pHookItem = NULL;
1378 WORD fuFlags;
1380 TRACE("%i, %04x, %08x\n", code, wParam, (unsigned)lParam );
1382 /* Check if we're being asked to process the message */
1383 if ( HC_ACTION != code )
1384 goto NEXTHOOK;
1386 /* Retrieve the current message being dispatched from lParam */
1387 pMsg = (LPCWPSTRUCT)lParam;
1389 /* Check if the message is destined for a window we are interested in:
1390 * If the window has an OLEMenu property we may need to dispatch
1391 * the menu message to its active objects window instead. */
1393 hOleMenu = (HOLEMENU)GetPropA( pMsg->hwnd, "PROP_OLEMenuDescriptor" );
1394 if ( !hOleMenu )
1395 goto NEXTHOOK;
1397 /* Get the menu descriptor */
1398 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1399 if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1400 goto NEXTHOOK;
1402 /* Process menu messages */
1403 switch( pMsg->message )
1405 case WM_INITMENU:
1407 /* Reset the menu descriptor state */
1408 pOleMenuDescriptor->bIsServerItem = FALSE;
1410 /* Send this message to the server as well */
1411 SendMessageA( pOleMenuDescriptor->hwndActiveObject,
1412 pMsg->message, pMsg->wParam, pMsg->lParam );
1413 goto NEXTHOOK;
1416 case WM_INITMENUPOPUP:
1418 /* Save the state for whether this is a server owned menu */
1419 OLEMenu_SetIsServerMenu( (HMENU)pMsg->wParam, pOleMenuDescriptor );
1420 break;
1423 case WM_MENUSELECT:
1425 fuFlags = HIWORD(pMsg->wParam); /* Get flags */
1426 if ( fuFlags & MF_SYSMENU )
1427 goto NEXTHOOK;
1429 /* Save the state for whether this is a server owned popup menu */
1430 else if ( fuFlags & MF_POPUP )
1431 OLEMenu_SetIsServerMenu( (HMENU)pMsg->lParam, pOleMenuDescriptor );
1433 break;
1436 case WM_DRAWITEM:
1438 LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT) pMsg->lParam;
1439 if ( pMsg->wParam != 0 || lpdis->CtlType != ODT_MENU )
1440 goto NEXTHOOK; /* Not a menu message */
1442 break;
1445 default:
1446 goto NEXTHOOK;
1449 /* If the message was for the server dispatch it accordingly */
1450 if ( pOleMenuDescriptor->bIsServerItem )
1452 SendMessageA( pOleMenuDescriptor->hwndActiveObject,
1453 pMsg->message, pMsg->wParam, pMsg->lParam );
1456 NEXTHOOK:
1457 if ( pOleMenuDescriptor )
1458 GlobalUnlock( hOleMenu );
1460 /* Lookup the hook item for the current thread */
1461 if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1463 /* This should never fail!! */
1464 WARN("could not retrieve hHook for current thread!\n" );
1465 return 0;
1468 /* Pass on the message to the next hooker */
1469 return CallNextHookEx( pHookItem->CallWndProc_hHook, code, wParam, lParam );
1472 /*************************************************************************
1473 * OLEMenu_GetMsgProc
1474 * Thread scope WH_GETMESSAGE hook proc filter function (callback)
1475 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1477 static LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam)
1479 LPMSG pMsg = NULL;
1480 HOLEMENU hOleMenu = 0;
1481 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1482 OleMenuHookItem *pHookItem = NULL;
1483 WORD wCode;
1485 TRACE("%i, %04x, %08x\n", code, wParam, (unsigned)lParam );
1487 /* Check if we're being asked to process a messages */
1488 if ( HC_ACTION != code )
1489 goto NEXTHOOK;
1491 /* Retrieve the current message being dispatched from lParam */
1492 pMsg = (LPMSG)lParam;
1494 /* Check if the message is destined for a window we are interested in:
1495 * If the window has an OLEMenu property we may need to dispatch
1496 * the menu message to its active objects window instead. */
1498 hOleMenu = (HOLEMENU)GetPropA( pMsg->hwnd, "PROP_OLEMenuDescriptor" );
1499 if ( !hOleMenu )
1500 goto NEXTHOOK;
1502 /* Process menu messages */
1503 switch( pMsg->message )
1505 case WM_COMMAND:
1507 wCode = HIWORD(pMsg->wParam); /* Get notification code */
1508 if ( wCode )
1509 goto NEXTHOOK; /* Not a menu message */
1510 break;
1512 default:
1513 goto NEXTHOOK;
1516 /* Get the menu descriptor */
1517 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1518 if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1519 goto NEXTHOOK;
1521 /* If the message was for the server dispatch it accordingly */
1522 if ( pOleMenuDescriptor->bIsServerItem )
1524 /* Change the hWnd in the message to the active objects hWnd.
1525 * The message loop which reads this message will automatically
1526 * dispatch it to the embedded objects window. */
1527 pMsg->hwnd = pOleMenuDescriptor->hwndActiveObject;
1530 NEXTHOOK:
1531 if ( pOleMenuDescriptor )
1532 GlobalUnlock( hOleMenu );
1534 /* Lookup the hook item for the current thread */
1535 if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1537 /* This should never fail!! */
1538 WARN("could not retrieve hHook for current thread!\n" );
1539 return FALSE;
1542 /* Pass on the message to the next hooker */
1543 return CallNextHookEx( pHookItem->GetMsg_hHook, code, wParam, lParam );
1546 /***********************************************************************
1547 * OleCreateMenuDescriptor [OLE32.@]
1548 * Creates an OLE menu descriptor for OLE to use when dispatching
1549 * menu messages and commands.
1551 * PARAMS:
1552 * hmenuCombined - Handle to the objects combined menu
1553 * lpMenuWidths - Pointer to array of 6 LONG's indicating menus per group
1556 HOLEMENU WINAPI OleCreateMenuDescriptor(
1557 HMENU hmenuCombined,
1558 LPOLEMENUGROUPWIDTHS lpMenuWidths)
1560 HOLEMENU hOleMenu;
1561 OleMenuDescriptor *pOleMenuDescriptor;
1562 int i;
1564 if ( !hmenuCombined || !lpMenuWidths )
1565 return 0;
1567 /* Create an OLE menu descriptor */
1568 if ( !(hOleMenu = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT,
1569 sizeof(OleMenuDescriptor) ) ) )
1570 return 0;
1572 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1573 if ( !pOleMenuDescriptor )
1574 return 0;
1576 /* Initialize menu group widths and hmenu */
1577 for ( i = 0; i < 6; i++ )
1578 pOleMenuDescriptor->mgw.width[i] = lpMenuWidths->width[i];
1580 pOleMenuDescriptor->hmenuCombined = hmenuCombined;
1581 pOleMenuDescriptor->bIsServerItem = FALSE;
1582 GlobalUnlock( hOleMenu );
1584 return hOleMenu;
1587 /***********************************************************************
1588 * OleDestroyMenuDescriptor [OLE32.@]
1589 * Destroy the shared menu descriptor
1591 HRESULT WINAPI OleDestroyMenuDescriptor(
1592 HOLEMENU hmenuDescriptor)
1594 if ( hmenuDescriptor )
1595 GlobalFree( hmenuDescriptor );
1596 return S_OK;
1599 /***********************************************************************
1600 * OleSetMenuDescriptor [OLE32.@]
1601 * Installs or removes OLE dispatching code for the containers frame window.
1603 * PARAMS
1604 * hOleMenu Handle to composite menu descriptor
1605 * hwndFrame Handle to containers frame window
1606 * hwndActiveObject Handle to objects in-place activation window
1607 * lpFrame Pointer to IOleInPlaceFrame on containers window
1608 * lpActiveObject Pointer to IOleInPlaceActiveObject on active in-place object
1610 * RETURNS
1611 * S_OK - menu installed correctly
1612 * E_FAIL, E_INVALIDARG, E_UNEXPECTED - failure
1614 * FIXME
1615 * The lpFrame and lpActiveObject parameters are currently ignored
1616 * OLE should install context sensitive help F1 filtering for the app when
1617 * these are non null.
1619 HRESULT WINAPI OleSetMenuDescriptor(
1620 HOLEMENU hOleMenu,
1621 HWND hwndFrame,
1622 HWND hwndActiveObject,
1623 LPOLEINPLACEFRAME lpFrame,
1624 LPOLEINPLACEACTIVEOBJECT lpActiveObject)
1626 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1628 /* Check args */
1629 if ( !hwndFrame || (hOleMenu && !hwndActiveObject) )
1630 return E_INVALIDARG;
1632 if ( lpFrame || lpActiveObject )
1634 FIXME("(%p, %p, %p, %p, %p), Context sensitive help filtering not implemented!\n",
1635 hOleMenu,
1636 hwndFrame,
1637 hwndActiveObject,
1638 lpFrame,
1639 lpActiveObject);
1642 /* Set up a message hook to intercept the containers frame window messages.
1643 * The message filter is responsible for dispatching menu messages from the
1644 * shared menu which are intended for the object.
1647 if ( hOleMenu ) /* Want to install dispatching code */
1649 /* If OLEMenu hooks are already installed for this thread, fail
1650 * Note: This effectively means that OleSetMenuDescriptor cannot
1651 * be called twice in succession on the same frame window
1652 * without first calling it with a null hOleMenu to uninstall */
1653 if ( OLEMenu_IsHookInstalled( GetCurrentThreadId() ) )
1654 return E_FAIL;
1656 /* Get the menu descriptor */
1657 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1658 if ( !pOleMenuDescriptor )
1659 return E_UNEXPECTED;
1661 /* Update the menu descriptor */
1662 pOleMenuDescriptor->hwndFrame = hwndFrame;
1663 pOleMenuDescriptor->hwndActiveObject = hwndActiveObject;
1665 GlobalUnlock( hOleMenu );
1666 pOleMenuDescriptor = NULL;
1668 /* Add a menu descriptor windows property to the frame window */
1669 SetPropA( hwndFrame, "PROP_OLEMenuDescriptor", hOleMenu );
1671 /* Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC */
1672 if ( !OLEMenu_InstallHooks( GetCurrentThreadId() ) )
1673 return E_FAIL;
1675 else /* Want to uninstall dispatching code */
1677 /* Uninstall the hooks */
1678 if ( !OLEMenu_UnInstallHooks( GetCurrentThreadId() ) )
1679 return E_FAIL;
1681 /* Remove the menu descriptor property from the frame window */
1682 RemovePropA( hwndFrame, "PROP_OLEMenuDescriptor" );
1685 return S_OK;
1688 /******************************************************************************
1689 * IsAccelerator [OLE32.@]
1690 * Mostly copied from controls/menu.c TranslateAccelerator implementation
1692 BOOL WINAPI IsAccelerator(HACCEL hAccel, int cAccelEntries, LPMSG lpMsg, WORD* lpwCmd)
1694 LPACCEL lpAccelTbl;
1695 int i;
1697 if(!lpMsg) return FALSE;
1698 if (!hAccel)
1700 WARN_(accel)("NULL accel handle\n");
1701 return FALSE;
1703 if((lpMsg->message != WM_KEYDOWN &&
1704 lpMsg->message != WM_SYSKEYDOWN &&
1705 lpMsg->message != WM_SYSCHAR &&
1706 lpMsg->message != WM_CHAR)) return FALSE;
1707 lpAccelTbl = HeapAlloc(GetProcessHeap(), 0, cAccelEntries * sizeof(ACCEL));
1708 if (NULL == lpAccelTbl)
1710 return FALSE;
1712 if (CopyAcceleratorTableW(hAccel, lpAccelTbl, cAccelEntries) != cAccelEntries)
1714 WARN_(accel)("CopyAcceleratorTableW failed\n");
1715 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
1716 return FALSE;
1719 TRACE_(accel)("hAccel=%p, cAccelEntries=%d,"
1720 "msg->hwnd=%p, msg->message=%04x, wParam=%08x, lParam=%08lx\n",
1721 hAccel, cAccelEntries,
1722 lpMsg->hwnd, lpMsg->message, lpMsg->wParam, lpMsg->lParam);
1723 for(i = 0; i < cAccelEntries; i++)
1725 if(lpAccelTbl[i].key != lpMsg->wParam)
1726 continue;
1728 if(lpMsg->message == WM_CHAR)
1730 if(!(lpAccelTbl[i].fVirt & FALT) && !(lpAccelTbl[i].fVirt & FVIRTKEY))
1732 TRACE_(accel)("found accel for WM_CHAR: ('%c')\n", lpMsg->wParam & 0xff);
1733 goto found;
1736 else
1738 if(lpAccelTbl[i].fVirt & FVIRTKEY)
1740 INT mask = 0;
1741 TRACE_(accel)("found accel for virt_key %04x (scan %04x)\n",
1742 lpMsg->wParam, HIWORD(lpMsg->lParam) & 0xff);
1743 if(GetKeyState(VK_SHIFT) & 0x8000) mask |= FSHIFT;
1744 if(GetKeyState(VK_CONTROL) & 0x8000) mask |= FCONTROL;
1745 if(GetKeyState(VK_MENU) & 0x8000) mask |= FALT;
1746 if(mask == (lpAccelTbl[i].fVirt & (FSHIFT | FCONTROL | FALT))) goto found;
1747 TRACE_(accel)("incorrect SHIFT/CTRL/ALT-state\n");
1749 else
1751 if(!(lpMsg->lParam & 0x01000000)) /* no special_key */
1753 if((lpAccelTbl[i].fVirt & FALT) && (lpMsg->lParam & 0x20000000))
1754 { /* ^^ ALT pressed */
1755 TRACE_(accel)("found accel for Alt-%c\n", lpMsg->wParam & 0xff);
1756 goto found;
1763 WARN_(accel)("couldn't translate accelerator key\n");
1764 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
1765 return FALSE;
1767 found:
1768 if(lpwCmd) *lpwCmd = lpAccelTbl[i].cmd;
1769 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
1770 return TRUE;
1773 /***********************************************************************
1774 * ReleaseStgMedium [OLE32.@]
1776 void WINAPI ReleaseStgMedium(
1777 STGMEDIUM* pmedium)
1779 switch (pmedium->tymed)
1781 case TYMED_HGLOBAL:
1783 if ( (pmedium->pUnkForRelease==0) &&
1784 (pmedium->u.hGlobal!=0) )
1785 GlobalFree(pmedium->u.hGlobal);
1786 break;
1788 case TYMED_FILE:
1790 if (pmedium->u.lpszFileName!=0)
1792 if (pmedium->pUnkForRelease==0)
1794 DeleteFileW(pmedium->u.lpszFileName);
1797 CoTaskMemFree(pmedium->u.lpszFileName);
1799 break;
1801 case TYMED_ISTREAM:
1803 if (pmedium->u.pstm!=0)
1805 IStream_Release(pmedium->u.pstm);
1807 break;
1809 case TYMED_ISTORAGE:
1811 if (pmedium->u.pstg!=0)
1813 IStorage_Release(pmedium->u.pstg);
1815 break;
1817 case TYMED_GDI:
1819 if ( (pmedium->pUnkForRelease==0) &&
1820 (pmedium->u.hBitmap!=0) )
1821 DeleteObject(pmedium->u.hBitmap);
1822 break;
1824 case TYMED_MFPICT:
1826 if ( (pmedium->pUnkForRelease==0) &&
1827 (pmedium->u.hMetaFilePict!=0) )
1829 LPMETAFILEPICT pMP = GlobalLock(pmedium->u.hMetaFilePict);
1830 DeleteMetaFile(pMP->hMF);
1831 GlobalUnlock(pmedium->u.hMetaFilePict);
1832 GlobalFree(pmedium->u.hMetaFilePict);
1834 break;
1836 case TYMED_ENHMF:
1838 if ( (pmedium->pUnkForRelease==0) &&
1839 (pmedium->u.hEnhMetaFile!=0) )
1841 DeleteEnhMetaFile(pmedium->u.hEnhMetaFile);
1843 break;
1845 case TYMED_NULL:
1846 default:
1847 break;
1849 pmedium->tymed=TYMED_NULL;
1852 * After cleaning up, the unknown is released
1854 if (pmedium->pUnkForRelease!=0)
1856 IUnknown_Release(pmedium->pUnkForRelease);
1857 pmedium->pUnkForRelease = 0;
1861 /***
1862 * OLEDD_Initialize()
1864 * Initializes the OLE drag and drop data structures.
1866 static void OLEDD_Initialize(void)
1868 WNDCLASSA wndClass;
1870 ZeroMemory (&wndClass, sizeof(WNDCLASSA));
1871 wndClass.style = CS_GLOBALCLASS;
1872 wndClass.lpfnWndProc = OLEDD_DragTrackerWindowProc;
1873 wndClass.cbClsExtra = 0;
1874 wndClass.cbWndExtra = sizeof(TrackerWindowInfo*);
1875 wndClass.hCursor = 0;
1876 wndClass.hbrBackground = 0;
1877 wndClass.lpszClassName = OLEDD_DRAGTRACKERCLASS;
1879 RegisterClassA (&wndClass);
1882 /***
1883 * OLEDD_FreeDropTarget()
1885 * Frees the drag and drop data structure
1887 static void OLEDD_FreeDropTarget(DropTargetNode *dropTargetInfo)
1889 list_remove(&dropTargetInfo->entry);
1890 IDropTarget_Release(dropTargetInfo->dropTarget);
1891 HeapFree(GetProcessHeap(), 0, dropTargetInfo);
1894 /***
1895 * OLEDD_UnInitialize()
1897 * Releases the OLE drag and drop data structures.
1899 static void OLEDD_UnInitialize(void)
1902 * Simply empty the list.
1904 while (!list_empty(&targetListHead))
1906 DropTargetNode* curNode;
1907 curNode = LIST_ENTRY(list_head(&targetListHead), DropTargetNode, entry);
1908 OLEDD_FreeDropTarget(curNode);
1912 /***
1913 * OLEDD_FindDropTarget()
1915 * Finds information about the drop target.
1917 static DropTargetNode* OLEDD_FindDropTarget(HWND hwndOfTarget)
1919 DropTargetNode* curNode;
1922 * Iterate the list to find the HWND value.
1924 LIST_FOR_EACH_ENTRY(curNode, &targetListHead, DropTargetNode, entry)
1925 if (hwndOfTarget==curNode->hwndTarget)
1926 return curNode;
1929 * If we get here, the item is not in the list
1931 return NULL;
1934 /***
1935 * OLEDD_DragTrackerWindowProc()
1937 * This method is the WindowProcedure of the drag n drop tracking
1938 * window. During a drag n Drop operation, an invisible window is created
1939 * to receive the user input and act upon it. This procedure is in charge
1940 * of this behavior.
1943 #define DRAG_TIMER_ID 1
1945 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(
1946 HWND hwnd,
1947 UINT uMsg,
1948 WPARAM wParam,
1949 LPARAM lParam)
1951 switch (uMsg)
1953 case WM_CREATE:
1955 LPCREATESTRUCTA createStruct = (LPCREATESTRUCTA)lParam;
1957 SetWindowLongA(hwnd, 0, (LONG)createStruct->lpCreateParams);
1958 SetTimer(hwnd, DRAG_TIMER_ID, 50, NULL);
1960 break;
1962 case WM_TIMER:
1963 case WM_MOUSEMOVE:
1965 OLEDD_TrackMouseMove((TrackerWindowInfo*)GetWindowLongA(hwnd, 0));
1966 break;
1968 case WM_LBUTTONUP:
1969 case WM_MBUTTONUP:
1970 case WM_RBUTTONUP:
1971 case WM_LBUTTONDOWN:
1972 case WM_MBUTTONDOWN:
1973 case WM_RBUTTONDOWN:
1975 OLEDD_TrackStateChange((TrackerWindowInfo*)GetWindowLongA(hwnd, 0));
1976 break;
1978 case WM_DESTROY:
1980 KillTimer(hwnd, DRAG_TIMER_ID);
1981 break;
1986 * This is a window proc after all. Let's call the default.
1988 return DefWindowProcA (hwnd, uMsg, wParam, lParam);
1991 /***
1992 * OLEDD_TrackMouseMove()
1994 * This method is invoked while a drag and drop operation is in effect.
1995 * it will generate the appropriate callbacks in the drop source
1996 * and drop target. It will also provide the expected feedback to
1997 * the user.
1999 * params:
2000 * trackerInfo - Pointer to the structure identifying the
2001 * drag & drop operation that is currently
2002 * active.
2004 static void OLEDD_TrackMouseMove(TrackerWindowInfo* trackerInfo)
2006 HWND hwndNewTarget = 0;
2007 HRESULT hr = S_OK;
2008 POINT pt;
2011 * Get the handle of the window under the mouse
2013 pt.x = trackerInfo->curMousePos.x;
2014 pt.y = trackerInfo->curMousePos.y;
2015 hwndNewTarget = WindowFromPoint(pt);
2018 * Every time, we re-initialize the effects passed to the
2019 * IDropTarget to the effects allowed by the source.
2021 *trackerInfo->pdwEffect = trackerInfo->dwOKEffect;
2024 * If we are hovering over the same target as before, send the
2025 * DragOver notification
2027 if ( (trackerInfo->curDragTarget != 0) &&
2028 (trackerInfo->curTargetHWND == hwndNewTarget) )
2030 IDropTarget_DragOver(trackerInfo->curDragTarget,
2031 trackerInfo->dwKeyState,
2032 trackerInfo->curMousePos,
2033 trackerInfo->pdwEffect);
2035 else
2037 DropTargetNode* newDropTargetNode = 0;
2040 * If we changed window, we have to notify our old target and check for
2041 * the new one.
2043 if (trackerInfo->curDragTarget!=0)
2045 IDropTarget_DragLeave(trackerInfo->curDragTarget);
2049 * Make sure we're hovering over a window.
2051 if (hwndNewTarget!=0)
2054 * Find-out if there is a drag target under the mouse
2056 HWND nexttar = hwndNewTarget;
2057 trackerInfo->curTargetHWND = hwndNewTarget;
2059 do {
2060 newDropTargetNode = OLEDD_FindDropTarget(nexttar);
2061 } while (!newDropTargetNode && (nexttar = GetParent(nexttar)) != 0);
2062 if(nexttar) hwndNewTarget = nexttar;
2064 trackerInfo->curDragTargetHWND = hwndNewTarget;
2065 trackerInfo->curDragTarget = newDropTargetNode ? newDropTargetNode->dropTarget : 0;
2068 * If there is, notify it that we just dragged-in
2070 if (trackerInfo->curDragTarget!=0)
2072 IDropTarget_DragEnter(trackerInfo->curDragTarget,
2073 trackerInfo->dataObject,
2074 trackerInfo->dwKeyState,
2075 trackerInfo->curMousePos,
2076 trackerInfo->pdwEffect);
2079 else
2082 * The mouse is not over a window so we don't track anything.
2084 trackerInfo->curDragTargetHWND = 0;
2085 trackerInfo->curTargetHWND = 0;
2086 trackerInfo->curDragTarget = 0;
2091 * Now that we have done that, we have to tell the source to give
2092 * us feedback on the work being done by the target. If we don't
2093 * have a target, simulate no effect.
2095 if (trackerInfo->curDragTarget==0)
2097 *trackerInfo->pdwEffect = DROPEFFECT_NONE;
2100 hr = IDropSource_GiveFeedback(trackerInfo->dropSource,
2101 *trackerInfo->pdwEffect);
2104 * When we ask for feedback from the drop source, sometimes it will
2105 * do all the necessary work and sometimes it will not handle it
2106 * when that's the case, we must display the standard drag and drop
2107 * cursors.
2109 if (hr==DRAGDROP_S_USEDEFAULTCURSORS)
2111 if (*trackerInfo->pdwEffect & DROPEFFECT_MOVE)
2113 SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(1)));
2115 else if (*trackerInfo->pdwEffect & DROPEFFECT_COPY)
2117 SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(2)));
2119 else if (*trackerInfo->pdwEffect & DROPEFFECT_LINK)
2121 SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(3)));
2123 else
2125 SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(0)));
2130 /***
2131 * OLEDD_TrackStateChange()
2133 * This method is invoked while a drag and drop operation is in effect.
2134 * It is used to notify the drop target/drop source callbacks when
2135 * the state of the keyboard or mouse button change.
2137 * params:
2138 * trackerInfo - Pointer to the structure identifying the
2139 * drag & drop operation that is currently
2140 * active.
2142 static void OLEDD_TrackStateChange(TrackerWindowInfo* trackerInfo)
2145 * Ask the drop source what to do with the operation.
2147 trackerInfo->returnValue = IDropSource_QueryContinueDrag(
2148 trackerInfo->dropSource,
2149 trackerInfo->escPressed,
2150 trackerInfo->dwKeyState);
2153 * All the return valued will stop the operation except the S_OK
2154 * return value.
2156 if (trackerInfo->returnValue!=S_OK)
2159 * Make sure the message loop in DoDragDrop stops
2161 trackerInfo->trackingDone = TRUE;
2164 * Release the mouse in case the drop target decides to show a popup
2165 * or a menu or something.
2167 ReleaseCapture();
2170 * If we end-up over a target, drop the object in the target or
2171 * inform the target that the operation was cancelled.
2173 if (trackerInfo->curDragTarget!=0)
2175 switch (trackerInfo->returnValue)
2178 * If the source wants us to complete the operation, we tell
2179 * the drop target that we just dropped the object in it.
2181 case DRAGDROP_S_DROP:
2183 IDropTarget_Drop(trackerInfo->curDragTarget,
2184 trackerInfo->dataObject,
2185 trackerInfo->dwKeyState,
2186 trackerInfo->curMousePos,
2187 trackerInfo->pdwEffect);
2188 break;
2191 * If the source told us that we should cancel, fool the drop
2192 * target by telling it that the mouse left it's window.
2193 * Also set the drop effect to "NONE" in case the application
2194 * ignores the result of DoDragDrop.
2196 case DRAGDROP_S_CANCEL:
2197 IDropTarget_DragLeave(trackerInfo->curDragTarget);
2198 *trackerInfo->pdwEffect = DROPEFFECT_NONE;
2199 break;
2205 /***
2206 * OLEDD_GetButtonState()
2208 * This method will use the current state of the keyboard to build
2209 * a button state mask equivalent to the one passed in the
2210 * WM_MOUSEMOVE wParam.
2212 static DWORD OLEDD_GetButtonState(void)
2214 BYTE keyboardState[256];
2215 DWORD keyMask = 0;
2217 GetKeyboardState(keyboardState);
2219 if ( (keyboardState[VK_SHIFT] & 0x80) !=0)
2220 keyMask |= MK_SHIFT;
2222 if ( (keyboardState[VK_CONTROL] & 0x80) !=0)
2223 keyMask |= MK_CONTROL;
2225 if ( (keyboardState[VK_LBUTTON] & 0x80) !=0)
2226 keyMask |= MK_LBUTTON;
2228 if ( (keyboardState[VK_RBUTTON] & 0x80) !=0)
2229 keyMask |= MK_RBUTTON;
2231 if ( (keyboardState[VK_MBUTTON] & 0x80) !=0)
2232 keyMask |= MK_MBUTTON;
2234 return keyMask;
2237 /***
2238 * OLEDD_GetButtonState()
2240 * This method will read the default value of the registry key in
2241 * parameter and extract a DWORD value from it. The registry key value
2242 * can be in a string key or a DWORD key.
2244 * params:
2245 * regKey - Key to read the default value from
2246 * pdwValue - Pointer to the location where the DWORD
2247 * value is returned. This value is not modified
2248 * if the value is not found.
2251 static void OLEUTL_ReadRegistryDWORDValue(
2252 HKEY regKey,
2253 DWORD* pdwValue)
2255 char buffer[20];
2256 DWORD dwKeyType;
2257 DWORD cbData = 20;
2258 LONG lres;
2260 lres = RegQueryValueExA(regKey,
2262 NULL,
2263 &dwKeyType,
2264 (LPBYTE)buffer,
2265 &cbData);
2267 if (lres==ERROR_SUCCESS)
2269 switch (dwKeyType)
2271 case REG_DWORD:
2272 *pdwValue = *(DWORD*)buffer;
2273 break;
2274 case REG_EXPAND_SZ:
2275 case REG_MULTI_SZ:
2276 case REG_SZ:
2277 *pdwValue = (DWORD)strtoul(buffer, NULL, 10);
2278 break;
2283 /******************************************************************************
2284 * OleDraw (OLE32.@)
2286 * The operation of this function is documented literally in the WinAPI
2287 * documentation to involve a QueryInterface for the IViewObject interface,
2288 * followed by a call to IViewObject::Draw.
2290 HRESULT WINAPI OleDraw(
2291 IUnknown *pUnk,
2292 DWORD dwAspect,
2293 HDC hdcDraw,
2294 LPCRECT lprcBounds)
2296 HRESULT hres;
2297 IViewObject *viewobject;
2299 hres = IUnknown_QueryInterface(pUnk,
2300 &IID_IViewObject,
2301 (void**)&viewobject);
2303 if (SUCCEEDED(hres))
2305 RECTL rectl;
2307 rectl.left = lprcBounds->left;
2308 rectl.right = lprcBounds->right;
2309 rectl.top = lprcBounds->top;
2310 rectl.bottom = lprcBounds->bottom;
2311 hres = IViewObject_Draw(viewobject, dwAspect, -1, 0, 0, 0, hdcDraw, &rectl, 0, 0, 0);
2313 IViewObject_Release(viewobject);
2314 return hres;
2316 else
2318 return DV_E_NOIVIEWOBJECT;
2322 /***********************************************************************
2323 * OleTranslateAccelerator [OLE32.@]
2325 HRESULT WINAPI OleTranslateAccelerator (LPOLEINPLACEFRAME lpFrame,
2326 LPOLEINPLACEFRAMEINFO lpFrameInfo, LPMSG lpmsg)
2328 WORD wID;
2330 TRACE("(%p,%p,%p)\n", lpFrame, lpFrameInfo, lpmsg);
2332 if (IsAccelerator(lpFrameInfo->haccel,lpFrameInfo->cAccelEntries,lpmsg,&wID))
2333 return IOleInPlaceFrame_TranslateAccelerator(lpFrame,lpmsg,wID);
2335 return S_FALSE;
2338 /******************************************************************************
2339 * OleCreate [OLE32.@]
2342 HRESULT WINAPI OleCreate(
2343 REFCLSID rclsid,
2344 REFIID riid,
2345 DWORD renderopt,
2346 LPFORMATETC pFormatEtc,
2347 LPOLECLIENTSITE pClientSite,
2348 LPSTORAGE pStg,
2349 LPVOID* ppvObj)
2351 HRESULT hres;
2352 IUnknown * pUnk = NULL;
2353 IOleObject *pOleObject = NULL;
2355 TRACE("(%s, %s, %d, %p, %p, %p, %p)\n", debugstr_guid(rclsid),
2356 debugstr_guid(riid), renderopt, pFormatEtc, pClientSite, pStg, ppvObj);
2358 hres = CoCreateInstance(rclsid, 0, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER, riid, (LPVOID*)&pUnk);
2361 * If that fails, as it will most times, load the default
2362 * OLE handler.
2364 if (FAILED(hres))
2366 hres = OleCreateDefaultHandler(rclsid,
2367 NULL,
2368 riid,
2369 (void**)&pUnk);
2372 if (SUCCEEDED(hres))
2373 hres = IStorage_SetClass(pStg, rclsid);
2375 if (pClientSite && SUCCEEDED(hres))
2377 hres = IUnknown_QueryInterface(pUnk, &IID_IOleObject, (LPVOID*)&pOleObject);
2378 if (SUCCEEDED(hres))
2380 DWORD dwStatus;
2381 hres = IOleObject_GetMiscStatus(pOleObject, DVASPECT_CONTENT, &dwStatus);
2385 if (SUCCEEDED(hres))
2387 IPersistStorage * pPS;
2388 if (SUCCEEDED((hres = IUnknown_QueryInterface(pUnk, &IID_IPersistStorage, (LPVOID*)&pPS))))
2390 TRACE("trying to set stg %p\n", pStg);
2391 hres = IPersistStorage_InitNew(pPS, pStg);
2392 TRACE("-- result 0x%08x\n", hres);
2393 IPersistStorage_Release(pPS);
2397 if (pClientSite && SUCCEEDED(hres))
2399 TRACE("trying to set clientsite %p\n", pClientSite);
2400 hres = IOleObject_SetClientSite(pOleObject, pClientSite);
2401 TRACE("-- result 0x%08x\n", hres);
2404 if (pOleObject)
2405 IOleObject_Release(pOleObject);
2407 if (((renderopt == OLERENDER_DRAW) || (renderopt == OLERENDER_FORMAT)) &&
2408 SUCCEEDED(hres))
2410 IRunnableObject *pRunnable;
2411 IOleCache *pOleCache;
2412 HRESULT hres2;
2414 hres2 = IUnknown_QueryInterface(pUnk, &IID_IRunnableObject, (void **)&pRunnable);
2415 if (SUCCEEDED(hres2))
2417 hres = IRunnableObject_Run(pRunnable, NULL);
2418 IRunnableObject_Release(pRunnable);
2421 if (SUCCEEDED(hres))
2423 hres2 = IUnknown_QueryInterface(pUnk, &IID_IOleCache, (void **)&pOleCache);
2424 if (SUCCEEDED(hres2))
2426 DWORD dwConnection;
2427 hres = IOleCache_Cache(pOleCache, pFormatEtc, ADVF_PRIMEFIRST, &dwConnection);
2428 IOleCache_Release(pOleCache);
2433 if (FAILED(hres) && pUnk)
2435 IUnknown_Release(pUnk);
2436 pUnk = NULL;
2439 *ppvObj = pUnk;
2441 TRACE("-- %p\n", pUnk);
2442 return hres;
2445 /******************************************************************************
2446 * OleGetAutoConvert [OLE32.@]
2448 HRESULT WINAPI OleGetAutoConvert(REFCLSID clsidOld, LPCLSID pClsidNew)
2450 static const WCHAR wszAutoConvertTo[] = {'A','u','t','o','C','o','n','v','e','r','t','T','o',0};
2451 HKEY hkey = NULL;
2452 WCHAR buf[CHARS_IN_GUID];
2453 LONG len;
2454 HRESULT res = S_OK;
2456 res = COM_OpenKeyForCLSID(clsidOld, wszAutoConvertTo, KEY_READ, &hkey);
2457 if (FAILED(res))
2458 goto done;
2460 len = sizeof(buf);
2461 if (RegQueryValueW(hkey, NULL, buf, &len))
2463 res = REGDB_E_KEYMISSING;
2464 goto done;
2466 res = CLSIDFromString(buf, pClsidNew);
2467 done:
2468 if (hkey) RegCloseKey(hkey);
2469 return res;
2472 /******************************************************************************
2473 * OleSetAutoConvert [OLE32.@]
2475 HRESULT WINAPI OleSetAutoConvert(REFCLSID clsidOld, REFCLSID clsidNew)
2477 static const WCHAR wszAutoConvertTo[] = {'A','u','t','o','C','o','n','v','e','r','t','T','o',0};
2478 HKEY hkey = NULL;
2479 WCHAR szClsidNew[CHARS_IN_GUID];
2480 HRESULT res = S_OK;
2482 TRACE("(%s,%s)\n", debugstr_guid(clsidOld), debugstr_guid(clsidNew));
2484 res = COM_OpenKeyForCLSID(clsidOld, NULL, KEY_READ | KEY_WRITE, &hkey);
2485 if (FAILED(res))
2486 goto done;
2487 StringFromGUID2(clsidNew, szClsidNew, CHARS_IN_GUID);
2488 if (RegSetValueW(hkey, wszAutoConvertTo, REG_SZ, szClsidNew, (strlenW(szClsidNew)+1) * sizeof(WCHAR)))
2490 res = REGDB_E_WRITEREGDB;
2491 goto done;
2494 done:
2495 if (hkey) RegCloseKey(hkey);
2496 return res;
2499 /******************************************************************************
2500 * OleDoAutoConvert [OLE32.@]
2502 HRESULT WINAPI OleDoAutoConvert(LPSTORAGE pStg, LPCLSID pClsidNew)
2504 FIXME("(%p,%p) : stub\n",pStg,pClsidNew);
2505 return E_NOTIMPL;
2508 /******************************************************************************
2509 * OleIsRunning [OLE32.@]
2511 BOOL WINAPI OleIsRunning(LPOLEOBJECT pObject)
2513 IRunnableObject *pRunnable;
2514 HRESULT hr;
2515 BOOL running;
2517 TRACE("(%p)\n", pObject);
2519 hr = IOleObject_QueryInterface(pObject, &IID_IRunnableObject, (void **)&pRunnable);
2520 if (FAILED(hr))
2521 return FALSE;
2522 running = IRunnableObject_IsRunning(pRunnable);
2523 IRunnableObject_Release(pRunnable);
2524 return running;
2527 /***********************************************************************
2528 * OleNoteObjectVisible [OLE32.@]
2530 HRESULT WINAPI OleNoteObjectVisible(LPUNKNOWN pUnknown, BOOL bVisible)
2532 TRACE("(%p, %s)\n", pUnknown, bVisible ? "TRUE" : "FALSE");
2533 return CoLockObjectExternal(pUnknown, bVisible, TRUE);
2537 /***********************************************************************
2538 * OLE_FreeClipDataArray [internal]
2540 * NOTES:
2541 * frees the data associated with an array of CLIPDATAs
2543 static void OLE_FreeClipDataArray(ULONG count, CLIPDATA * pClipDataArray)
2545 ULONG i;
2546 for (i = 0; i < count; i++)
2547 if (pClipDataArray[i].pClipData)
2548 CoTaskMemFree(pClipDataArray[i].pClipData);
2551 /***********************************************************************
2552 * PropSysAllocString [OLE32.@]
2553 * NOTES:
2554 * Basically a copy of SysAllocStringLen.
2556 BSTR WINAPI PropSysAllocString(LPCOLESTR str)
2558 DWORD bufferSize;
2559 DWORD* newBuffer;
2560 WCHAR* stringBuffer;
2561 int len;
2563 if (!str) return 0;
2565 len = lstrlenW(str);
2567 * Find the length of the buffer passed-in, in bytes.
2569 bufferSize = len * sizeof (WCHAR);
2572 * Allocate a new buffer to hold the string.
2573 * Don't forget to keep an empty spot at the beginning of the
2574 * buffer for the character count and an extra character at the
2575 * end for the NULL.
2577 newBuffer = HeapAlloc(GetProcessHeap(), 0,
2578 bufferSize + sizeof(WCHAR) + sizeof(DWORD));
2581 * If the memory allocation failed, return a null pointer.
2583 if (newBuffer==0)
2584 return 0;
2587 * Copy the length of the string in the placeholder.
2589 *newBuffer = bufferSize;
2592 * Skip the byte count.
2594 newBuffer++;
2596 memcpy(newBuffer, str, bufferSize);
2599 * Make sure that there is a nul character at the end of the
2600 * string.
2602 stringBuffer = (WCHAR*)newBuffer;
2603 stringBuffer[len] = L'\0';
2605 return (LPWSTR)stringBuffer;
2608 /***********************************************************************
2609 * PropSysFreeString [OLE32.@]
2610 * NOTES
2611 * Copy of SysFreeString.
2613 void WINAPI PropSysFreeString(LPOLESTR str)
2615 DWORD* bufferPointer;
2617 /* NULL is a valid parameter */
2618 if(!str) return;
2621 * We have to be careful when we free a BSTR pointer, it points to
2622 * the beginning of the string but it skips the byte count contained
2623 * before the string.
2625 bufferPointer = (DWORD*)str;
2627 bufferPointer--;
2630 * Free the memory from its "real" origin.
2632 HeapFree(GetProcessHeap(), 0, bufferPointer);
2635 /******************************************************************************
2636 * Check if a PROPVARIANT's type is valid.
2638 static inline HRESULT PROPVARIANT_ValidateType(VARTYPE vt)
2640 switch (vt)
2642 case VT_EMPTY:
2643 case VT_NULL:
2644 case VT_I2:
2645 case VT_I4:
2646 case VT_R4:
2647 case VT_R8:
2648 case VT_CY:
2649 case VT_DATE:
2650 case VT_BSTR:
2651 case VT_ERROR:
2652 case VT_BOOL:
2653 case VT_UI1:
2654 case VT_UI2:
2655 case VT_UI4:
2656 case VT_I8:
2657 case VT_UI8:
2658 case VT_LPSTR:
2659 case VT_LPWSTR:
2660 case VT_FILETIME:
2661 case VT_BLOB:
2662 case VT_STREAM:
2663 case VT_STORAGE:
2664 case VT_STREAMED_OBJECT:
2665 case VT_STORED_OBJECT:
2666 case VT_BLOB_OBJECT:
2667 case VT_CF:
2668 case VT_CLSID:
2669 case VT_I2|VT_VECTOR:
2670 case VT_I4|VT_VECTOR:
2671 case VT_R4|VT_VECTOR:
2672 case VT_R8|VT_VECTOR:
2673 case VT_CY|VT_VECTOR:
2674 case VT_DATE|VT_VECTOR:
2675 case VT_BSTR|VT_VECTOR:
2676 case VT_ERROR|VT_VECTOR:
2677 case VT_BOOL|VT_VECTOR:
2678 case VT_VARIANT|VT_VECTOR:
2679 case VT_UI1|VT_VECTOR:
2680 case VT_UI2|VT_VECTOR:
2681 case VT_UI4|VT_VECTOR:
2682 case VT_I8|VT_VECTOR:
2683 case VT_UI8|VT_VECTOR:
2684 case VT_LPSTR|VT_VECTOR:
2685 case VT_LPWSTR|VT_VECTOR:
2686 case VT_FILETIME|VT_VECTOR:
2687 case VT_CF|VT_VECTOR:
2688 case VT_CLSID|VT_VECTOR:
2689 return S_OK;
2691 WARN("Bad type %d\n", vt);
2692 return STG_E_INVALIDPARAMETER;
2695 /***********************************************************************
2696 * PropVariantClear [OLE32.@]
2698 HRESULT WINAPI PropVariantClear(PROPVARIANT * pvar) /* [in/out] */
2700 HRESULT hr;
2702 TRACE("(%p)\n", pvar);
2704 if (!pvar)
2705 return S_OK;
2707 hr = PROPVARIANT_ValidateType(pvar->vt);
2708 if (FAILED(hr))
2709 return hr;
2711 switch(pvar->vt)
2713 case VT_EMPTY:
2714 case VT_NULL:
2715 case VT_I2:
2716 case VT_I4:
2717 case VT_R4:
2718 case VT_R8:
2719 case VT_CY:
2720 case VT_DATE:
2721 case VT_ERROR:
2722 case VT_BOOL:
2723 case VT_UI1:
2724 case VT_UI2:
2725 case VT_UI4:
2726 case VT_I8:
2727 case VT_UI8:
2728 case VT_FILETIME:
2729 break;
2730 case VT_STREAM:
2731 case VT_STREAMED_OBJECT:
2732 case VT_STORAGE:
2733 case VT_STORED_OBJECT:
2734 if (pvar->u.pStream)
2735 IUnknown_Release(pvar->u.pStream);
2736 break;
2737 case VT_CLSID:
2738 case VT_LPSTR:
2739 case VT_LPWSTR:
2740 /* pick an arbitary typed pointer - we don't care about the type
2741 * as we are just freeing it */
2742 CoTaskMemFree(pvar->u.puuid);
2743 break;
2744 case VT_BLOB:
2745 case VT_BLOB_OBJECT:
2746 CoTaskMemFree(pvar->u.blob.pBlobData);
2747 break;
2748 case VT_BSTR:
2749 if (pvar->u.bstrVal)
2750 PropSysFreeString(pvar->u.bstrVal);
2751 break;
2752 case VT_CF:
2753 if (pvar->u.pclipdata)
2755 OLE_FreeClipDataArray(1, pvar->u.pclipdata);
2756 CoTaskMemFree(pvar->u.pclipdata);
2758 break;
2759 default:
2760 if (pvar->vt & VT_VECTOR)
2762 ULONG i;
2764 switch (pvar->vt & ~VT_VECTOR)
2766 case VT_VARIANT:
2767 FreePropVariantArray(pvar->u.capropvar.cElems, pvar->u.capropvar.pElems);
2768 break;
2769 case VT_CF:
2770 OLE_FreeClipDataArray(pvar->u.caclipdata.cElems, pvar->u.caclipdata.pElems);
2771 break;
2772 case VT_BSTR:
2773 for (i = 0; i < pvar->u.cabstr.cElems; i++)
2774 PropSysFreeString(pvar->u.cabstr.pElems[i]);
2775 break;
2776 case VT_LPSTR:
2777 for (i = 0; i < pvar->u.calpstr.cElems; i++)
2778 CoTaskMemFree(pvar->u.calpstr.pElems[i]);
2779 break;
2780 case VT_LPWSTR:
2781 for (i = 0; i < pvar->u.calpwstr.cElems; i++)
2782 CoTaskMemFree(pvar->u.calpwstr.pElems[i]);
2783 break;
2785 if (pvar->vt & ~VT_VECTOR)
2787 /* pick an arbitary VT_VECTOR structure - they all have the same
2788 * memory layout */
2789 CoTaskMemFree(pvar->u.capropvar.pElems);
2792 else
2793 WARN("Invalid/unsupported type %d\n", pvar->vt);
2796 ZeroMemory(pvar, sizeof(*pvar));
2798 return S_OK;
2801 /***********************************************************************
2802 * PropVariantCopy [OLE32.@]
2804 HRESULT WINAPI PropVariantCopy(PROPVARIANT *pvarDest, /* [out] */
2805 const PROPVARIANT *pvarSrc) /* [in] */
2807 ULONG len;
2808 HRESULT hr;
2810 TRACE("(%p, %p)\n", pvarDest, pvarSrc);
2812 hr = PROPVARIANT_ValidateType(pvarSrc->vt);
2813 if (FAILED(hr))
2814 return hr;
2816 /* this will deal with most cases */
2817 CopyMemory(pvarDest, pvarSrc, sizeof(*pvarDest));
2819 switch(pvarSrc->vt)
2821 case VT_FILETIME:
2822 break;
2823 case VT_STREAM:
2824 case VT_STREAMED_OBJECT:
2825 case VT_STORAGE:
2826 case VT_STORED_OBJECT:
2827 IUnknown_AddRef((LPUNKNOWN)pvarDest->u.pStream);
2828 break;
2829 case VT_CLSID:
2830 pvarDest->u.puuid = CoTaskMemAlloc(sizeof(CLSID));
2831 CopyMemory(pvarDest->u.puuid, pvarSrc->u.puuid, sizeof(CLSID));
2832 break;
2833 case VT_LPSTR:
2834 len = strlen(pvarSrc->u.pszVal);
2835 pvarDest->u.pszVal = CoTaskMemAlloc((len+1)*sizeof(CHAR));
2836 CopyMemory(pvarDest->u.pszVal, pvarSrc->u.pszVal, (len+1)*sizeof(CHAR));
2837 break;
2838 case VT_LPWSTR:
2839 len = lstrlenW(pvarSrc->u.pwszVal);
2840 pvarDest->u.pwszVal = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
2841 CopyMemory(pvarDest->u.pwszVal, pvarSrc->u.pwszVal, (len+1)*sizeof(WCHAR));
2842 break;
2843 case VT_BLOB:
2844 case VT_BLOB_OBJECT:
2845 if (pvarSrc->u.blob.pBlobData)
2847 len = pvarSrc->u.blob.cbSize;
2848 pvarDest->u.blob.pBlobData = CoTaskMemAlloc(len);
2849 CopyMemory(pvarDest->u.blob.pBlobData, pvarSrc->u.blob.pBlobData, len);
2851 break;
2852 case VT_BSTR:
2853 pvarDest->u.bstrVal = PropSysAllocString(pvarSrc->u.bstrVal);
2854 break;
2855 case VT_CF:
2856 if (pvarSrc->u.pclipdata)
2858 len = pvarSrc->u.pclipdata->cbSize - sizeof(pvarSrc->u.pclipdata->ulClipFmt);
2859 pvarDest->u.pclipdata = CoTaskMemAlloc(sizeof (CLIPDATA));
2860 pvarDest->u.pclipdata->cbSize = pvarSrc->u.pclipdata->cbSize;
2861 pvarDest->u.pclipdata->ulClipFmt = pvarSrc->u.pclipdata->ulClipFmt;
2862 pvarDest->u.pclipdata->pClipData = CoTaskMemAlloc(len);
2863 CopyMemory(pvarDest->u.pclipdata->pClipData, pvarSrc->u.pclipdata->pClipData, len);
2865 break;
2866 default:
2867 if (pvarSrc->vt & VT_VECTOR)
2869 int elemSize;
2870 ULONG i;
2872 switch(pvarSrc->vt & ~VT_VECTOR)
2874 case VT_I1: elemSize = sizeof(pvarSrc->u.cVal); break;
2875 case VT_UI1: elemSize = sizeof(pvarSrc->u.bVal); break;
2876 case VT_I2: elemSize = sizeof(pvarSrc->u.iVal); break;
2877 case VT_UI2: elemSize = sizeof(pvarSrc->u.uiVal); break;
2878 case VT_BOOL: elemSize = sizeof(pvarSrc->u.boolVal); break;
2879 case VT_I4: elemSize = sizeof(pvarSrc->u.lVal); break;
2880 case VT_UI4: elemSize = sizeof(pvarSrc->u.ulVal); break;
2881 case VT_R4: elemSize = sizeof(pvarSrc->u.fltVal); break;
2882 case VT_R8: elemSize = sizeof(pvarSrc->u.dblVal); break;
2883 case VT_ERROR: elemSize = sizeof(pvarSrc->u.scode); break;
2884 case VT_I8: elemSize = sizeof(pvarSrc->u.hVal); break;
2885 case VT_UI8: elemSize = sizeof(pvarSrc->u.uhVal); break;
2886 case VT_CY: elemSize = sizeof(pvarSrc->u.cyVal); break;
2887 case VT_DATE: elemSize = sizeof(pvarSrc->u.date); break;
2888 case VT_FILETIME: elemSize = sizeof(pvarSrc->u.filetime); break;
2889 case VT_CLSID: elemSize = sizeof(*pvarSrc->u.puuid); break;
2890 case VT_CF: elemSize = sizeof(*pvarSrc->u.pclipdata); break;
2891 case VT_BSTR: elemSize = sizeof(*pvarSrc->u.bstrVal); break;
2892 case VT_LPSTR: elemSize = sizeof(*pvarSrc->u.pszVal); break;
2893 case VT_LPWSTR: elemSize = sizeof(*pvarSrc->u.pwszVal); break;
2895 case VT_VARIANT:
2896 default:
2897 FIXME("Invalid element type: %ul\n", pvarSrc->vt & ~VT_VECTOR);
2898 return E_INVALIDARG;
2900 len = pvarSrc->u.capropvar.cElems;
2901 pvarDest->u.capropvar.pElems = CoTaskMemAlloc(len * elemSize);
2902 if (pvarSrc->vt == (VT_VECTOR | VT_VARIANT))
2904 for (i = 0; i < len; i++)
2905 PropVariantCopy(&pvarDest->u.capropvar.pElems[i], &pvarSrc->u.capropvar.pElems[i]);
2907 else if (pvarSrc->vt == (VT_VECTOR | VT_CF))
2909 FIXME("Copy clipformats\n");
2911 else if (pvarSrc->vt == (VT_VECTOR | VT_BSTR))
2913 for (i = 0; i < len; i++)
2914 pvarDest->u.cabstr.pElems[i] = PropSysAllocString(pvarSrc->u.cabstr.pElems[i]);
2916 else if (pvarSrc->vt == (VT_VECTOR | VT_LPSTR))
2918 size_t strLen;
2919 for (i = 0; i < len; i++)
2921 strLen = lstrlenA(pvarSrc->u.calpstr.pElems[i]) + 1;
2922 pvarDest->u.calpstr.pElems[i] = CoTaskMemAlloc(strLen);
2923 memcpy(pvarDest->u.calpstr.pElems[i],
2924 pvarSrc->u.calpstr.pElems[i], strLen);
2927 else if (pvarSrc->vt == (VT_VECTOR | VT_LPWSTR))
2929 size_t strLen;
2930 for (i = 0; i < len; i++)
2932 strLen = (lstrlenW(pvarSrc->u.calpwstr.pElems[i]) + 1) *
2933 sizeof(WCHAR);
2934 pvarDest->u.calpstr.pElems[i] = CoTaskMemAlloc(strLen);
2935 memcpy(pvarDest->u.calpstr.pElems[i],
2936 pvarSrc->u.calpstr.pElems[i], strLen);
2939 else
2940 CopyMemory(pvarDest->u.capropvar.pElems, pvarSrc->u.capropvar.pElems, len * elemSize);
2942 else
2943 WARN("Invalid/unsupported type %d\n", pvarSrc->vt);
2946 return S_OK;
2949 /***********************************************************************
2950 * FreePropVariantArray [OLE32.@]
2952 HRESULT WINAPI FreePropVariantArray(ULONG cVariants, /* [in] */
2953 PROPVARIANT *rgvars) /* [in/out] */
2955 ULONG i;
2957 TRACE("(%u, %p)\n", cVariants, rgvars);
2959 if (!rgvars)
2960 return E_INVALIDARG;
2962 for(i = 0; i < cVariants; i++)
2963 PropVariantClear(&rgvars[i]);
2965 return S_OK;
2968 /******************************************************************************
2969 * DllDebugObjectRPCHook (OLE32.@)
2970 * turns on and off internal debugging, pointer is only used on macintosh
2973 BOOL WINAPI DllDebugObjectRPCHook(BOOL b, void *dummy)
2975 FIXME("stub\n");
2976 return TRUE;