msi: Merge OLE automation conformance tests.
[wine/hacks.git] / dlls / ole32 / ole2.c
blob127ef8765448a80526536aa067d8f559e56a78a9
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 DropTargetNode* OLEDD_FindDropTarget(
151 HWND hwndOfTarget);
152 static void OLEDD_FreeDropTarget(DropTargetNode*);
153 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(
154 HWND hwnd,
155 UINT uMsg,
156 WPARAM wParam,
157 LPARAM lParam);
158 static void OLEDD_TrackMouseMove(
159 TrackerWindowInfo* trackerInfo);
160 static void OLEDD_TrackStateChange(
161 TrackerWindowInfo* trackerInfo);
162 static DWORD OLEDD_GetButtonState(void);
165 /******************************************************************************
166 * OleBuildVersion [OLE2.1]
167 * OleBuildVersion [OLE32.@]
169 DWORD WINAPI OleBuildVersion(void)
171 TRACE("Returning version %d, build %d.\n", rmm, rup);
172 return (rmm<<16)+rup;
175 /***********************************************************************
176 * OleInitialize (OLE2.2)
177 * OleInitialize (OLE32.@)
179 HRESULT WINAPI OleInitialize(LPVOID reserved)
181 HRESULT hr;
183 TRACE("(%p)\n", reserved);
186 * The first duty of the OleInitialize is to initialize the COM libraries.
188 hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
191 * If the CoInitializeEx call failed, the OLE libraries can't be
192 * initialized.
194 if (FAILED(hr))
195 return hr;
198 * Then, it has to initialize the OLE specific modules.
199 * This includes:
200 * Clipboard
201 * Drag and Drop
202 * Object linking and Embedding
203 * In-place activation
205 if (!COM_CurrentInfo()->ole_inits++ &&
206 InterlockedIncrement(&OLE_moduleLockCount) == 1)
209 * Initialize the libraries.
211 TRACE("() - Initializing the OLE libraries\n");
214 * OLE Clipboard
216 OLEClipbrd_Initialize();
219 * Drag and Drop
221 OLEDD_Initialize();
224 * OLE shared menu
226 OLEMenu_Initialize();
229 return hr;
232 /******************************************************************************
233 * OleUninitialize [OLE2.3]
234 * OleUninitialize [OLE32.@]
236 void WINAPI OleUninitialize(void)
238 TRACE("()\n");
241 * If we hit the bottom of the lock stack, free the libraries.
243 if (!--COM_CurrentInfo()->ole_inits && !InterlockedDecrement(&OLE_moduleLockCount))
246 * Actually free the libraries.
248 TRACE("() - Freeing the last reference count\n");
251 * OLE Clipboard
253 OLEClipbrd_UnInitialize();
256 * OLE shared menu
258 OLEMenu_UnInitialize();
262 * Then, uninitialize the COM libraries.
264 CoUninitialize();
267 /******************************************************************************
268 * OleInitializeWOW [OLE32.@]
270 HRESULT WINAPI OleInitializeWOW(DWORD x, DWORD y) {
271 FIXME("(0x%08x, 0x%08x),stub!\n",x, y);
272 return 0;
275 /***********************************************************************
276 * RegisterDragDrop (OLE32.@)
278 HRESULT WINAPI RegisterDragDrop(
279 HWND hwnd,
280 LPDROPTARGET pDropTarget)
282 DropTargetNode* dropTargetInfo;
284 TRACE("(%p,%p)\n", hwnd, pDropTarget);
286 if (!COM_CurrentApt())
288 ERR("COM not initialized\n");
289 return CO_E_NOTINITIALIZED;
292 if (!pDropTarget)
293 return E_INVALIDARG;
295 if (!IsWindow(hwnd))
297 ERR("invalid hwnd %p\n", hwnd);
298 return DRAGDROP_E_INVALIDHWND;
302 * First, check if the window is already registered.
304 dropTargetInfo = OLEDD_FindDropTarget(hwnd);
306 if (dropTargetInfo!=NULL)
307 return DRAGDROP_E_ALREADYREGISTERED;
310 * If it's not there, we can add it. We first create a node for it.
312 dropTargetInfo = HeapAlloc(GetProcessHeap(), 0, sizeof(DropTargetNode));
314 if (dropTargetInfo==NULL)
315 return E_OUTOFMEMORY;
317 dropTargetInfo->hwndTarget = hwnd;
320 * Don't forget that this is an interface pointer, need to nail it down since
321 * we keep a copy of it.
323 IDropTarget_AddRef(pDropTarget);
324 dropTargetInfo->dropTarget = pDropTarget;
326 list_add_tail(&targetListHead, &dropTargetInfo->entry);
328 return S_OK;
331 /***********************************************************************
332 * RevokeDragDrop (OLE32.@)
334 HRESULT WINAPI RevokeDragDrop(
335 HWND hwnd)
337 DropTargetNode* dropTargetInfo;
339 TRACE("(%p)\n", hwnd);
341 if (!IsWindow(hwnd))
343 ERR("invalid hwnd %p\n", hwnd);
344 return DRAGDROP_E_INVALIDHWND;
348 * First, check if the window is already registered.
350 dropTargetInfo = OLEDD_FindDropTarget(hwnd);
353 * If it ain't in there, it's an error.
355 if (dropTargetInfo==NULL)
356 return DRAGDROP_E_NOTREGISTERED;
358 OLEDD_FreeDropTarget(dropTargetInfo);
360 return S_OK;
363 /***********************************************************************
364 * OleRegGetUserType (OLE32.@)
366 * This implementation of OleRegGetUserType ignores the dwFormOfType
367 * parameter and always returns the full name of the object. This is
368 * not too bad since this is the case for many objects because of the
369 * way they are registered.
371 HRESULT WINAPI OleRegGetUserType(
372 REFCLSID clsid,
373 DWORD dwFormOfType,
374 LPOLESTR* pszUserType)
376 char keyName[60];
377 DWORD dwKeyType;
378 DWORD cbData;
379 HKEY clsidKey;
380 LONG hres;
381 LPSTR buffer;
382 HRESULT retVal;
384 * Initialize the out parameter.
386 *pszUserType = NULL;
389 * Build the key name we're looking for
391 sprintf( keyName, "CLSID\\{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\",
392 clsid->Data1, clsid->Data2, clsid->Data3,
393 clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
394 clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );
396 TRACE("(%s, %d, %p)\n", keyName, dwFormOfType, pszUserType);
399 * Open the class id Key
401 hres = RegOpenKeyA(HKEY_CLASSES_ROOT,
402 keyName,
403 &clsidKey);
405 if (hres != ERROR_SUCCESS)
406 return REGDB_E_CLASSNOTREG;
409 * Retrieve the size of the name string.
411 cbData = 0;
413 hres = RegQueryValueExA(clsidKey,
415 NULL,
416 &dwKeyType,
417 NULL,
418 &cbData);
420 if (hres!=ERROR_SUCCESS)
422 RegCloseKey(clsidKey);
423 return REGDB_E_READREGDB;
427 * Allocate a buffer for the registry value.
429 *pszUserType = CoTaskMemAlloc(cbData*2);
431 if (*pszUserType==NULL)
433 RegCloseKey(clsidKey);
434 return E_OUTOFMEMORY;
437 buffer = HeapAlloc(GetProcessHeap(), 0, cbData);
439 if (buffer == NULL)
441 RegCloseKey(clsidKey);
442 CoTaskMemFree(*pszUserType);
443 *pszUserType=NULL;
444 return E_OUTOFMEMORY;
447 hres = RegQueryValueExA(clsidKey,
449 NULL,
450 &dwKeyType,
451 (LPBYTE) buffer,
452 &cbData);
454 RegCloseKey(clsidKey);
457 if (hres!=ERROR_SUCCESS)
459 CoTaskMemFree(*pszUserType);
460 *pszUserType=NULL;
462 retVal = REGDB_E_READREGDB;
464 else
466 MultiByteToWideChar( CP_ACP, 0, buffer, -1, *pszUserType, cbData /*FIXME*/ );
467 retVal = S_OK;
469 HeapFree(GetProcessHeap(), 0, buffer);
471 return retVal;
474 /***********************************************************************
475 * DoDragDrop [OLE32.@]
477 HRESULT WINAPI DoDragDrop (
478 IDataObject *pDataObject, /* [in] ptr to the data obj */
479 IDropSource* pDropSource, /* [in] ptr to the source obj */
480 DWORD dwOKEffect, /* [in] effects allowed by the source */
481 DWORD *pdwEffect) /* [out] ptr to effects of the source */
483 TrackerWindowInfo trackerInfo;
484 HWND hwndTrackWindow;
485 MSG msg;
487 TRACE("(DataObject %p, DropSource %p)\n", pDataObject, pDropSource);
490 * Setup the drag n drop tracking window.
492 if (!IsValidInterface((LPUNKNOWN)pDropSource))
493 return E_INVALIDARG;
495 trackerInfo.dataObject = pDataObject;
496 trackerInfo.dropSource = pDropSource;
497 trackerInfo.dwOKEffect = dwOKEffect;
498 trackerInfo.pdwEffect = pdwEffect;
499 trackerInfo.trackingDone = FALSE;
500 trackerInfo.escPressed = FALSE;
501 trackerInfo.curDragTargetHWND = 0;
502 trackerInfo.curTargetHWND = 0;
503 trackerInfo.curDragTarget = 0;
505 hwndTrackWindow = CreateWindowA(OLEDD_DRAGTRACKERCLASS,
506 "TrackerWindow",
507 WS_POPUP,
508 CW_USEDEFAULT, CW_USEDEFAULT,
509 CW_USEDEFAULT, CW_USEDEFAULT,
513 (LPVOID)&trackerInfo);
515 if (hwndTrackWindow!=0)
518 * Capture the mouse input
520 SetCapture(hwndTrackWindow);
522 msg.message = 0;
525 * Pump messages. All mouse input should go the the capture window.
527 while (!trackerInfo.trackingDone && GetMessageA(&msg, 0, 0, 0) )
529 trackerInfo.curMousePos.x = msg.pt.x;
530 trackerInfo.curMousePos.y = msg.pt.y;
531 trackerInfo.dwKeyState = OLEDD_GetButtonState();
533 if ( (msg.message >= WM_KEYFIRST) &&
534 (msg.message <= WM_KEYLAST) )
537 * When keyboard messages are sent to windows on this thread, we
538 * want to ignore notify the drop source that the state changed.
539 * in the case of the Escape key, we also notify the drop source
540 * we give it a special meaning.
542 if ( (msg.message==WM_KEYDOWN) &&
543 (msg.wParam==VK_ESCAPE) )
545 trackerInfo.escPressed = TRUE;
549 * Notify the drop source.
551 OLEDD_TrackStateChange(&trackerInfo);
553 else
556 * Dispatch the messages only when it's not a keyboard message.
558 DispatchMessageA(&msg);
562 /* re-post the quit message to outer message loop */
563 if (msg.message == WM_QUIT)
564 PostQuitMessage(msg.wParam);
566 * Destroy the temporary window.
568 DestroyWindow(hwndTrackWindow);
570 return trackerInfo.returnValue;
573 return E_FAIL;
576 /***********************************************************************
577 * OleQueryLinkFromData [OLE32.@]
579 HRESULT WINAPI OleQueryLinkFromData(
580 IDataObject* pSrcDataObject)
582 FIXME("(%p),stub!\n", pSrcDataObject);
583 return S_OK;
586 /***********************************************************************
587 * OleRegGetMiscStatus [OLE32.@]
589 HRESULT WINAPI OleRegGetMiscStatus(
590 REFCLSID clsid,
591 DWORD dwAspect,
592 DWORD* pdwStatus)
594 char keyName[60];
595 HKEY clsidKey;
596 HKEY miscStatusKey;
597 HKEY aspectKey;
598 LONG result;
601 * Initialize the out parameter.
603 *pdwStatus = 0;
606 * Build the key name we're looking for
608 sprintf( keyName, "CLSID\\{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\",
609 clsid->Data1, clsid->Data2, clsid->Data3,
610 clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
611 clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );
613 TRACE("(%s, %d, %p)\n", keyName, dwAspect, pdwStatus);
616 * Open the class id Key
618 result = RegOpenKeyA(HKEY_CLASSES_ROOT,
619 keyName,
620 &clsidKey);
622 if (result != ERROR_SUCCESS)
623 return REGDB_E_CLASSNOTREG;
626 * Get the MiscStatus
628 result = RegOpenKeyA(clsidKey,
629 "MiscStatus",
630 &miscStatusKey);
633 if (result != ERROR_SUCCESS)
635 RegCloseKey(clsidKey);
636 return REGDB_E_READREGDB;
640 * Read the default value
642 OLEUTL_ReadRegistryDWORDValue(miscStatusKey, pdwStatus);
645 * Open the key specific to the requested aspect.
647 sprintf(keyName, "%d", dwAspect);
649 result = RegOpenKeyA(miscStatusKey,
650 keyName,
651 &aspectKey);
653 if (result == ERROR_SUCCESS)
655 OLEUTL_ReadRegistryDWORDValue(aspectKey, pdwStatus);
656 RegCloseKey(aspectKey);
660 * Cleanup
662 RegCloseKey(miscStatusKey);
663 RegCloseKey(clsidKey);
665 return S_OK;
668 static HRESULT EnumOLEVERB_Construct(HKEY hkeyVerb, ULONG index, IEnumOLEVERB **ppenum);
670 typedef struct
672 const IEnumOLEVERBVtbl *lpvtbl;
673 LONG ref;
675 HKEY hkeyVerb;
676 ULONG index;
677 } EnumOLEVERB;
679 static HRESULT WINAPI EnumOLEVERB_QueryInterface(
680 IEnumOLEVERB *iface, REFIID riid, void **ppv)
682 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
683 if (IsEqualIID(riid, &IID_IUnknown) ||
684 IsEqualIID(riid, &IID_IEnumOLEVERB))
686 IUnknown_AddRef(iface);
687 *ppv = iface;
688 return S_OK;
690 return E_NOINTERFACE;
693 static ULONG WINAPI EnumOLEVERB_AddRef(
694 IEnumOLEVERB *iface)
696 EnumOLEVERB *This = (EnumOLEVERB *)iface;
697 TRACE("()\n");
698 return InterlockedIncrement(&This->ref);
701 static ULONG WINAPI EnumOLEVERB_Release(
702 IEnumOLEVERB *iface)
704 EnumOLEVERB *This = (EnumOLEVERB *)iface;
705 LONG refs = InterlockedDecrement(&This->ref);
706 TRACE("()\n");
707 if (!refs)
709 RegCloseKey(This->hkeyVerb);
710 HeapFree(GetProcessHeap(), 0, This);
712 return refs;
715 static HRESULT WINAPI EnumOLEVERB_Next(
716 IEnumOLEVERB *iface, ULONG celt, LPOLEVERB rgelt,
717 ULONG *pceltFetched)
719 EnumOLEVERB *This = (EnumOLEVERB *)iface;
720 HRESULT hr = S_OK;
722 TRACE("(%d, %p, %p)\n", celt, rgelt, pceltFetched);
724 if (pceltFetched)
725 *pceltFetched = 0;
727 for (; celt; celt--, rgelt++)
729 WCHAR wszSubKey[20];
730 LONG cbData;
731 LPWSTR pwszOLEVERB;
732 LPWSTR pwszMenuFlags;
733 LPWSTR pwszAttribs;
734 LONG res = RegEnumKeyW(This->hkeyVerb, This->index, wszSubKey, sizeof(wszSubKey)/sizeof(wszSubKey[0]));
735 if (res == ERROR_NO_MORE_ITEMS)
737 hr = S_FALSE;
738 break;
740 else if (res != ERROR_SUCCESS)
742 ERR("RegEnumKeyW failed with error %d\n", res);
743 hr = REGDB_E_READREGDB;
744 break;
746 res = RegQueryValueW(This->hkeyVerb, wszSubKey, NULL, &cbData);
747 if (res != ERROR_SUCCESS)
749 ERR("RegQueryValueW failed with error %d\n", res);
750 hr = REGDB_E_READREGDB;
751 break;
753 pwszOLEVERB = CoTaskMemAlloc(cbData);
754 if (!pwszOLEVERB)
756 hr = E_OUTOFMEMORY;
757 break;
759 res = RegQueryValueW(This->hkeyVerb, wszSubKey, pwszOLEVERB, &cbData);
760 if (res != ERROR_SUCCESS)
762 ERR("RegQueryValueW failed with error %d\n", res);
763 hr = REGDB_E_READREGDB;
764 CoTaskMemFree(pwszOLEVERB);
765 break;
768 TRACE("verb string: %s\n", debugstr_w(pwszOLEVERB));
769 pwszMenuFlags = strchrW(pwszOLEVERB, ',');
770 if (!pwszMenuFlags)
772 hr = OLEOBJ_E_INVALIDVERB;
773 CoTaskMemFree(pwszOLEVERB);
774 break;
776 /* nul terminate the name string and advance to first character */
777 *pwszMenuFlags = '\0';
778 pwszMenuFlags++;
779 pwszAttribs = strchrW(pwszMenuFlags, ',');
780 if (!pwszAttribs)
782 hr = OLEOBJ_E_INVALIDVERB;
783 CoTaskMemFree(pwszOLEVERB);
784 break;
786 /* nul terminate the menu string and advance to first character */
787 *pwszAttribs = '\0';
788 pwszAttribs++;
790 /* fill out structure for this verb */
791 rgelt->lVerb = atolW(wszSubKey);
792 rgelt->lpszVerbName = pwszOLEVERB; /* user should free */
793 rgelt->fuFlags = atolW(pwszMenuFlags);
794 rgelt->grfAttribs = atolW(pwszAttribs);
796 if (pceltFetched)
797 (*pceltFetched)++;
798 This->index++;
800 return hr;
803 static HRESULT WINAPI EnumOLEVERB_Skip(
804 IEnumOLEVERB *iface, ULONG celt)
806 EnumOLEVERB *This = (EnumOLEVERB *)iface;
808 TRACE("(%d)\n", celt);
810 This->index += celt;
811 return S_OK;
814 static HRESULT WINAPI EnumOLEVERB_Reset(
815 IEnumOLEVERB *iface)
817 EnumOLEVERB *This = (EnumOLEVERB *)iface;
819 TRACE("()\n");
821 This->index = 0;
822 return S_OK;
825 static HRESULT WINAPI EnumOLEVERB_Clone(
826 IEnumOLEVERB *iface,
827 IEnumOLEVERB **ppenum)
829 EnumOLEVERB *This = (EnumOLEVERB *)iface;
830 HKEY hkeyVerb;
831 TRACE("(%p)\n", ppenum);
832 if (!DuplicateHandle(GetCurrentProcess(), This->hkeyVerb, GetCurrentProcess(), (HANDLE *)&hkeyVerb, 0, FALSE, DUPLICATE_SAME_ACCESS))
833 return HRESULT_FROM_WIN32(GetLastError());
834 return EnumOLEVERB_Construct(hkeyVerb, This->index, ppenum);
837 static const IEnumOLEVERBVtbl EnumOLEVERB_VTable =
839 EnumOLEVERB_QueryInterface,
840 EnumOLEVERB_AddRef,
841 EnumOLEVERB_Release,
842 EnumOLEVERB_Next,
843 EnumOLEVERB_Skip,
844 EnumOLEVERB_Reset,
845 EnumOLEVERB_Clone
848 static HRESULT EnumOLEVERB_Construct(HKEY hkeyVerb, ULONG index, IEnumOLEVERB **ppenum)
850 EnumOLEVERB *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
851 if (!This)
853 RegCloseKey(hkeyVerb);
854 return E_OUTOFMEMORY;
856 This->lpvtbl = &EnumOLEVERB_VTable;
857 This->ref = 1;
858 This->index = index;
859 This->hkeyVerb = hkeyVerb;
860 *ppenum = (IEnumOLEVERB *)&This->lpvtbl;
861 return S_OK;
864 /***********************************************************************
865 * OleRegEnumVerbs [OLE32.@]
867 * Enumerates verbs associated with a class stored in the registry.
869 * PARAMS
870 * clsid [I] Class ID to enumerate the verbs for.
871 * ppenum [O] Enumerator.
873 * RETURNS
874 * S_OK: Success.
875 * REGDB_E_CLASSNOTREG: The specified class does not have a key in the registry.
876 * REGDB_E_READREGDB: The class key could not be opened for some other reason.
877 * OLE_E_REGDB_KEY: The Verb subkey for the class is not present.
878 * OLEOBJ_E_NOVERBS: The Verb subkey for the class is empty.
880 HRESULT WINAPI OleRegEnumVerbs (REFCLSID clsid, LPENUMOLEVERB* ppenum)
882 LONG res;
883 HKEY hkeyVerb;
884 DWORD dwSubKeys;
885 static const WCHAR wszVerb[] = {'V','e','r','b',0};
887 TRACE("(%s, %p)\n", debugstr_guid(clsid), ppenum);
889 res = COM_OpenKeyForCLSID(clsid, wszVerb, KEY_READ, &hkeyVerb);
890 if (FAILED(res))
892 if (res == REGDB_E_CLASSNOTREG)
893 ERR("CLSID %s not registered\n", debugstr_guid(clsid));
894 else if (res == REGDB_E_KEYMISSING)
895 ERR("no Verbs key for class %s\n", debugstr_guid(clsid));
896 else
897 ERR("failed to open Verbs key for CLSID %s with error %d\n",
898 debugstr_guid(clsid), res);
899 return res;
902 res = RegQueryInfoKeyW(hkeyVerb, NULL, NULL, NULL, &dwSubKeys, NULL,
903 NULL, NULL, NULL, NULL, NULL, NULL);
904 if (res != ERROR_SUCCESS)
906 ERR("failed to get subkey count with error %d\n", GetLastError());
907 return REGDB_E_READREGDB;
910 if (!dwSubKeys)
912 WARN("class %s has no verbs\n", debugstr_guid(clsid));
913 RegCloseKey(hkeyVerb);
914 return OLEOBJ_E_NOVERBS;
917 return EnumOLEVERB_Construct(hkeyVerb, 0, ppenum);
920 /******************************************************************************
921 * OleSetContainedObject [OLE32.@]
923 HRESULT WINAPI OleSetContainedObject(
924 LPUNKNOWN pUnknown,
925 BOOL fContained)
927 IRunnableObject* runnable = NULL;
928 HRESULT hres;
930 TRACE("(%p,%x)\n", pUnknown, fContained);
932 hres = IUnknown_QueryInterface(pUnknown,
933 &IID_IRunnableObject,
934 (void**)&runnable);
936 if (SUCCEEDED(hres))
938 hres = IRunnableObject_SetContainedObject(runnable, fContained);
940 IRunnableObject_Release(runnable);
942 return hres;
945 return S_OK;
948 /******************************************************************************
949 * OleRun [OLE32.@]
951 * Set the OLE object to the running state.
953 * PARAMS
954 * pUnknown [I] OLE object to run.
956 * RETURNS
957 * Success: S_OK.
958 * Failure: Any HRESULT code.
960 HRESULT WINAPI OleRun(LPUNKNOWN pUnknown)
962 IRunnableObject *runable;
963 HRESULT hres;
965 TRACE("(%p)\n", pUnknown);
967 hres = IUnknown_QueryInterface(pUnknown, &IID_IRunnableObject, (void**)&runable);
968 if (FAILED(hres))
969 return S_OK; /* Appears to return no error. */
971 hres = IRunnableObject_Run(runable, NULL);
972 IRunnableObject_Release(runable);
973 return hres;
976 /******************************************************************************
977 * OleLoad [OLE32.@]
979 HRESULT WINAPI OleLoad(
980 LPSTORAGE pStg,
981 REFIID riid,
982 LPOLECLIENTSITE pClientSite,
983 LPVOID* ppvObj)
985 IPersistStorage* persistStorage = NULL;
986 IUnknown* pUnk;
987 IOleObject* pOleObject = NULL;
988 STATSTG storageInfo;
989 HRESULT hres;
991 TRACE("(%p, %s, %p, %p)\n", pStg, debugstr_guid(riid), pClientSite, ppvObj);
993 *ppvObj = NULL;
996 * TODO, Conversion ... OleDoAutoConvert
1000 * Get the class ID for the object.
1002 hres = IStorage_Stat(pStg, &storageInfo, STATFLAG_NONAME);
1005 * Now, try and create the handler for the object
1007 hres = CoCreateInstance(&storageInfo.clsid,
1008 NULL,
1009 CLSCTX_INPROC_HANDLER|CLSCTX_INPROC_SERVER,
1010 riid,
1011 (void**)&pUnk);
1014 * If that fails, as it will most times, load the default
1015 * OLE handler.
1017 if (FAILED(hres))
1019 hres = OleCreateDefaultHandler(&storageInfo.clsid,
1020 NULL,
1021 riid,
1022 (void**)&pUnk);
1026 * If we couldn't find a handler... this is bad. Abort the whole thing.
1028 if (FAILED(hres))
1029 return hres;
1031 if (pClientSite)
1033 hres = IUnknown_QueryInterface(pUnk, &IID_IOleObject, (void **)&pOleObject);
1034 if (SUCCEEDED(hres))
1036 DWORD dwStatus;
1037 hres = IOleObject_GetMiscStatus(pOleObject, DVASPECT_CONTENT, &dwStatus);
1041 if (SUCCEEDED(hres))
1043 * Initialize the object with it's IPersistStorage interface.
1045 hres = IOleObject_QueryInterface(pUnk,
1046 &IID_IPersistStorage,
1047 (void**)&persistStorage);
1049 if (SUCCEEDED(hres))
1051 hres = IPersistStorage_Load(persistStorage, pStg);
1053 IPersistStorage_Release(persistStorage);
1054 persistStorage = NULL;
1057 if (SUCCEEDED(hres) && pClientSite)
1059 * Inform the new object of it's client site.
1061 hres = IOleObject_SetClientSite(pOleObject, pClientSite);
1064 * Cleanup interfaces used internally
1066 if (pOleObject)
1067 IOleObject_Release(pOleObject);
1069 if (SUCCEEDED(hres))
1071 IOleLink *pOleLink;
1072 HRESULT hres1;
1073 hres1 = IUnknown_QueryInterface(pUnk, &IID_IOleLink, (void **)&pOleLink);
1074 if (SUCCEEDED(hres1))
1076 FIXME("handle OLE link\n");
1077 IOleLink_Release(pOleLink);
1081 if (FAILED(hres))
1083 IUnknown_Release(pUnk);
1084 pUnk = NULL;
1087 *ppvObj = pUnk;
1089 return hres;
1092 /***********************************************************************
1093 * OleSave [OLE32.@]
1095 HRESULT WINAPI OleSave(
1096 LPPERSISTSTORAGE pPS,
1097 LPSTORAGE pStg,
1098 BOOL fSameAsLoad)
1100 HRESULT hres;
1101 CLSID objectClass;
1103 TRACE("(%p,%p,%x)\n", pPS, pStg, fSameAsLoad);
1106 * First, we transfer the class ID (if available)
1108 hres = IPersistStorage_GetClassID(pPS, &objectClass);
1110 if (SUCCEEDED(hres))
1112 WriteClassStg(pStg, &objectClass);
1116 * Then, we ask the object to save itself to the
1117 * storage. If it is successful, we commit the storage.
1119 hres = IPersistStorage_Save(pPS, pStg, fSameAsLoad);
1121 if (SUCCEEDED(hres))
1123 IStorage_Commit(pStg,
1124 STGC_DEFAULT);
1127 return hres;
1131 /******************************************************************************
1132 * OleLockRunning [OLE32.@]
1134 HRESULT WINAPI OleLockRunning(LPUNKNOWN pUnknown, BOOL fLock, BOOL fLastUnlockCloses)
1136 IRunnableObject* runnable = NULL;
1137 HRESULT hres;
1139 TRACE("(%p,%x,%x)\n", pUnknown, fLock, fLastUnlockCloses);
1141 hres = IUnknown_QueryInterface(pUnknown,
1142 &IID_IRunnableObject,
1143 (void**)&runnable);
1145 if (SUCCEEDED(hres))
1147 hres = IRunnableObject_LockRunning(runnable, fLock, fLastUnlockCloses);
1149 IRunnableObject_Release(runnable);
1151 return hres;
1153 else
1154 return E_INVALIDARG;
1158 /**************************************************************************
1159 * Internal methods to manage the shared OLE menu in response to the
1160 * OLE***MenuDescriptor API
1163 /***
1164 * OLEMenu_Initialize()
1166 * Initializes the OLEMENU data structures.
1168 static void OLEMenu_Initialize(void)
1172 /***
1173 * OLEMenu_UnInitialize()
1175 * Releases the OLEMENU data structures.
1177 static void OLEMenu_UnInitialize(void)
1181 /*************************************************************************
1182 * OLEMenu_InstallHooks
1183 * Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
1185 * RETURNS: TRUE if message hooks were successfully installed
1186 * FALSE on failure
1188 static BOOL OLEMenu_InstallHooks( DWORD tid )
1190 OleMenuHookItem *pHookItem = NULL;
1192 /* Create an entry for the hook table */
1193 if ( !(pHookItem = HeapAlloc(GetProcessHeap(), 0,
1194 sizeof(OleMenuHookItem)) ) )
1195 return FALSE;
1197 pHookItem->tid = tid;
1198 pHookItem->hHeap = GetProcessHeap();
1200 /* Install a thread scope message hook for WH_GETMESSAGE */
1201 pHookItem->GetMsg_hHook = SetWindowsHookExA( WH_GETMESSAGE, OLEMenu_GetMsgProc,
1202 0, GetCurrentThreadId() );
1203 if ( !pHookItem->GetMsg_hHook )
1204 goto CLEANUP;
1206 /* Install a thread scope message hook for WH_CALLWNDPROC */
1207 pHookItem->CallWndProc_hHook = SetWindowsHookExA( WH_CALLWNDPROC, OLEMenu_CallWndProc,
1208 0, GetCurrentThreadId() );
1209 if ( !pHookItem->CallWndProc_hHook )
1210 goto CLEANUP;
1212 /* Insert the hook table entry */
1213 pHookItem->next = hook_list;
1214 hook_list = pHookItem;
1216 return TRUE;
1218 CLEANUP:
1219 /* Unhook any hooks */
1220 if ( pHookItem->GetMsg_hHook )
1221 UnhookWindowsHookEx( pHookItem->GetMsg_hHook );
1222 if ( pHookItem->CallWndProc_hHook )
1223 UnhookWindowsHookEx( pHookItem->CallWndProc_hHook );
1224 /* Release the hook table entry */
1225 HeapFree(pHookItem->hHeap, 0, pHookItem );
1227 return FALSE;
1230 /*************************************************************************
1231 * OLEMenu_UnInstallHooks
1232 * UnInstall thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
1234 * RETURNS: TRUE if message hooks were successfully installed
1235 * FALSE on failure
1237 static BOOL OLEMenu_UnInstallHooks( DWORD tid )
1239 OleMenuHookItem *pHookItem = NULL;
1240 OleMenuHookItem **ppHook = &hook_list;
1242 while (*ppHook)
1244 if ((*ppHook)->tid == tid)
1246 pHookItem = *ppHook;
1247 *ppHook = pHookItem->next;
1248 break;
1250 ppHook = &(*ppHook)->next;
1252 if (!pHookItem) return FALSE;
1254 /* Uninstall the hooks installed for this thread */
1255 if ( !UnhookWindowsHookEx( pHookItem->GetMsg_hHook ) )
1256 goto CLEANUP;
1257 if ( !UnhookWindowsHookEx( pHookItem->CallWndProc_hHook ) )
1258 goto CLEANUP;
1260 /* Release the hook table entry */
1261 HeapFree(pHookItem->hHeap, 0, pHookItem );
1263 return TRUE;
1265 CLEANUP:
1266 /* Release the hook table entry */
1267 HeapFree(pHookItem->hHeap, 0, pHookItem );
1269 return FALSE;
1272 /*************************************************************************
1273 * OLEMenu_IsHookInstalled
1274 * Tests if OLEMenu hooks have been installed for a thread
1276 * RETURNS: The pointer and index of the hook table entry for the tid
1277 * NULL and -1 for the index if no hooks were installed for this thread
1279 static OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid )
1281 OleMenuHookItem *pHookItem = NULL;
1283 /* Do a simple linear search for an entry whose tid matches ours.
1284 * We really need a map but efficiency is not a concern here. */
1285 for (pHookItem = hook_list; pHookItem; pHookItem = pHookItem->next)
1287 if ( tid == pHookItem->tid )
1288 return pHookItem;
1291 return NULL;
1294 /***********************************************************************
1295 * OLEMenu_FindMainMenuIndex
1297 * Used by OLEMenu API to find the top level group a menu item belongs to.
1298 * On success pnPos contains the index of the item in the top level menu group
1300 * RETURNS: TRUE if the ID was found, FALSE on failure
1302 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos )
1304 UINT i, nItems;
1306 nItems = GetMenuItemCount( hMainMenu );
1308 for (i = 0; i < nItems; i++)
1310 HMENU hsubmenu;
1312 /* Is the current item a submenu? */
1313 if ( (hsubmenu = GetSubMenu(hMainMenu, i)) )
1315 /* If the handle is the same we're done */
1316 if ( hsubmenu == hPopupMenu )
1318 if (pnPos)
1319 *pnPos = i;
1320 return TRUE;
1322 /* Recursively search without updating pnPos */
1323 else if ( OLEMenu_FindMainMenuIndex( hsubmenu, hPopupMenu, NULL ) )
1325 if (pnPos)
1326 *pnPos = i;
1327 return TRUE;
1332 return FALSE;
1335 /***********************************************************************
1336 * OLEMenu_SetIsServerMenu
1338 * Checks whether a popup menu belongs to a shared menu group which is
1339 * owned by the server, and sets the menu descriptor state accordingly.
1340 * All menu messages from these groups should be routed to the server.
1342 * RETURNS: TRUE if the popup menu is part of a server owned group
1343 * FALSE if the popup menu is part of a container owned group
1345 static BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor )
1347 UINT nPos = 0, nWidth, i;
1349 pOleMenuDescriptor->bIsServerItem = FALSE;
1351 /* Don't bother searching if the popup is the combined menu itself */
1352 if ( hmenu == pOleMenuDescriptor->hmenuCombined )
1353 return FALSE;
1355 /* Find the menu item index in the shared OLE menu that this item belongs to */
1356 if ( !OLEMenu_FindMainMenuIndex( pOleMenuDescriptor->hmenuCombined, hmenu, &nPos ) )
1357 return FALSE;
1359 /* The group widths array has counts for the number of elements
1360 * in the groups File, Edit, Container, Object, Window, Help.
1361 * The Edit, Object & Help groups belong to the server object
1362 * and the other three belong to the container.
1363 * Loop through the group widths and locate the group we are a member of.
1365 for ( i = 0, nWidth = 0; i < 6; i++ )
1367 nWidth += pOleMenuDescriptor->mgw.width[i];
1368 if ( nPos < nWidth )
1370 /* Odd elements are server menu widths */
1371 pOleMenuDescriptor->bIsServerItem = (i%2) ? TRUE : FALSE;
1372 break;
1376 return pOleMenuDescriptor->bIsServerItem;
1379 /*************************************************************************
1380 * OLEMenu_CallWndProc
1381 * Thread scope WH_CALLWNDPROC hook proc filter function (callback)
1382 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1384 static LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam)
1386 LPCWPSTRUCT pMsg = NULL;
1387 HOLEMENU hOleMenu = 0;
1388 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1389 OleMenuHookItem *pHookItem = NULL;
1390 WORD fuFlags;
1392 TRACE("%i, %04x, %08x\n", code, wParam, (unsigned)lParam );
1394 /* Check if we're being asked to process the message */
1395 if ( HC_ACTION != code )
1396 goto NEXTHOOK;
1398 /* Retrieve the current message being dispatched from lParam */
1399 pMsg = (LPCWPSTRUCT)lParam;
1401 /* Check if the message is destined for a window we are interested in:
1402 * If the window has an OLEMenu property we may need to dispatch
1403 * the menu message to its active objects window instead. */
1405 hOleMenu = (HOLEMENU)GetPropA( pMsg->hwnd, "PROP_OLEMenuDescriptor" );
1406 if ( !hOleMenu )
1407 goto NEXTHOOK;
1409 /* Get the menu descriptor */
1410 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1411 if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1412 goto NEXTHOOK;
1414 /* Process menu messages */
1415 switch( pMsg->message )
1417 case WM_INITMENU:
1419 /* Reset the menu descriptor state */
1420 pOleMenuDescriptor->bIsServerItem = FALSE;
1422 /* Send this message to the server as well */
1423 SendMessageA( pOleMenuDescriptor->hwndActiveObject,
1424 pMsg->message, pMsg->wParam, pMsg->lParam );
1425 goto NEXTHOOK;
1428 case WM_INITMENUPOPUP:
1430 /* Save the state for whether this is a server owned menu */
1431 OLEMenu_SetIsServerMenu( (HMENU)pMsg->wParam, pOleMenuDescriptor );
1432 break;
1435 case WM_MENUSELECT:
1437 fuFlags = HIWORD(pMsg->wParam); /* Get flags */
1438 if ( fuFlags & MF_SYSMENU )
1439 goto NEXTHOOK;
1441 /* Save the state for whether this is a server owned popup menu */
1442 else if ( fuFlags & MF_POPUP )
1443 OLEMenu_SetIsServerMenu( (HMENU)pMsg->lParam, pOleMenuDescriptor );
1445 break;
1448 case WM_DRAWITEM:
1450 LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT) pMsg->lParam;
1451 if ( pMsg->wParam != 0 || lpdis->CtlType != ODT_MENU )
1452 goto NEXTHOOK; /* Not a menu message */
1454 break;
1457 default:
1458 goto NEXTHOOK;
1461 /* If the message was for the server dispatch it accordingly */
1462 if ( pOleMenuDescriptor->bIsServerItem )
1464 SendMessageA( pOleMenuDescriptor->hwndActiveObject,
1465 pMsg->message, pMsg->wParam, pMsg->lParam );
1468 NEXTHOOK:
1469 if ( pOleMenuDescriptor )
1470 GlobalUnlock( hOleMenu );
1472 /* Lookup the hook item for the current thread */
1473 if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1475 /* This should never fail!! */
1476 WARN("could not retrieve hHook for current thread!\n" );
1477 return 0;
1480 /* Pass on the message to the next hooker */
1481 return CallNextHookEx( pHookItem->CallWndProc_hHook, code, wParam, lParam );
1484 /*************************************************************************
1485 * OLEMenu_GetMsgProc
1486 * Thread scope WH_GETMESSAGE hook proc filter function (callback)
1487 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1489 static LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam)
1491 LPMSG pMsg = NULL;
1492 HOLEMENU hOleMenu = 0;
1493 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1494 OleMenuHookItem *pHookItem = NULL;
1495 WORD wCode;
1497 TRACE("%i, %04x, %08x\n", code, wParam, (unsigned)lParam );
1499 /* Check if we're being asked to process a messages */
1500 if ( HC_ACTION != code )
1501 goto NEXTHOOK;
1503 /* Retrieve the current message being dispatched from lParam */
1504 pMsg = (LPMSG)lParam;
1506 /* Check if the message is destined for a window we are interested in:
1507 * If the window has an OLEMenu property we may need to dispatch
1508 * the menu message to its active objects window instead. */
1510 hOleMenu = (HOLEMENU)GetPropA( pMsg->hwnd, "PROP_OLEMenuDescriptor" );
1511 if ( !hOleMenu )
1512 goto NEXTHOOK;
1514 /* Process menu messages */
1515 switch( pMsg->message )
1517 case WM_COMMAND:
1519 wCode = HIWORD(pMsg->wParam); /* Get notification code */
1520 if ( wCode )
1521 goto NEXTHOOK; /* Not a menu message */
1522 break;
1524 default:
1525 goto NEXTHOOK;
1528 /* Get the menu descriptor */
1529 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1530 if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1531 goto NEXTHOOK;
1533 /* If the message was for the server dispatch it accordingly */
1534 if ( pOleMenuDescriptor->bIsServerItem )
1536 /* Change the hWnd in the message to the active objects hWnd.
1537 * The message loop which reads this message will automatically
1538 * dispatch it to the embedded objects window. */
1539 pMsg->hwnd = pOleMenuDescriptor->hwndActiveObject;
1542 NEXTHOOK:
1543 if ( pOleMenuDescriptor )
1544 GlobalUnlock( hOleMenu );
1546 /* Lookup the hook item for the current thread */
1547 if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1549 /* This should never fail!! */
1550 WARN("could not retrieve hHook for current thread!\n" );
1551 return FALSE;
1554 /* Pass on the message to the next hooker */
1555 return CallNextHookEx( pHookItem->GetMsg_hHook, code, wParam, lParam );
1558 /***********************************************************************
1559 * OleCreateMenuDescriptor [OLE32.@]
1560 * Creates an OLE menu descriptor for OLE to use when dispatching
1561 * menu messages and commands.
1563 * PARAMS:
1564 * hmenuCombined - Handle to the objects combined menu
1565 * lpMenuWidths - Pointer to array of 6 LONG's indicating menus per group
1568 HOLEMENU WINAPI OleCreateMenuDescriptor(
1569 HMENU hmenuCombined,
1570 LPOLEMENUGROUPWIDTHS lpMenuWidths)
1572 HOLEMENU hOleMenu;
1573 OleMenuDescriptor *pOleMenuDescriptor;
1574 int i;
1576 if ( !hmenuCombined || !lpMenuWidths )
1577 return 0;
1579 /* Create an OLE menu descriptor */
1580 if ( !(hOleMenu = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT,
1581 sizeof(OleMenuDescriptor) ) ) )
1582 return 0;
1584 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1585 if ( !pOleMenuDescriptor )
1586 return 0;
1588 /* Initialize menu group widths and hmenu */
1589 for ( i = 0; i < 6; i++ )
1590 pOleMenuDescriptor->mgw.width[i] = lpMenuWidths->width[i];
1592 pOleMenuDescriptor->hmenuCombined = hmenuCombined;
1593 pOleMenuDescriptor->bIsServerItem = FALSE;
1594 GlobalUnlock( hOleMenu );
1596 return hOleMenu;
1599 /***********************************************************************
1600 * OleDestroyMenuDescriptor [OLE32.@]
1601 * Destroy the shared menu descriptor
1603 HRESULT WINAPI OleDestroyMenuDescriptor(
1604 HOLEMENU hmenuDescriptor)
1606 if ( hmenuDescriptor )
1607 GlobalFree( hmenuDescriptor );
1608 return S_OK;
1611 /***********************************************************************
1612 * OleSetMenuDescriptor [OLE32.@]
1613 * Installs or removes OLE dispatching code for the containers frame window.
1615 * PARAMS
1616 * hOleMenu Handle to composite menu descriptor
1617 * hwndFrame Handle to containers frame window
1618 * hwndActiveObject Handle to objects in-place activation window
1619 * lpFrame Pointer to IOleInPlaceFrame on containers window
1620 * lpActiveObject Pointer to IOleInPlaceActiveObject on active in-place object
1622 * RETURNS
1623 * S_OK - menu installed correctly
1624 * E_FAIL, E_INVALIDARG, E_UNEXPECTED - failure
1626 * FIXME
1627 * The lpFrame and lpActiveObject parameters are currently ignored
1628 * OLE should install context sensitive help F1 filtering for the app when
1629 * these are non null.
1631 HRESULT WINAPI OleSetMenuDescriptor(
1632 HOLEMENU hOleMenu,
1633 HWND hwndFrame,
1634 HWND hwndActiveObject,
1635 LPOLEINPLACEFRAME lpFrame,
1636 LPOLEINPLACEACTIVEOBJECT lpActiveObject)
1638 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1640 /* Check args */
1641 if ( !hwndFrame || (hOleMenu && !hwndActiveObject) )
1642 return E_INVALIDARG;
1644 if ( lpFrame || lpActiveObject )
1646 FIXME("(%p, %p, %p, %p, %p), Context sensitive help filtering not implemented!\n",
1647 hOleMenu,
1648 hwndFrame,
1649 hwndActiveObject,
1650 lpFrame,
1651 lpActiveObject);
1654 /* Set up a message hook to intercept the containers frame window messages.
1655 * The message filter is responsible for dispatching menu messages from the
1656 * shared menu which are intended for the object.
1659 if ( hOleMenu ) /* Want to install dispatching code */
1661 /* If OLEMenu hooks are already installed for this thread, fail
1662 * Note: This effectively means that OleSetMenuDescriptor cannot
1663 * be called twice in succession on the same frame window
1664 * without first calling it with a null hOleMenu to uninstall */
1665 if ( OLEMenu_IsHookInstalled( GetCurrentThreadId() ) )
1666 return E_FAIL;
1668 /* Get the menu descriptor */
1669 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1670 if ( !pOleMenuDescriptor )
1671 return E_UNEXPECTED;
1673 /* Update the menu descriptor */
1674 pOleMenuDescriptor->hwndFrame = hwndFrame;
1675 pOleMenuDescriptor->hwndActiveObject = hwndActiveObject;
1677 GlobalUnlock( hOleMenu );
1678 pOleMenuDescriptor = NULL;
1680 /* Add a menu descriptor windows property to the frame window */
1681 SetPropA( hwndFrame, "PROP_OLEMenuDescriptor", hOleMenu );
1683 /* Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC */
1684 if ( !OLEMenu_InstallHooks( GetCurrentThreadId() ) )
1685 return E_FAIL;
1687 else /* Want to uninstall dispatching code */
1689 /* Uninstall the hooks */
1690 if ( !OLEMenu_UnInstallHooks( GetCurrentThreadId() ) )
1691 return E_FAIL;
1693 /* Remove the menu descriptor property from the frame window */
1694 RemovePropA( hwndFrame, "PROP_OLEMenuDescriptor" );
1697 return S_OK;
1700 /******************************************************************************
1701 * IsAccelerator [OLE32.@]
1702 * Mostly copied from controls/menu.c TranslateAccelerator implementation
1704 BOOL WINAPI IsAccelerator(HACCEL hAccel, int cAccelEntries, LPMSG lpMsg, WORD* lpwCmd)
1706 LPACCEL lpAccelTbl;
1707 int i;
1709 if(!lpMsg) return FALSE;
1710 if (!hAccel)
1712 WARN_(accel)("NULL accel handle\n");
1713 return FALSE;
1715 if((lpMsg->message != WM_KEYDOWN &&
1716 lpMsg->message != WM_SYSKEYDOWN &&
1717 lpMsg->message != WM_SYSCHAR &&
1718 lpMsg->message != WM_CHAR)) return FALSE;
1719 lpAccelTbl = HeapAlloc(GetProcessHeap(), 0, cAccelEntries * sizeof(ACCEL));
1720 if (NULL == lpAccelTbl)
1722 return FALSE;
1724 if (CopyAcceleratorTableW(hAccel, lpAccelTbl, cAccelEntries) != cAccelEntries)
1726 WARN_(accel)("CopyAcceleratorTableW failed\n");
1727 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
1728 return FALSE;
1731 TRACE_(accel)("hAccel=%p, cAccelEntries=%d,"
1732 "msg->hwnd=%p, msg->message=%04x, wParam=%08x, lParam=%08lx\n",
1733 hAccel, cAccelEntries,
1734 lpMsg->hwnd, lpMsg->message, lpMsg->wParam, lpMsg->lParam);
1735 for(i = 0; i < cAccelEntries; i++)
1737 if(lpAccelTbl[i].key != lpMsg->wParam)
1738 continue;
1740 if(lpMsg->message == WM_CHAR)
1742 if(!(lpAccelTbl[i].fVirt & FALT) && !(lpAccelTbl[i].fVirt & FVIRTKEY))
1744 TRACE_(accel)("found accel for WM_CHAR: ('%c')\n", lpMsg->wParam & 0xff);
1745 goto found;
1748 else
1750 if(lpAccelTbl[i].fVirt & FVIRTKEY)
1752 INT mask = 0;
1753 TRACE_(accel)("found accel for virt_key %04x (scan %04x)\n",
1754 lpMsg->wParam, HIWORD(lpMsg->lParam) & 0xff);
1755 if(GetKeyState(VK_SHIFT) & 0x8000) mask |= FSHIFT;
1756 if(GetKeyState(VK_CONTROL) & 0x8000) mask |= FCONTROL;
1757 if(GetKeyState(VK_MENU) & 0x8000) mask |= FALT;
1758 if(mask == (lpAccelTbl[i].fVirt & (FSHIFT | FCONTROL | FALT))) goto found;
1759 TRACE_(accel)("incorrect SHIFT/CTRL/ALT-state\n");
1761 else
1763 if(!(lpMsg->lParam & 0x01000000)) /* no special_key */
1765 if((lpAccelTbl[i].fVirt & FALT) && (lpMsg->lParam & 0x20000000))
1766 { /* ^^ ALT pressed */
1767 TRACE_(accel)("found accel for Alt-%c\n", lpMsg->wParam & 0xff);
1768 goto found;
1775 WARN_(accel)("couldn't translate accelerator key\n");
1776 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
1777 return FALSE;
1779 found:
1780 if(lpwCmd) *lpwCmd = lpAccelTbl[i].cmd;
1781 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
1782 return TRUE;
1785 /***********************************************************************
1786 * ReleaseStgMedium [OLE32.@]
1788 void WINAPI ReleaseStgMedium(
1789 STGMEDIUM* pmedium)
1791 switch (pmedium->tymed)
1793 case TYMED_HGLOBAL:
1795 if ( (pmedium->pUnkForRelease==0) &&
1796 (pmedium->u.hGlobal!=0) )
1797 GlobalFree(pmedium->u.hGlobal);
1798 break;
1800 case TYMED_FILE:
1802 if (pmedium->u.lpszFileName!=0)
1804 if (pmedium->pUnkForRelease==0)
1806 DeleteFileW(pmedium->u.lpszFileName);
1809 CoTaskMemFree(pmedium->u.lpszFileName);
1811 break;
1813 case TYMED_ISTREAM:
1815 if (pmedium->u.pstm!=0)
1817 IStream_Release(pmedium->u.pstm);
1819 break;
1821 case TYMED_ISTORAGE:
1823 if (pmedium->u.pstg!=0)
1825 IStorage_Release(pmedium->u.pstg);
1827 break;
1829 case TYMED_GDI:
1831 if ( (pmedium->pUnkForRelease==0) &&
1832 (pmedium->u.hBitmap!=0) )
1833 DeleteObject(pmedium->u.hBitmap);
1834 break;
1836 case TYMED_MFPICT:
1838 if ( (pmedium->pUnkForRelease==0) &&
1839 (pmedium->u.hMetaFilePict!=0) )
1841 LPMETAFILEPICT pMP = GlobalLock(pmedium->u.hMetaFilePict);
1842 DeleteMetaFile(pMP->hMF);
1843 GlobalUnlock(pmedium->u.hMetaFilePict);
1844 GlobalFree(pmedium->u.hMetaFilePict);
1846 break;
1848 case TYMED_ENHMF:
1850 if ( (pmedium->pUnkForRelease==0) &&
1851 (pmedium->u.hEnhMetaFile!=0) )
1853 DeleteEnhMetaFile(pmedium->u.hEnhMetaFile);
1855 break;
1857 case TYMED_NULL:
1858 default:
1859 break;
1861 pmedium->tymed=TYMED_NULL;
1864 * After cleaning up, the unknown is released
1866 if (pmedium->pUnkForRelease!=0)
1868 IUnknown_Release(pmedium->pUnkForRelease);
1869 pmedium->pUnkForRelease = 0;
1873 /***
1874 * OLEDD_Initialize()
1876 * Initializes the OLE drag and drop data structures.
1878 static void OLEDD_Initialize(void)
1880 WNDCLASSA wndClass;
1882 ZeroMemory (&wndClass, sizeof(WNDCLASSA));
1883 wndClass.style = CS_GLOBALCLASS;
1884 wndClass.lpfnWndProc = OLEDD_DragTrackerWindowProc;
1885 wndClass.cbClsExtra = 0;
1886 wndClass.cbWndExtra = sizeof(TrackerWindowInfo*);
1887 wndClass.hCursor = 0;
1888 wndClass.hbrBackground = 0;
1889 wndClass.lpszClassName = OLEDD_DRAGTRACKERCLASS;
1891 RegisterClassA (&wndClass);
1894 /***
1895 * OLEDD_FreeDropTarget()
1897 * Frees the drag and drop data structure
1899 static void OLEDD_FreeDropTarget(DropTargetNode *dropTargetInfo)
1901 list_remove(&dropTargetInfo->entry);
1902 IDropTarget_Release(dropTargetInfo->dropTarget);
1903 HeapFree(GetProcessHeap(), 0, dropTargetInfo);
1906 /***
1907 * OLEDD_UnInitialize()
1909 * Releases the OLE drag and drop data structures.
1911 void OLEDD_UnInitialize(void)
1914 * Simply empty the list.
1916 while (!list_empty(&targetListHead))
1918 DropTargetNode* curNode;
1919 curNode = LIST_ENTRY(list_head(&targetListHead), DropTargetNode, entry);
1920 OLEDD_FreeDropTarget(curNode);
1924 /***
1925 * OLEDD_FindDropTarget()
1927 * Finds information about the drop target.
1929 static DropTargetNode* OLEDD_FindDropTarget(HWND hwndOfTarget)
1931 DropTargetNode* curNode;
1934 * Iterate the list to find the HWND value.
1936 LIST_FOR_EACH_ENTRY(curNode, &targetListHead, DropTargetNode, entry)
1937 if (hwndOfTarget==curNode->hwndTarget)
1938 return curNode;
1941 * If we get here, the item is not in the list
1943 return NULL;
1946 /***
1947 * OLEDD_DragTrackerWindowProc()
1949 * This method is the WindowProcedure of the drag n drop tracking
1950 * window. During a drag n Drop operation, an invisible window is created
1951 * to receive the user input and act upon it. This procedure is in charge
1952 * of this behavior.
1955 #define DRAG_TIMER_ID 1
1957 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(
1958 HWND hwnd,
1959 UINT uMsg,
1960 WPARAM wParam,
1961 LPARAM lParam)
1963 switch (uMsg)
1965 case WM_CREATE:
1967 LPCREATESTRUCTA createStruct = (LPCREATESTRUCTA)lParam;
1969 SetWindowLongA(hwnd, 0, (LONG)createStruct->lpCreateParams);
1970 SetTimer(hwnd, DRAG_TIMER_ID, 50, NULL);
1972 break;
1974 case WM_TIMER:
1975 case WM_MOUSEMOVE:
1977 OLEDD_TrackMouseMove((TrackerWindowInfo*)GetWindowLongA(hwnd, 0));
1978 break;
1980 case WM_LBUTTONUP:
1981 case WM_MBUTTONUP:
1982 case WM_RBUTTONUP:
1983 case WM_LBUTTONDOWN:
1984 case WM_MBUTTONDOWN:
1985 case WM_RBUTTONDOWN:
1987 OLEDD_TrackStateChange((TrackerWindowInfo*)GetWindowLongA(hwnd, 0));
1988 break;
1990 case WM_DESTROY:
1992 KillTimer(hwnd, DRAG_TIMER_ID);
1993 break;
1998 * This is a window proc after all. Let's call the default.
2000 return DefWindowProcA (hwnd, uMsg, wParam, lParam);
2003 /***
2004 * OLEDD_TrackMouseMove()
2006 * This method is invoked while a drag and drop operation is in effect.
2007 * it will generate the appropriate callbacks in the drop source
2008 * and drop target. It will also provide the expected feedback to
2009 * the user.
2011 * params:
2012 * trackerInfo - Pointer to the structure identifying the
2013 * drag & drop operation that is currently
2014 * active.
2016 static void OLEDD_TrackMouseMove(TrackerWindowInfo* trackerInfo)
2018 HWND hwndNewTarget = 0;
2019 HRESULT hr = S_OK;
2020 POINT pt;
2023 * Get the handle of the window under the mouse
2025 pt.x = trackerInfo->curMousePos.x;
2026 pt.y = trackerInfo->curMousePos.y;
2027 hwndNewTarget = WindowFromPoint(pt);
2030 * Every time, we re-initialize the effects passed to the
2031 * IDropTarget to the effects allowed by the source.
2033 *trackerInfo->pdwEffect = trackerInfo->dwOKEffect;
2036 * If we are hovering over the same target as before, send the
2037 * DragOver notification
2039 if ( (trackerInfo->curDragTarget != 0) &&
2040 (trackerInfo->curTargetHWND == hwndNewTarget) )
2042 IDropTarget_DragOver(trackerInfo->curDragTarget,
2043 trackerInfo->dwKeyState,
2044 trackerInfo->curMousePos,
2045 trackerInfo->pdwEffect);
2047 else
2049 DropTargetNode* newDropTargetNode = 0;
2052 * If we changed window, we have to notify our old target and check for
2053 * the new one.
2055 if (trackerInfo->curDragTarget!=0)
2057 IDropTarget_DragLeave(trackerInfo->curDragTarget);
2061 * Make sure we're hovering over a window.
2063 if (hwndNewTarget!=0)
2066 * Find-out if there is a drag target under the mouse
2068 HWND nexttar = hwndNewTarget;
2069 trackerInfo->curTargetHWND = hwndNewTarget;
2071 do {
2072 newDropTargetNode = OLEDD_FindDropTarget(nexttar);
2073 } while (!newDropTargetNode && (nexttar = GetParent(nexttar)) != 0);
2074 if(nexttar) hwndNewTarget = nexttar;
2076 trackerInfo->curDragTargetHWND = hwndNewTarget;
2077 trackerInfo->curDragTarget = newDropTargetNode ? newDropTargetNode->dropTarget : 0;
2080 * If there is, notify it that we just dragged-in
2082 if (trackerInfo->curDragTarget!=0)
2084 IDropTarget_DragEnter(trackerInfo->curDragTarget,
2085 trackerInfo->dataObject,
2086 trackerInfo->dwKeyState,
2087 trackerInfo->curMousePos,
2088 trackerInfo->pdwEffect);
2091 else
2094 * The mouse is not over a window so we don't track anything.
2096 trackerInfo->curDragTargetHWND = 0;
2097 trackerInfo->curTargetHWND = 0;
2098 trackerInfo->curDragTarget = 0;
2103 * Now that we have done that, we have to tell the source to give
2104 * us feedback on the work being done by the target. If we don't
2105 * have a target, simulate no effect.
2107 if (trackerInfo->curDragTarget==0)
2109 *trackerInfo->pdwEffect = DROPEFFECT_NONE;
2112 hr = IDropSource_GiveFeedback(trackerInfo->dropSource,
2113 *trackerInfo->pdwEffect);
2116 * When we ask for feedback from the drop source, sometimes it will
2117 * do all the necessary work and sometimes it will not handle it
2118 * when that's the case, we must display the standard drag and drop
2119 * cursors.
2121 if (hr==DRAGDROP_S_USEDEFAULTCURSORS)
2123 if (*trackerInfo->pdwEffect & DROPEFFECT_MOVE)
2125 SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(1)));
2127 else if (*trackerInfo->pdwEffect & DROPEFFECT_COPY)
2129 SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(2)));
2131 else if (*trackerInfo->pdwEffect & DROPEFFECT_LINK)
2133 SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(3)));
2135 else
2137 SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(0)));
2142 /***
2143 * OLEDD_TrackStateChange()
2145 * This method is invoked while a drag and drop operation is in effect.
2146 * It is used to notify the drop target/drop source callbacks when
2147 * the state of the keyboard or mouse button change.
2149 * params:
2150 * trackerInfo - Pointer to the structure identifying the
2151 * drag & drop operation that is currently
2152 * active.
2154 static void OLEDD_TrackStateChange(TrackerWindowInfo* trackerInfo)
2157 * Ask the drop source what to do with the operation.
2159 trackerInfo->returnValue = IDropSource_QueryContinueDrag(
2160 trackerInfo->dropSource,
2161 trackerInfo->escPressed,
2162 trackerInfo->dwKeyState);
2165 * All the return valued will stop the operation except the S_OK
2166 * return value.
2168 if (trackerInfo->returnValue!=S_OK)
2171 * Make sure the message loop in DoDragDrop stops
2173 trackerInfo->trackingDone = TRUE;
2176 * Release the mouse in case the drop target decides to show a popup
2177 * or a menu or something.
2179 ReleaseCapture();
2182 * If we end-up over a target, drop the object in the target or
2183 * inform the target that the operation was cancelled.
2185 if (trackerInfo->curDragTarget!=0)
2187 switch (trackerInfo->returnValue)
2190 * If the source wants us to complete the operation, we tell
2191 * the drop target that we just dropped the object in it.
2193 case DRAGDROP_S_DROP:
2195 IDropTarget_Drop(trackerInfo->curDragTarget,
2196 trackerInfo->dataObject,
2197 trackerInfo->dwKeyState,
2198 trackerInfo->curMousePos,
2199 trackerInfo->pdwEffect);
2200 break;
2203 * If the source told us that we should cancel, fool the drop
2204 * target by telling it that the mouse left it's window.
2205 * Also set the drop effect to "NONE" in case the application
2206 * ignores the result of DoDragDrop.
2208 case DRAGDROP_S_CANCEL:
2209 IDropTarget_DragLeave(trackerInfo->curDragTarget);
2210 *trackerInfo->pdwEffect = DROPEFFECT_NONE;
2211 break;
2217 /***
2218 * OLEDD_GetButtonState()
2220 * This method will use the current state of the keyboard to build
2221 * a button state mask equivalent to the one passed in the
2222 * WM_MOUSEMOVE wParam.
2224 static DWORD OLEDD_GetButtonState(void)
2226 BYTE keyboardState[256];
2227 DWORD keyMask = 0;
2229 GetKeyboardState(keyboardState);
2231 if ( (keyboardState[VK_SHIFT] & 0x80) !=0)
2232 keyMask |= MK_SHIFT;
2234 if ( (keyboardState[VK_CONTROL] & 0x80) !=0)
2235 keyMask |= MK_CONTROL;
2237 if ( (keyboardState[VK_LBUTTON] & 0x80) !=0)
2238 keyMask |= MK_LBUTTON;
2240 if ( (keyboardState[VK_RBUTTON] & 0x80) !=0)
2241 keyMask |= MK_RBUTTON;
2243 if ( (keyboardState[VK_MBUTTON] & 0x80) !=0)
2244 keyMask |= MK_MBUTTON;
2246 return keyMask;
2249 /***
2250 * OLEDD_GetButtonState()
2252 * This method will read the default value of the registry key in
2253 * parameter and extract a DWORD value from it. The registry key value
2254 * can be in a string key or a DWORD key.
2256 * params:
2257 * regKey - Key to read the default value from
2258 * pdwValue - Pointer to the location where the DWORD
2259 * value is returned. This value is not modified
2260 * if the value is not found.
2263 static void OLEUTL_ReadRegistryDWORDValue(
2264 HKEY regKey,
2265 DWORD* pdwValue)
2267 char buffer[20];
2268 DWORD dwKeyType;
2269 DWORD cbData = 20;
2270 LONG lres;
2272 lres = RegQueryValueExA(regKey,
2274 NULL,
2275 &dwKeyType,
2276 (LPBYTE)buffer,
2277 &cbData);
2279 if (lres==ERROR_SUCCESS)
2281 switch (dwKeyType)
2283 case REG_DWORD:
2284 *pdwValue = *(DWORD*)buffer;
2285 break;
2286 case REG_EXPAND_SZ:
2287 case REG_MULTI_SZ:
2288 case REG_SZ:
2289 *pdwValue = (DWORD)strtoul(buffer, NULL, 10);
2290 break;
2295 /******************************************************************************
2296 * OleDraw (OLE32.@)
2298 * The operation of this function is documented literally in the WinAPI
2299 * documentation to involve a QueryInterface for the IViewObject interface,
2300 * followed by a call to IViewObject::Draw.
2302 HRESULT WINAPI OleDraw(
2303 IUnknown *pUnk,
2304 DWORD dwAspect,
2305 HDC hdcDraw,
2306 LPCRECT lprcBounds)
2308 HRESULT hres;
2309 IViewObject *viewobject;
2311 hres = IUnknown_QueryInterface(pUnk,
2312 &IID_IViewObject,
2313 (void**)&viewobject);
2315 if (SUCCEEDED(hres))
2317 RECTL rectl;
2319 rectl.left = lprcBounds->left;
2320 rectl.right = lprcBounds->right;
2321 rectl.top = lprcBounds->top;
2322 rectl.bottom = lprcBounds->bottom;
2323 hres = IViewObject_Draw(viewobject, dwAspect, -1, 0, 0, 0, hdcDraw, &rectl, 0, 0, 0);
2325 IViewObject_Release(viewobject);
2326 return hres;
2328 else
2330 return DV_E_NOIVIEWOBJECT;
2334 /***********************************************************************
2335 * OleTranslateAccelerator [OLE32.@]
2337 HRESULT WINAPI OleTranslateAccelerator (LPOLEINPLACEFRAME lpFrame,
2338 LPOLEINPLACEFRAMEINFO lpFrameInfo, LPMSG lpmsg)
2340 WORD wID;
2342 TRACE("(%p,%p,%p)\n", lpFrame, lpFrameInfo, lpmsg);
2344 if (IsAccelerator(lpFrameInfo->haccel,lpFrameInfo->cAccelEntries,lpmsg,&wID))
2345 return IOleInPlaceFrame_TranslateAccelerator(lpFrame,lpmsg,wID);
2347 return S_FALSE;
2350 /******************************************************************************
2351 * OleCreate [OLE32.@]
2354 HRESULT WINAPI OleCreate(
2355 REFCLSID rclsid,
2356 REFIID riid,
2357 DWORD renderopt,
2358 LPFORMATETC pFormatEtc,
2359 LPOLECLIENTSITE pClientSite,
2360 LPSTORAGE pStg,
2361 LPVOID* ppvObj)
2363 HRESULT hres;
2364 IUnknown * pUnk = NULL;
2365 IOleObject *pOleObject = NULL;
2367 TRACE("(%s, %s, %d, %p, %p, %p, %p)\n", debugstr_guid(rclsid),
2368 debugstr_guid(riid), renderopt, pFormatEtc, pClientSite, pStg, ppvObj);
2370 hres = CoCreateInstance(rclsid, 0, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER, riid, (LPVOID*)&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;