server: Moved get/set_thread_context implementation to ptrace.c.
[wine/wine-kai.git] / dlls / ole32 / ole2.c
blob1e890f9eb3d2167844f6012b1c85fe33c40810b6
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 ULONG 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 BOOL OLEMenu_InstallHooks( DWORD tid );
133 BOOL OLEMenu_UnInstallHooks( DWORD tid );
134 OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid );
135 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos );
136 BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor );
137 LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam);
138 LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam);
140 /******************************************************************************
141 * These are the prototypes of the OLE Clipboard initialization methods (in clipboard.c)
143 extern void OLEClipbrd_UnInitialize(void);
144 extern void OLEClipbrd_Initialize(void);
146 /******************************************************************************
147 * These are the prototypes of the utility methods used for OLE Drag n Drop
149 static void OLEDD_Initialize(void);
150 static void OLEDD_UnInitialize(void);
151 static DropTargetNode* OLEDD_FindDropTarget(
152 HWND hwndOfTarget);
153 static void OLEDD_FreeDropTarget(DropTargetNode*);
154 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(
155 HWND hwnd,
156 UINT uMsg,
157 WPARAM wParam,
158 LPARAM lParam);
159 static void OLEDD_TrackMouseMove(
160 TrackerWindowInfo* trackerInfo);
161 static void OLEDD_TrackStateChange(
162 TrackerWindowInfo* trackerInfo);
163 static DWORD OLEDD_GetButtonState(void);
166 /******************************************************************************
167 * OleBuildVersion [OLE2.1]
168 * OleBuildVersion [OLE32.@]
170 DWORD WINAPI OleBuildVersion(void)
172 TRACE("Returning version %d, build %d.\n", rmm, rup);
173 return (rmm<<16)+rup;
176 /***********************************************************************
177 * OleInitialize (OLE2.2)
178 * OleInitialize (OLE32.@)
180 HRESULT WINAPI OleInitialize(LPVOID reserved)
182 HRESULT hr;
184 TRACE("(%p)\n", reserved);
187 * The first duty of the OleInitialize is to initialize the COM libraries.
189 hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
192 * If the CoInitializeEx call failed, the OLE libraries can't be
193 * initialized.
195 if (FAILED(hr))
196 return hr;
199 * Then, it has to initialize the OLE specific modules.
200 * This includes:
201 * Clipboard
202 * Drag and Drop
203 * Object linking and Embedding
204 * In-place activation
206 if (OLE_moduleLockCount==0)
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();
230 * Then, we increase the lock count on the OLE module.
232 OLE_moduleLockCount++;
234 return hr;
237 /******************************************************************************
238 * OleUninitialize [OLE2.3]
239 * OleUninitialize [OLE32.@]
241 void WINAPI OleUninitialize(void)
243 TRACE("()\n");
246 * Decrease the lock count on the OLE module.
248 OLE_moduleLockCount--;
251 * If we hit the bottom of the lock stack, free the libraries.
253 if (OLE_moduleLockCount==0)
256 * Actually free the libraries.
258 TRACE("() - Freeing the last reference count\n");
261 * OLE Clipboard
263 OLEClipbrd_UnInitialize();
266 * Drag and Drop
268 OLEDD_UnInitialize();
271 * OLE shared menu
273 OLEMenu_UnInitialize();
277 * Then, uninitialize the COM libraries.
279 CoUninitialize();
282 /******************************************************************************
283 * OleInitializeWOW [OLE32.@]
285 HRESULT WINAPI OleInitializeWOW(DWORD x, DWORD y) {
286 FIXME("(0x%08lx, 0x%08lx),stub!\n",x, y);
287 return 0;
290 /***********************************************************************
291 * RegisterDragDrop (OLE32.@)
293 HRESULT WINAPI RegisterDragDrop(
294 HWND hwnd,
295 LPDROPTARGET pDropTarget)
297 DropTargetNode* dropTargetInfo;
299 TRACE("(%p,%p)\n", hwnd, pDropTarget);
301 if (!pDropTarget)
302 return E_INVALIDARG;
305 * First, check if the window is already registered.
307 dropTargetInfo = OLEDD_FindDropTarget(hwnd);
309 if (dropTargetInfo!=NULL)
310 return DRAGDROP_E_ALREADYREGISTERED;
313 * If it's not there, we can add it. We first create a node for it.
315 dropTargetInfo = HeapAlloc(GetProcessHeap(), 0, sizeof(DropTargetNode));
317 if (dropTargetInfo==NULL)
318 return E_OUTOFMEMORY;
320 dropTargetInfo->hwndTarget = hwnd;
323 * Don't forget that this is an interface pointer, need to nail it down since
324 * we keep a copy of it.
326 IDropTarget_AddRef(pDropTarget);
327 dropTargetInfo->dropTarget = pDropTarget;
329 list_add_tail(&targetListHead, &dropTargetInfo->entry);
331 return S_OK;
334 /***********************************************************************
335 * RevokeDragDrop (OLE32.@)
337 HRESULT WINAPI RevokeDragDrop(
338 HWND hwnd)
340 DropTargetNode* dropTargetInfo;
342 TRACE("(%p)\n", hwnd);
345 * First, check if the window is already registered.
347 dropTargetInfo = OLEDD_FindDropTarget(hwnd);
350 * If it ain't in there, it's an error.
352 if (dropTargetInfo==NULL)
353 return DRAGDROP_E_NOTREGISTERED;
355 OLEDD_FreeDropTarget(dropTargetInfo);
357 return S_OK;
360 /***********************************************************************
361 * OleRegGetUserType (OLE32.@)
363 * This implementation of OleRegGetUserType ignores the dwFormOfType
364 * parameter and always returns the full name of the object. This is
365 * not too bad since this is the case for many objects because of the
366 * way they are registered.
368 HRESULT WINAPI OleRegGetUserType(
369 REFCLSID clsid,
370 DWORD dwFormOfType,
371 LPOLESTR* pszUserType)
373 char keyName[60];
374 DWORD dwKeyType;
375 DWORD cbData;
376 HKEY clsidKey;
377 LONG hres;
378 LPSTR buffer;
379 HRESULT retVal;
381 * Initialize the out parameter.
383 *pszUserType = NULL;
386 * Build the key name we're looking for
388 sprintf( keyName, "CLSID\\{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\",
389 clsid->Data1, clsid->Data2, clsid->Data3,
390 clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
391 clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );
393 TRACE("(%s, %ld, %p)\n", keyName, dwFormOfType, pszUserType);
396 * Open the class id Key
398 hres = RegOpenKeyA(HKEY_CLASSES_ROOT,
399 keyName,
400 &clsidKey);
402 if (hres != ERROR_SUCCESS)
403 return REGDB_E_CLASSNOTREG;
406 * Retrieve the size of the name string.
408 cbData = 0;
410 hres = RegQueryValueExA(clsidKey,
412 NULL,
413 &dwKeyType,
414 NULL,
415 &cbData);
417 if (hres!=ERROR_SUCCESS)
419 RegCloseKey(clsidKey);
420 return REGDB_E_READREGDB;
424 * Allocate a buffer for the registry value.
426 *pszUserType = CoTaskMemAlloc(cbData*2);
428 if (*pszUserType==NULL)
430 RegCloseKey(clsidKey);
431 return E_OUTOFMEMORY;
434 buffer = HeapAlloc(GetProcessHeap(), 0, cbData);
436 if (buffer == NULL)
438 RegCloseKey(clsidKey);
439 CoTaskMemFree(*pszUserType);
440 *pszUserType=NULL;
441 return E_OUTOFMEMORY;
444 hres = RegQueryValueExA(clsidKey,
446 NULL,
447 &dwKeyType,
448 (LPBYTE) buffer,
449 &cbData);
451 RegCloseKey(clsidKey);
454 if (hres!=ERROR_SUCCESS)
456 CoTaskMemFree(*pszUserType);
457 *pszUserType=NULL;
459 retVal = REGDB_E_READREGDB;
461 else
463 MultiByteToWideChar( CP_ACP, 0, buffer, -1, *pszUserType, cbData /*FIXME*/ );
464 retVal = S_OK;
466 HeapFree(GetProcessHeap(), 0, buffer);
468 return retVal;
471 /***********************************************************************
472 * DoDragDrop [OLE32.@]
474 HRESULT WINAPI DoDragDrop (
475 IDataObject *pDataObject, /* [in] ptr to the data obj */
476 IDropSource* pDropSource, /* [in] ptr to the source obj */
477 DWORD dwOKEffect, /* [in] effects allowed by the source */
478 DWORD *pdwEffect) /* [out] ptr to effects of the source */
480 TrackerWindowInfo trackerInfo;
481 HWND hwndTrackWindow;
482 MSG msg;
484 TRACE("(DataObject %p, DropSource %p)\n", pDataObject, pDropSource);
487 * Setup the drag n drop tracking window.
489 if (!IsValidInterface((LPUNKNOWN)pDropSource))
490 return E_INVALIDARG;
492 trackerInfo.dataObject = pDataObject;
493 trackerInfo.dropSource = pDropSource;
494 trackerInfo.dwOKEffect = dwOKEffect;
495 trackerInfo.pdwEffect = pdwEffect;
496 trackerInfo.trackingDone = FALSE;
497 trackerInfo.escPressed = FALSE;
498 trackerInfo.curDragTargetHWND = 0;
499 trackerInfo.curTargetHWND = 0;
500 trackerInfo.curDragTarget = 0;
502 hwndTrackWindow = CreateWindowA(OLEDD_DRAGTRACKERCLASS,
503 "TrackerWindow",
504 WS_POPUP,
505 CW_USEDEFAULT, CW_USEDEFAULT,
506 CW_USEDEFAULT, CW_USEDEFAULT,
510 (LPVOID)&trackerInfo);
512 if (hwndTrackWindow!=0)
515 * Capture the mouse input
517 SetCapture(hwndTrackWindow);
519 msg.message = 0;
522 * Pump messages. All mouse input should go the the capture window.
524 while (!trackerInfo.trackingDone && GetMessageA(&msg, 0, 0, 0) )
526 trackerInfo.curMousePos.x = msg.pt.x;
527 trackerInfo.curMousePos.y = msg.pt.y;
528 trackerInfo.dwKeyState = OLEDD_GetButtonState();
530 if ( (msg.message >= WM_KEYFIRST) &&
531 (msg.message <= WM_KEYLAST) )
534 * When keyboard messages are sent to windows on this thread, we
535 * want to ignore notify the drop source that the state changed.
536 * in the case of the Escape key, we also notify the drop source
537 * we give it a special meaning.
539 if ( (msg.message==WM_KEYDOWN) &&
540 (msg.wParam==VK_ESCAPE) )
542 trackerInfo.escPressed = TRUE;
546 * Notify the drop source.
548 OLEDD_TrackStateChange(&trackerInfo);
550 else
553 * Dispatch the messages only when it's not a keyboard message.
555 DispatchMessageA(&msg);
559 /* re-post the quit message to outer message loop */
560 if (msg.message == WM_QUIT)
561 PostQuitMessage(msg.wParam);
563 * Destroy the temporary window.
565 DestroyWindow(hwndTrackWindow);
567 return trackerInfo.returnValue;
570 return E_FAIL;
573 /***********************************************************************
574 * OleQueryLinkFromData [OLE32.@]
576 HRESULT WINAPI OleQueryLinkFromData(
577 IDataObject* pSrcDataObject)
579 FIXME("(%p),stub!\n", pSrcDataObject);
580 return S_OK;
583 /***********************************************************************
584 * OleRegGetMiscStatus [OLE32.@]
586 HRESULT WINAPI OleRegGetMiscStatus(
587 REFCLSID clsid,
588 DWORD dwAspect,
589 DWORD* pdwStatus)
591 char keyName[60];
592 HKEY clsidKey;
593 HKEY miscStatusKey;
594 HKEY aspectKey;
595 LONG result;
598 * Initialize the out parameter.
600 *pdwStatus = 0;
603 * Build the key name we're looking for
605 sprintf( keyName, "CLSID\\{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\\",
606 clsid->Data1, clsid->Data2, clsid->Data3,
607 clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
608 clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );
610 TRACE("(%s, %ld, %p)\n", keyName, dwAspect, pdwStatus);
613 * Open the class id Key
615 result = RegOpenKeyA(HKEY_CLASSES_ROOT,
616 keyName,
617 &clsidKey);
619 if (result != ERROR_SUCCESS)
620 return REGDB_E_CLASSNOTREG;
623 * Get the MiscStatus
625 result = RegOpenKeyA(clsidKey,
626 "MiscStatus",
627 &miscStatusKey);
630 if (result != ERROR_SUCCESS)
632 RegCloseKey(clsidKey);
633 return REGDB_E_READREGDB;
637 * Read the default value
639 OLEUTL_ReadRegistryDWORDValue(miscStatusKey, pdwStatus);
642 * Open the key specific to the requested aspect.
644 sprintf(keyName, "%ld", dwAspect);
646 result = RegOpenKeyA(miscStatusKey,
647 keyName,
648 &aspectKey);
650 if (result == ERROR_SUCCESS)
652 OLEUTL_ReadRegistryDWORDValue(aspectKey, pdwStatus);
653 RegCloseKey(aspectKey);
657 * Cleanup
659 RegCloseKey(miscStatusKey);
660 RegCloseKey(clsidKey);
662 return S_OK;
665 static HRESULT EnumOLEVERB_Construct(HKEY hkeyVerb, ULONG index, IEnumOLEVERB **ppenum);
667 typedef struct
669 const IEnumOLEVERBVtbl *lpvtbl;
670 LONG ref;
672 HKEY hkeyVerb;
673 ULONG index;
674 } EnumOLEVERB;
676 static HRESULT WINAPI EnumOLEVERB_QueryInterface(
677 IEnumOLEVERB *iface, REFIID riid, void **ppv)
679 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
680 if (IsEqualIID(riid, &IID_IUnknown) ||
681 IsEqualIID(riid, &IID_IEnumOLEVERB))
683 IUnknown_AddRef(iface);
684 *ppv = iface;
685 return S_OK;
687 return E_NOINTERFACE;
690 static ULONG WINAPI EnumOLEVERB_AddRef(
691 IEnumOLEVERB *iface)
693 EnumOLEVERB *This = (EnumOLEVERB *)iface;
694 TRACE("()\n");
695 return InterlockedIncrement(&This->ref);
698 static ULONG WINAPI EnumOLEVERB_Release(
699 IEnumOLEVERB *iface)
701 EnumOLEVERB *This = (EnumOLEVERB *)iface;
702 LONG refs = InterlockedDecrement(&This->ref);
703 TRACE("()\n");
704 if (!refs)
706 RegCloseKey(This->hkeyVerb);
707 HeapFree(GetProcessHeap(), 0, This);
709 return refs;
712 static HRESULT WINAPI EnumOLEVERB_Next(
713 IEnumOLEVERB *iface, ULONG celt, LPOLEVERB rgelt,
714 ULONG *pceltFetched)
716 EnumOLEVERB *This = (EnumOLEVERB *)iface;
717 HRESULT hr = S_OK;
719 TRACE("(%ld, %p, %p)\n", celt, rgelt, pceltFetched);
721 if (pceltFetched)
722 *pceltFetched = 0;
724 for (; celt; celt--, rgelt++)
726 WCHAR wszSubKey[20];
727 LONG cbData;
728 LPWSTR pwszOLEVERB;
729 LPWSTR pwszMenuFlags;
730 LPWSTR pwszAttribs;
731 LONG res = RegEnumKeyW(This->hkeyVerb, This->index, wszSubKey, sizeof(wszSubKey)/sizeof(wszSubKey[0]));
732 if (res == ERROR_NO_MORE_ITEMS)
734 hr = S_FALSE;
735 break;
737 else if (res != ERROR_SUCCESS)
739 ERR("RegEnumKeyW failed with error %ld\n", res);
740 hr = REGDB_E_READREGDB;
741 break;
743 res = RegQueryValueW(This->hkeyVerb, wszSubKey, NULL, &cbData);
744 if (res != ERROR_SUCCESS)
746 ERR("RegQueryValueW failed with error %ld\n", res);
747 hr = REGDB_E_READREGDB;
748 break;
750 pwszOLEVERB = CoTaskMemAlloc(cbData);
751 if (!pwszOLEVERB)
753 hr = E_OUTOFMEMORY;
754 break;
756 res = RegQueryValueW(This->hkeyVerb, wszSubKey, pwszOLEVERB, &cbData);
757 if (res != ERROR_SUCCESS)
759 ERR("RegQueryValueW failed with error %ld\n", res);
760 hr = REGDB_E_READREGDB;
761 CoTaskMemFree(pwszOLEVERB);
762 break;
765 TRACE("verb string: %s\n", debugstr_w(pwszOLEVERB));
766 pwszMenuFlags = strchrW(pwszOLEVERB, ',');
767 if (!pwszMenuFlags)
769 hr = OLEOBJ_E_INVALIDVERB;
770 CoTaskMemFree(pwszOLEVERB);
771 break;
773 /* nul terminate the name string and advance to first character */
774 *pwszMenuFlags = '\0';
775 pwszMenuFlags++;
776 pwszAttribs = strchrW(pwszMenuFlags, ',');
777 if (!pwszAttribs)
779 hr = OLEOBJ_E_INVALIDVERB;
780 CoTaskMemFree(pwszOLEVERB);
781 break;
783 /* nul terminate the menu string and advance to first character */
784 *pwszAttribs = '\0';
785 pwszAttribs++;
787 /* fill out structure for this verb */
788 rgelt->lVerb = atolW(wszSubKey);
789 rgelt->lpszVerbName = pwszOLEVERB; /* user should free */
790 rgelt->fuFlags = atolW(pwszMenuFlags);
791 rgelt->grfAttribs = atolW(pwszAttribs);
793 if (pceltFetched)
794 (*pceltFetched)++;
795 This->index++;
797 return hr;
800 static HRESULT WINAPI EnumOLEVERB_Skip(
801 IEnumOLEVERB *iface, ULONG celt)
803 EnumOLEVERB *This = (EnumOLEVERB *)iface;
805 TRACE("(%ld)\n", celt);
807 This->index += celt;
808 return S_OK;
811 static HRESULT WINAPI EnumOLEVERB_Reset(
812 IEnumOLEVERB *iface)
814 EnumOLEVERB *This = (EnumOLEVERB *)iface;
816 TRACE("()\n");
818 This->index = 0;
819 return S_OK;
822 static HRESULT WINAPI EnumOLEVERB_Clone(
823 IEnumOLEVERB *iface,
824 IEnumOLEVERB **ppenum)
826 EnumOLEVERB *This = (EnumOLEVERB *)iface;
827 HKEY hkeyVerb;
828 TRACE("(%p)\n", ppenum);
829 if (!DuplicateHandle(GetCurrentProcess(), This->hkeyVerb, GetCurrentProcess(), (HANDLE *)&hkeyVerb, 0, FALSE, DUPLICATE_SAME_ACCESS))
830 return HRESULT_FROM_WIN32(GetLastError());
831 return EnumOLEVERB_Construct(hkeyVerb, This->index, ppenum);
834 static const IEnumOLEVERBVtbl EnumOLEVERB_VTable =
836 EnumOLEVERB_QueryInterface,
837 EnumOLEVERB_AddRef,
838 EnumOLEVERB_Release,
839 EnumOLEVERB_Next,
840 EnumOLEVERB_Skip,
841 EnumOLEVERB_Reset,
842 EnumOLEVERB_Clone
845 static HRESULT EnumOLEVERB_Construct(HKEY hkeyVerb, ULONG index, IEnumOLEVERB **ppenum)
847 EnumOLEVERB *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
848 if (!This)
850 RegCloseKey(hkeyVerb);
851 return E_OUTOFMEMORY;
853 This->lpvtbl = &EnumOLEVERB_VTable;
854 This->ref = 1;
855 This->index = index;
856 This->hkeyVerb = hkeyVerb;
857 *ppenum = (IEnumOLEVERB *)&This->lpvtbl;
858 return S_OK;
861 /***********************************************************************
862 * OleRegEnumVerbs [OLE32.@]
864 * Enumerates verbs associated with a class stored in the registry.
866 * PARAMS
867 * clsid [I] Class ID to enumerate the verbs for.
868 * ppenum [O] Enumerator.
870 * RETURNS
871 * S_OK: Success.
872 * REGDB_E_CLASSNOTREG: The specified class does not have a key in the registry.
873 * REGDB_E_READREGDB: The class key could not be opened for some other reason.
874 * OLE_E_REGDB_KEY: The Verb subkey for the class is not present.
875 * OLEOBJ_E_NOVERBS: The Verb subkey for the class is empty.
877 HRESULT WINAPI OleRegEnumVerbs (REFCLSID clsid, LPENUMOLEVERB* ppenum)
879 LONG res;
880 HKEY hkeyVerb;
881 DWORD dwSubKeys;
882 static const WCHAR wszVerb[] = {'V','e','r','b',0};
884 TRACE("(%s, %p)\n", debugstr_guid(clsid), ppenum);
886 res = COM_OpenKeyForCLSID(clsid, wszVerb, KEY_READ, &hkeyVerb);
887 if (FAILED(res))
889 if (res == REGDB_E_CLASSNOTREG)
890 ERR("CLSID %s not registered\n", debugstr_guid(clsid));
891 else if (res == REGDB_E_KEYMISSING)
892 ERR("no Verbs key for class %s\n", debugstr_guid(clsid));
893 else
894 ERR("failed to open Verbs key for CLSID %s with error %ld\n",
895 debugstr_guid(clsid), res);
896 return res;
899 res = RegQueryInfoKeyW(hkeyVerb, NULL, NULL, NULL, &dwSubKeys, NULL,
900 NULL, NULL, NULL, NULL, NULL, NULL);
901 if (res != ERROR_SUCCESS)
903 ERR("failed to get subkey count with error %ld\n", GetLastError());
904 return REGDB_E_READREGDB;
907 if (!dwSubKeys)
909 WARN("class %s has no verbs\n", debugstr_guid(clsid));
910 RegCloseKey(hkeyVerb);
911 return OLEOBJ_E_NOVERBS;
914 return EnumOLEVERB_Construct(hkeyVerb, 0, ppenum);
917 /******************************************************************************
918 * OleSetContainedObject [OLE32.@]
920 HRESULT WINAPI OleSetContainedObject(
921 LPUNKNOWN pUnknown,
922 BOOL fContained)
924 IRunnableObject* runnable = NULL;
925 HRESULT hres;
927 TRACE("(%p,%x)\n", pUnknown, fContained);
929 hres = IUnknown_QueryInterface(pUnknown,
930 &IID_IRunnableObject,
931 (void**)&runnable);
933 if (SUCCEEDED(hres))
935 hres = IRunnableObject_SetContainedObject(runnable, fContained);
937 IRunnableObject_Release(runnable);
939 return hres;
942 return S_OK;
945 /******************************************************************************
946 * OleLoad [OLE32.@]
948 HRESULT WINAPI OleLoad(
949 LPSTORAGE pStg,
950 REFIID riid,
951 LPOLECLIENTSITE pClientSite,
952 LPVOID* ppvObj)
954 IPersistStorage* persistStorage = NULL;
955 IOleObject* oleObject = NULL;
956 STATSTG storageInfo;
957 HRESULT hres;
959 TRACE("(%p,%p,%p,%p)\n", pStg, riid, pClientSite, ppvObj);
962 * TODO, Conversion ... OleDoAutoConvert
966 * Get the class ID for the object.
968 hres = IStorage_Stat(pStg, &storageInfo, STATFLAG_NONAME);
971 * Now, try and create the handler for the object
973 hres = CoCreateInstance(&storageInfo.clsid,
974 NULL,
975 CLSCTX_INPROC_HANDLER,
976 &IID_IOleObject,
977 (void**)&oleObject);
980 * If that fails, as it will most times, load the default
981 * OLE handler.
983 if (FAILED(hres))
985 hres = OleCreateDefaultHandler(&storageInfo.clsid,
986 NULL,
987 &IID_IOleObject,
988 (void**)&oleObject);
992 * If we couldn't find a handler... this is bad. Abort the whole thing.
994 if (FAILED(hres))
995 return hres;
998 * Inform the new object of it's client site.
1000 hres = IOleObject_SetClientSite(oleObject, pClientSite);
1003 * Initialize the object with it's IPersistStorage interface.
1005 hres = IOleObject_QueryInterface(oleObject,
1006 &IID_IPersistStorage,
1007 (void**)&persistStorage);
1009 if (SUCCEEDED(hres))
1011 IPersistStorage_Load(persistStorage, pStg);
1013 IPersistStorage_Release(persistStorage);
1014 persistStorage = NULL;
1018 * Return the requested interface to the caller.
1020 hres = IOleObject_QueryInterface(oleObject, riid, ppvObj);
1023 * Cleanup interfaces used internally
1025 IOleObject_Release(oleObject);
1027 return hres;
1030 /***********************************************************************
1031 * OleSave [OLE32.@]
1033 HRESULT WINAPI OleSave(
1034 LPPERSISTSTORAGE pPS,
1035 LPSTORAGE pStg,
1036 BOOL fSameAsLoad)
1038 HRESULT hres;
1039 CLSID objectClass;
1041 TRACE("(%p,%p,%x)\n", pPS, pStg, fSameAsLoad);
1044 * First, we transfer the class ID (if available)
1046 hres = IPersistStorage_GetClassID(pPS, &objectClass);
1048 if (SUCCEEDED(hres))
1050 WriteClassStg(pStg, &objectClass);
1054 * Then, we ask the object to save itself to the
1055 * storage. If it is successful, we commit the storage.
1057 hres = IPersistStorage_Save(pPS, pStg, fSameAsLoad);
1059 if (SUCCEEDED(hres))
1061 IStorage_Commit(pStg,
1062 STGC_DEFAULT);
1065 return hres;
1069 /******************************************************************************
1070 * OleLockRunning [OLE32.@]
1072 HRESULT WINAPI OleLockRunning(LPUNKNOWN pUnknown, BOOL fLock, BOOL fLastUnlockCloses)
1074 IRunnableObject* runnable = NULL;
1075 HRESULT hres;
1077 TRACE("(%p,%x,%x)\n", pUnknown, fLock, fLastUnlockCloses);
1079 hres = IUnknown_QueryInterface(pUnknown,
1080 &IID_IRunnableObject,
1081 (void**)&runnable);
1083 if (SUCCEEDED(hres))
1085 hres = IRunnableObject_LockRunning(runnable, fLock, fLastUnlockCloses);
1087 IRunnableObject_Release(runnable);
1089 return hres;
1091 else
1092 return E_INVALIDARG;
1096 /**************************************************************************
1097 * Internal methods to manage the shared OLE menu in response to the
1098 * OLE***MenuDescriptor API
1101 /***
1102 * OLEMenu_Initialize()
1104 * Initializes the OLEMENU data structures.
1106 static void OLEMenu_Initialize()
1110 /***
1111 * OLEMenu_UnInitialize()
1113 * Releases the OLEMENU data structures.
1115 static void OLEMenu_UnInitialize()
1119 /*************************************************************************
1120 * OLEMenu_InstallHooks
1121 * Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
1123 * RETURNS: TRUE if message hooks were successfully installed
1124 * FALSE on failure
1126 BOOL OLEMenu_InstallHooks( DWORD tid )
1128 OleMenuHookItem *pHookItem = NULL;
1130 /* Create an entry for the hook table */
1131 if ( !(pHookItem = HeapAlloc(GetProcessHeap(), 0,
1132 sizeof(OleMenuHookItem)) ) )
1133 return FALSE;
1135 pHookItem->tid = tid;
1136 pHookItem->hHeap = GetProcessHeap();
1138 /* Install a thread scope message hook for WH_GETMESSAGE */
1139 pHookItem->GetMsg_hHook = SetWindowsHookExA( WH_GETMESSAGE, OLEMenu_GetMsgProc,
1140 0, GetCurrentThreadId() );
1141 if ( !pHookItem->GetMsg_hHook )
1142 goto CLEANUP;
1144 /* Install a thread scope message hook for WH_CALLWNDPROC */
1145 pHookItem->CallWndProc_hHook = SetWindowsHookExA( WH_CALLWNDPROC, OLEMenu_CallWndProc,
1146 0, GetCurrentThreadId() );
1147 if ( !pHookItem->CallWndProc_hHook )
1148 goto CLEANUP;
1150 /* Insert the hook table entry */
1151 pHookItem->next = hook_list;
1152 hook_list = pHookItem;
1154 return TRUE;
1156 CLEANUP:
1157 /* Unhook any hooks */
1158 if ( pHookItem->GetMsg_hHook )
1159 UnhookWindowsHookEx( pHookItem->GetMsg_hHook );
1160 if ( pHookItem->CallWndProc_hHook )
1161 UnhookWindowsHookEx( pHookItem->CallWndProc_hHook );
1162 /* Release the hook table entry */
1163 HeapFree(pHookItem->hHeap, 0, pHookItem );
1165 return FALSE;
1168 /*************************************************************************
1169 * OLEMenu_UnInstallHooks
1170 * UnInstall thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
1172 * RETURNS: TRUE if message hooks were successfully installed
1173 * FALSE on failure
1175 BOOL OLEMenu_UnInstallHooks( DWORD tid )
1177 OleMenuHookItem *pHookItem = NULL;
1178 OleMenuHookItem **ppHook = &hook_list;
1180 while (*ppHook)
1182 if ((*ppHook)->tid == tid)
1184 pHookItem = *ppHook;
1185 *ppHook = pHookItem->next;
1186 break;
1188 ppHook = &(*ppHook)->next;
1190 if (!pHookItem) return FALSE;
1192 /* Uninstall the hooks installed for this thread */
1193 if ( !UnhookWindowsHookEx( pHookItem->GetMsg_hHook ) )
1194 goto CLEANUP;
1195 if ( !UnhookWindowsHookEx( pHookItem->CallWndProc_hHook ) )
1196 goto CLEANUP;
1198 /* Release the hook table entry */
1199 HeapFree(pHookItem->hHeap, 0, pHookItem );
1201 return TRUE;
1203 CLEANUP:
1204 /* Release the hook table entry */
1205 HeapFree(pHookItem->hHeap, 0, pHookItem );
1207 return FALSE;
1210 /*************************************************************************
1211 * OLEMenu_IsHookInstalled
1212 * Tests if OLEMenu hooks have been installed for a thread
1214 * RETURNS: The pointer and index of the hook table entry for the tid
1215 * NULL and -1 for the index if no hooks were installed for this thread
1217 OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid )
1219 OleMenuHookItem *pHookItem = NULL;
1221 /* Do a simple linear search for an entry whose tid matches ours.
1222 * We really need a map but efficiency is not a concern here. */
1223 for (pHookItem = hook_list; pHookItem; pHookItem = pHookItem->next)
1225 if ( tid == pHookItem->tid )
1226 return pHookItem;
1229 return NULL;
1232 /***********************************************************************
1233 * OLEMenu_FindMainMenuIndex
1235 * Used by OLEMenu API to find the top level group a menu item belongs to.
1236 * On success pnPos contains the index of the item in the top level menu group
1238 * RETURNS: TRUE if the ID was found, FALSE on failure
1240 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos )
1242 UINT i, nItems;
1244 nItems = GetMenuItemCount( hMainMenu );
1246 for (i = 0; i < nItems; i++)
1248 HMENU hsubmenu;
1250 /* Is the current item a submenu? */
1251 if ( (hsubmenu = GetSubMenu(hMainMenu, i)) )
1253 /* If the handle is the same we're done */
1254 if ( hsubmenu == hPopupMenu )
1256 if (pnPos)
1257 *pnPos = i;
1258 return TRUE;
1260 /* Recursively search without updating pnPos */
1261 else if ( OLEMenu_FindMainMenuIndex( hsubmenu, hPopupMenu, NULL ) )
1263 if (pnPos)
1264 *pnPos = i;
1265 return TRUE;
1270 return FALSE;
1273 /***********************************************************************
1274 * OLEMenu_SetIsServerMenu
1276 * Checks whether a popup menu belongs to a shared menu group which is
1277 * owned by the server, and sets the menu descriptor state accordingly.
1278 * All menu messages from these groups should be routed to the server.
1280 * RETURNS: TRUE if the popup menu is part of a server owned group
1281 * FALSE if the popup menu is part of a container owned group
1283 BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor )
1285 UINT nPos = 0, nWidth, i;
1287 pOleMenuDescriptor->bIsServerItem = FALSE;
1289 /* Don't bother searching if the popup is the combined menu itself */
1290 if ( hmenu == pOleMenuDescriptor->hmenuCombined )
1291 return FALSE;
1293 /* Find the menu item index in the shared OLE menu that this item belongs to */
1294 if ( !OLEMenu_FindMainMenuIndex( pOleMenuDescriptor->hmenuCombined, hmenu, &nPos ) )
1295 return FALSE;
1297 /* The group widths array has counts for the number of elements
1298 * in the groups File, Edit, Container, Object, Window, Help.
1299 * The Edit, Object & Help groups belong to the server object
1300 * and the other three belong to the container.
1301 * Loop through the group widths and locate the group we are a member of.
1303 for ( i = 0, nWidth = 0; i < 6; i++ )
1305 nWidth += pOleMenuDescriptor->mgw.width[i];
1306 if ( nPos < nWidth )
1308 /* Odd elements are server menu widths */
1309 pOleMenuDescriptor->bIsServerItem = (i%2) ? TRUE : FALSE;
1310 break;
1314 return pOleMenuDescriptor->bIsServerItem;
1317 /*************************************************************************
1318 * OLEMenu_CallWndProc
1319 * Thread scope WH_CALLWNDPROC hook proc filter function (callback)
1320 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1322 LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam)
1324 LPCWPSTRUCT pMsg = NULL;
1325 HOLEMENU hOleMenu = 0;
1326 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1327 OleMenuHookItem *pHookItem = NULL;
1328 WORD fuFlags;
1330 TRACE("%i, %04x, %08x\n", code, wParam, (unsigned)lParam );
1332 /* Check if we're being asked to process the message */
1333 if ( HC_ACTION != code )
1334 goto NEXTHOOK;
1336 /* Retrieve the current message being dispatched from lParam */
1337 pMsg = (LPCWPSTRUCT)lParam;
1339 /* Check if the message is destined for a window we are interested in:
1340 * If the window has an OLEMenu property we may need to dispatch
1341 * the menu message to its active objects window instead. */
1343 hOleMenu = (HOLEMENU)GetPropA( pMsg->hwnd, "PROP_OLEMenuDescriptor" );
1344 if ( !hOleMenu )
1345 goto NEXTHOOK;
1347 /* Get the menu descriptor */
1348 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1349 if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1350 goto NEXTHOOK;
1352 /* Process menu messages */
1353 switch( pMsg->message )
1355 case WM_INITMENU:
1357 /* Reset the menu descriptor state */
1358 pOleMenuDescriptor->bIsServerItem = FALSE;
1360 /* Send this message to the server as well */
1361 SendMessageA( pOleMenuDescriptor->hwndActiveObject,
1362 pMsg->message, pMsg->wParam, pMsg->lParam );
1363 goto NEXTHOOK;
1366 case WM_INITMENUPOPUP:
1368 /* Save the state for whether this is a server owned menu */
1369 OLEMenu_SetIsServerMenu( (HMENU)pMsg->wParam, pOleMenuDescriptor );
1370 break;
1373 case WM_MENUSELECT:
1375 fuFlags = HIWORD(pMsg->wParam); /* Get flags */
1376 if ( fuFlags & MF_SYSMENU )
1377 goto NEXTHOOK;
1379 /* Save the state for whether this is a server owned popup menu */
1380 else if ( fuFlags & MF_POPUP )
1381 OLEMenu_SetIsServerMenu( (HMENU)pMsg->lParam, pOleMenuDescriptor );
1383 break;
1386 case WM_DRAWITEM:
1388 LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT) pMsg->lParam;
1389 if ( pMsg->wParam != 0 || lpdis->CtlType != ODT_MENU )
1390 goto NEXTHOOK; /* Not a menu message */
1392 break;
1395 default:
1396 goto NEXTHOOK;
1399 /* If the message was for the server dispatch it accordingly */
1400 if ( pOleMenuDescriptor->bIsServerItem )
1402 SendMessageA( pOleMenuDescriptor->hwndActiveObject,
1403 pMsg->message, pMsg->wParam, pMsg->lParam );
1406 NEXTHOOK:
1407 if ( pOleMenuDescriptor )
1408 GlobalUnlock( hOleMenu );
1410 /* Lookup the hook item for the current thread */
1411 if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1413 /* This should never fail!! */
1414 WARN("could not retrieve hHook for current thread!\n" );
1415 return 0;
1418 /* Pass on the message to the next hooker */
1419 return CallNextHookEx( pHookItem->CallWndProc_hHook, code, wParam, lParam );
1422 /*************************************************************************
1423 * OLEMenu_GetMsgProc
1424 * Thread scope WH_GETMESSAGE hook proc filter function (callback)
1425 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1427 LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam)
1429 LPMSG pMsg = NULL;
1430 HOLEMENU hOleMenu = 0;
1431 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1432 OleMenuHookItem *pHookItem = NULL;
1433 WORD wCode;
1435 TRACE("%i, %04x, %08x\n", code, wParam, (unsigned)lParam );
1437 /* Check if we're being asked to process a messages */
1438 if ( HC_ACTION != code )
1439 goto NEXTHOOK;
1441 /* Retrieve the current message being dispatched from lParam */
1442 pMsg = (LPMSG)lParam;
1444 /* Check if the message is destined for a window we are interested in:
1445 * If the window has an OLEMenu property we may need to dispatch
1446 * the menu message to its active objects window instead. */
1448 hOleMenu = (HOLEMENU)GetPropA( pMsg->hwnd, "PROP_OLEMenuDescriptor" );
1449 if ( !hOleMenu )
1450 goto NEXTHOOK;
1452 /* Process menu messages */
1453 switch( pMsg->message )
1455 case WM_COMMAND:
1457 wCode = HIWORD(pMsg->wParam); /* Get notification code */
1458 if ( wCode )
1459 goto NEXTHOOK; /* Not a menu message */
1460 break;
1462 default:
1463 goto NEXTHOOK;
1466 /* Get the menu descriptor */
1467 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1468 if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1469 goto NEXTHOOK;
1471 /* If the message was for the server dispatch it accordingly */
1472 if ( pOleMenuDescriptor->bIsServerItem )
1474 /* Change the hWnd in the message to the active objects hWnd.
1475 * The message loop which reads this message will automatically
1476 * dispatch it to the embedded objects window. */
1477 pMsg->hwnd = pOleMenuDescriptor->hwndActiveObject;
1480 NEXTHOOK:
1481 if ( pOleMenuDescriptor )
1482 GlobalUnlock( hOleMenu );
1484 /* Lookup the hook item for the current thread */
1485 if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1487 /* This should never fail!! */
1488 WARN("could not retrieve hHook for current thread!\n" );
1489 return FALSE;
1492 /* Pass on the message to the next hooker */
1493 return CallNextHookEx( pHookItem->GetMsg_hHook, code, wParam, lParam );
1496 /***********************************************************************
1497 * OleCreateMenuDescriptor [OLE32.@]
1498 * Creates an OLE menu descriptor for OLE to use when dispatching
1499 * menu messages and commands.
1501 * PARAMS:
1502 * hmenuCombined - Handle to the objects combined menu
1503 * lpMenuWidths - Pointer to array of 6 LONG's indicating menus per group
1506 HOLEMENU WINAPI OleCreateMenuDescriptor(
1507 HMENU hmenuCombined,
1508 LPOLEMENUGROUPWIDTHS lpMenuWidths)
1510 HOLEMENU hOleMenu;
1511 OleMenuDescriptor *pOleMenuDescriptor;
1512 int i;
1514 if ( !hmenuCombined || !lpMenuWidths )
1515 return 0;
1517 /* Create an OLE menu descriptor */
1518 if ( !(hOleMenu = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT,
1519 sizeof(OleMenuDescriptor) ) ) )
1520 return 0;
1522 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1523 if ( !pOleMenuDescriptor )
1524 return 0;
1526 /* Initialize menu group widths and hmenu */
1527 for ( i = 0; i < 6; i++ )
1528 pOleMenuDescriptor->mgw.width[i] = lpMenuWidths->width[i];
1530 pOleMenuDescriptor->hmenuCombined = hmenuCombined;
1531 pOleMenuDescriptor->bIsServerItem = FALSE;
1532 GlobalUnlock( hOleMenu );
1534 return hOleMenu;
1537 /***********************************************************************
1538 * OleDestroyMenuDescriptor [OLE32.@]
1539 * Destroy the shared menu descriptor
1541 HRESULT WINAPI OleDestroyMenuDescriptor(
1542 HOLEMENU hmenuDescriptor)
1544 if ( hmenuDescriptor )
1545 GlobalFree( hmenuDescriptor );
1546 return S_OK;
1549 /***********************************************************************
1550 * OleSetMenuDescriptor [OLE32.@]
1551 * Installs or removes OLE dispatching code for the containers frame window.
1553 * PARAMS
1554 * hOleMenu Handle to composite menu descriptor
1555 * hwndFrame Handle to containers frame window
1556 * hwndActiveObject Handle to objects in-place activation window
1557 * lpFrame Pointer to IOleInPlaceFrame on containers window
1558 * lpActiveObject Pointer to IOleInPlaceActiveObject on active in-place object
1560 * RETURNS
1561 * S_OK - menu installed correctly
1562 * E_FAIL, E_INVALIDARG, E_UNEXPECTED - failure
1564 * FIXME
1565 * The lpFrame and lpActiveObject parameters are currently ignored
1566 * OLE should install context sensitive help F1 filtering for the app when
1567 * these are non null.
1569 HRESULT WINAPI OleSetMenuDescriptor(
1570 HOLEMENU hOleMenu,
1571 HWND hwndFrame,
1572 HWND hwndActiveObject,
1573 LPOLEINPLACEFRAME lpFrame,
1574 LPOLEINPLACEACTIVEOBJECT lpActiveObject)
1576 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1578 /* Check args */
1579 if ( !hwndFrame || (hOleMenu && !hwndActiveObject) )
1580 return E_INVALIDARG;
1582 if ( lpFrame || lpActiveObject )
1584 FIXME("(%p, %p, %p, %p, %p), Context sensitive help filtering not implemented!\n",
1585 hOleMenu,
1586 hwndFrame,
1587 hwndActiveObject,
1588 lpFrame,
1589 lpActiveObject);
1592 /* Set up a message hook to intercept the containers frame window messages.
1593 * The message filter is responsible for dispatching menu messages from the
1594 * shared menu which are intended for the object.
1597 if ( hOleMenu ) /* Want to install dispatching code */
1599 /* If OLEMenu hooks are already installed for this thread, fail
1600 * Note: This effectively means that OleSetMenuDescriptor cannot
1601 * be called twice in succession on the same frame window
1602 * without first calling it with a null hOleMenu to uninstall */
1603 if ( OLEMenu_IsHookInstalled( GetCurrentThreadId() ) )
1604 return E_FAIL;
1606 /* Get the menu descriptor */
1607 pOleMenuDescriptor = (OleMenuDescriptor *) GlobalLock( hOleMenu );
1608 if ( !pOleMenuDescriptor )
1609 return E_UNEXPECTED;
1611 /* Update the menu descriptor */
1612 pOleMenuDescriptor->hwndFrame = hwndFrame;
1613 pOleMenuDescriptor->hwndActiveObject = hwndActiveObject;
1615 GlobalUnlock( hOleMenu );
1616 pOleMenuDescriptor = NULL;
1618 /* Add a menu descriptor windows property to the frame window */
1619 SetPropA( hwndFrame, "PROP_OLEMenuDescriptor", hOleMenu );
1621 /* Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC */
1622 if ( !OLEMenu_InstallHooks( GetCurrentThreadId() ) )
1623 return E_FAIL;
1625 else /* Want to uninstall dispatching code */
1627 /* Uninstall the hooks */
1628 if ( !OLEMenu_UnInstallHooks( GetCurrentThreadId() ) )
1629 return E_FAIL;
1631 /* Remove the menu descriptor property from the frame window */
1632 RemovePropA( hwndFrame, "PROP_OLEMenuDescriptor" );
1635 return S_OK;
1638 /******************************************************************************
1639 * IsAccelerator [OLE32.@]
1640 * Mostly copied from controls/menu.c TranslateAccelerator implementation
1642 BOOL WINAPI IsAccelerator(HACCEL hAccel, int cAccelEntries, LPMSG lpMsg, WORD* lpwCmd)
1644 LPACCEL lpAccelTbl;
1645 int i;
1647 if(!lpMsg) return FALSE;
1648 if (!hAccel)
1650 WARN_(accel)("NULL accel handle\n");
1651 return FALSE;
1653 if((lpMsg->message != WM_KEYDOWN &&
1654 lpMsg->message != WM_KEYUP &&
1655 lpMsg->message != WM_SYSKEYDOWN &&
1656 lpMsg->message != WM_SYSKEYUP &&
1657 lpMsg->message != WM_CHAR)) return FALSE;
1658 lpAccelTbl = HeapAlloc(GetProcessHeap(), 0, cAccelEntries * sizeof(ACCEL));
1659 if (NULL == lpAccelTbl)
1661 return FALSE;
1663 if (CopyAcceleratorTableW(hAccel, lpAccelTbl, cAccelEntries) != cAccelEntries)
1665 WARN_(accel)("CopyAcceleratorTableW failed\n");
1666 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
1667 return FALSE;
1670 TRACE_(accel)("hAccel=%p, cAccelEntries=%d,"
1671 "msg->hwnd=%p, msg->message=%04x, wParam=%08x, lParam=%08lx\n",
1672 hAccel, cAccelEntries,
1673 lpMsg->hwnd, lpMsg->message, lpMsg->wParam, lpMsg->lParam);
1674 for(i = 0; i < cAccelEntries; i++)
1676 if(lpAccelTbl[i].key != lpMsg->wParam)
1677 continue;
1679 if(lpMsg->message == WM_CHAR)
1681 if(!(lpAccelTbl[i].fVirt & FALT) && !(lpAccelTbl[i].fVirt & FVIRTKEY))
1683 TRACE_(accel)("found accel for WM_CHAR: ('%c')\n", lpMsg->wParam & 0xff);
1684 goto found;
1687 else
1689 if(lpAccelTbl[i].fVirt & FVIRTKEY)
1691 INT mask = 0;
1692 TRACE_(accel)("found accel for virt_key %04x (scan %04x)\n",
1693 lpMsg->wParam, HIWORD(lpMsg->lParam) & 0xff);
1694 if(GetKeyState(VK_SHIFT) & 0x8000) mask |= FSHIFT;
1695 if(GetKeyState(VK_CONTROL) & 0x8000) mask |= FCONTROL;
1696 if(GetKeyState(VK_MENU) & 0x8000) mask |= FALT;
1697 if(mask == (lpAccelTbl[i].fVirt & (FSHIFT | FCONTROL | FALT))) goto found;
1698 TRACE_(accel)("incorrect SHIFT/CTRL/ALT-state\n");
1700 else
1702 if(!(lpMsg->lParam & 0x01000000)) /* no special_key */
1704 if((lpAccelTbl[i].fVirt & FALT) && (lpMsg->lParam & 0x20000000))
1705 { /* ^^ ALT pressed */
1706 TRACE_(accel)("found accel for Alt-%c\n", lpMsg->wParam & 0xff);
1707 goto found;
1714 WARN_(accel)("couldn't translate accelerator key\n");
1715 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
1716 return FALSE;
1718 found:
1719 if(lpwCmd) *lpwCmd = lpAccelTbl[i].cmd;
1720 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
1721 return TRUE;
1724 /***********************************************************************
1725 * ReleaseStgMedium [OLE32.@]
1727 void WINAPI ReleaseStgMedium(
1728 STGMEDIUM* pmedium)
1730 switch (pmedium->tymed)
1732 case TYMED_HGLOBAL:
1734 if ( (pmedium->pUnkForRelease==0) &&
1735 (pmedium->u.hGlobal!=0) )
1736 GlobalFree(pmedium->u.hGlobal);
1737 break;
1739 case TYMED_FILE:
1741 if (pmedium->u.lpszFileName!=0)
1743 if (pmedium->pUnkForRelease==0)
1745 DeleteFileW(pmedium->u.lpszFileName);
1748 CoTaskMemFree(pmedium->u.lpszFileName);
1750 break;
1752 case TYMED_ISTREAM:
1754 if (pmedium->u.pstm!=0)
1756 IStream_Release(pmedium->u.pstm);
1758 break;
1760 case TYMED_ISTORAGE:
1762 if (pmedium->u.pstg!=0)
1764 IStorage_Release(pmedium->u.pstg);
1766 break;
1768 case TYMED_GDI:
1770 if ( (pmedium->pUnkForRelease==0) &&
1771 (pmedium->u.hBitmap!=0) )
1772 DeleteObject(pmedium->u.hBitmap);
1773 break;
1775 case TYMED_MFPICT:
1777 if ( (pmedium->pUnkForRelease==0) &&
1778 (pmedium->u.hMetaFilePict!=0) )
1780 LPMETAFILEPICT pMP = GlobalLock(pmedium->u.hMetaFilePict);
1781 DeleteMetaFile(pMP->hMF);
1782 GlobalUnlock(pmedium->u.hMetaFilePict);
1783 GlobalFree(pmedium->u.hMetaFilePict);
1785 break;
1787 case TYMED_ENHMF:
1789 if ( (pmedium->pUnkForRelease==0) &&
1790 (pmedium->u.hEnhMetaFile!=0) )
1792 DeleteEnhMetaFile(pmedium->u.hEnhMetaFile);
1794 break;
1796 case TYMED_NULL:
1797 default:
1798 break;
1800 pmedium->tymed=TYMED_NULL;
1803 * After cleaning up, the unknown is released
1805 if (pmedium->pUnkForRelease!=0)
1807 IUnknown_Release(pmedium->pUnkForRelease);
1808 pmedium->pUnkForRelease = 0;
1812 /***
1813 * OLEDD_Initialize()
1815 * Initializes the OLE drag and drop data structures.
1817 static void OLEDD_Initialize()
1819 WNDCLASSA wndClass;
1821 ZeroMemory (&wndClass, sizeof(WNDCLASSA));
1822 wndClass.style = CS_GLOBALCLASS;
1823 wndClass.lpfnWndProc = OLEDD_DragTrackerWindowProc;
1824 wndClass.cbClsExtra = 0;
1825 wndClass.cbWndExtra = sizeof(TrackerWindowInfo*);
1826 wndClass.hCursor = 0;
1827 wndClass.hbrBackground = 0;
1828 wndClass.lpszClassName = OLEDD_DRAGTRACKERCLASS;
1830 RegisterClassA (&wndClass);
1833 /***
1834 * OLEDD_FreeDropTarget()
1836 * Frees the drag and drop data structure
1838 static void OLEDD_FreeDropTarget(DropTargetNode *dropTargetInfo)
1840 list_remove(&dropTargetInfo->entry);
1841 IDropTarget_Release(dropTargetInfo->dropTarget);
1842 HeapFree(GetProcessHeap(), 0, dropTargetInfo);
1845 /***
1846 * OLEDD_UnInitialize()
1848 * Releases the OLE drag and drop data structures.
1850 static void OLEDD_UnInitialize(void)
1853 * Simply empty the list.
1855 while (!list_empty(&targetListHead))
1857 DropTargetNode* curNode;
1858 curNode = LIST_ENTRY(list_head(&targetListHead), DropTargetNode, entry);
1859 OLEDD_FreeDropTarget(curNode);
1863 /***
1864 * OLEDD_FindDropTarget()
1866 * Finds information about the drop target.
1868 static DropTargetNode* OLEDD_FindDropTarget(HWND hwndOfTarget)
1870 DropTargetNode* curNode;
1873 * Iterate the list to find the HWND value.
1875 LIST_FOR_EACH_ENTRY(curNode, &targetListHead, DropTargetNode, entry)
1876 if (hwndOfTarget==curNode->hwndTarget)
1877 return curNode;
1880 * If we get here, the item is not in the list
1882 return NULL;
1885 /***
1886 * OLEDD_DragTrackerWindowProc()
1888 * This method is the WindowProcedure of the drag n drop tracking
1889 * window. During a drag n Drop operation, an invisible window is created
1890 * to receive the user input and act upon it. This procedure is in charge
1891 * of this behavior.
1894 #define DRAG_TIMER_ID 1
1896 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(
1897 HWND hwnd,
1898 UINT uMsg,
1899 WPARAM wParam,
1900 LPARAM lParam)
1902 switch (uMsg)
1904 case WM_CREATE:
1906 LPCREATESTRUCTA createStruct = (LPCREATESTRUCTA)lParam;
1908 SetWindowLongA(hwnd, 0, (LONG)createStruct->lpCreateParams);
1909 SetTimer(hwnd, DRAG_TIMER_ID, 50, NULL);
1911 break;
1913 case WM_TIMER:
1914 case WM_MOUSEMOVE:
1916 OLEDD_TrackMouseMove((TrackerWindowInfo*)GetWindowLongA(hwnd, 0));
1917 break;
1919 case WM_LBUTTONUP:
1920 case WM_MBUTTONUP:
1921 case WM_RBUTTONUP:
1922 case WM_LBUTTONDOWN:
1923 case WM_MBUTTONDOWN:
1924 case WM_RBUTTONDOWN:
1926 OLEDD_TrackStateChange((TrackerWindowInfo*)GetWindowLongA(hwnd, 0));
1927 break;
1929 case WM_DESTROY:
1931 KillTimer(hwnd, DRAG_TIMER_ID);
1932 break;
1937 * This is a window proc after all. Let's call the default.
1939 return DefWindowProcA (hwnd, uMsg, wParam, lParam);
1942 /***
1943 * OLEDD_TrackMouseMove()
1945 * This method is invoked while a drag and drop operation is in effect.
1946 * it will generate the appropriate callbacks in the drop source
1947 * and drop target. It will also provide the expected feedback to
1948 * the user.
1950 * params:
1951 * trackerInfo - Pointer to the structure identifying the
1952 * drag & drop operation that is currently
1953 * active.
1955 static void OLEDD_TrackMouseMove(TrackerWindowInfo* trackerInfo)
1957 HWND hwndNewTarget = 0;
1958 HRESULT hr = S_OK;
1959 POINT pt;
1962 * Get the handle of the window under the mouse
1964 pt.x = trackerInfo->curMousePos.x;
1965 pt.y = trackerInfo->curMousePos.y;
1966 hwndNewTarget = WindowFromPoint(pt);
1969 * Every time, we re-initialize the effects passed to the
1970 * IDropTarget to the effects allowed by the source.
1972 *trackerInfo->pdwEffect = trackerInfo->dwOKEffect;
1975 * If we are hovering over the same target as before, send the
1976 * DragOver notification
1978 if ( (trackerInfo->curDragTarget != 0) &&
1979 (trackerInfo->curTargetHWND == hwndNewTarget) )
1981 IDropTarget_DragOver(trackerInfo->curDragTarget,
1982 trackerInfo->dwKeyState,
1983 trackerInfo->curMousePos,
1984 trackerInfo->pdwEffect);
1986 else
1988 DropTargetNode* newDropTargetNode = 0;
1991 * If we changed window, we have to notify our old target and check for
1992 * the new one.
1994 if (trackerInfo->curDragTarget!=0)
1996 IDropTarget_DragLeave(trackerInfo->curDragTarget);
2000 * Make sure we're hovering over a window.
2002 if (hwndNewTarget!=0)
2005 * Find-out if there is a drag target under the mouse
2007 HWND nexttar = hwndNewTarget;
2008 trackerInfo->curTargetHWND = hwndNewTarget;
2010 do {
2011 newDropTargetNode = OLEDD_FindDropTarget(nexttar);
2012 } while (!newDropTargetNode && (nexttar = GetParent(nexttar)) != 0);
2013 if(nexttar) hwndNewTarget = nexttar;
2015 trackerInfo->curDragTargetHWND = hwndNewTarget;
2016 trackerInfo->curDragTarget = newDropTargetNode ? newDropTargetNode->dropTarget : 0;
2019 * If there is, notify it that we just dragged-in
2021 if (trackerInfo->curDragTarget!=0)
2023 IDropTarget_DragEnter(trackerInfo->curDragTarget,
2024 trackerInfo->dataObject,
2025 trackerInfo->dwKeyState,
2026 trackerInfo->curMousePos,
2027 trackerInfo->pdwEffect);
2030 else
2033 * The mouse is not over a window so we don't track anything.
2035 trackerInfo->curDragTargetHWND = 0;
2036 trackerInfo->curTargetHWND = 0;
2037 trackerInfo->curDragTarget = 0;
2042 * Now that we have done that, we have to tell the source to give
2043 * us feedback on the work being done by the target. If we don't
2044 * have a target, simulate no effect.
2046 if (trackerInfo->curDragTarget==0)
2048 *trackerInfo->pdwEffect = DROPEFFECT_NONE;
2051 hr = IDropSource_GiveFeedback(trackerInfo->dropSource,
2052 *trackerInfo->pdwEffect);
2055 * When we ask for feedback from the drop source, sometimes it will
2056 * do all the necessary work and sometimes it will not handle it
2057 * when that's the case, we must display the standard drag and drop
2058 * cursors.
2060 if (hr==DRAGDROP_S_USEDEFAULTCURSORS)
2062 if (*trackerInfo->pdwEffect & DROPEFFECT_MOVE)
2064 SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(1)));
2066 else if (*trackerInfo->pdwEffect & DROPEFFECT_COPY)
2068 SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(2)));
2070 else if (*trackerInfo->pdwEffect & DROPEFFECT_LINK)
2072 SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(3)));
2074 else
2076 SetCursor(LoadCursorA(OLE32_hInstance, MAKEINTRESOURCEA(0)));
2081 /***
2082 * OLEDD_TrackStateChange()
2084 * This method is invoked while a drag and drop operation is in effect.
2085 * It is used to notify the drop target/drop source callbacks when
2086 * the state of the keyboard or mouse button change.
2088 * params:
2089 * trackerInfo - Pointer to the structure identifying the
2090 * drag & drop operation that is currently
2091 * active.
2093 static void OLEDD_TrackStateChange(TrackerWindowInfo* trackerInfo)
2096 * Ask the drop source what to do with the operation.
2098 trackerInfo->returnValue = IDropSource_QueryContinueDrag(
2099 trackerInfo->dropSource,
2100 trackerInfo->escPressed,
2101 trackerInfo->dwKeyState);
2104 * All the return valued will stop the operation except the S_OK
2105 * return value.
2107 if (trackerInfo->returnValue!=S_OK)
2110 * Make sure the message loop in DoDragDrop stops
2112 trackerInfo->trackingDone = TRUE;
2115 * Release the mouse in case the drop target decides to show a popup
2116 * or a menu or something.
2118 ReleaseCapture();
2121 * If we end-up over a target, drop the object in the target or
2122 * inform the target that the operation was cancelled.
2124 if (trackerInfo->curDragTarget!=0)
2126 switch (trackerInfo->returnValue)
2129 * If the source wants us to complete the operation, we tell
2130 * the drop target that we just dropped the object in it.
2132 case DRAGDROP_S_DROP:
2134 IDropTarget_Drop(trackerInfo->curDragTarget,
2135 trackerInfo->dataObject,
2136 trackerInfo->dwKeyState,
2137 trackerInfo->curMousePos,
2138 trackerInfo->pdwEffect);
2139 break;
2142 * If the source told us that we should cancel, fool the drop
2143 * target by telling it that the mouse left it's window.
2144 * Also set the drop effect to "NONE" in case the application
2145 * ignores the result of DoDragDrop.
2147 case DRAGDROP_S_CANCEL:
2148 IDropTarget_DragLeave(trackerInfo->curDragTarget);
2149 *trackerInfo->pdwEffect = DROPEFFECT_NONE;
2150 break;
2156 /***
2157 * OLEDD_GetButtonState()
2159 * This method will use the current state of the keyboard to build
2160 * a button state mask equivalent to the one passed in the
2161 * WM_MOUSEMOVE wParam.
2163 static DWORD OLEDD_GetButtonState()
2165 BYTE keyboardState[256];
2166 DWORD keyMask = 0;
2168 GetKeyboardState(keyboardState);
2170 if ( (keyboardState[VK_SHIFT] & 0x80) !=0)
2171 keyMask |= MK_SHIFT;
2173 if ( (keyboardState[VK_CONTROL] & 0x80) !=0)
2174 keyMask |= MK_CONTROL;
2176 if ( (keyboardState[VK_LBUTTON] & 0x80) !=0)
2177 keyMask |= MK_LBUTTON;
2179 if ( (keyboardState[VK_RBUTTON] & 0x80) !=0)
2180 keyMask |= MK_RBUTTON;
2182 if ( (keyboardState[VK_MBUTTON] & 0x80) !=0)
2183 keyMask |= MK_MBUTTON;
2185 return keyMask;
2188 /***
2189 * OLEDD_GetButtonState()
2191 * This method will read the default value of the registry key in
2192 * parameter and extract a DWORD value from it. The registry key value
2193 * can be in a string key or a DWORD key.
2195 * params:
2196 * regKey - Key to read the default value from
2197 * pdwValue - Pointer to the location where the DWORD
2198 * value is returned. This value is not modified
2199 * if the value is not found.
2202 static void OLEUTL_ReadRegistryDWORDValue(
2203 HKEY regKey,
2204 DWORD* pdwValue)
2206 char buffer[20];
2207 DWORD dwKeyType;
2208 DWORD cbData = 20;
2209 LONG lres;
2211 lres = RegQueryValueExA(regKey,
2213 NULL,
2214 &dwKeyType,
2215 (LPBYTE)buffer,
2216 &cbData);
2218 if (lres==ERROR_SUCCESS)
2220 switch (dwKeyType)
2222 case REG_DWORD:
2223 *pdwValue = *(DWORD*)buffer;
2224 break;
2225 case REG_EXPAND_SZ:
2226 case REG_MULTI_SZ:
2227 case REG_SZ:
2228 *pdwValue = (DWORD)strtoul(buffer, NULL, 10);
2229 break;
2234 /******************************************************************************
2235 * OleDraw (OLE32.@)
2237 * The operation of this function is documented literally in the WinAPI
2238 * documentation to involve a QueryInterface for the IViewObject interface,
2239 * followed by a call to IViewObject::Draw.
2241 HRESULT WINAPI OleDraw(
2242 IUnknown *pUnk,
2243 DWORD dwAspect,
2244 HDC hdcDraw,
2245 LPCRECT lprcBounds)
2247 HRESULT hres;
2248 IViewObject *viewobject;
2250 hres = IUnknown_QueryInterface(pUnk,
2251 &IID_IViewObject,
2252 (void**)&viewobject);
2254 if (SUCCEEDED(hres))
2256 RECTL rectl;
2258 rectl.left = lprcBounds->left;
2259 rectl.right = lprcBounds->right;
2260 rectl.top = lprcBounds->top;
2261 rectl.bottom = lprcBounds->bottom;
2262 hres = IViewObject_Draw(viewobject, dwAspect, -1, 0, 0, 0, hdcDraw, &rectl, 0, 0, 0);
2264 IViewObject_Release(viewobject);
2265 return hres;
2267 else
2269 return DV_E_NOIVIEWOBJECT;
2273 /***********************************************************************
2274 * OleTranslateAccelerator [OLE32.@]
2276 HRESULT WINAPI OleTranslateAccelerator (LPOLEINPLACEFRAME lpFrame,
2277 LPOLEINPLACEFRAMEINFO lpFrameInfo, LPMSG lpmsg)
2279 WORD wID;
2281 TRACE("(%p,%p,%p)\n", lpFrame, lpFrameInfo, lpmsg);
2283 if (IsAccelerator(lpFrameInfo->haccel,lpFrameInfo->cAccelEntries,lpmsg,&wID))
2284 return IOleInPlaceFrame_TranslateAccelerator(lpFrame,lpmsg,wID);
2286 return S_FALSE;
2289 /******************************************************************************
2290 * OleCreate [OLE32.@]
2293 HRESULT WINAPI OleCreate(
2294 REFCLSID rclsid,
2295 REFIID riid,
2296 DWORD renderopt,
2297 LPFORMATETC pFormatEtc,
2298 LPOLECLIENTSITE pClientSite,
2299 LPSTORAGE pStg,
2300 LPVOID* ppvObj)
2302 HRESULT hres, hres1;
2303 IUnknown * pUnk = NULL;
2305 FIXME("\n\t%s\n\t%s semi-stub!\n", debugstr_guid(rclsid), debugstr_guid(riid));
2307 if (SUCCEEDED((hres = CoCreateInstance(rclsid, 0, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER|CLSCTX_LOCAL_SERVER , riid, (LPVOID*)&pUnk))))
2309 if (pClientSite)
2311 IOleObject * pOE;
2312 IPersistStorage * pPS;
2313 if (SUCCEEDED((hres = IUnknown_QueryInterface( pUnk, &IID_IOleObject, (LPVOID*)&pOE))))
2315 TRACE("trying to set clientsite %p\n", pClientSite);
2316 hres1 = IOleObject_SetClientSite(pOE, pClientSite);
2317 TRACE("-- result 0x%08lx\n", hres1);
2318 IOleObject_Release(pOE);
2320 if (SUCCEEDED((hres = IUnknown_QueryInterface( pUnk, &IID_IPersistStorage, (LPVOID*)&pPS))))
2322 TRACE("trying to set stg %p\n", pStg);
2323 hres1 = IPersistStorage_InitNew(pPS, pStg);
2324 TRACE("-- result 0x%08lx\n", hres1);
2325 IPersistStorage_Release(pPS);
2330 *ppvObj = pUnk;
2332 TRACE("-- %p\n", pUnk);
2333 return hres;
2336 /******************************************************************************
2337 * OleSetAutoConvert [OLE32.@]
2339 HRESULT WINAPI OleSetAutoConvert(REFCLSID clsidOld, REFCLSID clsidNew)
2341 static const WCHAR wszAutoConvertTo[] = {'A','u','t','o','C','o','n','v','e','r','t','T','o',0};
2342 HKEY hkey = NULL;
2343 WCHAR szClsidNew[CHARS_IN_GUID];
2344 HRESULT res = S_OK;
2346 TRACE("(%s,%s)\n", debugstr_guid(clsidOld), debugstr_guid(clsidNew));
2348 res = COM_OpenKeyForCLSID(clsidOld, NULL, KEY_READ | KEY_WRITE, &hkey);
2349 if (FAILED(res))
2350 goto done;
2351 StringFromGUID2(clsidNew, szClsidNew, CHARS_IN_GUID);
2352 if (RegSetValueW(hkey, wszAutoConvertTo, REG_SZ, szClsidNew, (strlenW(szClsidNew)+1) * sizeof(WCHAR)))
2354 res = REGDB_E_WRITEREGDB;
2355 goto done;
2358 done:
2359 if (hkey) RegCloseKey(hkey);
2360 return res;
2363 /******************************************************************************
2364 * OleDoAutoConvert [OLE32.@]
2366 HRESULT WINAPI OleDoAutoConvert(LPSTORAGE pStg, LPCLSID pClsidNew)
2368 FIXME("(%p,%p) : stub\n",pStg,pClsidNew);
2369 return E_NOTIMPL;
2372 /******************************************************************************
2373 * OleIsRunning [OLE32.@]
2375 BOOL WINAPI OleIsRunning(LPOLEOBJECT pObject)
2377 IRunnableObject *pRunnable;
2378 HRESULT hr;
2379 BOOL running;
2381 TRACE("(%p)\n", pObject);
2383 hr = IOleObject_QueryInterface(pObject, &IID_IRunnableObject, (void **)&pRunnable);
2384 if (FAILED(hr))
2385 return FALSE;
2386 running = IRunnableObject_IsRunning(pRunnable);
2387 IRunnableObject_Release(pRunnable);
2388 return running;
2391 /***********************************************************************
2392 * OLE_FreeClipDataArray [internal]
2394 * NOTES:
2395 * frees the data associated with an array of CLIPDATAs
2397 static void OLE_FreeClipDataArray(ULONG count, CLIPDATA * pClipDataArray)
2399 ULONG i;
2400 for (i = 0; i < count; i++)
2401 if (pClipDataArray[i].pClipData)
2402 CoTaskMemFree(pClipDataArray[i].pClipData);
2405 /***********************************************************************
2406 * PropSysAllocString [OLE32.@]
2407 * NOTES:
2408 * Basically a copy of SysAllocStringLen.
2410 BSTR WINAPI PropSysAllocString(LPCOLESTR str)
2412 DWORD bufferSize;
2413 DWORD* newBuffer;
2414 WCHAR* stringBuffer;
2415 int len;
2417 if (!str) return 0;
2419 len = lstrlenW(str);
2421 * Find the length of the buffer passed-in in bytes.
2423 bufferSize = len * sizeof (WCHAR);
2426 * Allocate a new buffer to hold the string.
2427 * don't forget to keep an empty spot at the beginning of the
2428 * buffer for the character count and an extra character at the
2429 * end for the NULL.
2431 newBuffer = HeapAlloc(GetProcessHeap(), 0,
2432 bufferSize + sizeof(WCHAR) + sizeof(DWORD));
2435 * If the memory allocation failed, return a null pointer.
2437 if (newBuffer==0)
2438 return 0;
2441 * Copy the length of the string in the placeholder.
2443 *newBuffer = bufferSize;
2446 * Skip the byte count.
2448 newBuffer++;
2451 * Copy the information in the buffer.
2452 * Since it is valid to pass a NULL pointer here, we'll initialize the
2453 * buffer to nul if it is the case.
2455 if (str != 0)
2456 memcpy(newBuffer, str, bufferSize);
2457 else
2458 memset(newBuffer, 0, bufferSize);
2461 * Make sure that there is a nul character at the end of the
2462 * string.
2464 stringBuffer = (WCHAR*)newBuffer;
2465 stringBuffer[len] = L'\0';
2467 return (LPWSTR)stringBuffer;
2470 /***********************************************************************
2471 * PropSysFreeString [OLE32.@]
2472 * NOTES
2473 * Copy of SysFreeString.
2475 void WINAPI PropSysFreeString(LPOLESTR str)
2477 DWORD* bufferPointer;
2479 /* NULL is a valid parameter */
2480 if(!str) return;
2483 * We have to be careful when we free a BSTR pointer, it points to
2484 * the beginning of the string but it skips the byte count contained
2485 * before the string.
2487 bufferPointer = (DWORD*)str;
2489 bufferPointer--;
2492 * Free the memory from its "real" origin.
2494 HeapFree(GetProcessHeap(), 0, bufferPointer);
2497 /******************************************************************************
2498 * Check if a PROPVARIANT's type is valid.
2500 static inline HRESULT PROPVARIANT_ValidateType(VARTYPE vt)
2502 switch (vt)
2504 case VT_EMPTY:
2505 case VT_NULL:
2506 case VT_I2:
2507 case VT_I4:
2508 case VT_R4:
2509 case VT_R8:
2510 case VT_CY:
2511 case VT_DATE:
2512 case VT_BSTR:
2513 case VT_ERROR:
2514 case VT_BOOL:
2515 case VT_UI1:
2516 case VT_UI2:
2517 case VT_UI4:
2518 case VT_I8:
2519 case VT_UI8:
2520 case VT_LPSTR:
2521 case VT_LPWSTR:
2522 case VT_FILETIME:
2523 case VT_BLOB:
2524 case VT_STREAM:
2525 case VT_STORAGE:
2526 case VT_STREAMED_OBJECT:
2527 case VT_STORED_OBJECT:
2528 case VT_BLOB_OBJECT:
2529 case VT_CF:
2530 case VT_CLSID:
2531 case VT_I2|VT_VECTOR:
2532 case VT_I4|VT_VECTOR:
2533 case VT_R4|VT_VECTOR:
2534 case VT_R8|VT_VECTOR:
2535 case VT_CY|VT_VECTOR:
2536 case VT_DATE|VT_VECTOR:
2537 case VT_BSTR|VT_VECTOR:
2538 case VT_ERROR|VT_VECTOR:
2539 case VT_BOOL|VT_VECTOR:
2540 case VT_VARIANT|VT_VECTOR:
2541 case VT_UI1|VT_VECTOR:
2542 case VT_UI2|VT_VECTOR:
2543 case VT_UI4|VT_VECTOR:
2544 case VT_I8|VT_VECTOR:
2545 case VT_UI8|VT_VECTOR:
2546 case VT_LPSTR|VT_VECTOR:
2547 case VT_LPWSTR|VT_VECTOR:
2548 case VT_FILETIME|VT_VECTOR:
2549 case VT_CF|VT_VECTOR:
2550 case VT_CLSID|VT_VECTOR:
2551 return S_OK;
2553 WARN("Bad type %d\n", vt);
2554 return STG_E_INVALIDPARAMETER;
2557 /***********************************************************************
2558 * PropVariantClear [OLE32.@]
2560 HRESULT WINAPI PropVariantClear(PROPVARIANT * pvar) /* [in/out] */
2562 HRESULT hr;
2564 TRACE("(%p)\n", pvar);
2566 if (!pvar)
2567 return S_OK;
2569 hr = PROPVARIANT_ValidateType(pvar->vt);
2570 if (FAILED(hr))
2571 return hr;
2573 switch(pvar->vt)
2575 case VT_STREAM:
2576 case VT_STREAMED_OBJECT:
2577 case VT_STORAGE:
2578 case VT_STORED_OBJECT:
2579 if (pvar->u.pStream)
2580 IUnknown_Release(pvar->u.pStream);
2581 break;
2582 case VT_CLSID:
2583 case VT_LPSTR:
2584 case VT_LPWSTR:
2585 /* pick an arbitary typed pointer - we don't care about the type
2586 * as we are just freeing it */
2587 CoTaskMemFree(pvar->u.puuid);
2588 break;
2589 case VT_BLOB:
2590 case VT_BLOB_OBJECT:
2591 CoTaskMemFree(pvar->u.blob.pBlobData);
2592 break;
2593 case VT_BSTR:
2594 if (pvar->u.bstrVal)
2595 PropSysFreeString(pvar->u.bstrVal);
2596 break;
2597 case VT_CF:
2598 if (pvar->u.pclipdata)
2600 OLE_FreeClipDataArray(1, pvar->u.pclipdata);
2601 CoTaskMemFree(pvar->u.pclipdata);
2603 break;
2604 default:
2605 if (pvar->vt & VT_VECTOR)
2607 ULONG i;
2609 switch (pvar->vt & ~VT_VECTOR)
2611 case VT_VARIANT:
2612 FreePropVariantArray(pvar->u.capropvar.cElems, pvar->u.capropvar.pElems);
2613 break;
2614 case VT_CF:
2615 OLE_FreeClipDataArray(pvar->u.caclipdata.cElems, pvar->u.caclipdata.pElems);
2616 break;
2617 case VT_BSTR:
2618 for (i = 0; i < pvar->u.cabstr.cElems; i++)
2619 PropSysFreeString(pvar->u.cabstr.pElems[i]);
2620 break;
2621 case VT_LPSTR:
2622 for (i = 0; i < pvar->u.calpstr.cElems; i++)
2623 CoTaskMemFree(pvar->u.calpstr.pElems[i]);
2624 break;
2625 case VT_LPWSTR:
2626 for (i = 0; i < pvar->u.calpwstr.cElems; i++)
2627 CoTaskMemFree(pvar->u.calpwstr.pElems[i]);
2628 break;
2630 if (pvar->vt & ~VT_VECTOR)
2632 /* pick an arbitary VT_VECTOR structure - they all have the same
2633 * memory layout */
2634 CoTaskMemFree(pvar->u.capropvar.pElems);
2637 else
2638 WARN("Invalid/unsupported type %d\n", pvar->vt);
2641 ZeroMemory(pvar, sizeof(*pvar));
2643 return S_OK;
2646 /***********************************************************************
2647 * PropVariantCopy [OLE32.@]
2649 HRESULT WINAPI PropVariantCopy(PROPVARIANT *pvarDest, /* [out] */
2650 const PROPVARIANT *pvarSrc) /* [in] */
2652 ULONG len;
2653 HRESULT hr;
2655 TRACE("(%p, %p)\n", pvarDest, pvarSrc);
2657 hr = PROPVARIANT_ValidateType(pvarSrc->vt);
2658 if (FAILED(hr))
2659 return hr;
2661 /* this will deal with most cases */
2662 CopyMemory(pvarDest, pvarSrc, sizeof(*pvarDest));
2664 switch(pvarSrc->vt)
2666 case VT_STREAM:
2667 case VT_STREAMED_OBJECT:
2668 case VT_STORAGE:
2669 case VT_STORED_OBJECT:
2670 IUnknown_AddRef((LPUNKNOWN)pvarDest->u.pStream);
2671 break;
2672 case VT_CLSID:
2673 pvarDest->u.puuid = CoTaskMemAlloc(sizeof(CLSID));
2674 CopyMemory(pvarDest->u.puuid, pvarSrc->u.puuid, sizeof(CLSID));
2675 break;
2676 case VT_LPSTR:
2677 len = strlen(pvarSrc->u.pszVal);
2678 pvarDest->u.pszVal = CoTaskMemAlloc((len+1)*sizeof(CHAR));
2679 CopyMemory(pvarDest->u.pszVal, pvarSrc->u.pszVal, (len+1)*sizeof(CHAR));
2680 break;
2681 case VT_LPWSTR:
2682 len = lstrlenW(pvarSrc->u.pwszVal);
2683 pvarDest->u.pwszVal = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
2684 CopyMemory(pvarDest->u.pwszVal, pvarSrc->u.pwszVal, (len+1)*sizeof(WCHAR));
2685 break;
2686 case VT_BLOB:
2687 case VT_BLOB_OBJECT:
2688 if (pvarSrc->u.blob.pBlobData)
2690 len = pvarSrc->u.blob.cbSize;
2691 pvarDest->u.blob.pBlobData = CoTaskMemAlloc(len);
2692 CopyMemory(pvarDest->u.blob.pBlobData, pvarSrc->u.blob.pBlobData, len);
2694 break;
2695 case VT_BSTR:
2696 pvarDest->u.bstrVal = PropSysAllocString(pvarSrc->u.bstrVal);
2697 break;
2698 case VT_CF:
2699 if (pvarSrc->u.pclipdata)
2701 len = pvarSrc->u.pclipdata->cbSize - sizeof(pvarSrc->u.pclipdata->ulClipFmt);
2702 pvarDest->u.pclipdata->pClipData = CoTaskMemAlloc(len);
2703 CopyMemory(pvarDest->u.pclipdata->pClipData, pvarSrc->u.pclipdata->pClipData, len);
2705 break;
2706 default:
2707 if (pvarSrc->vt & VT_VECTOR)
2709 int elemSize;
2710 ULONG i;
2712 switch(pvarSrc->vt & ~VT_VECTOR)
2714 case VT_I1: elemSize = sizeof(pvarSrc->u.cVal); break;
2715 case VT_UI1: elemSize = sizeof(pvarSrc->u.bVal); break;
2716 case VT_I2: elemSize = sizeof(pvarSrc->u.iVal); break;
2717 case VT_UI2: elemSize = sizeof(pvarSrc->u.uiVal); break;
2718 case VT_BOOL: elemSize = sizeof(pvarSrc->u.boolVal); break;
2719 case VT_I4: elemSize = sizeof(pvarSrc->u.lVal); break;
2720 case VT_UI4: elemSize = sizeof(pvarSrc->u.ulVal); break;
2721 case VT_R4: elemSize = sizeof(pvarSrc->u.fltVal); break;
2722 case VT_R8: elemSize = sizeof(pvarSrc->u.dblVal); break;
2723 case VT_ERROR: elemSize = sizeof(pvarSrc->u.scode); break;
2724 case VT_I8: elemSize = sizeof(pvarSrc->u.hVal); break;
2725 case VT_UI8: elemSize = sizeof(pvarSrc->u.uhVal); break;
2726 case VT_CY: elemSize = sizeof(pvarSrc->u.cyVal); break;
2727 case VT_DATE: elemSize = sizeof(pvarSrc->u.date); break;
2728 case VT_FILETIME: elemSize = sizeof(pvarSrc->u.filetime); break;
2729 case VT_CLSID: elemSize = sizeof(*pvarSrc->u.puuid); break;
2730 case VT_CF: elemSize = sizeof(*pvarSrc->u.pclipdata); break;
2731 case VT_BSTR: elemSize = sizeof(*pvarSrc->u.bstrVal); break;
2732 case VT_LPSTR: elemSize = sizeof(*pvarSrc->u.pszVal); break;
2733 case VT_LPWSTR: elemSize = sizeof(*pvarSrc->u.pwszVal); break;
2735 case VT_VARIANT:
2736 default:
2737 FIXME("Invalid element type: %ul\n", pvarSrc->vt & ~VT_VECTOR);
2738 return E_INVALIDARG;
2740 len = pvarSrc->u.capropvar.cElems;
2741 pvarDest->u.capropvar.pElems = CoTaskMemAlloc(len * elemSize);
2742 if (pvarSrc->vt == (VT_VECTOR | VT_VARIANT))
2744 for (i = 0; i < len; i++)
2745 PropVariantCopy(&pvarDest->u.capropvar.pElems[i], &pvarSrc->u.capropvar.pElems[i]);
2747 else if (pvarSrc->vt == (VT_VECTOR | VT_CF))
2749 FIXME("Copy clipformats\n");
2751 else if (pvarSrc->vt == (VT_VECTOR | VT_BSTR))
2753 for (i = 0; i < len; i++)
2754 pvarDest->u.cabstr.pElems[i] = PropSysAllocString(pvarSrc->u.cabstr.pElems[i]);
2756 else if (pvarSrc->vt == (VT_VECTOR | VT_LPSTR))
2758 size_t strLen;
2759 for (i = 0; i < len; i++)
2761 strLen = lstrlenA(pvarSrc->u.calpstr.pElems[i]) + 1;
2762 pvarDest->u.calpstr.pElems[i] = CoTaskMemAlloc(strLen);
2763 memcpy(pvarDest->u.calpstr.pElems[i],
2764 pvarSrc->u.calpstr.pElems[i], strLen);
2767 else if (pvarSrc->vt == (VT_VECTOR | VT_LPWSTR))
2769 size_t strLen;
2770 for (i = 0; i < len; i++)
2772 strLen = (lstrlenW(pvarSrc->u.calpwstr.pElems[i]) + 1) *
2773 sizeof(WCHAR);
2774 pvarDest->u.calpstr.pElems[i] = CoTaskMemAlloc(strLen);
2775 memcpy(pvarDest->u.calpstr.pElems[i],
2776 pvarSrc->u.calpstr.pElems[i], strLen);
2779 else
2780 CopyMemory(pvarDest->u.capropvar.pElems, pvarSrc->u.capropvar.pElems, len * elemSize);
2782 else
2783 WARN("Invalid/unsupported type %d\n", pvarSrc->vt);
2786 return S_OK;
2789 /***********************************************************************
2790 * FreePropVariantArray [OLE32.@]
2792 HRESULT WINAPI FreePropVariantArray(ULONG cVariants, /* [in] */
2793 PROPVARIANT *rgvars) /* [in/out] */
2795 ULONG i;
2797 TRACE("(%lu, %p)\n", cVariants, rgvars);
2799 for(i = 0; i < cVariants; i++)
2800 PropVariantClear(&rgvars[i]);
2802 return S_OK;
2805 /******************************************************************************
2806 * DllDebugObjectRPCHook (OLE32.@)
2807 * turns on and off internal debugging, pointer is only used on macintosh
2810 BOOL WINAPI DllDebugObjectRPCHook(BOOL b, void *dummy)
2812 FIXME("stub\n");
2813 return TRUE;