libwine: Set install name to an absolute path for the installed library on Mac OS.
[wine/wine64.git] / dlls / ole32 / ole2.c
blob3ae9f550ae0c50f75bb00174f77c87ab1f57586b
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 "ole2.h"
45 #include "ole2ver.h"
47 #include "wine/unicode.h"
48 #include "compobj_private.h"
49 #include "wine/list.h"
51 #include "wine/debug.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(ole);
54 WINE_DECLARE_DEBUG_CHANNEL(accel);
56 /******************************************************************************
57 * These are static/global variables and internal data structures that the
58 * OLE module uses to maintain it's state.
60 typedef struct tagDropTargetNode
62 HWND hwndTarget;
63 IDropTarget* dropTarget;
64 struct list entry;
65 } DropTargetNode;
67 typedef struct tagTrackerWindowInfo
69 IDataObject* dataObject;
70 IDropSource* dropSource;
71 DWORD dwOKEffect;
72 DWORD* pdwEffect;
73 BOOL trackingDone;
74 HRESULT returnValue;
76 BOOL escPressed;
77 HWND curTargetHWND; /* window the mouse is hovering over */
78 HWND curDragTargetHWND; /* might be a ancestor of curTargetHWND */
79 IDropTarget* curDragTarget;
80 POINTL curMousePos; /* current position of the mouse in screen coordinates */
81 DWORD dwKeyState; /* current state of the shift and ctrl keys and the mouse buttons */
82 } TrackerWindowInfo;
84 typedef struct tagOleMenuDescriptor /* OleMenuDescriptor */
86 HWND hwndFrame; /* The containers frame window */
87 HWND hwndActiveObject; /* The active objects window */
88 OLEMENUGROUPWIDTHS mgw; /* OLE menu group widths for the shared menu */
89 HMENU hmenuCombined; /* The combined menu */
90 BOOL bIsServerItem; /* True if the currently open popup belongs to the server */
91 } OleMenuDescriptor;
93 typedef struct tagOleMenuHookItem /* OleMenu hook item in per thread hook list */
95 DWORD tid; /* Thread Id */
96 HANDLE hHeap; /* Heap this is allocated from */
97 HHOOK GetMsg_hHook; /* message hook for WH_GETMESSAGE */
98 HHOOK CallWndProc_hHook; /* message hook for WH_CALLWNDPROC */
99 struct tagOleMenuHookItem *next;
100 } OleMenuHookItem;
102 static OleMenuHookItem *hook_list;
105 * This is the lock count on the OLE library. It is controlled by the
106 * OLEInitialize/OLEUninitialize methods.
108 static LONG OLE_moduleLockCount = 0;
111 * Name of our registered window class.
113 static const char OLEDD_DRAGTRACKERCLASS[] = "WineDragDropTracker32";
116 * This is the head of the Drop target container.
118 static struct list targetListHead = LIST_INIT(targetListHead);
120 /******************************************************************************
121 * These are the prototypes of miscelaneous utility methods
123 static void OLEUTL_ReadRegistryDWORDValue(HKEY regKey, DWORD* pdwValue);
125 /******************************************************************************
126 * These are the prototypes of the utility methods used to manage a shared menu
128 static void OLEMenu_Initialize(void);
129 static void OLEMenu_UnInitialize(void);
130 static BOOL OLEMenu_InstallHooks( DWORD tid );
131 static BOOL OLEMenu_UnInstallHooks( DWORD tid );
132 static OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid );
133 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos );
134 static BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor );
135 static LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam);
136 static LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam);
138 /******************************************************************************
139 * These are the prototypes of the OLE Clipboard initialization methods (in clipboard.c)
141 extern void OLEClipbrd_UnInitialize(void);
142 extern void OLEClipbrd_Initialize(void);
144 /******************************************************************************
145 * These are the prototypes of the utility methods used for OLE Drag n Drop
147 static void OLEDD_Initialize(void);
148 static DropTargetNode* OLEDD_FindDropTarget(
149 HWND hwndOfTarget);
150 static void OLEDD_FreeDropTarget(DropTargetNode*, BOOL);
151 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(
152 HWND hwnd,
153 UINT uMsg,
154 WPARAM wParam,
155 LPARAM lParam);
156 static void OLEDD_TrackMouseMove(
157 TrackerWindowInfo* trackerInfo);
158 static void OLEDD_TrackStateChange(
159 TrackerWindowInfo* trackerInfo);
160 static DWORD OLEDD_GetButtonState(void);
163 /******************************************************************************
164 * OleBuildVersion [OLE2.1]
165 * OleBuildVersion [OLE32.@]
167 DWORD WINAPI OleBuildVersion(void)
169 TRACE("Returning version %d, build %d.\n", rmm, rup);
170 return (rmm<<16)+rup;
173 /***********************************************************************
174 * OleInitialize (OLE2.2)
175 * OleInitialize (OLE32.@)
177 HRESULT WINAPI OleInitialize(LPVOID reserved)
179 HRESULT hr;
181 TRACE("(%p)\n", reserved);
184 * The first duty of the OleInitialize is to initialize the COM libraries.
186 hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
189 * If the CoInitializeEx call failed, the OLE libraries can't be
190 * initialized.
192 if (FAILED(hr))
193 return hr;
196 * Then, it has to initialize the OLE specific modules.
197 * This includes:
198 * Clipboard
199 * Drag and Drop
200 * Object linking and Embedding
201 * In-place activation
203 if (!COM_CurrentInfo()->ole_inits++ &&
204 InterlockedIncrement(&OLE_moduleLockCount) == 1)
207 * Initialize the libraries.
209 TRACE("() - Initializing the OLE libraries\n");
212 * OLE Clipboard
214 OLEClipbrd_Initialize();
217 * Drag and Drop
219 OLEDD_Initialize();
222 * OLE shared menu
224 OLEMenu_Initialize();
227 return hr;
230 /******************************************************************************
231 * OleUninitialize [OLE2.3]
232 * OleUninitialize [OLE32.@]
234 void WINAPI OleUninitialize(void)
236 TRACE("()\n");
239 * If we hit the bottom of the lock stack, free the libraries.
241 if (!--COM_CurrentInfo()->ole_inits && !InterlockedDecrement(&OLE_moduleLockCount))
244 * Actually free the libraries.
246 TRACE("() - Freeing the last reference count\n");
249 * OLE Clipboard
251 OLEClipbrd_UnInitialize();
254 * OLE shared menu
256 OLEMenu_UnInitialize();
260 * Then, uninitialize the COM libraries.
262 CoUninitialize();
265 /******************************************************************************
266 * OleInitializeWOW [OLE32.@]
268 HRESULT WINAPI OleInitializeWOW(DWORD x, DWORD y) {
269 FIXME("(0x%08x, 0x%08x),stub!\n",x, y);
270 return 0;
273 /***********************************************************************
274 * RegisterDragDrop (OLE32.@)
276 HRESULT WINAPI RegisterDragDrop(
277 HWND hwnd,
278 LPDROPTARGET pDropTarget)
280 DropTargetNode* dropTargetInfo;
282 TRACE("(%p,%p)\n", hwnd, pDropTarget);
284 if (!COM_CurrentApt())
286 ERR("COM not initialized\n");
287 return CO_E_NOTINITIALIZED;
290 if (!pDropTarget)
291 return E_INVALIDARG;
293 if (!IsWindow(hwnd))
295 ERR("invalid hwnd %p\n", hwnd);
296 return DRAGDROP_E_INVALIDHWND;
300 * First, check if the window is already registered.
302 dropTargetInfo = OLEDD_FindDropTarget(hwnd);
304 if (dropTargetInfo!=NULL)
305 return DRAGDROP_E_ALREADYREGISTERED;
308 * If it's not there, we can add it. We first create a node for it.
310 dropTargetInfo = HeapAlloc(GetProcessHeap(), 0, sizeof(DropTargetNode));
312 if (dropTargetInfo==NULL)
313 return E_OUTOFMEMORY;
315 dropTargetInfo->hwndTarget = hwnd;
318 * Don't forget that this is an interface pointer, need to nail it down since
319 * we keep a copy of it.
321 IDropTarget_AddRef(pDropTarget);
322 dropTargetInfo->dropTarget = pDropTarget;
324 list_add_tail(&targetListHead, &dropTargetInfo->entry);
326 return S_OK;
329 /***********************************************************************
330 * RevokeDragDrop (OLE32.@)
332 HRESULT WINAPI RevokeDragDrop(
333 HWND hwnd)
335 DropTargetNode* dropTargetInfo;
337 TRACE("(%p)\n", hwnd);
339 if (!IsWindow(hwnd))
341 ERR("invalid hwnd %p\n", hwnd);
342 return DRAGDROP_E_INVALIDHWND;
346 * First, check if the window is already registered.
348 dropTargetInfo = OLEDD_FindDropTarget(hwnd);
351 * If it ain't in there, it's an error.
353 if (dropTargetInfo==NULL)
354 return DRAGDROP_E_NOTREGISTERED;
356 OLEDD_FreeDropTarget(dropTargetInfo, TRUE);
358 return S_OK;
361 /***********************************************************************
362 * OleRegGetUserType (OLE32.@)
364 * This implementation of OleRegGetUserType ignores the dwFormOfType
365 * parameter and always returns the full name of the object. This is
366 * not too bad since this is the case for many objects because of the
367 * way they are registered.
369 HRESULT WINAPI OleRegGetUserType(
370 REFCLSID clsid,
371 DWORD dwFormOfType,
372 LPOLESTR* pszUserType)
374 char keyName[60];
375 DWORD dwKeyType;
376 DWORD cbData;
377 HKEY clsidKey;
378 LONG hres;
379 LPSTR buffer;
380 HRESULT retVal;
382 * Initialize the out parameter.
384 *pszUserType = NULL;
387 * Build the key name we're looking for
389 sprintf( keyName, "CLSID\\{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\",
390 clsid->Data1, clsid->Data2, clsid->Data3,
391 clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
392 clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );
394 TRACE("(%s, %d, %p)\n", keyName, dwFormOfType, pszUserType);
397 * Open the class id Key
399 hres = RegOpenKeyA(HKEY_CLASSES_ROOT,
400 keyName,
401 &clsidKey);
403 if (hres != ERROR_SUCCESS)
404 return REGDB_E_CLASSNOTREG;
407 * Retrieve the size of the name string.
409 cbData = 0;
411 hres = RegQueryValueExA(clsidKey,
413 NULL,
414 &dwKeyType,
415 NULL,
416 &cbData);
418 if (hres!=ERROR_SUCCESS)
420 RegCloseKey(clsidKey);
421 return REGDB_E_READREGDB;
425 * Allocate a buffer for the registry value.
427 *pszUserType = CoTaskMemAlloc(cbData*2);
429 if (*pszUserType==NULL)
431 RegCloseKey(clsidKey);
432 return E_OUTOFMEMORY;
435 buffer = HeapAlloc(GetProcessHeap(), 0, cbData);
437 if (buffer == NULL)
439 RegCloseKey(clsidKey);
440 CoTaskMemFree(*pszUserType);
441 *pszUserType=NULL;
442 return E_OUTOFMEMORY;
445 hres = RegQueryValueExA(clsidKey,
447 NULL,
448 &dwKeyType,
449 (LPBYTE) buffer,
450 &cbData);
452 RegCloseKey(clsidKey);
455 if (hres!=ERROR_SUCCESS)
457 CoTaskMemFree(*pszUserType);
458 *pszUserType=NULL;
460 retVal = REGDB_E_READREGDB;
462 else
464 MultiByteToWideChar( CP_ACP, 0, buffer, -1, *pszUserType, cbData /*FIXME*/ );
465 retVal = S_OK;
467 HeapFree(GetProcessHeap(), 0, buffer);
469 return retVal;
472 /***********************************************************************
473 * DoDragDrop [OLE32.@]
475 HRESULT WINAPI DoDragDrop (
476 IDataObject *pDataObject, /* [in] ptr to the data obj */
477 IDropSource* pDropSource, /* [in] ptr to the source obj */
478 DWORD dwOKEffect, /* [in] effects allowed by the source */
479 DWORD *pdwEffect) /* [out] ptr to effects of the source */
481 TrackerWindowInfo trackerInfo;
482 HWND hwndTrackWindow;
483 MSG msg;
485 TRACE("(DataObject %p, DropSource %p)\n", pDataObject, pDropSource);
488 * Setup the drag n drop tracking window.
490 if (!IsValidInterface((LPUNKNOWN)pDropSource))
491 return E_INVALIDARG;
493 trackerInfo.dataObject = pDataObject;
494 trackerInfo.dropSource = pDropSource;
495 trackerInfo.dwOKEffect = dwOKEffect;
496 trackerInfo.pdwEffect = pdwEffect;
497 trackerInfo.trackingDone = FALSE;
498 trackerInfo.escPressed = FALSE;
499 trackerInfo.curDragTargetHWND = 0;
500 trackerInfo.curTargetHWND = 0;
501 trackerInfo.curDragTarget = 0;
503 hwndTrackWindow = CreateWindowA(OLEDD_DRAGTRACKERCLASS,
504 "TrackerWindow",
505 WS_POPUP,
506 CW_USEDEFAULT, CW_USEDEFAULT,
507 CW_USEDEFAULT, CW_USEDEFAULT,
511 (LPVOID)&trackerInfo);
513 if (hwndTrackWindow!=0)
516 * Capture the mouse input
518 SetCapture(hwndTrackWindow);
520 msg.message = 0;
523 * Pump messages. All mouse input should go the the capture window.
525 while (!trackerInfo.trackingDone && GetMessageA(&msg, 0, 0, 0) )
527 trackerInfo.curMousePos.x = msg.pt.x;
528 trackerInfo.curMousePos.y = msg.pt.y;
529 trackerInfo.dwKeyState = OLEDD_GetButtonState();
531 if ( (msg.message >= WM_KEYFIRST) &&
532 (msg.message <= WM_KEYLAST) )
535 * When keyboard messages are sent to windows on this thread, we
536 * want to ignore notify the drop source that the state changed.
537 * in the case of the Escape key, we also notify the drop source
538 * we give it a special meaning.
540 if ( (msg.message==WM_KEYDOWN) &&
541 (msg.wParam==VK_ESCAPE) )
543 trackerInfo.escPressed = TRUE;
547 * Notify the drop source.
549 OLEDD_TrackStateChange(&trackerInfo);
551 else
554 * Dispatch the messages only when it's not a keyboard message.
556 DispatchMessageA(&msg);
560 /* re-post the quit message to outer message loop */
561 if (msg.message == WM_QUIT)
562 PostQuitMessage(msg.wParam);
564 * Destroy the temporary window.
566 DestroyWindow(hwndTrackWindow);
568 return trackerInfo.returnValue;
571 return E_FAIL;
574 /***********************************************************************
575 * OleQueryLinkFromData [OLE32.@]
577 HRESULT WINAPI OleQueryLinkFromData(
578 IDataObject* pSrcDataObject)
580 FIXME("(%p),stub!\n", pSrcDataObject);
581 return S_OK;
584 /***********************************************************************
585 * OleRegGetMiscStatus [OLE32.@]
587 HRESULT WINAPI OleRegGetMiscStatus(
588 REFCLSID clsid,
589 DWORD dwAspect,
590 DWORD* pdwStatus)
592 char keyName[60];
593 HKEY clsidKey;
594 HKEY miscStatusKey;
595 HKEY aspectKey;
596 LONG result;
599 * Initialize the out parameter.
601 *pdwStatus = 0;
604 * Build the key name we're looking for
606 sprintf( keyName, "CLSID\\{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\",
607 clsid->Data1, clsid->Data2, clsid->Data3,
608 clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
609 clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );
611 TRACE("(%s, %d, %p)\n", keyName, dwAspect, pdwStatus);
614 * Open the class id Key
616 result = RegOpenKeyA(HKEY_CLASSES_ROOT,
617 keyName,
618 &clsidKey);
620 if (result != ERROR_SUCCESS)
621 return REGDB_E_CLASSNOTREG;
624 * Get the MiscStatus
626 result = RegOpenKeyA(clsidKey,
627 "MiscStatus",
628 &miscStatusKey);
631 if (result != ERROR_SUCCESS)
633 RegCloseKey(clsidKey);
634 return REGDB_E_READREGDB;
638 * Read the default value
640 OLEUTL_ReadRegistryDWORDValue(miscStatusKey, pdwStatus);
643 * Open the key specific to the requested aspect.
645 sprintf(keyName, "%d", dwAspect);
647 result = RegOpenKeyA(miscStatusKey,
648 keyName,
649 &aspectKey);
651 if (result == ERROR_SUCCESS)
653 OLEUTL_ReadRegistryDWORDValue(aspectKey, pdwStatus);
654 RegCloseKey(aspectKey);
658 * Cleanup
660 RegCloseKey(miscStatusKey);
661 RegCloseKey(clsidKey);
663 return S_OK;
666 static HRESULT EnumOLEVERB_Construct(HKEY hkeyVerb, ULONG index, IEnumOLEVERB **ppenum);
668 typedef struct
670 const IEnumOLEVERBVtbl *lpvtbl;
671 LONG ref;
673 HKEY hkeyVerb;
674 ULONG index;
675 } EnumOLEVERB;
677 static HRESULT WINAPI EnumOLEVERB_QueryInterface(
678 IEnumOLEVERB *iface, REFIID riid, void **ppv)
680 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
681 if (IsEqualIID(riid, &IID_IUnknown) ||
682 IsEqualIID(riid, &IID_IEnumOLEVERB))
684 IUnknown_AddRef(iface);
685 *ppv = iface;
686 return S_OK;
688 return E_NOINTERFACE;
691 static ULONG WINAPI EnumOLEVERB_AddRef(
692 IEnumOLEVERB *iface)
694 EnumOLEVERB *This = (EnumOLEVERB *)iface;
695 TRACE("()\n");
696 return InterlockedIncrement(&This->ref);
699 static ULONG WINAPI EnumOLEVERB_Release(
700 IEnumOLEVERB *iface)
702 EnumOLEVERB *This = (EnumOLEVERB *)iface;
703 LONG refs = InterlockedDecrement(&This->ref);
704 TRACE("()\n");
705 if (!refs)
707 RegCloseKey(This->hkeyVerb);
708 HeapFree(GetProcessHeap(), 0, This);
710 return refs;
713 static HRESULT WINAPI EnumOLEVERB_Next(
714 IEnumOLEVERB *iface, ULONG celt, LPOLEVERB rgelt,
715 ULONG *pceltFetched)
717 EnumOLEVERB *This = (EnumOLEVERB *)iface;
718 HRESULT hr = S_OK;
720 TRACE("(%d, %p, %p)\n", celt, rgelt, pceltFetched);
722 if (pceltFetched)
723 *pceltFetched = 0;
725 for (; celt; celt--, rgelt++)
727 WCHAR wszSubKey[20];
728 LONG cbData;
729 LPWSTR pwszOLEVERB;
730 LPWSTR pwszMenuFlags;
731 LPWSTR pwszAttribs;
732 LONG res = RegEnumKeyW(This->hkeyVerb, This->index, wszSubKey, sizeof(wszSubKey)/sizeof(wszSubKey[0]));
733 if (res == ERROR_NO_MORE_ITEMS)
735 hr = S_FALSE;
736 break;
738 else if (res != ERROR_SUCCESS)
740 ERR("RegEnumKeyW failed with error %d\n", res);
741 hr = REGDB_E_READREGDB;
742 break;
744 res = RegQueryValueW(This->hkeyVerb, wszSubKey, NULL, &cbData);
745 if (res != ERROR_SUCCESS)
747 ERR("RegQueryValueW failed with error %d\n", res);
748 hr = REGDB_E_READREGDB;
749 break;
751 pwszOLEVERB = CoTaskMemAlloc(cbData);
752 if (!pwszOLEVERB)
754 hr = E_OUTOFMEMORY;
755 break;
757 res = RegQueryValueW(This->hkeyVerb, wszSubKey, pwszOLEVERB, &cbData);
758 if (res != ERROR_SUCCESS)
760 ERR("RegQueryValueW failed with error %d\n", res);
761 hr = REGDB_E_READREGDB;
762 CoTaskMemFree(pwszOLEVERB);
763 break;
766 TRACE("verb string: %s\n", debugstr_w(pwszOLEVERB));
767 pwszMenuFlags = strchrW(pwszOLEVERB, ',');
768 if (!pwszMenuFlags)
770 hr = OLEOBJ_E_INVALIDVERB;
771 CoTaskMemFree(pwszOLEVERB);
772 break;
774 /* nul terminate the name string and advance to first character */
775 *pwszMenuFlags = '\0';
776 pwszMenuFlags++;
777 pwszAttribs = strchrW(pwszMenuFlags, ',');
778 if (!pwszAttribs)
780 hr = OLEOBJ_E_INVALIDVERB;
781 CoTaskMemFree(pwszOLEVERB);
782 break;
784 /* nul terminate the menu string and advance to first character */
785 *pwszAttribs = '\0';
786 pwszAttribs++;
788 /* fill out structure for this verb */
789 rgelt->lVerb = atolW(wszSubKey);
790 rgelt->lpszVerbName = pwszOLEVERB; /* user should free */
791 rgelt->fuFlags = atolW(pwszMenuFlags);
792 rgelt->grfAttribs = atolW(pwszAttribs);
794 if (pceltFetched)
795 (*pceltFetched)++;
796 This->index++;
798 return hr;
801 static HRESULT WINAPI EnumOLEVERB_Skip(
802 IEnumOLEVERB *iface, ULONG celt)
804 EnumOLEVERB *This = (EnumOLEVERB *)iface;
806 TRACE("(%d)\n", celt);
808 This->index += celt;
809 return S_OK;
812 static HRESULT WINAPI EnumOLEVERB_Reset(
813 IEnumOLEVERB *iface)
815 EnumOLEVERB *This = (EnumOLEVERB *)iface;
817 TRACE("()\n");
819 This->index = 0;
820 return S_OK;
823 static HRESULT WINAPI EnumOLEVERB_Clone(
824 IEnumOLEVERB *iface,
825 IEnumOLEVERB **ppenum)
827 EnumOLEVERB *This = (EnumOLEVERB *)iface;
828 HKEY hkeyVerb;
829 TRACE("(%p)\n", ppenum);
830 if (!DuplicateHandle(GetCurrentProcess(), This->hkeyVerb, GetCurrentProcess(), (HANDLE *)&hkeyVerb, 0, FALSE, DUPLICATE_SAME_ACCESS))
831 return HRESULT_FROM_WIN32(GetLastError());
832 return EnumOLEVERB_Construct(hkeyVerb, This->index, ppenum);
835 static const IEnumOLEVERBVtbl EnumOLEVERB_VTable =
837 EnumOLEVERB_QueryInterface,
838 EnumOLEVERB_AddRef,
839 EnumOLEVERB_Release,
840 EnumOLEVERB_Next,
841 EnumOLEVERB_Skip,
842 EnumOLEVERB_Reset,
843 EnumOLEVERB_Clone
846 static HRESULT EnumOLEVERB_Construct(HKEY hkeyVerb, ULONG index, IEnumOLEVERB **ppenum)
848 EnumOLEVERB *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
849 if (!This)
851 RegCloseKey(hkeyVerb);
852 return E_OUTOFMEMORY;
854 This->lpvtbl = &EnumOLEVERB_VTable;
855 This->ref = 1;
856 This->index = index;
857 This->hkeyVerb = hkeyVerb;
858 *ppenum = (IEnumOLEVERB *)&This->lpvtbl;
859 return S_OK;
862 /***********************************************************************
863 * OleRegEnumVerbs [OLE32.@]
865 * Enumerates verbs associated with a class stored in the registry.
867 * PARAMS
868 * clsid [I] Class ID to enumerate the verbs for.
869 * ppenum [O] Enumerator.
871 * RETURNS
872 * S_OK: Success.
873 * REGDB_E_CLASSNOTREG: The specified class does not have a key in the registry.
874 * REGDB_E_READREGDB: The class key could not be opened for some other reason.
875 * OLE_E_REGDB_KEY: The Verb subkey for the class is not present.
876 * OLEOBJ_E_NOVERBS: The Verb subkey for the class is empty.
878 HRESULT WINAPI OleRegEnumVerbs (REFCLSID clsid, LPENUMOLEVERB* ppenum)
880 LONG res;
881 HKEY hkeyVerb;
882 DWORD dwSubKeys;
883 static const WCHAR wszVerb[] = {'V','e','r','b',0};
885 TRACE("(%s, %p)\n", debugstr_guid(clsid), ppenum);
887 res = COM_OpenKeyForCLSID(clsid, wszVerb, KEY_READ, &hkeyVerb);
888 if (FAILED(res))
890 if (res == REGDB_E_CLASSNOTREG)
891 ERR("CLSID %s not registered\n", debugstr_guid(clsid));
892 else if (res == REGDB_E_KEYMISSING)
893 ERR("no Verbs key for class %s\n", debugstr_guid(clsid));
894 else
895 ERR("failed to open Verbs key for CLSID %s with error %d\n",
896 debugstr_guid(clsid), res);
897 return res;
900 res = RegQueryInfoKeyW(hkeyVerb, NULL, NULL, NULL, &dwSubKeys, NULL,
901 NULL, NULL, NULL, NULL, NULL, NULL);
902 if (res != ERROR_SUCCESS)
904 ERR("failed to get subkey count with error %d\n", GetLastError());
905 return REGDB_E_READREGDB;
908 if (!dwSubKeys)
910 WARN("class %s has no verbs\n", debugstr_guid(clsid));
911 RegCloseKey(hkeyVerb);
912 return OLEOBJ_E_NOVERBS;
915 return EnumOLEVERB_Construct(hkeyVerb, 0, ppenum);
918 /******************************************************************************
919 * OleSetContainedObject [OLE32.@]
921 HRESULT WINAPI OleSetContainedObject(
922 LPUNKNOWN pUnknown,
923 BOOL fContained)
925 IRunnableObject* runnable = NULL;
926 HRESULT hres;
928 TRACE("(%p,%x)\n", pUnknown, fContained);
930 hres = IUnknown_QueryInterface(pUnknown,
931 &IID_IRunnableObject,
932 (void**)&runnable);
934 if (SUCCEEDED(hres))
936 hres = IRunnableObject_SetContainedObject(runnable, fContained);
938 IRunnableObject_Release(runnable);
940 return hres;
943 return S_OK;
946 /******************************************************************************
947 * OleRun [OLE32.@]
949 * Set the OLE object to the running state.
951 * PARAMS
952 * pUnknown [I] OLE object to run.
954 * RETURNS
955 * Success: S_OK.
956 * Failure: Any HRESULT code.
958 HRESULT WINAPI OleRun(LPUNKNOWN pUnknown)
960 IRunnableObject *runable;
961 HRESULT hres;
963 TRACE("(%p)\n", pUnknown);
965 hres = IUnknown_QueryInterface(pUnknown, &IID_IRunnableObject, (void**)&runable);
966 if (FAILED(hres))
967 return S_OK; /* Appears to return no error. */
969 hres = IRunnableObject_Run(runable, NULL);
970 IRunnableObject_Release(runable);
971 return hres;
974 /******************************************************************************
975 * OleLoad [OLE32.@]
977 HRESULT WINAPI OleLoad(
978 LPSTORAGE pStg,
979 REFIID riid,
980 LPOLECLIENTSITE pClientSite,
981 LPVOID* ppvObj)
983 IPersistStorage* persistStorage = NULL;
984 IUnknown* pUnk;
985 IOleObject* pOleObject = NULL;
986 STATSTG storageInfo;
987 HRESULT hres;
989 TRACE("(%p, %s, %p, %p)\n", pStg, debugstr_guid(riid), pClientSite, ppvObj);
991 *ppvObj = NULL;
994 * TODO, Conversion ... OleDoAutoConvert
998 * Get the class ID for the object.
1000 hres = IStorage_Stat(pStg, &storageInfo, STATFLAG_NONAME);
1003 * Now, try and create the handler for the object
1005 hres = CoCreateInstance(&storageInfo.clsid,
1006 NULL,
1007 CLSCTX_INPROC_HANDLER|CLSCTX_INPROC_SERVER,
1008 riid,
1009 (void**)&pUnk);
1012 * If that fails, as it will most times, load the default
1013 * OLE handler.
1015 if (FAILED(hres))
1017 hres = OleCreateDefaultHandler(&storageInfo.clsid,
1018 NULL,
1019 riid,
1020 (void**)&pUnk);
1024 * If we couldn't find a handler... this is bad. Abort the whole thing.
1026 if (FAILED(hres))
1027 return hres;
1029 if (pClientSite)
1031 hres = IUnknown_QueryInterface(pUnk, &IID_IOleObject, (void **)&pOleObject);
1032 if (SUCCEEDED(hres))
1034 DWORD dwStatus;
1035 hres = IOleObject_GetMiscStatus(pOleObject, DVASPECT_CONTENT, &dwStatus);
1039 if (SUCCEEDED(hres))
1041 * Initialize the object with it's IPersistStorage interface.
1043 hres = IOleObject_QueryInterface(pUnk,
1044 &IID_IPersistStorage,
1045 (void**)&persistStorage);
1047 if (SUCCEEDED(hres))
1049 hres = IPersistStorage_Load(persistStorage, pStg);
1051 IPersistStorage_Release(persistStorage);
1052 persistStorage = NULL;
1055 if (SUCCEEDED(hres) && pClientSite)
1057 * Inform the new object of it's client site.
1059 hres = IOleObject_SetClientSite(pOleObject, pClientSite);
1062 * Cleanup interfaces used internally
1064 if (pOleObject)
1065 IOleObject_Release(pOleObject);
1067 if (SUCCEEDED(hres))
1069 IOleLink *pOleLink;
1070 HRESULT hres1;
1071 hres1 = IUnknown_QueryInterface(pUnk, &IID_IOleLink, (void **)&pOleLink);
1072 if (SUCCEEDED(hres1))
1074 FIXME("handle OLE link\n");
1075 IOleLink_Release(pOleLink);
1079 if (FAILED(hres))
1081 IUnknown_Release(pUnk);
1082 pUnk = NULL;
1085 *ppvObj = pUnk;
1087 return hres;
1090 /***********************************************************************
1091 * OleSave [OLE32.@]
1093 HRESULT WINAPI OleSave(
1094 LPPERSISTSTORAGE pPS,
1095 LPSTORAGE pStg,
1096 BOOL fSameAsLoad)
1098 HRESULT hres;
1099 CLSID objectClass;
1101 TRACE("(%p,%p,%x)\n", pPS, pStg, fSameAsLoad);
1104 * First, we transfer the class ID (if available)
1106 hres = IPersistStorage_GetClassID(pPS, &objectClass);
1108 if (SUCCEEDED(hres))
1110 WriteClassStg(pStg, &objectClass);
1114 * Then, we ask the object to save itself to the
1115 * storage. If it is successful, we commit the storage.
1117 hres = IPersistStorage_Save(pPS, pStg, fSameAsLoad);
1119 if (SUCCEEDED(hres))
1121 IStorage_Commit(pStg,
1122 STGC_DEFAULT);
1125 return hres;
1129 /******************************************************************************
1130 * OleLockRunning [OLE32.@]
1132 HRESULT WINAPI OleLockRunning(LPUNKNOWN pUnknown, BOOL fLock, BOOL fLastUnlockCloses)
1134 IRunnableObject* runnable = NULL;
1135 HRESULT hres;
1137 TRACE("(%p,%x,%x)\n", pUnknown, fLock, fLastUnlockCloses);
1139 hres = IUnknown_QueryInterface(pUnknown,
1140 &IID_IRunnableObject,
1141 (void**)&runnable);
1143 if (SUCCEEDED(hres))
1145 hres = IRunnableObject_LockRunning(runnable, fLock, fLastUnlockCloses);
1147 IRunnableObject_Release(runnable);
1149 return hres;
1151 else
1152 return E_INVALIDARG;
1156 /**************************************************************************
1157 * Internal methods to manage the shared OLE menu in response to the
1158 * OLE***MenuDescriptor API
1161 /***
1162 * OLEMenu_Initialize()
1164 * Initializes the OLEMENU data structures.
1166 static void OLEMenu_Initialize(void)
1170 /***
1171 * OLEMenu_UnInitialize()
1173 * Releases the OLEMENU data structures.
1175 static void OLEMenu_UnInitialize(void)
1179 /*************************************************************************
1180 * OLEMenu_InstallHooks
1181 * Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
1183 * RETURNS: TRUE if message hooks were successfully installed
1184 * FALSE on failure
1186 static BOOL OLEMenu_InstallHooks( DWORD tid )
1188 OleMenuHookItem *pHookItem = NULL;
1190 /* Create an entry for the hook table */
1191 if ( !(pHookItem = HeapAlloc(GetProcessHeap(), 0,
1192 sizeof(OleMenuHookItem)) ) )
1193 return FALSE;
1195 pHookItem->tid = tid;
1196 pHookItem->hHeap = GetProcessHeap();
1198 /* Install a thread scope message hook for WH_GETMESSAGE */
1199 pHookItem->GetMsg_hHook = SetWindowsHookExA( WH_GETMESSAGE, OLEMenu_GetMsgProc,
1200 0, GetCurrentThreadId() );
1201 if ( !pHookItem->GetMsg_hHook )
1202 goto CLEANUP;
1204 /* Install a thread scope message hook for WH_CALLWNDPROC */
1205 pHookItem->CallWndProc_hHook = SetWindowsHookExA( WH_CALLWNDPROC, OLEMenu_CallWndProc,
1206 0, GetCurrentThreadId() );
1207 if ( !pHookItem->CallWndProc_hHook )
1208 goto CLEANUP;
1210 /* Insert the hook table entry */
1211 pHookItem->next = hook_list;
1212 hook_list = pHookItem;
1214 return TRUE;
1216 CLEANUP:
1217 /* Unhook any hooks */
1218 if ( pHookItem->GetMsg_hHook )
1219 UnhookWindowsHookEx( pHookItem->GetMsg_hHook );
1220 if ( pHookItem->CallWndProc_hHook )
1221 UnhookWindowsHookEx( pHookItem->CallWndProc_hHook );
1222 /* Release the hook table entry */
1223 HeapFree(pHookItem->hHeap, 0, pHookItem );
1225 return FALSE;
1228 /*************************************************************************
1229 * OLEMenu_UnInstallHooks
1230 * UnInstall thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
1232 * RETURNS: TRUE if message hooks were successfully installed
1233 * FALSE on failure
1235 static BOOL OLEMenu_UnInstallHooks( DWORD tid )
1237 OleMenuHookItem *pHookItem = NULL;
1238 OleMenuHookItem **ppHook = &hook_list;
1240 while (*ppHook)
1242 if ((*ppHook)->tid == tid)
1244 pHookItem = *ppHook;
1245 *ppHook = pHookItem->next;
1246 break;
1248 ppHook = &(*ppHook)->next;
1250 if (!pHookItem) return FALSE;
1252 /* Uninstall the hooks installed for this thread */
1253 if ( !UnhookWindowsHookEx( pHookItem->GetMsg_hHook ) )
1254 goto CLEANUP;
1255 if ( !UnhookWindowsHookEx( pHookItem->CallWndProc_hHook ) )
1256 goto CLEANUP;
1258 /* Release the hook table entry */
1259 HeapFree(pHookItem->hHeap, 0, pHookItem );
1261 return TRUE;
1263 CLEANUP:
1264 /* Release the hook table entry */
1265 HeapFree(pHookItem->hHeap, 0, pHookItem );
1267 return FALSE;
1270 /*************************************************************************
1271 * OLEMenu_IsHookInstalled
1272 * Tests if OLEMenu hooks have been installed for a thread
1274 * RETURNS: The pointer and index of the hook table entry for the tid
1275 * NULL and -1 for the index if no hooks were installed for this thread
1277 static OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid )
1279 OleMenuHookItem *pHookItem = NULL;
1281 /* Do a simple linear search for an entry whose tid matches ours.
1282 * We really need a map but efficiency is not a concern here. */
1283 for (pHookItem = hook_list; pHookItem; pHookItem = pHookItem->next)
1285 if ( tid == pHookItem->tid )
1286 return pHookItem;
1289 return NULL;
1292 /***********************************************************************
1293 * OLEMenu_FindMainMenuIndex
1295 * Used by OLEMenu API to find the top level group a menu item belongs to.
1296 * On success pnPos contains the index of the item in the top level menu group
1298 * RETURNS: TRUE if the ID was found, FALSE on failure
1300 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos )
1302 UINT i, nItems;
1304 nItems = GetMenuItemCount( hMainMenu );
1306 for (i = 0; i < nItems; i++)
1308 HMENU hsubmenu;
1310 /* Is the current item a submenu? */
1311 if ( (hsubmenu = GetSubMenu(hMainMenu, i)) )
1313 /* If the handle is the same we're done */
1314 if ( hsubmenu == hPopupMenu )
1316 if (pnPos)
1317 *pnPos = i;
1318 return TRUE;
1320 /* Recursively search without updating pnPos */
1321 else if ( OLEMenu_FindMainMenuIndex( hsubmenu, hPopupMenu, NULL ) )
1323 if (pnPos)
1324 *pnPos = i;
1325 return TRUE;
1330 return FALSE;
1333 /***********************************************************************
1334 * OLEMenu_SetIsServerMenu
1336 * Checks whether a popup menu belongs to a shared menu group which is
1337 * owned by the server, and sets the menu descriptor state accordingly.
1338 * All menu messages from these groups should be routed to the server.
1340 * RETURNS: TRUE if the popup menu is part of a server owned group
1341 * FALSE if the popup menu is part of a container owned group
1343 static BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor )
1345 UINT nPos = 0, nWidth, i;
1347 pOleMenuDescriptor->bIsServerItem = FALSE;
1349 /* Don't bother searching if the popup is the combined menu itself */
1350 if ( hmenu == pOleMenuDescriptor->hmenuCombined )
1351 return FALSE;
1353 /* Find the menu item index in the shared OLE menu that this item belongs to */
1354 if ( !OLEMenu_FindMainMenuIndex( pOleMenuDescriptor->hmenuCombined, hmenu, &nPos ) )
1355 return FALSE;
1357 /* The group widths array has counts for the number of elements
1358 * in the groups File, Edit, Container, Object, Window, Help.
1359 * The Edit, Object & Help groups belong to the server object
1360 * and the other three belong to the container.
1361 * Loop through the group widths and locate the group we are a member of.
1363 for ( i = 0, nWidth = 0; i < 6; i++ )
1365 nWidth += pOleMenuDescriptor->mgw.width[i];
1366 if ( nPos < nWidth )
1368 /* Odd elements are server menu widths */
1369 pOleMenuDescriptor->bIsServerItem = (i%2) ? TRUE : FALSE;
1370 break;
1374 return pOleMenuDescriptor->bIsServerItem;
1377 /*************************************************************************
1378 * OLEMenu_CallWndProc
1379 * Thread scope WH_CALLWNDPROC hook proc filter function (callback)
1380 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1382 static LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam)
1384 LPCWPSTRUCT pMsg = NULL;
1385 HOLEMENU hOleMenu = 0;
1386 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1387 OleMenuHookItem *pHookItem = NULL;
1388 WORD fuFlags;
1390 TRACE("%i, %04lx, %08lx\n", code, wParam, lParam );
1392 /* Check if we're being asked to process the message */
1393 if ( HC_ACTION != code )
1394 goto NEXTHOOK;
1396 /* Retrieve the current message being dispatched from lParam */
1397 pMsg = (LPCWPSTRUCT)lParam;
1399 /* Check if the message is destined for a window we are interested in:
1400 * If the window has an OLEMenu property we may need to dispatch
1401 * the menu message to its active objects window instead. */
1403 hOleMenu = (HOLEMENU)GetPropA( pMsg->hwnd, "PROP_OLEMenuDescriptor" );
1404 if ( !hOleMenu )
1405 goto NEXTHOOK;
1407 /* Get the menu descriptor */
1408 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1409 if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1410 goto NEXTHOOK;
1412 /* Process menu messages */
1413 switch( pMsg->message )
1415 case WM_INITMENU:
1417 /* Reset the menu descriptor state */
1418 pOleMenuDescriptor->bIsServerItem = FALSE;
1420 /* Send this message to the server as well */
1421 SendMessageA( pOleMenuDescriptor->hwndActiveObject,
1422 pMsg->message, pMsg->wParam, pMsg->lParam );
1423 goto NEXTHOOK;
1426 case WM_INITMENUPOPUP:
1428 /* Save the state for whether this is a server owned menu */
1429 OLEMenu_SetIsServerMenu( (HMENU)pMsg->wParam, pOleMenuDescriptor );
1430 break;
1433 case WM_MENUSELECT:
1435 fuFlags = HIWORD(pMsg->wParam); /* Get flags */
1436 if ( fuFlags & MF_SYSMENU )
1437 goto NEXTHOOK;
1439 /* Save the state for whether this is a server owned popup menu */
1440 else if ( fuFlags & MF_POPUP )
1441 OLEMenu_SetIsServerMenu( (HMENU)pMsg->lParam, pOleMenuDescriptor );
1443 break;
1446 case WM_DRAWITEM:
1448 LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT) pMsg->lParam;
1449 if ( pMsg->wParam != 0 || lpdis->CtlType != ODT_MENU )
1450 goto NEXTHOOK; /* Not a menu message */
1452 break;
1455 default:
1456 goto NEXTHOOK;
1459 /* If the message was for the server dispatch it accordingly */
1460 if ( pOleMenuDescriptor->bIsServerItem )
1462 SendMessageA( pOleMenuDescriptor->hwndActiveObject,
1463 pMsg->message, pMsg->wParam, pMsg->lParam );
1466 NEXTHOOK:
1467 if ( pOleMenuDescriptor )
1468 GlobalUnlock( hOleMenu );
1470 /* Lookup the hook item for the current thread */
1471 if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1473 /* This should never fail!! */
1474 WARN("could not retrieve hHook for current thread!\n" );
1475 return 0;
1478 /* Pass on the message to the next hooker */
1479 return CallNextHookEx( pHookItem->CallWndProc_hHook, code, wParam, lParam );
1482 /*************************************************************************
1483 * OLEMenu_GetMsgProc
1484 * Thread scope WH_GETMESSAGE hook proc filter function (callback)
1485 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1487 static LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam)
1489 LPMSG pMsg = NULL;
1490 HOLEMENU hOleMenu = 0;
1491 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1492 OleMenuHookItem *pHookItem = NULL;
1493 WORD wCode;
1495 TRACE("%i, %04lx, %08lx\n", code, wParam, lParam );
1497 /* Check if we're being asked to process a messages */
1498 if ( HC_ACTION != code )
1499 goto NEXTHOOK;
1501 /* Retrieve the current message being dispatched from lParam */
1502 pMsg = (LPMSG)lParam;
1504 /* Check if the message is destined for a window we are interested in:
1505 * If the window has an OLEMenu property we may need to dispatch
1506 * the menu message to its active objects window instead. */
1508 hOleMenu = (HOLEMENU)GetPropA( pMsg->hwnd, "PROP_OLEMenuDescriptor" );
1509 if ( !hOleMenu )
1510 goto NEXTHOOK;
1512 /* Process menu messages */
1513 switch( pMsg->message )
1515 case WM_COMMAND:
1517 wCode = HIWORD(pMsg->wParam); /* Get notification code */
1518 if ( wCode )
1519 goto NEXTHOOK; /* Not a menu message */
1520 break;
1522 default:
1523 goto NEXTHOOK;
1526 /* Get the menu descriptor */
1527 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1528 if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1529 goto NEXTHOOK;
1531 /* If the message was for the server dispatch it accordingly */
1532 if ( pOleMenuDescriptor->bIsServerItem )
1534 /* Change the hWnd in the message to the active objects hWnd.
1535 * The message loop which reads this message will automatically
1536 * dispatch it to the embedded objects window. */
1537 pMsg->hwnd = pOleMenuDescriptor->hwndActiveObject;
1540 NEXTHOOK:
1541 if ( pOleMenuDescriptor )
1542 GlobalUnlock( hOleMenu );
1544 /* Lookup the hook item for the current thread */
1545 if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1547 /* This should never fail!! */
1548 WARN("could not retrieve hHook for current thread!\n" );
1549 return FALSE;
1552 /* Pass on the message to the next hooker */
1553 return CallNextHookEx( pHookItem->GetMsg_hHook, code, wParam, lParam );
1556 /***********************************************************************
1557 * OleCreateMenuDescriptor [OLE32.@]
1558 * Creates an OLE menu descriptor for OLE to use when dispatching
1559 * menu messages and commands.
1561 * PARAMS:
1562 * hmenuCombined - Handle to the objects combined menu
1563 * lpMenuWidths - Pointer to array of 6 LONG's indicating menus per group
1566 HOLEMENU WINAPI OleCreateMenuDescriptor(
1567 HMENU hmenuCombined,
1568 LPOLEMENUGROUPWIDTHS lpMenuWidths)
1570 HOLEMENU hOleMenu;
1571 OleMenuDescriptor *pOleMenuDescriptor;
1572 int i;
1574 if ( !hmenuCombined || !lpMenuWidths )
1575 return 0;
1577 /* Create an OLE menu descriptor */
1578 if ( !(hOleMenu = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT,
1579 sizeof(OleMenuDescriptor) ) ) )
1580 return 0;
1582 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1583 if ( !pOleMenuDescriptor )
1584 return 0;
1586 /* Initialize menu group widths and hmenu */
1587 for ( i = 0; i < 6; i++ )
1588 pOleMenuDescriptor->mgw.width[i] = lpMenuWidths->width[i];
1590 pOleMenuDescriptor->hmenuCombined = hmenuCombined;
1591 pOleMenuDescriptor->bIsServerItem = FALSE;
1592 GlobalUnlock( hOleMenu );
1594 return hOleMenu;
1597 /***********************************************************************
1598 * OleDestroyMenuDescriptor [OLE32.@]
1599 * Destroy the shared menu descriptor
1601 HRESULT WINAPI OleDestroyMenuDescriptor(
1602 HOLEMENU hmenuDescriptor)
1604 if ( hmenuDescriptor )
1605 GlobalFree( hmenuDescriptor );
1606 return S_OK;
1609 /***********************************************************************
1610 * OleSetMenuDescriptor [OLE32.@]
1611 * Installs or removes OLE dispatching code for the containers frame window.
1613 * PARAMS
1614 * hOleMenu Handle to composite menu descriptor
1615 * hwndFrame Handle to containers frame window
1616 * hwndActiveObject Handle to objects in-place activation window
1617 * lpFrame Pointer to IOleInPlaceFrame on containers window
1618 * lpActiveObject Pointer to IOleInPlaceActiveObject on active in-place object
1620 * RETURNS
1621 * S_OK - menu installed correctly
1622 * E_FAIL, E_INVALIDARG, E_UNEXPECTED - failure
1624 * FIXME
1625 * The lpFrame and lpActiveObject parameters are currently ignored
1626 * OLE should install context sensitive help F1 filtering for the app when
1627 * these are non null.
1629 HRESULT WINAPI OleSetMenuDescriptor(
1630 HOLEMENU hOleMenu,
1631 HWND hwndFrame,
1632 HWND hwndActiveObject,
1633 LPOLEINPLACEFRAME lpFrame,
1634 LPOLEINPLACEACTIVEOBJECT lpActiveObject)
1636 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1638 /* Check args */
1639 if ( !hwndFrame || (hOleMenu && !hwndActiveObject) )
1640 return E_INVALIDARG;
1642 if ( lpFrame || lpActiveObject )
1644 FIXME("(%p, %p, %p, %p, %p), Context sensitive help filtering not implemented!\n",
1645 hOleMenu,
1646 hwndFrame,
1647 hwndActiveObject,
1648 lpFrame,
1649 lpActiveObject);
1652 /* Set up a message hook to intercept the containers frame window messages.
1653 * The message filter is responsible for dispatching menu messages from the
1654 * shared menu which are intended for the object.
1657 if ( hOleMenu ) /* Want to install dispatching code */
1659 /* If OLEMenu hooks are already installed for this thread, fail
1660 * Note: This effectively means that OleSetMenuDescriptor cannot
1661 * be called twice in succession on the same frame window
1662 * without first calling it with a null hOleMenu to uninstall */
1663 if ( OLEMenu_IsHookInstalled( GetCurrentThreadId() ) )
1664 return E_FAIL;
1666 /* Get the menu descriptor */
1667 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1668 if ( !pOleMenuDescriptor )
1669 return E_UNEXPECTED;
1671 /* Update the menu descriptor */
1672 pOleMenuDescriptor->hwndFrame = hwndFrame;
1673 pOleMenuDescriptor->hwndActiveObject = hwndActiveObject;
1675 GlobalUnlock( hOleMenu );
1676 pOleMenuDescriptor = NULL;
1678 /* Add a menu descriptor windows property to the frame window */
1679 SetPropA( hwndFrame, "PROP_OLEMenuDescriptor", hOleMenu );
1681 /* Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC */
1682 if ( !OLEMenu_InstallHooks( GetCurrentThreadId() ) )
1683 return E_FAIL;
1685 else /* Want to uninstall dispatching code */
1687 /* Uninstall the hooks */
1688 if ( !OLEMenu_UnInstallHooks( GetCurrentThreadId() ) )
1689 return E_FAIL;
1691 /* Remove the menu descriptor property from the frame window */
1692 RemovePropA( hwndFrame, "PROP_OLEMenuDescriptor" );
1695 return S_OK;
1698 /******************************************************************************
1699 * IsAccelerator [OLE32.@]
1700 * Mostly copied from controls/menu.c TranslateAccelerator implementation
1702 BOOL WINAPI IsAccelerator(HACCEL hAccel, int cAccelEntries, LPMSG lpMsg, WORD* lpwCmd)
1704 LPACCEL lpAccelTbl;
1705 int i;
1707 if(!lpMsg) return FALSE;
1708 if (!hAccel)
1710 WARN_(accel)("NULL accel handle\n");
1711 return FALSE;
1713 if((lpMsg->message != WM_KEYDOWN &&
1714 lpMsg->message != WM_SYSKEYDOWN &&
1715 lpMsg->message != WM_SYSCHAR &&
1716 lpMsg->message != WM_CHAR)) return FALSE;
1717 lpAccelTbl = HeapAlloc(GetProcessHeap(), 0, cAccelEntries * sizeof(ACCEL));
1718 if (NULL == lpAccelTbl)
1720 return FALSE;
1722 if (CopyAcceleratorTableW(hAccel, lpAccelTbl, cAccelEntries) != cAccelEntries)
1724 WARN_(accel)("CopyAcceleratorTableW failed\n");
1725 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
1726 return FALSE;
1729 TRACE_(accel)("hAccel=%p, cAccelEntries=%d,"
1730 "msg->hwnd=%p, msg->message=%04x, wParam=%08lx, lParam=%08lx\n",
1731 hAccel, cAccelEntries,
1732 lpMsg->hwnd, lpMsg->message, lpMsg->wParam, lpMsg->lParam);
1733 for(i = 0; i < cAccelEntries; i++)
1735 if(lpAccelTbl[i].key != lpMsg->wParam)
1736 continue;
1738 if(lpMsg->message == WM_CHAR)
1740 if(!(lpAccelTbl[i].fVirt & FALT) && !(lpAccelTbl[i].fVirt & FVIRTKEY))
1742 TRACE_(accel)("found accel for WM_CHAR: ('%c')\n", LOWORD(lpMsg->wParam) & 0xff);
1743 goto found;
1746 else
1748 if(lpAccelTbl[i].fVirt & FVIRTKEY)
1750 INT mask = 0;
1751 TRACE_(accel)("found accel for virt_key %04lx (scan %04x)\n",
1752 lpMsg->wParam, HIWORD(lpMsg->lParam) & 0xff);
1753 if(GetKeyState(VK_SHIFT) & 0x8000) mask |= FSHIFT;
1754 if(GetKeyState(VK_CONTROL) & 0x8000) mask |= FCONTROL;
1755 if(GetKeyState(VK_MENU) & 0x8000) mask |= FALT;
1756 if(mask == (lpAccelTbl[i].fVirt & (FSHIFT | FCONTROL | FALT))) goto found;
1757 TRACE_(accel)("incorrect SHIFT/CTRL/ALT-state\n");
1759 else
1761 if(!(lpMsg->lParam & 0x01000000)) /* no special_key */
1763 if((lpAccelTbl[i].fVirt & FALT) && (lpMsg->lParam & 0x20000000))
1764 { /* ^^ ALT pressed */
1765 TRACE_(accel)("found accel for Alt-%c\n", LOWORD(lpMsg->wParam) & 0xff);
1766 goto found;
1773 WARN_(accel)("couldn't translate accelerator key\n");
1774 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
1775 return FALSE;
1777 found:
1778 if(lpwCmd) *lpwCmd = lpAccelTbl[i].cmd;
1779 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
1780 return TRUE;
1783 /***********************************************************************
1784 * ReleaseStgMedium [OLE32.@]
1786 void WINAPI ReleaseStgMedium(
1787 STGMEDIUM* pmedium)
1789 switch (pmedium->tymed)
1791 case TYMED_HGLOBAL:
1793 if ( (pmedium->pUnkForRelease==0) &&
1794 (pmedium->u.hGlobal!=0) )
1795 GlobalFree(pmedium->u.hGlobal);
1796 break;
1798 case TYMED_FILE:
1800 if (pmedium->u.lpszFileName!=0)
1802 if (pmedium->pUnkForRelease==0)
1804 DeleteFileW(pmedium->u.lpszFileName);
1807 CoTaskMemFree(pmedium->u.lpszFileName);
1809 break;
1811 case TYMED_ISTREAM:
1813 if (pmedium->u.pstm!=0)
1815 IStream_Release(pmedium->u.pstm);
1817 break;
1819 case TYMED_ISTORAGE:
1821 if (pmedium->u.pstg!=0)
1823 IStorage_Release(pmedium->u.pstg);
1825 break;
1827 case TYMED_GDI:
1829 if ( (pmedium->pUnkForRelease==0) &&
1830 (pmedium->u.hBitmap!=0) )
1831 DeleteObject(pmedium->u.hBitmap);
1832 break;
1834 case TYMED_MFPICT:
1836 if ( (pmedium->pUnkForRelease==0) &&
1837 (pmedium->u.hMetaFilePict!=0) )
1839 LPMETAFILEPICT pMP = GlobalLock(pmedium->u.hMetaFilePict);
1840 DeleteMetaFile(pMP->hMF);
1841 GlobalUnlock(pmedium->u.hMetaFilePict);
1842 GlobalFree(pmedium->u.hMetaFilePict);
1844 break;
1846 case TYMED_ENHMF:
1848 if ( (pmedium->pUnkForRelease==0) &&
1849 (pmedium->u.hEnhMetaFile!=0) )
1851 DeleteEnhMetaFile(pmedium->u.hEnhMetaFile);
1853 break;
1855 case TYMED_NULL:
1856 default:
1857 break;
1859 pmedium->tymed=TYMED_NULL;
1862 * After cleaning up, the unknown is released
1864 if (pmedium->pUnkForRelease!=0)
1866 IUnknown_Release(pmedium->pUnkForRelease);
1867 pmedium->pUnkForRelease = 0;
1871 /***
1872 * OLEDD_Initialize()
1874 * Initializes the OLE drag and drop data structures.
1876 static void OLEDD_Initialize(void)
1878 WNDCLASSA wndClass;
1880 ZeroMemory (&wndClass, sizeof(WNDCLASSA));
1881 wndClass.style = CS_GLOBALCLASS;
1882 wndClass.lpfnWndProc = OLEDD_DragTrackerWindowProc;
1883 wndClass.cbClsExtra = 0;
1884 wndClass.cbWndExtra = sizeof(TrackerWindowInfo*);
1885 wndClass.hCursor = 0;
1886 wndClass.hbrBackground = 0;
1887 wndClass.lpszClassName = OLEDD_DRAGTRACKERCLASS;
1889 RegisterClassA (&wndClass);
1892 /***
1893 * OLEDD_FreeDropTarget()
1895 * Frees the drag and drop data structure
1897 static void OLEDD_FreeDropTarget(DropTargetNode *dropTargetInfo, BOOL release_drop_target)
1899 list_remove(&dropTargetInfo->entry);
1900 if (release_drop_target) IDropTarget_Release(dropTargetInfo->dropTarget);
1901 HeapFree(GetProcessHeap(), 0, dropTargetInfo);
1904 /***
1905 * OLEDD_UnInitialize()
1907 * Releases the OLE drag and drop data structures.
1909 void OLEDD_UnInitialize(void)
1912 * Simply empty the list.
1914 while (!list_empty(&targetListHead))
1916 DropTargetNode* curNode = LIST_ENTRY(list_head(&targetListHead), DropTargetNode, entry);
1917 OLEDD_FreeDropTarget(curNode, FALSE);
1921 /***
1922 * OLEDD_FindDropTarget()
1924 * Finds information about the drop target.
1926 static DropTargetNode* OLEDD_FindDropTarget(HWND hwndOfTarget)
1928 DropTargetNode* curNode;
1931 * Iterate the list to find the HWND value.
1933 LIST_FOR_EACH_ENTRY(curNode, &targetListHead, DropTargetNode, entry)
1934 if (hwndOfTarget==curNode->hwndTarget)
1935 return curNode;
1938 * If we get here, the item is not in the list
1940 return NULL;
1943 /***
1944 * OLEDD_DragTrackerWindowProc()
1946 * This method is the WindowProcedure of the drag n drop tracking
1947 * window. During a drag n Drop operation, an invisible window is created
1948 * to receive the user input and act upon it. This procedure is in charge
1949 * of this behavior.
1952 #define DRAG_TIMER_ID 1
1954 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(
1955 HWND hwnd,
1956 UINT uMsg,
1957 WPARAM wParam,
1958 LPARAM lParam)
1960 switch (uMsg)
1962 case WM_CREATE:
1964 LPCREATESTRUCTA createStruct = (LPCREATESTRUCTA)lParam;
1966 SetWindowLongA(hwnd, 0, (LONG)createStruct->lpCreateParams);
1967 SetTimer(hwnd, DRAG_TIMER_ID, 50, NULL);
1969 break;
1971 case WM_TIMER:
1972 case WM_MOUSEMOVE:
1974 OLEDD_TrackMouseMove((TrackerWindowInfo*)GetWindowLongA(hwnd, 0));
1975 break;
1977 case WM_LBUTTONUP:
1978 case WM_MBUTTONUP:
1979 case WM_RBUTTONUP:
1980 case WM_LBUTTONDOWN:
1981 case WM_MBUTTONDOWN:
1982 case WM_RBUTTONDOWN:
1984 OLEDD_TrackStateChange((TrackerWindowInfo*)GetWindowLongA(hwnd, 0));
1985 break;
1987 case WM_DESTROY:
1989 KillTimer(hwnd, DRAG_TIMER_ID);
1990 break;
1995 * This is a window proc after all. Let's call the default.
1997 return DefWindowProcA (hwnd, uMsg, wParam, lParam);
2000 /***
2001 * OLEDD_TrackMouseMove()
2003 * This method is invoked while a drag and drop operation is in effect.
2004 * it will generate the appropriate callbacks in the drop source
2005 * and drop target. It will also provide the expected feedback to
2006 * the user.
2008 * params:
2009 * trackerInfo - Pointer to the structure identifying the
2010 * drag & drop operation that is currently
2011 * active.
2013 static void OLEDD_TrackMouseMove(TrackerWindowInfo* trackerInfo)
2015 HWND hwndNewTarget = 0;
2016 HRESULT hr = S_OK;
2017 POINT pt;
2020 * Get the handle of the window under the mouse
2022 pt.x = trackerInfo->curMousePos.x;
2023 pt.y = trackerInfo->curMousePos.y;
2024 hwndNewTarget = WindowFromPoint(pt);
2027 * Every time, we re-initialize the effects passed to the
2028 * IDropTarget to the effects allowed by the source.
2030 *trackerInfo->pdwEffect = trackerInfo->dwOKEffect;
2033 * If we are hovering over the same target as before, send the
2034 * DragOver notification
2036 if ( (trackerInfo->curDragTarget != 0) &&
2037 (trackerInfo->curTargetHWND == hwndNewTarget) )
2039 IDropTarget_DragOver(trackerInfo->curDragTarget,
2040 trackerInfo->dwKeyState,
2041 trackerInfo->curMousePos,
2042 trackerInfo->pdwEffect);
2044 else
2046 DropTargetNode* newDropTargetNode = 0;
2049 * If we changed window, we have to notify our old target and check for
2050 * the new one.
2052 if (trackerInfo->curDragTarget!=0)
2054 IDropTarget_DragLeave(trackerInfo->curDragTarget);
2058 * Make sure we're hovering over a window.
2060 if (hwndNewTarget!=0)
2063 * Find-out if there is a drag target under the mouse
2065 HWND nexttar = hwndNewTarget;
2066 trackerInfo->curTargetHWND = hwndNewTarget;
2068 do {
2069 newDropTargetNode = OLEDD_FindDropTarget(nexttar);
2070 } while (!newDropTargetNode && (nexttar = GetParent(nexttar)) != 0);
2071 if(nexttar) hwndNewTarget = nexttar;
2073 trackerInfo->curDragTargetHWND = hwndNewTarget;
2074 trackerInfo->curDragTarget = newDropTargetNode ? newDropTargetNode->dropTarget : 0;
2077 * If there is, notify it that we just dragged-in
2079 if (trackerInfo->curDragTarget!=0)
2081 IDropTarget_DragEnter(trackerInfo->curDragTarget,
2082 trackerInfo->dataObject,
2083 trackerInfo->dwKeyState,
2084 trackerInfo->curMousePos,
2085 trackerInfo->pdwEffect);
2088 else
2091 * The mouse is not over a window so we don't track anything.
2093 trackerInfo->curDragTargetHWND = 0;
2094 trackerInfo->curTargetHWND = 0;
2095 trackerInfo->curDragTarget = 0;
2100 * Now that we have done that, we have to tell the source to give
2101 * us feedback on the work being done by the target. If we don't
2102 * have a target, simulate no effect.
2104 if (trackerInfo->curDragTarget==0)
2106 *trackerInfo->pdwEffect = DROPEFFECT_NONE;
2109 hr = IDropSource_GiveFeedback(trackerInfo->dropSource,
2110 *trackerInfo->pdwEffect);
2113 * When we ask for feedback from the drop source, sometimes it will
2114 * do all the necessary work and sometimes it will not handle it
2115 * when that's the case, we must display the standard drag and drop
2116 * cursors.
2118 if (hr==DRAGDROP_S_USEDEFAULTCURSORS)
2120 if (*trackerInfo->pdwEffect & DROPEFFECT_MOVE)
2122 SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(1)));
2124 else if (*trackerInfo->pdwEffect & DROPEFFECT_COPY)
2126 SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(2)));
2128 else if (*trackerInfo->pdwEffect & DROPEFFECT_LINK)
2130 SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(3)));
2132 else
2134 SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(0)));
2139 /***
2140 * OLEDD_TrackStateChange()
2142 * This method is invoked while a drag and drop operation is in effect.
2143 * It is used to notify the drop target/drop source callbacks when
2144 * the state of the keyboard or mouse button change.
2146 * params:
2147 * trackerInfo - Pointer to the structure identifying the
2148 * drag & drop operation that is currently
2149 * active.
2151 static void OLEDD_TrackStateChange(TrackerWindowInfo* trackerInfo)
2154 * Ask the drop source what to do with the operation.
2156 trackerInfo->returnValue = IDropSource_QueryContinueDrag(
2157 trackerInfo->dropSource,
2158 trackerInfo->escPressed,
2159 trackerInfo->dwKeyState);
2162 * All the return valued will stop the operation except the S_OK
2163 * return value.
2165 if (trackerInfo->returnValue!=S_OK)
2168 * Make sure the message loop in DoDragDrop stops
2170 trackerInfo->trackingDone = TRUE;
2173 * Release the mouse in case the drop target decides to show a popup
2174 * or a menu or something.
2176 ReleaseCapture();
2179 * If we end-up over a target, drop the object in the target or
2180 * inform the target that the operation was cancelled.
2182 if (trackerInfo->curDragTarget!=0)
2184 switch (trackerInfo->returnValue)
2187 * If the source wants us to complete the operation, we tell
2188 * the drop target that we just dropped the object in it.
2190 case DRAGDROP_S_DROP:
2192 IDropTarget_Drop(trackerInfo->curDragTarget,
2193 trackerInfo->dataObject,
2194 trackerInfo->dwKeyState,
2195 trackerInfo->curMousePos,
2196 trackerInfo->pdwEffect);
2197 break;
2200 * If the source told us that we should cancel, fool the drop
2201 * target by telling it that the mouse left it's window.
2202 * Also set the drop effect to "NONE" in case the application
2203 * ignores the result of DoDragDrop.
2205 case DRAGDROP_S_CANCEL:
2206 IDropTarget_DragLeave(trackerInfo->curDragTarget);
2207 *trackerInfo->pdwEffect = DROPEFFECT_NONE;
2208 break;
2214 /***
2215 * OLEDD_GetButtonState()
2217 * This method will use the current state of the keyboard to build
2218 * a button state mask equivalent to the one passed in the
2219 * WM_MOUSEMOVE wParam.
2221 static DWORD OLEDD_GetButtonState(void)
2223 BYTE keyboardState[256];
2224 DWORD keyMask = 0;
2226 GetKeyboardState(keyboardState);
2228 if ( (keyboardState[VK_SHIFT] & 0x80) !=0)
2229 keyMask |= MK_SHIFT;
2231 if ( (keyboardState[VK_CONTROL] & 0x80) !=0)
2232 keyMask |= MK_CONTROL;
2234 if ( (keyboardState[VK_LBUTTON] & 0x80) !=0)
2235 keyMask |= MK_LBUTTON;
2237 if ( (keyboardState[VK_RBUTTON] & 0x80) !=0)
2238 keyMask |= MK_RBUTTON;
2240 if ( (keyboardState[VK_MBUTTON] & 0x80) !=0)
2241 keyMask |= MK_MBUTTON;
2243 return keyMask;
2246 /***
2247 * OLEDD_GetButtonState()
2249 * This method will read the default value of the registry key in
2250 * parameter and extract a DWORD value from it. The registry key value
2251 * can be in a string key or a DWORD key.
2253 * params:
2254 * regKey - Key to read the default value from
2255 * pdwValue - Pointer to the location where the DWORD
2256 * value is returned. This value is not modified
2257 * if the value is not found.
2260 static void OLEUTL_ReadRegistryDWORDValue(
2261 HKEY regKey,
2262 DWORD* pdwValue)
2264 char buffer[20];
2265 DWORD dwKeyType;
2266 DWORD cbData = 20;
2267 LONG lres;
2269 lres = RegQueryValueExA(regKey,
2271 NULL,
2272 &dwKeyType,
2273 (LPBYTE)buffer,
2274 &cbData);
2276 if (lres==ERROR_SUCCESS)
2278 switch (dwKeyType)
2280 case REG_DWORD:
2281 *pdwValue = *(DWORD*)buffer;
2282 break;
2283 case REG_EXPAND_SZ:
2284 case REG_MULTI_SZ:
2285 case REG_SZ:
2286 *pdwValue = (DWORD)strtoul(buffer, NULL, 10);
2287 break;
2292 /******************************************************************************
2293 * OleDraw (OLE32.@)
2295 * The operation of this function is documented literally in the WinAPI
2296 * documentation to involve a QueryInterface for the IViewObject interface,
2297 * followed by a call to IViewObject::Draw.
2299 HRESULT WINAPI OleDraw(
2300 IUnknown *pUnk,
2301 DWORD dwAspect,
2302 HDC hdcDraw,
2303 LPCRECT lprcBounds)
2305 HRESULT hres;
2306 IViewObject *viewobject;
2308 hres = IUnknown_QueryInterface(pUnk,
2309 &IID_IViewObject,
2310 (void**)&viewobject);
2312 if (SUCCEEDED(hres))
2314 RECTL rectl;
2316 rectl.left = lprcBounds->left;
2317 rectl.right = lprcBounds->right;
2318 rectl.top = lprcBounds->top;
2319 rectl.bottom = lprcBounds->bottom;
2320 hres = IViewObject_Draw(viewobject, dwAspect, -1, 0, 0, 0, hdcDraw, &rectl, 0, 0, 0);
2322 IViewObject_Release(viewobject);
2323 return hres;
2325 else
2327 return DV_E_NOIVIEWOBJECT;
2331 /***********************************************************************
2332 * OleTranslateAccelerator [OLE32.@]
2334 HRESULT WINAPI OleTranslateAccelerator (LPOLEINPLACEFRAME lpFrame,
2335 LPOLEINPLACEFRAMEINFO lpFrameInfo, LPMSG lpmsg)
2337 WORD wID;
2339 TRACE("(%p,%p,%p)\n", lpFrame, lpFrameInfo, lpmsg);
2341 if (IsAccelerator(lpFrameInfo->haccel,lpFrameInfo->cAccelEntries,lpmsg,&wID))
2342 return IOleInPlaceFrame_TranslateAccelerator(lpFrame,lpmsg,wID);
2344 return S_FALSE;
2347 /******************************************************************************
2348 * OleCreate [OLE32.@]
2351 HRESULT WINAPI OleCreate(
2352 REFCLSID rclsid,
2353 REFIID riid,
2354 DWORD renderopt,
2355 LPFORMATETC pFormatEtc,
2356 LPOLECLIENTSITE pClientSite,
2357 LPSTORAGE pStg,
2358 LPVOID* ppvObj)
2360 HRESULT hres;
2361 IUnknown * pUnk = NULL;
2362 IOleObject *pOleObject = NULL;
2364 TRACE("(%s, %s, %d, %p, %p, %p, %p)\n", debugstr_guid(rclsid),
2365 debugstr_guid(riid), renderopt, pFormatEtc, pClientSite, pStg, ppvObj);
2367 hres = CoCreateInstance(rclsid, 0, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER, riid, (LPVOID*)&pUnk);
2369 if (SUCCEEDED(hres))
2370 hres = IStorage_SetClass(pStg, rclsid);
2372 if (pClientSite && SUCCEEDED(hres))
2374 hres = IUnknown_QueryInterface(pUnk, &IID_IOleObject, (LPVOID*)&pOleObject);
2375 if (SUCCEEDED(hres))
2377 DWORD dwStatus;
2378 hres = IOleObject_GetMiscStatus(pOleObject, DVASPECT_CONTENT, &dwStatus);
2382 if (SUCCEEDED(hres))
2384 IPersistStorage * pPS;
2385 if (SUCCEEDED((hres = IUnknown_QueryInterface(pUnk, &IID_IPersistStorage, (LPVOID*)&pPS))))
2387 TRACE("trying to set stg %p\n", pStg);
2388 hres = IPersistStorage_InitNew(pPS, pStg);
2389 TRACE("-- result 0x%08x\n", hres);
2390 IPersistStorage_Release(pPS);
2394 if (pClientSite && SUCCEEDED(hres))
2396 TRACE("trying to set clientsite %p\n", pClientSite);
2397 hres = IOleObject_SetClientSite(pOleObject, pClientSite);
2398 TRACE("-- result 0x%08x\n", hres);
2401 if (pOleObject)
2402 IOleObject_Release(pOleObject);
2404 if (((renderopt == OLERENDER_DRAW) || (renderopt == OLERENDER_FORMAT)) &&
2405 SUCCEEDED(hres))
2407 IRunnableObject *pRunnable;
2408 IOleCache *pOleCache;
2409 HRESULT hres2;
2411 hres2 = IUnknown_QueryInterface(pUnk, &IID_IRunnableObject, (void **)&pRunnable);
2412 if (SUCCEEDED(hres2))
2414 hres = IRunnableObject_Run(pRunnable, NULL);
2415 IRunnableObject_Release(pRunnable);
2418 if (SUCCEEDED(hres))
2420 hres2 = IUnknown_QueryInterface(pUnk, &IID_IOleCache, (void **)&pOleCache);
2421 if (SUCCEEDED(hres2))
2423 DWORD dwConnection;
2424 hres = IOleCache_Cache(pOleCache, pFormatEtc, ADVF_PRIMEFIRST, &dwConnection);
2425 IOleCache_Release(pOleCache);
2430 if (FAILED(hres) && pUnk)
2432 IUnknown_Release(pUnk);
2433 pUnk = NULL;
2436 *ppvObj = pUnk;
2438 TRACE("-- %p\n", pUnk);
2439 return hres;
2442 /******************************************************************************
2443 * OleGetAutoConvert [OLE32.@]
2445 HRESULT WINAPI OleGetAutoConvert(REFCLSID clsidOld, LPCLSID pClsidNew)
2447 static const WCHAR wszAutoConvertTo[] = {'A','u','t','o','C','o','n','v','e','r','t','T','o',0};
2448 HKEY hkey = NULL;
2449 WCHAR buf[CHARS_IN_GUID];
2450 LONG len;
2451 HRESULT res = S_OK;
2453 res = COM_OpenKeyForCLSID(clsidOld, wszAutoConvertTo, KEY_READ, &hkey);
2454 if (FAILED(res))
2455 goto done;
2457 len = sizeof(buf);
2458 if (RegQueryValueW(hkey, NULL, buf, &len))
2460 res = REGDB_E_KEYMISSING;
2461 goto done;
2463 res = CLSIDFromString(buf, pClsidNew);
2464 done:
2465 if (hkey) RegCloseKey(hkey);
2466 return res;
2469 /******************************************************************************
2470 * OleSetAutoConvert [OLE32.@]
2472 HRESULT WINAPI OleSetAutoConvert(REFCLSID clsidOld, REFCLSID clsidNew)
2474 static const WCHAR wszAutoConvertTo[] = {'A','u','t','o','C','o','n','v','e','r','t','T','o',0};
2475 HKEY hkey = NULL;
2476 WCHAR szClsidNew[CHARS_IN_GUID];
2477 HRESULT res = S_OK;
2479 TRACE("(%s,%s)\n", debugstr_guid(clsidOld), debugstr_guid(clsidNew));
2481 res = COM_OpenKeyForCLSID(clsidOld, NULL, KEY_READ | KEY_WRITE, &hkey);
2482 if (FAILED(res))
2483 goto done;
2484 StringFromGUID2(clsidNew, szClsidNew, CHARS_IN_GUID);
2485 if (RegSetValueW(hkey, wszAutoConvertTo, REG_SZ, szClsidNew, (strlenW(szClsidNew)+1) * sizeof(WCHAR)))
2487 res = REGDB_E_WRITEREGDB;
2488 goto done;
2491 done:
2492 if (hkey) RegCloseKey(hkey);
2493 return res;
2496 /******************************************************************************
2497 * OleDoAutoConvert [OLE32.@]
2499 HRESULT WINAPI OleDoAutoConvert(LPSTORAGE pStg, LPCLSID pClsidNew)
2501 FIXME("(%p,%p) : stub\n",pStg,pClsidNew);
2502 return E_NOTIMPL;
2505 /******************************************************************************
2506 * OleIsRunning [OLE32.@]
2508 BOOL WINAPI OleIsRunning(LPOLEOBJECT pObject)
2510 IRunnableObject *pRunnable;
2511 HRESULT hr;
2512 BOOL running;
2514 TRACE("(%p)\n", pObject);
2516 hr = IOleObject_QueryInterface(pObject, &IID_IRunnableObject, (void **)&pRunnable);
2517 if (FAILED(hr))
2518 return FALSE;
2519 running = IRunnableObject_IsRunning(pRunnable);
2520 IRunnableObject_Release(pRunnable);
2521 return running;
2524 /***********************************************************************
2525 * OleNoteObjectVisible [OLE32.@]
2527 HRESULT WINAPI OleNoteObjectVisible(LPUNKNOWN pUnknown, BOOL bVisible)
2529 TRACE("(%p, %s)\n", pUnknown, bVisible ? "TRUE" : "FALSE");
2530 return CoLockObjectExternal(pUnknown, bVisible, TRUE);
2534 /***********************************************************************
2535 * OLE_FreeClipDataArray [internal]
2537 * NOTES:
2538 * frees the data associated with an array of CLIPDATAs
2540 static void OLE_FreeClipDataArray(ULONG count, CLIPDATA * pClipDataArray)
2542 ULONG i;
2543 for (i = 0; i < count; i++)
2544 if (pClipDataArray[i].pClipData)
2545 CoTaskMemFree(pClipDataArray[i].pClipData);
2548 /***********************************************************************
2549 * PropSysAllocString [OLE32.@]
2550 * NOTES:
2551 * Basically a copy of SysAllocStringLen.
2553 BSTR WINAPI PropSysAllocString(LPCOLESTR str)
2555 DWORD bufferSize;
2556 DWORD* newBuffer;
2557 WCHAR* stringBuffer;
2558 int len;
2560 if (!str) return 0;
2562 len = lstrlenW(str);
2564 * Find the length of the buffer passed-in, in bytes.
2566 bufferSize = len * sizeof (WCHAR);
2569 * Allocate a new buffer to hold the string.
2570 * Don't forget to keep an empty spot at the beginning of the
2571 * buffer for the character count and an extra character at the
2572 * end for the NULL.
2574 newBuffer = HeapAlloc(GetProcessHeap(), 0,
2575 bufferSize + sizeof(WCHAR) + sizeof(DWORD));
2578 * If the memory allocation failed, return a null pointer.
2580 if (newBuffer==0)
2581 return 0;
2584 * Copy the length of the string in the placeholder.
2586 *newBuffer = bufferSize;
2589 * Skip the byte count.
2591 newBuffer++;
2593 memcpy(newBuffer, str, bufferSize);
2596 * Make sure that there is a nul character at the end of the
2597 * string.
2599 stringBuffer = (WCHAR*)newBuffer;
2600 stringBuffer[len] = L'\0';
2602 return (LPWSTR)stringBuffer;
2605 /***********************************************************************
2606 * PropSysFreeString [OLE32.@]
2607 * NOTES
2608 * Copy of SysFreeString.
2610 void WINAPI PropSysFreeString(LPOLESTR str)
2612 DWORD* bufferPointer;
2614 /* NULL is a valid parameter */
2615 if(!str) return;
2618 * We have to be careful when we free a BSTR pointer, it points to
2619 * the beginning of the string but it skips the byte count contained
2620 * before the string.
2622 bufferPointer = (DWORD*)str;
2624 bufferPointer--;
2627 * Free the memory from its "real" origin.
2629 HeapFree(GetProcessHeap(), 0, bufferPointer);
2632 /******************************************************************************
2633 * Check if a PROPVARIANT's type is valid.
2635 static inline HRESULT PROPVARIANT_ValidateType(VARTYPE vt)
2637 switch (vt)
2639 case VT_EMPTY:
2640 case VT_NULL:
2641 case VT_I2:
2642 case VT_I4:
2643 case VT_R4:
2644 case VT_R8:
2645 case VT_CY:
2646 case VT_DATE:
2647 case VT_BSTR:
2648 case VT_ERROR:
2649 case VT_BOOL:
2650 case VT_UI1:
2651 case VT_UI2:
2652 case VT_UI4:
2653 case VT_I8:
2654 case VT_UI8:
2655 case VT_LPSTR:
2656 case VT_LPWSTR:
2657 case VT_FILETIME:
2658 case VT_BLOB:
2659 case VT_STREAM:
2660 case VT_STORAGE:
2661 case VT_STREAMED_OBJECT:
2662 case VT_STORED_OBJECT:
2663 case VT_BLOB_OBJECT:
2664 case VT_CF:
2665 case VT_CLSID:
2666 case VT_I2|VT_VECTOR:
2667 case VT_I4|VT_VECTOR:
2668 case VT_R4|VT_VECTOR:
2669 case VT_R8|VT_VECTOR:
2670 case VT_CY|VT_VECTOR:
2671 case VT_DATE|VT_VECTOR:
2672 case VT_BSTR|VT_VECTOR:
2673 case VT_ERROR|VT_VECTOR:
2674 case VT_BOOL|VT_VECTOR:
2675 case VT_VARIANT|VT_VECTOR:
2676 case VT_UI1|VT_VECTOR:
2677 case VT_UI2|VT_VECTOR:
2678 case VT_UI4|VT_VECTOR:
2679 case VT_I8|VT_VECTOR:
2680 case VT_UI8|VT_VECTOR:
2681 case VT_LPSTR|VT_VECTOR:
2682 case VT_LPWSTR|VT_VECTOR:
2683 case VT_FILETIME|VT_VECTOR:
2684 case VT_CF|VT_VECTOR:
2685 case VT_CLSID|VT_VECTOR:
2686 return S_OK;
2688 WARN("Bad type %d\n", vt);
2689 return STG_E_INVALIDPARAMETER;
2692 /***********************************************************************
2693 * PropVariantClear [OLE32.@]
2695 HRESULT WINAPI PropVariantClear(PROPVARIANT * pvar) /* [in/out] */
2697 HRESULT hr;
2699 TRACE("(%p)\n", pvar);
2701 if (!pvar)
2702 return S_OK;
2704 hr = PROPVARIANT_ValidateType(pvar->vt);
2705 if (FAILED(hr))
2706 return hr;
2708 switch(pvar->vt)
2710 case VT_EMPTY:
2711 case VT_NULL:
2712 case VT_I2:
2713 case VT_I4:
2714 case VT_R4:
2715 case VT_R8:
2716 case VT_CY:
2717 case VT_DATE:
2718 case VT_ERROR:
2719 case VT_BOOL:
2720 case VT_UI1:
2721 case VT_UI2:
2722 case VT_UI4:
2723 case VT_I8:
2724 case VT_UI8:
2725 case VT_FILETIME:
2726 break;
2727 case VT_STREAM:
2728 case VT_STREAMED_OBJECT:
2729 case VT_STORAGE:
2730 case VT_STORED_OBJECT:
2731 if (pvar->u.pStream)
2732 IUnknown_Release(pvar->u.pStream);
2733 break;
2734 case VT_CLSID:
2735 case VT_LPSTR:
2736 case VT_LPWSTR:
2737 /* pick an arbitary typed pointer - we don't care about the type
2738 * as we are just freeing it */
2739 CoTaskMemFree(pvar->u.puuid);
2740 break;
2741 case VT_BLOB:
2742 case VT_BLOB_OBJECT:
2743 CoTaskMemFree(pvar->u.blob.pBlobData);
2744 break;
2745 case VT_BSTR:
2746 if (pvar->u.bstrVal)
2747 PropSysFreeString(pvar->u.bstrVal);
2748 break;
2749 case VT_CF:
2750 if (pvar->u.pclipdata)
2752 OLE_FreeClipDataArray(1, pvar->u.pclipdata);
2753 CoTaskMemFree(pvar->u.pclipdata);
2755 break;
2756 default:
2757 if (pvar->vt & VT_VECTOR)
2759 ULONG i;
2761 switch (pvar->vt & ~VT_VECTOR)
2763 case VT_VARIANT:
2764 FreePropVariantArray(pvar->u.capropvar.cElems, pvar->u.capropvar.pElems);
2765 break;
2766 case VT_CF:
2767 OLE_FreeClipDataArray(pvar->u.caclipdata.cElems, pvar->u.caclipdata.pElems);
2768 break;
2769 case VT_BSTR:
2770 for (i = 0; i < pvar->u.cabstr.cElems; i++)
2771 PropSysFreeString(pvar->u.cabstr.pElems[i]);
2772 break;
2773 case VT_LPSTR:
2774 for (i = 0; i < pvar->u.calpstr.cElems; i++)
2775 CoTaskMemFree(pvar->u.calpstr.pElems[i]);
2776 break;
2777 case VT_LPWSTR:
2778 for (i = 0; i < pvar->u.calpwstr.cElems; i++)
2779 CoTaskMemFree(pvar->u.calpwstr.pElems[i]);
2780 break;
2782 if (pvar->vt & ~VT_VECTOR)
2784 /* pick an arbitary VT_VECTOR structure - they all have the same
2785 * memory layout */
2786 CoTaskMemFree(pvar->u.capropvar.pElems);
2789 else
2790 WARN("Invalid/unsupported type %d\n", pvar->vt);
2793 ZeroMemory(pvar, sizeof(*pvar));
2795 return S_OK;
2798 /***********************************************************************
2799 * PropVariantCopy [OLE32.@]
2801 HRESULT WINAPI PropVariantCopy(PROPVARIANT *pvarDest, /* [out] */
2802 const PROPVARIANT *pvarSrc) /* [in] */
2804 ULONG len;
2805 HRESULT hr;
2807 TRACE("(%p, %p)\n", pvarDest, pvarSrc);
2809 hr = PROPVARIANT_ValidateType(pvarSrc->vt);
2810 if (FAILED(hr))
2811 return hr;
2813 /* this will deal with most cases */
2814 CopyMemory(pvarDest, pvarSrc, sizeof(*pvarDest));
2816 switch(pvarSrc->vt)
2818 case VT_FILETIME:
2819 break;
2820 case VT_STREAM:
2821 case VT_STREAMED_OBJECT:
2822 case VT_STORAGE:
2823 case VT_STORED_OBJECT:
2824 IUnknown_AddRef((LPUNKNOWN)pvarDest->u.pStream);
2825 break;
2826 case VT_CLSID:
2827 pvarDest->u.puuid = CoTaskMemAlloc(sizeof(CLSID));
2828 CopyMemory(pvarDest->u.puuid, pvarSrc->u.puuid, sizeof(CLSID));
2829 break;
2830 case VT_LPSTR:
2831 len = strlen(pvarSrc->u.pszVal);
2832 pvarDest->u.pszVal = CoTaskMemAlloc((len+1)*sizeof(CHAR));
2833 CopyMemory(pvarDest->u.pszVal, pvarSrc->u.pszVal, (len+1)*sizeof(CHAR));
2834 break;
2835 case VT_LPWSTR:
2836 len = lstrlenW(pvarSrc->u.pwszVal);
2837 pvarDest->u.pwszVal = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
2838 CopyMemory(pvarDest->u.pwszVal, pvarSrc->u.pwszVal, (len+1)*sizeof(WCHAR));
2839 break;
2840 case VT_BLOB:
2841 case VT_BLOB_OBJECT:
2842 if (pvarSrc->u.blob.pBlobData)
2844 len = pvarSrc->u.blob.cbSize;
2845 pvarDest->u.blob.pBlobData = CoTaskMemAlloc(len);
2846 CopyMemory(pvarDest->u.blob.pBlobData, pvarSrc->u.blob.pBlobData, len);
2848 break;
2849 case VT_BSTR:
2850 pvarDest->u.bstrVal = PropSysAllocString(pvarSrc->u.bstrVal);
2851 break;
2852 case VT_CF:
2853 if (pvarSrc->u.pclipdata)
2855 len = pvarSrc->u.pclipdata->cbSize - sizeof(pvarSrc->u.pclipdata->ulClipFmt);
2856 pvarDest->u.pclipdata = CoTaskMemAlloc(sizeof (CLIPDATA));
2857 pvarDest->u.pclipdata->cbSize = pvarSrc->u.pclipdata->cbSize;
2858 pvarDest->u.pclipdata->ulClipFmt = pvarSrc->u.pclipdata->ulClipFmt;
2859 pvarDest->u.pclipdata->pClipData = CoTaskMemAlloc(len);
2860 CopyMemory(pvarDest->u.pclipdata->pClipData, pvarSrc->u.pclipdata->pClipData, len);
2862 break;
2863 default:
2864 if (pvarSrc->vt & VT_VECTOR)
2866 int elemSize;
2867 ULONG i;
2869 switch(pvarSrc->vt & ~VT_VECTOR)
2871 case VT_I1: elemSize = sizeof(pvarSrc->u.cVal); break;
2872 case VT_UI1: elemSize = sizeof(pvarSrc->u.bVal); break;
2873 case VT_I2: elemSize = sizeof(pvarSrc->u.iVal); break;
2874 case VT_UI2: elemSize = sizeof(pvarSrc->u.uiVal); break;
2875 case VT_BOOL: elemSize = sizeof(pvarSrc->u.boolVal); break;
2876 case VT_I4: elemSize = sizeof(pvarSrc->u.lVal); break;
2877 case VT_UI4: elemSize = sizeof(pvarSrc->u.ulVal); break;
2878 case VT_R4: elemSize = sizeof(pvarSrc->u.fltVal); break;
2879 case VT_R8: elemSize = sizeof(pvarSrc->u.dblVal); break;
2880 case VT_ERROR: elemSize = sizeof(pvarSrc->u.scode); break;
2881 case VT_I8: elemSize = sizeof(pvarSrc->u.hVal); break;
2882 case VT_UI8: elemSize = sizeof(pvarSrc->u.uhVal); break;
2883 case VT_CY: elemSize = sizeof(pvarSrc->u.cyVal); break;
2884 case VT_DATE: elemSize = sizeof(pvarSrc->u.date); break;
2885 case VT_FILETIME: elemSize = sizeof(pvarSrc->u.filetime); break;
2886 case VT_CLSID: elemSize = sizeof(*pvarSrc->u.puuid); break;
2887 case VT_CF: elemSize = sizeof(*pvarSrc->u.pclipdata); break;
2888 case VT_BSTR: elemSize = sizeof(*pvarSrc->u.bstrVal); break;
2889 case VT_LPSTR: elemSize = sizeof(*pvarSrc->u.pszVal); break;
2890 case VT_LPWSTR: elemSize = sizeof(*pvarSrc->u.pwszVal); break;
2892 case VT_VARIANT:
2893 default:
2894 FIXME("Invalid element type: %ul\n", pvarSrc->vt & ~VT_VECTOR);
2895 return E_INVALIDARG;
2897 len = pvarSrc->u.capropvar.cElems;
2898 pvarDest->u.capropvar.pElems = CoTaskMemAlloc(len * elemSize);
2899 if (pvarSrc->vt == (VT_VECTOR | VT_VARIANT))
2901 for (i = 0; i < len; i++)
2902 PropVariantCopy(&pvarDest->u.capropvar.pElems[i], &pvarSrc->u.capropvar.pElems[i]);
2904 else if (pvarSrc->vt == (VT_VECTOR | VT_CF))
2906 FIXME("Copy clipformats\n");
2908 else if (pvarSrc->vt == (VT_VECTOR | VT_BSTR))
2910 for (i = 0; i < len; i++)
2911 pvarDest->u.cabstr.pElems[i] = PropSysAllocString(pvarSrc->u.cabstr.pElems[i]);
2913 else if (pvarSrc->vt == (VT_VECTOR | VT_LPSTR))
2915 size_t strLen;
2916 for (i = 0; i < len; i++)
2918 strLen = lstrlenA(pvarSrc->u.calpstr.pElems[i]) + 1;
2919 pvarDest->u.calpstr.pElems[i] = CoTaskMemAlloc(strLen);
2920 memcpy(pvarDest->u.calpstr.pElems[i],
2921 pvarSrc->u.calpstr.pElems[i], strLen);
2924 else if (pvarSrc->vt == (VT_VECTOR | VT_LPWSTR))
2926 size_t strLen;
2927 for (i = 0; i < len; i++)
2929 strLen = (lstrlenW(pvarSrc->u.calpwstr.pElems[i]) + 1) *
2930 sizeof(WCHAR);
2931 pvarDest->u.calpstr.pElems[i] = CoTaskMemAlloc(strLen);
2932 memcpy(pvarDest->u.calpstr.pElems[i],
2933 pvarSrc->u.calpstr.pElems[i], strLen);
2936 else
2937 CopyMemory(pvarDest->u.capropvar.pElems, pvarSrc->u.capropvar.pElems, len * elemSize);
2939 else
2940 WARN("Invalid/unsupported type %d\n", pvarSrc->vt);
2943 return S_OK;
2946 /***********************************************************************
2947 * FreePropVariantArray [OLE32.@]
2949 HRESULT WINAPI FreePropVariantArray(ULONG cVariants, /* [in] */
2950 PROPVARIANT *rgvars) /* [in/out] */
2952 ULONG i;
2954 TRACE("(%u, %p)\n", cVariants, rgvars);
2956 if (!rgvars)
2957 return E_INVALIDARG;
2959 for(i = 0; i < cVariants; i++)
2960 PropVariantClear(&rgvars[i]);
2962 return S_OK;
2965 /******************************************************************************
2966 * DllDebugObjectRPCHook (OLE32.@)
2967 * turns on and off internal debugging, pointer is only used on macintosh
2970 BOOL WINAPI DllDebugObjectRPCHook(BOOL b, void *dummy)
2972 FIXME("stub\n");
2973 return TRUE;