1 /*
2 * OLE2 library
3 *
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
9 * Copyright 2011 Adam Martinson for CodeWeavers
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26 #include "config.h"
27
28 #include <assert.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <string.h>
33
34 #define COBJMACROS
35 #define NONAMELESSUNION
36 #define NONAMELESSSTRUCT
37
38 #include "windef.h"
39 #include "winbase.h"
40 #include "winerror.h"
41 #include "wingdi.h"
42 #include "winuser.h"
43 #include "winnls.h"
44 #include "winreg.h"
45 #include "ole2.h"
46 #include "ole2ver.h"
47
48 #include "wine/unicode.h"
49 #include "compobj_private.h"
50 #include "wine/list.h"
51
52 #include "wine/debug.h"
53
54 WINE_DEFAULT_DEBUG_CHANNEL(ole);
55 WINE_DECLARE_DEBUG_CHANNEL(accel);
56
57 /******************************************************************************
58 * These are static/global variables and internal data structures that the
59 * OLE module uses to maintain it's state.
60 */
61 typedef struct tagTrackerWindowInfo
62 {
63 IDataObject* dataObject;
64 IDropSource* dropSource;
65 DWORD dwOKEffect;
66 DWORD* pdwEffect;
67 BOOL trackingDone;
68 HRESULT returnValue;
69
70 BOOL escPressed;
71 HWND curTargetHWND; /* window the mouse is hovering over */
72 HWND curDragTargetHWND; /* might be a ancestor of curTargetHWND */
73 IDropTarget* curDragTarget;
74 POINTL curMousePos; /* current position of the mouse in screen coordinates */
75 DWORD dwKeyState; /* current state of the shift and ctrl keys and the mouse buttons */
76 } TrackerWindowInfo;
77
78 typedef struct tagOleMenuDescriptor /* OleMenuDescriptor */
79 {
80 HWND hwndFrame; /* The containers frame window */
81 HWND hwndActiveObject; /* The active objects window */
82 OLEMENUGROUPWIDTHS mgw; /* OLE menu group widths for the shared menu */
83 HMENU hmenuCombined; /* The combined menu */
84 BOOL bIsServerItem; /* True if the currently open popup belongs to the server */
85 } OleMenuDescriptor;
86
87 typedef struct tagOleMenuHookItem /* OleMenu hook item in per thread hook list */
88 {
89 DWORD tid; /* Thread Id */
90 HANDLE hHeap; /* Heap this is allocated from */
91 HHOOK GetMsg_hHook; /* message hook for WH_GETMESSAGE */
92 HHOOK CallWndProc_hHook; /* message hook for WH_CALLWNDPROC */
93 struct tagOleMenuHookItem *next;
94 } OleMenuHookItem;
95
96 static OleMenuHookItem *hook_list;
97
98 /*
99 * This is the lock count on the OLE library. It is controlled by the
100 * OLEInitialize/OLEUninitialize methods.
101 */
102 static LONG OLE_moduleLockCount = 0;
103
104 /*
105 * Name of our registered window class.
106 */
107 static const WCHAR OLEDD_DRAGTRACKERCLASS[] =
108 {'W','i','n','e','D','r','a','g','D','r','o','p','T','r','a','c','k','e','r','3','2',0};
109
110 /*
111 * Name of menu descriptor property.
112 */
113 static const WCHAR prop_olemenuW[] =
114 {'P','R','O','P','_','O','L','E','M','e','n','u','D','e','s','c','r','i','p','t','o','r',0};
115
116 /* property to store IDropTarget pointer */
117 static const WCHAR prop_oledroptarget[] =
118 {'O','l','e','D','r','o','p','T','a','r','g','e','t','I','n','t','e','r','f','a','c','e',0};
119
120 /* property to store Marshalled IDropTarget pointer */
121 static const WCHAR prop_marshalleddroptarget[] =
122 {'W','i','n','e','M','a','r','s','h','a','l','l','e','d','D','r','o','p','T','a','r','g','e','t',0};
123
124 static const WCHAR clsidfmtW[] =
125 {'C','L','S','I','D','\\','{','%','','8','x','-','%','','4','x','-','%','','4','x','-',
126 '%','','2','x','%','','2','x','-','%','','2','x','%','','2','x','%','','2','x','%','','2','x',
127 '%','','2','x','%','','2','x','}','\\',0};
128
129 static const WCHAR emptyW[] = { 0 };
130
131 /******************************************************************************
132 * These are the prototypes of miscellaneous utility methods
133 */
134 static void OLEUTL_ReadRegistryDWORDValue(HKEY regKey, DWORD* pdwValue);
135
136 /******************************************************************************
137 * These are the prototypes of the utility methods used to manage a shared menu
138 */
139 static void OLEMenu_Initialize(void);
140 static void OLEMenu_UnInitialize(void);
141 static BOOL OLEMenu_InstallHooks( DWORD tid );
142 static BOOL OLEMenu_UnInstallHooks( DWORD tid );
143 static OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid );
144 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos );
145 static BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor );
146 static LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam);
147 static LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam);
148
149 /******************************************************************************
150 * These are the prototypes of the OLE Clipboard initialization methods (in clipboard.c)
151 */
152 extern void OLEClipbrd_UnInitialize(void);
153 extern void OLEClipbrd_Initialize(void);
154
155 /******************************************************************************
156 * These are the prototypes of the utility methods used for OLE Drag n Drop
157 */
158 static void OLEDD_Initialize(void);
159 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
160 static void OLEDD_TrackMouseMove(TrackerWindowInfo* trackerInfo);
161 static void OLEDD_TrackStateChange(TrackerWindowInfo* trackerInfo);
162 static DWORD OLEDD_GetButtonState(void);
163
164 /******************************************************************************
165 * OleBuildVersion [OLE32.@]
166 */
167 DWORD WINAPI OleBuildVersion(void)
168 {
169 TRACE("Returning version %d, build %d.\n", rmm, rup);
170 return (rmm<<16)+rup;
171 }
172
173 /***********************************************************************
174 * OleInitialize (OLE32.@)
175 */
176 HRESULT WINAPI OleInitialize(LPVOID reserved)
177 {
178 HRESULT hr;
179
180 TRACE("(%p)\n", reserved);
181
182 /*
183 * The first duty of the OleInitialize is to initialize the COM libraries.
184 */
185 hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
186
187 /*
188 * If the CoInitializeEx call failed, the OLE libraries can't be
189 * initialized.
190 */
191 if (FAILED(hr))
192 return hr;
193
194 if (!COM_CurrentInfo()->ole_inits)
195 hr = S_OK;
196
197 /*
198 * Then, it has to initialize the OLE specific modules.
199 * This includes:
200 * Clipboard
201 * Drag and Drop
202 * Object linking and Embedding
203 * In-place activation
204 */
205 if (!COM_CurrentInfo()->ole_inits++ &&
206 InterlockedIncrement(&OLE_moduleLockCount) == 1)
207 {
208 /*
209 * Initialize the libraries.
210 */
211 TRACE("() - Initializing the OLE libraries\n");
212
213 /*
214 * OLE Clipboard
215 */
216 OLEClipbrd_Initialize();
217
218 /*
219 * Drag and Drop
220 */
221 OLEDD_Initialize();
222
223 /*
224 * OLE shared menu
225 */
226 OLEMenu_Initialize();
227 }
228
229 return hr;
230 }
231
232 /******************************************************************************
233 * OleUninitialize [OLE32.@]
234 */
235 void WINAPI OleUninitialize(void)
236 {
237 TRACE("()\n");
238
239 /*
240 * If we hit the bottom of the lock stack, free the libraries.
241 */
242 if (!--COM_CurrentInfo()->ole_inits && !InterlockedDecrement(&OLE_moduleLockCount))
243 {
244 /*
245 * Actually free the libraries.
246 */
247 TRACE("() - Freeing the last reference count\n");
248
249 /*
250 * OLE Clipboard
251 */
252 OLEClipbrd_UnInitialize();
253
254 /*
255 * OLE shared menu
256 */
257 OLEMenu_UnInitialize();
258 }
259
260 /*
261 * Then, uninitialize the COM libraries.
262 */
263 CoUninitialize();
264 }
265
266 /******************************************************************************
267 * OleInitializeWOW [OLE32.@]
268 */
269 HRESULT WINAPI OleInitializeWOW(DWORD x, DWORD y) {
270 FIXME("(0x%08x, 0x%08x),stub!\n",x, y);
271 return 0;
272 }
273
274 /*************************************************************
275 * get_droptarget_handle
276 *
277 * Retrieve a handle to the map containing the marshalled IDropTarget.
278 * This handle belongs to the process that called RegisterDragDrop.
279 * See get_droptarget_local_handle().
280 */
281 static inline HANDLE get_droptarget_handle(HWND hwnd)
282 {
283 return GetPropW(hwnd, prop_marshalleddroptarget);
284 }
285
286 /*************************************************************
287 * is_droptarget
288 *
289 * Is the window a droptarget.
290 */
291 static inline BOOL is_droptarget(HWND hwnd)
292 {
293 return get_droptarget_handle(hwnd) ? TRUE : FALSE;
294 }
295
296 /*************************************************************
297 * get_droptarget_local_handle
298 *
299 * Retrieve a handle to the map containing the marshalled IDropTarget.
300 * The handle should be closed when finished with.
301 */
302 static HANDLE get_droptarget_local_handle(HWND hwnd)
303 {
304 HANDLE handle, local_handle = 0;
305
306 handle = get_droptarget_handle(hwnd);
307
308 if(handle)
309 {
310 DWORD pid;
311 HANDLE process;
312
313 GetWindowThreadProcessId(hwnd, &pid);
314 process = OpenProcess(PROCESS_DUP_HANDLE, FALSE, pid);
315 if(process)
316 {
317 DuplicateHandle(process, handle, GetCurrentProcess(), &local_handle, 0, FALSE, DUPLICATE_SAME_ACCESS);
318 CloseHandle(process);
319 }
320 }
321 return local_handle;
322 }
323
324 /***********************************************************************
325 * create_map_from_stream
326 *
327 * Helper for RegisterDragDrop. Creates a file mapping object
328 * with the contents of the provided stream. The stream must
329 * be a global memory backed stream.
330 */
331 static HRESULT create_map_from_stream(IStream *stream, HANDLE *map)
332 {
333 HGLOBAL hmem;
334 DWORD size;
335 HRESULT hr;
336 void *data;
337
338 hr = GetHGlobalFromStream(stream, &hmem);
339 if(FAILED(hr)) return hr;
340
341 size = GlobalSize(hmem);
342 *map = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, size, NULL);
343 if(!*map) return E_OUTOFMEMORY;
344
345 data = MapViewOfFile(*map, FILE_MAP_WRITE, 0, 0, size);
346 memcpy(data, GlobalLock(hmem), size);
347 GlobalUnlock(hmem);
348 UnmapViewOfFile(data);
349 return S_OK;
350 }
351
352 /***********************************************************************
353 * create_stream_from_map
354 *
355 * Creates a stream from the provided map.
356 */
357 static HRESULT create_stream_from_map(HANDLE map, IStream **stream)
358 {
359 HRESULT hr = E_OUTOFMEMORY;
360 HGLOBAL hmem;
361 void *data;
362 MEMORY_BASIC_INFORMATION info;
363
364 data = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
365 if(!data) return hr;
366
367 VirtualQuery(data, &info, sizeof(info));
368 TRACE("size %d\n", (int)info.RegionSize);
369
370 hmem = GlobalAlloc(GMEM_MOVEABLE, info.RegionSize);
371 if(hmem)
372 {
373 memcpy(GlobalLock(hmem), data, info.RegionSize);
374 GlobalUnlock(hmem);
375 hr = CreateStreamOnHGlobal(hmem, TRUE, stream);
376 }
377 UnmapViewOfFile(data);
378 return hr;
379 }
380
381 /* This is to work around apps which break COM rules by not implementing
382 * IDropTarget::QueryInterface(). Windows doesn't expose this because it
383 * doesn't call CoMarshallInterface() in RegisterDragDrop().
384 * The wrapper is only used internally, and only exists for the life of
385 * the marshal. We don't want to hold a ref on the app provided target
386 * as some apps destroy this prior to CoUninitialize without calling
387 * RevokeDragDrop. The only (long-term) ref is held by the window prop. */
388 typedef struct {
389 IDropTarget IDropTarget_iface;
390 HWND hwnd;
391 LONG refs;
392 } DropTargetWrapper;
393
394 static inline DropTargetWrapper* impl_from_IDropTarget(IDropTarget* iface)
395 {
396 return CONTAINING_RECORD(iface, DropTargetWrapper, IDropTarget_iface);
397 }
398
399 static HRESULT WINAPI DropTargetWrapper_QueryInterface(IDropTarget* iface,
400 REFIID riid,
401 void** ppvObject)
402 {
403 DropTargetWrapper* This = impl_from_IDropTarget(iface);
404 if (IsEqualIID(riid, &IID_IUnknown) ||
405 IsEqualIID(riid, &IID_IDropTarget))
406 {
407 IDropTarget_AddRef(&This->IDropTarget_iface);
408 *ppvObject = &This->IDropTarget_iface;
409 return S_OK;
410 }
411 *ppvObject = NULL;
412 return E_NOINTERFACE;
413 }
414
415 static ULONG WINAPI DropTargetWrapper_AddRef(IDropTarget* iface)
416 {
417 DropTargetWrapper* This = impl_from_IDropTarget(iface);
418 return InterlockedIncrement(&This->refs);
419 }
420
421 static ULONG WINAPI DropTargetWrapper_Release(IDropTarget* iface)
422 {
423 DropTargetWrapper* This = impl_from_IDropTarget(iface);
424 ULONG refs = InterlockedDecrement(&This->refs);
425 if (!refs) HeapFree(GetProcessHeap(), 0, This);
426 return refs;
427 }
428
429 static inline HRESULT get_target_from_wrapper( IDropTarget *wrapper, IDropTarget **target )
430 {
431 DropTargetWrapper* This = impl_from_IDropTarget( wrapper );
432 *target = GetPropW( This->hwnd, prop_oledroptarget );
433 if (!*target) return DRAGDROP_E_NOTREGISTERED;
434 IDropTarget_AddRef( *target );
435 return S_OK;
436 }
437
438 static HRESULT WINAPI DropTargetWrapper_DragEnter(IDropTarget* iface,
439 IDataObject* pDataObj,
440 DWORD grfKeyState,
441 POINTL pt,
442 DWORD* pdwEffect)
443 {
444 IDropTarget *target;
445 HRESULT r = get_target_from_wrapper( iface, &target );
446
447 if (SUCCEEDED( r ))
448 {
449 r = IDropTarget_DragEnter( target, pDataObj, grfKeyState, pt, pdwEffect );
450 IDropTarget_Release( target );
451 }
452 return r;
453 }
454
455 static HRESULT WINAPI DropTargetWrapper_DragOver(IDropTarget* iface,
456 DWORD grfKeyState,
457 POINTL pt,
458 DWORD* pdwEffect)
459 {
460 IDropTarget *target;
461 HRESULT r = get_target_from_wrapper( iface, &target );
462
463 if (SUCCEEDED( r ))
464 {
465 r = IDropTarget_DragOver( target, grfKeyState, pt, pdwEffect );
466 IDropTarget_Release( target );
467 }
468 return r;
469 }
470
471 static HRESULT WINAPI DropTargetWrapper_DragLeave(IDropTarget* iface)
472 {
473 IDropTarget *target;
474 HRESULT r = get_target_from_wrapper( iface, &target );
475
476 if (SUCCEEDED( r ))
477 {
478 r = IDropTarget_DragLeave( target );
479 IDropTarget_Release( target );
480 }
481 return r;
482 }
483
484 static HRESULT WINAPI DropTargetWrapper_Drop(IDropTarget* iface,
485 IDataObject* pDataObj,
486 DWORD grfKeyState,
487 POINTL pt,
488 DWORD* pdwEffect)
489 {
490 IDropTarget *target;
491 HRESULT r = get_target_from_wrapper( iface, &target );
492
493 if (SUCCEEDED( r ))
494 {
495 r = IDropTarget_Drop( target, pDataObj, grfKeyState, pt, pdwEffect );
496 IDropTarget_Release( target );
497 }
498 return r;
499 }
500
501 static const IDropTargetVtbl DropTargetWrapperVTbl =
502 {
503 DropTargetWrapper_QueryInterface,
504 DropTargetWrapper_AddRef,
505 DropTargetWrapper_Release,
506 DropTargetWrapper_DragEnter,
507 DropTargetWrapper_DragOver,
508 DropTargetWrapper_DragLeave,
509 DropTargetWrapper_Drop
510 };
511
512 static IDropTarget* WrapDropTarget( HWND hwnd )
513 {
514 DropTargetWrapper* This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
515
516 if (This)
517 {
518 This->IDropTarget_iface.lpVtbl = &DropTargetWrapperVTbl;
519 This->hwnd = hwnd;
520 This->refs = 1;
521 }
522 return &This->IDropTarget_iface;
523 }
524
525 /***********************************************************************
526 * get_droptarget_pointer
527 *
528 * Retrieves the marshalled IDropTarget from the window.
529 */
530 static IDropTarget* get_droptarget_pointer(HWND hwnd)
531 {
532 IDropTarget *droptarget = NULL;
533 HANDLE map;
534 IStream *stream;
535
536 map = get_droptarget_local_handle(hwnd);
537 if(!map) return NULL;
538
539 if(SUCCEEDED(create_stream_from_map(map, &stream)))
540 {
541 CoUnmarshalInterface(stream, &IID_IDropTarget, (void**)&droptarget);
542 IStream_Release(stream);
543 }
544 CloseHandle(map);
545 return droptarget;
546 }
547
548 /***********************************************************************
549 * RegisterDragDrop (OLE32.@)
550 */
551 HRESULT WINAPI RegisterDragDrop(HWND hwnd, LPDROPTARGET pDropTarget)
552 {
553 DWORD pid = 0;
554 HRESULT hr;
555 IStream *stream;
556 HANDLE map;
557 IDropTarget *wrapper;
558
559 TRACE("(%p,%p)\n", hwnd, pDropTarget);
560
561 if (!COM_CurrentApt())
562 {
563 ERR("COM not initialized\n");
564 return E_OUTOFMEMORY;
565 }
566
567 if (!pDropTarget)
568 return E_INVALIDARG;
569
570 if (!IsWindow(hwnd))
571 {
572 ERR("invalid hwnd %p\n", hwnd);
573 return DRAGDROP_E_INVALIDHWND;
574 }
575
576 /* block register for other processes windows */
577 GetWindowThreadProcessId(hwnd, &pid);
578 if (pid != GetCurrentProcessId())
579 {
580 FIXME("register for another process windows is disabled\n");
581 return DRAGDROP_E_INVALIDHWND;
582 }
583
584 /* check if the window is already registered */
585 if (is_droptarget(hwnd))
586 return DRAGDROP_E_ALREADYREGISTERED;
587
588 /*
589 * Marshal the drop target pointer into a shared memory map and
590 * store the map's handle in a Wine specific window prop. We also
591 * store the drop target pointer itself in the
592 * "OleDropTargetInterface" prop for compatibility with Windows.
593 */
594
595 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
596 if(FAILED(hr)) return hr;
597
598 /* IDropTarget::QueryInterface() shouldn't be called, some (broken) apps depend on this. */
599 wrapper = WrapDropTarget( hwnd );
600 if(!wrapper)
601 {
602 IStream_Release(stream);
603 return E_OUTOFMEMORY;
604 }
605 hr = CoMarshalInterface(stream, &IID_IDropTarget, (IUnknown*)wrapper, MSHCTX_LOCAL, NULL, MSHLFLAGS_TABLESTRONG);
606 IDropTarget_Release(wrapper);
607
608 if(SUCCEEDED(hr))
609 {
610 hr = create_map_from_stream(stream, &map);
611 if(SUCCEEDED(hr))
612 {
613 IDropTarget_AddRef(pDropTarget);
614 SetPropW(hwnd, prop_oledroptarget, pDropTarget);
615 SetPropW(hwnd, prop_marshalleddroptarget, map);
616 }
617 else
618 {
619 LARGE_INTEGER zero;
620 zero.QuadPart = 0;
621 IStream_Seek(stream, zero, STREAM_SEEK_SET, NULL);
622 CoReleaseMarshalData(stream);
623 }
624 }
625 IStream_Release(stream);
626
627 return hr;
628 }
629
630 /***********************************************************************
631 * RevokeDragDrop (OLE32.@)
632 */
633 HRESULT WINAPI RevokeDragDrop(HWND hwnd)
634 {
635 HANDLE map;
636 IStream *stream;
637 IDropTarget *drop_target;
638 HRESULT hr;
639
640 TRACE("(%p)\n", hwnd);
641
642 if (!IsWindow(hwnd))
643 {
644 ERR("invalid hwnd %p\n", hwnd);
645 return DRAGDROP_E_INVALIDHWND;
646 }
647
648 /* no registration data */
649 if (!(map = get_droptarget_handle(hwnd)))
650 return DRAGDROP_E_NOTREGISTERED;
651
652 drop_target = GetPropW(hwnd, prop_oledroptarget);
653 if(drop_target) IDropTarget_Release(drop_target);
654
655 RemovePropW(hwnd, prop_oledroptarget);
656 RemovePropW(hwnd, prop_marshalleddroptarget);
657
658 hr = create_stream_from_map(map, &stream);
659 if(SUCCEEDED(hr))
660 {
661 CoReleaseMarshalData(stream);
662 IStream_Release(stream);
663 }
664 CloseHandle(map);
665
666 return hr;
667 }
668
669 /***********************************************************************
670 * OleRegGetUserType (OLE32.@)
671 *
672 * This implementation of OleRegGetUserType ignores the dwFormOfType
673 * parameter and always returns the full name of the object. This is
674 * not too bad since this is the case for many objects because of the
675 * way they are registered.
676 */
677 HRESULT WINAPI OleRegGetUserType(
678 REFCLSID clsid,
679 DWORD dwFormOfType,
680 LPOLESTR* pszUserType)
681 {
682 WCHAR keyName[60];
683 DWORD dwKeyType;
684 DWORD cbData;
685 HKEY clsidKey;
686 LONG hres;
687
688 /*
689 * Initialize the out parameter.
690 */
691 *pszUserType = NULL;
692
693 /*
694 * Build the key name we're looking for
695 */
696 sprintfW( keyName, clsidfmtW,
697 clsid->Data1, clsid->Data2, clsid->Data3,
698 clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
699 clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );
700
701 TRACE("(%s, %d, %p)\n", debugstr_w(keyName), dwFormOfType, pszUserType);
702
703 /*
704 * Open the class id Key
705 */
706 hres = RegOpenKeyW(HKEY_CLASSES_ROOT,
707 keyName,
708 &clsidKey);
709
710 if (hres != ERROR_SUCCESS)
711 return REGDB_E_CLASSNOTREG;
712
713 /*
714 * Retrieve the size of the name string.
715 */
716 cbData = 0;
717
718 hres = RegQueryValueExW(clsidKey,
719 emptyW,
720 NULL,
721 &dwKeyType,
722 NULL,
723 &cbData);
724
725 if (hres!=ERROR_SUCCESS)
726 {
727 RegCloseKey(clsidKey);
728 return REGDB_E_READREGDB;
729 }
730
731 /*
732 * Allocate a buffer for the registry value.
733 */
734 *pszUserType = CoTaskMemAlloc(cbData);
735
736 if (*pszUserType==NULL)
737 {
738 RegCloseKey(clsidKey);
739 return E_OUTOFMEMORY;
740 }
741
742 hres = RegQueryValueExW(clsidKey,
743 emptyW,
744 NULL,
745 &dwKeyType,
746 (LPBYTE) *pszUserType,
747 &cbData);
748
749 RegCloseKey(clsidKey);
750
751 if (hres != ERROR_SUCCESS)
752 {
753 CoTaskMemFree(*pszUserType);
754 *pszUserType = NULL;
755
756 return REGDB_E_READREGDB;
757 }
758
759 return S_OK;
760 }
761
762 /***********************************************************************
763 * DoDragDrop [OLE32.@]
764 */
765 HRESULT WINAPI DoDragDrop (
766 IDataObject *pDataObject, /* [in] ptr to the data obj */
767 IDropSource* pDropSource, /* [in] ptr to the source obj */
768 DWORD dwOKEffect, /* [in] effects allowed by the source */
769 DWORD *pdwEffect) /* [out] ptr to effects of the source */
770 {
771 static const WCHAR trackerW[] = {'T','r','a','c','k','e','r','W','i','n','d','o','w',0};
772 TrackerWindowInfo trackerInfo;
773 HWND hwndTrackWindow;
774 MSG msg;
775
776 TRACE("(%p, %p, %08x, %p)\n", pDataObject, pDropSource, dwOKEffect, pdwEffect);
777
778 if (!pDataObject || !pDropSource || !pdwEffect)
779 return E_INVALIDARG;
780
781 /*
782 * Setup the drag n drop tracking window.
783 */
784
785 trackerInfo.dataObject = pDataObject;
786 trackerInfo.dropSource = pDropSource;
787 trackerInfo.dwOKEffect = dwOKEffect;
788 trackerInfo.pdwEffect = pdwEffect;
789 trackerInfo.trackingDone = FALSE;
790 trackerInfo.escPressed = FALSE;
791 trackerInfo.curDragTargetHWND = 0;
792 trackerInfo.curTargetHWND = 0;
793 trackerInfo.curDragTarget = 0;
794
795 hwndTrackWindow = CreateWindowW(OLEDD_DRAGTRACKERCLASS, trackerW,
796 WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT,
797 CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, 0,
798 &trackerInfo);
799
800 if (hwndTrackWindow)
801 {
802 /*
803 * Capture the mouse input
804 */
805 SetCapture(hwndTrackWindow);
806
807 msg.message = 0;
808
809 /*
810 * Pump messages. All mouse input should go to the capture window.
811 */
812 while (!trackerInfo.trackingDone && GetMessageW(&msg, 0, 0, 0) )
813 {
814 trackerInfo.curMousePos.x = msg.pt.x;
815 trackerInfo.curMousePos.y = msg.pt.y;
816 trackerInfo.dwKeyState = OLEDD_GetButtonState();
817
818 if ( (msg.message >= WM_KEYFIRST) &&
819 (msg.message <= WM_KEYLAST) )
820 {
821 /*
822 * When keyboard messages are sent to windows on this thread, we
823 * want to ignore notify the drop source that the state changed.
824 * in the case of the Escape key, we also notify the drop source
825 * we give it a special meaning.
826 */
827 if ( (msg.message==WM_KEYDOWN) &&
828 (msg.wParam==VK_ESCAPE) )
829 {
830 trackerInfo.escPressed = TRUE;
831 }
832
833 /*
834 * Notify the drop source.
835 */
836 OLEDD_TrackStateChange(&trackerInfo);
837 }
838 else
839 {
840 /*
841 * Dispatch the messages only when it's not a keyboard message.
842 */
843 DispatchMessageW(&msg);
844 }
845 }
846
847 /* re-post the quit message to outer message loop */
848 if (msg.message == WM_QUIT)
849 PostQuitMessage(msg.wParam);
850 /*
851 * Destroy the temporary window.
852 */
853 DestroyWindow(hwndTrackWindow);
854
855 return trackerInfo.returnValue;
856 }
857
858 return E_FAIL;
859 }
860
861 /***********************************************************************
862 * OleQueryLinkFromData [OLE32.@]
863 */
864 HRESULT WINAPI OleQueryLinkFromData(
865 IDataObject* pSrcDataObject)
866 {
867 FIXME("(%p),stub!\n", pSrcDataObject);
868 return S_FALSE;
869 }
870
871 /***********************************************************************
872 * OleRegGetMiscStatus [OLE32.@]
873 */
874 HRESULT WINAPI OleRegGetMiscStatus(
875 REFCLSID clsid,
876 DWORD dwAspect,
877 DWORD* pdwStatus)
878 {
879 static const WCHAR miscstatusW[] = {'M','i','s','c','S','t','a','t','u','s',0};
880 static const WCHAR dfmtW[] = {'%','d',0};
881 WCHAR keyName[60];
882 HKEY clsidKey;
883 HKEY miscStatusKey;
884 HKEY aspectKey;
885 LONG result;
886
887 /*
888 * Initialize the out parameter.
889 */
890 *pdwStatus = 0;
891
892 /*
893 * Build the key name we're looking for
894 */
895 sprintfW( keyName, clsidfmtW,
896 clsid->Data1, clsid->Data2, clsid->Data3,
897 clsid->Data4[0], clsid->Data4[1], clsid->Data4[2], clsid->Data4[3],
898 clsid->Data4[4], clsid->Data4[5], clsid->Data4[6], clsid->Data4[7] );
899
900 TRACE("(%s, %d, %p)\n", debugstr_w(keyName), dwAspect, pdwStatus);
901
902 /*
903 * Open the class id Key
904 */
905 result = RegOpenKeyW(HKEY_CLASSES_ROOT,
906 keyName,
907 &clsidKey);
908
909 if (result != ERROR_SUCCESS)
910 return REGDB_E_CLASSNOTREG;
911
912 /*
913 * Get the MiscStatus
914 */
915 result = RegOpenKeyW(clsidKey,
916 miscstatusW,
917 &miscStatusKey);
918
919
920 if (result != ERROR_SUCCESS)
921 {
922 RegCloseKey(clsidKey);
923 return REGDB_E_READREGDB;
924 }
925
926 /*
927 * Read the default value
928 */
929 OLEUTL_ReadRegistryDWORDValue(miscStatusKey, pdwStatus);
930
931 /*
932 * Open the key specific to the requested aspect.
933 */
934 sprintfW(keyName, dfmtW, dwAspect);
935
936 result = RegOpenKeyW(miscStatusKey,
937 keyName,
938 &aspectKey);
939
940 if (result == ERROR_SUCCESS)
941 {
942 OLEUTL_ReadRegistryDWORDValue(aspectKey, pdwStatus);
943 RegCloseKey(aspectKey);
944 }
945
946 /*
947 * Cleanup
948 */
949 RegCloseKey(miscStatusKey);
950 RegCloseKey(clsidKey);
951
952 return S_OK;
953 }
954
955 static HRESULT EnumOLEVERB_Construct(HKEY hkeyVerb, ULONG index, IEnumOLEVERB **ppenum);
956
957 typedef struct
958 {
959 IEnumOLEVERB IEnumOLEVERB_iface;
960 LONG ref;
961
962 HKEY hkeyVerb;
963 ULONG index;
964 } EnumOLEVERB;
965
966 static inline EnumOLEVERB *impl_from_IEnumOLEVERB(IEnumOLEVERB *iface)
967 {
968 return CONTAINING_RECORD(iface, EnumOLEVERB, IEnumOLEVERB_iface);
969 }
970
971 static HRESULT WINAPI EnumOLEVERB_QueryInterface(
972 IEnumOLEVERB *iface, REFIID riid, void **ppv)
973 {
974 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
975 if (IsEqualIID(riid, &IID_IUnknown) ||
976 IsEqualIID(riid, &IID_IEnumOLEVERB))
977 {
978 IUnknown_AddRef(iface);
979 *ppv = iface;
980 return S_OK;
981 }
982 return E_NOINTERFACE;
983 }
984
985 static ULONG WINAPI EnumOLEVERB_AddRef(
986 IEnumOLEVERB *iface)
987 {
988 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
989 TRACE("()\n");
990 return InterlockedIncrement(&This->ref);
991 }
992
993 static ULONG WINAPI EnumOLEVERB_Release(
994 IEnumOLEVERB *iface)
995 {
996 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
997 LONG refs = InterlockedDecrement(&This->ref);
998 TRACE("()\n");
999 if (!refs)
1000 {
1001 RegCloseKey(This->hkeyVerb);
1002 HeapFree(GetProcessHeap(), 0, This);
1003 }
1004 return refs;
1005 }
1006
1007 static HRESULT WINAPI EnumOLEVERB_Next(
1008 IEnumOLEVERB *iface, ULONG celt, LPOLEVERB rgelt,
1009 ULONG *pceltFetched)
1010 {
1011 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
1012 HRESULT hr = S_OK;
1013
1014 TRACE("(%d, %p, %p)\n", celt, rgelt, pceltFetched);
1015
1016 if (pceltFetched)
1017 *pceltFetched = 0;
1018
1019 for (; celt; celt--, rgelt++)
1020 {
1021 WCHAR wszSubKey[20];
1022 LONG cbData;
1023 LPWSTR pwszOLEVERB;
1024 LPWSTR pwszMenuFlags;
1025 LPWSTR pwszAttribs;
1026 LONG res = RegEnumKeyW(This->hkeyVerb, This->index, wszSubKey, sizeof(wszSubKey)/sizeof(wszSubKey[0]));
1027 if (res == ERROR_NO_MORE_ITEMS)
1028 {
1029 hr = S_FALSE;
1030 break;
1031 }
1032 else if (res != ERROR_SUCCESS)
1033 {
1034 ERR("RegEnumKeyW failed with error %d\n", res);
1035 hr = REGDB_E_READREGDB;
1036 break;
1037 }
1038 res = RegQueryValueW(This->hkeyVerb, wszSubKey, NULL, &cbData);
1039 if (res != ERROR_SUCCESS)
1040 {
1041 ERR("RegQueryValueW failed with error %d\n", res);
1042 hr = REGDB_E_READREGDB;
1043 break;
1044 }
1045 pwszOLEVERB = CoTaskMemAlloc(cbData);
1046 if (!pwszOLEVERB)
1047 {
1048 hr = E_OUTOFMEMORY;
1049 break;
1050 }
1051 res = RegQueryValueW(This->hkeyVerb, wszSubKey, pwszOLEVERB, &cbData);
1052 if (res != ERROR_SUCCESS)
1053 {
1054 ERR("RegQueryValueW failed with error %d\n", res);
1055 hr = REGDB_E_READREGDB;
1056 CoTaskMemFree(pwszOLEVERB);
1057 break;
1058 }
1059
1060 TRACE("verb string: %s\n", debugstr_w(pwszOLEVERB));
1061 pwszMenuFlags = strchrW(pwszOLEVERB, ',');
1062 if (!pwszMenuFlags)
1063 {
1064 hr = OLEOBJ_E_INVALIDVERB;
1065 CoTaskMemFree(pwszOLEVERB);
1066 break;
1067 }
1068 /* nul terminate the name string and advance to first character */
1069 *pwszMenuFlags = '\0';
1070 pwszMenuFlags++;
1071 pwszAttribs = strchrW(pwszMenuFlags, ',');
1072 if (!pwszAttribs)
1073 {
1074 hr = OLEOBJ_E_INVALIDVERB;
1075 CoTaskMemFree(pwszOLEVERB);
1076 break;
1077 }
1078 /* nul terminate the menu string and advance to first character */
1079 *pwszAttribs = '\0';
1080 pwszAttribs++;
1081
1082 /* fill out structure for this verb */
1083 rgelt->lVerb = atolW(wszSubKey);
1084 rgelt->lpszVerbName = pwszOLEVERB; /* user should free */
1085 rgelt->fuFlags = atolW(pwszMenuFlags);
1086 rgelt->grfAttribs = atolW(pwszAttribs);
1087
1088 if (pceltFetched)
1089 (*pceltFetched)++;
1090 This->index++;
1091 }
1092 return hr;
1093 }
1094
1095 static HRESULT WINAPI EnumOLEVERB_Skip(
1096 IEnumOLEVERB *iface, ULONG celt)
1097 {
1098 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
1099
1100 TRACE("(%d)\n", celt);
1101
1102 This->index += celt;
1103 return S_OK;
1104 }
1105
1106 static HRESULT WINAPI EnumOLEVERB_Reset(
1107 IEnumOLEVERB *iface)
1108 {
1109 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
1110
1111 TRACE("()\n");
1112
1113 This->index = 0;
1114 return S_OK;
1115 }
1116
1117 static HRESULT WINAPI EnumOLEVERB_Clone(
1118 IEnumOLEVERB *iface,
1119 IEnumOLEVERB **ppenum)
1120 {
1121 EnumOLEVERB *This = impl_from_IEnumOLEVERB(iface);
1122 HKEY hkeyVerb;
1123 TRACE("(%p)\n", ppenum);
1124 if (!DuplicateHandle(GetCurrentProcess(), This->hkeyVerb, GetCurrentProcess(), (HANDLE *)&hkeyVerb, 0, FALSE, DUPLICATE_SAME_ACCESS))
1125 return HRESULT_FROM_WIN32(GetLastError());
1126 return EnumOLEVERB_Construct(hkeyVerb, This->index, ppenum);
1127 }
1128
1129 static const IEnumOLEVERBVtbl EnumOLEVERB_VTable =
1130 {
1131 EnumOLEVERB_QueryInterface,
1132 EnumOLEVERB_AddRef,
1133 EnumOLEVERB_Release,
1134 EnumOLEVERB_Next,
1135 EnumOLEVERB_Skip,
1136 EnumOLEVERB_Reset,
1137 EnumOLEVERB_Clone
1138 };
1139
1140 static HRESULT EnumOLEVERB_Construct(HKEY hkeyVerb, ULONG index, IEnumOLEVERB **ppenum)
1141 {
1142 EnumOLEVERB *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1143 if (!This)
1144 {
1145 RegCloseKey(hkeyVerb);
1146 return E_OUTOFMEMORY;
1147 }
1148 This->IEnumOLEVERB_iface.lpVtbl = &EnumOLEVERB_VTable;
1149 This->ref = 1;
1150 This->index = index;
1151 This->hkeyVerb = hkeyVerb;
1152 *ppenum = &This->IEnumOLEVERB_iface;
1153 return S_OK;
1154 }
1155
1156 /***********************************************************************
1157 * OleRegEnumVerbs [OLE32.@]
1158 *
1159 * Enumerates verbs associated with a class stored in the registry.
1160 *
1161 * PARAMS
1162 * clsid [I] Class ID to enumerate the verbs for.
1163 * ppenum [O] Enumerator.
1164 *
1165 * RETURNS
1166 * S_OK: Success.
1167 * REGDB_E_CLASSNOTREG: The specified class does not have a key in the registry.
1168 * REGDB_E_READREGDB: The class key could not be opened for some other reason.
1169 * OLE_E_REGDB_KEY: The Verb subkey for the class is not present.
1170 * OLEOBJ_E_NOVERBS: The Verb subkey for the class is empty.
1171 */
1172 HRESULT WINAPI OleRegEnumVerbs (REFCLSID clsid, LPENUMOLEVERB* ppenum)
1173 {
1174 LONG res;
1175 HKEY hkeyVerb;
1176 DWORD dwSubKeys;
1177 static const WCHAR wszVerb[] = {'V','e','r','b',0};
1178
1179 TRACE("(%s, %p)\n", debugstr_guid(clsid), ppenum);
1180
1181 res = COM_OpenKeyForCLSID(clsid, wszVerb, KEY_READ, &hkeyVerb);
1182 if (FAILED(res))
1183 {
1184 if (res == REGDB_E_CLASSNOTREG)
1185 ERR("CLSID %s not registered\n", debugstr_guid(clsid));
1186 else if (res == REGDB_E_KEYMISSING)
1187 ERR("no Verbs key for class %s\n", debugstr_guid(clsid));
1188 else
1189 ERR("failed to open Verbs key for CLSID %s with error %d\n",
1190 debugstr_guid(clsid), res);
1191 return res;
1192 }
1193
1194 res = RegQueryInfoKeyW(hkeyVerb, NULL, NULL, NULL, &dwSubKeys, NULL,
1195 NULL, NULL, NULL, NULL, NULL, NULL);
1196 if (res != ERROR_SUCCESS)
1197 {
1198 ERR("failed to get subkey count with error %d\n", GetLastError());
1199 return REGDB_E_READREGDB;
1200 }
1201
1202 if (!dwSubKeys)
1203 {
1204 WARN("class %s has no verbs\n", debugstr_guid(clsid));
1205 RegCloseKey(hkeyVerb);
1206 return OLEOBJ_E_NOVERBS;
1207 }
1208
1209 return EnumOLEVERB_Construct(hkeyVerb, 0, ppenum);
1210 }
1211
1212 /******************************************************************************
1213 * OleSetContainedObject [OLE32.@]
1214 */
1215 HRESULT WINAPI OleSetContainedObject(
1216 LPUNKNOWN pUnknown,
1217 BOOL fContained)
1218 {
1219 IRunnableObject* runnable = NULL;
1220 HRESULT hres;
1221
1222 TRACE("(%p,%x)\n", pUnknown, fContained);
1223
1224 hres = IUnknown_QueryInterface(pUnknown,
1225 &IID_IRunnableObject,
1226 (void**)&runnable);
1227
1228 if (SUCCEEDED(hres))
1229 {
1230 hres = IRunnableObject_SetContainedObject(runnable, fContained);
1231
1232 IRunnableObject_Release(runnable);
1233
1234 return hres;
1235 }
1236
1237 return S_OK;
1238 }
1239
1240 /******************************************************************************
1241 * OleRun [OLE32.@]
1242 *
1243 * Set the OLE object to the running state.
1244 *
1245 * PARAMS
1246 * pUnknown [I] OLE object to run.
1247 *
1248 * RETURNS
1249 * Success: S_OK.
1250 * Failure: Any HRESULT code.
1251 */
1252 HRESULT WINAPI OleRun(LPUNKNOWN pUnknown)
1253 {
1254 IRunnableObject *runable;
1255 HRESULT hres;
1256
1257 TRACE("(%p)\n", pUnknown);
1258
1259 hres = IUnknown_QueryInterface(pUnknown, &IID_IRunnableObject, (void**)&runable);
1260 if (FAILED(hres))
1261 return S_OK; /* Appears to return no error. */
1262
1263 hres = IRunnableObject_Run(runable, NULL);
1264 IRunnableObject_Release(runable);
1265 return hres;
1266 }
1267
1268 /******************************************************************************
1269 * OleLoad [OLE32.@]
1270 */
1271 HRESULT WINAPI OleLoad(
1272 LPSTORAGE pStg,
1273 REFIID riid,
1274 LPOLECLIENTSITE pClientSite,
1275 LPVOID* ppvObj)
1276 {
1277 IPersistStorage* persistStorage = NULL;
1278 IUnknown* pUnk;
1279 IOleObject* pOleObject = NULL;
1280 STATSTG storageInfo;
1281 HRESULT hres;
1282
1283 TRACE("(%p, %s, %p, %p)\n", pStg, debugstr_guid(riid), pClientSite, ppvObj);
1284
1285 *ppvObj = NULL;
1286
1287 /*
1288 * TODO, Conversion ... OleDoAutoConvert
1289 */
1290
1291 /*
1292 * Get the class ID for the object.
1293 */
1294 hres = IStorage_Stat(pStg, &storageInfo, STATFLAG_NONAME);
1295
1296 /*
1297 * Now, try and create the handler for the object
1298 */
1299 hres = CoCreateInstance(&storageInfo.clsid,
1300 NULL,
1301 CLSCTX_INPROC_HANDLER|CLSCTX_INPROC_SERVER,
1302 riid,
1303 (void**)&pUnk);
1304
1305 /*
1306 * If that fails, as it will most times, load the default
1307 * OLE handler.
1308 */
1309 if (FAILED(hres))
1310 {
1311 hres = OleCreateDefaultHandler(&storageInfo.clsid,
1312 NULL,
1313 riid,
1314 (void**)&pUnk);
1315 }
1316
1317 /*
1318 * If we couldn't find a handler... this is bad. Abort the whole thing.
1319 */
1320 if (FAILED(hres))
1321 return hres;
1322
1323 if (pClientSite)
1324 {
1325 hres = IUnknown_QueryInterface(pUnk, &IID_IOleObject, (void **)&pOleObject);
1326 if (SUCCEEDED(hres))
1327 {
1328 DWORD dwStatus;
1329 hres = IOleObject_GetMiscStatus(pOleObject, DVASPECT_CONTENT, &dwStatus);
1330 }
1331 }
1332
1333 /*
1334 * Initialize the object with its IPersistStorage interface.
1335 */
1336 hres = IOleObject_QueryInterface(pUnk,
1337 &IID_IPersistStorage,
1338 (void**)&persistStorage);
1339
1340 if (SUCCEEDED(hres))
1341 {
1342 hres = IPersistStorage_Load(persistStorage, pStg);
1343
1344 IPersistStorage_Release(persistStorage);
1345 persistStorage = NULL;
1346 }
1347
1348 if (SUCCEEDED(hres) && pClientSite)
1349 /*
1350 * Inform the new object of it's client site.
1351 */
1352 hres = IOleObject_SetClientSite(pOleObject, pClientSite);
1353
1354 /*
1355 * Cleanup interfaces used internally
1356 */
1357 if (pOleObject)
1358 IOleObject_Release(pOleObject);
1359
1360 if (SUCCEEDED(hres))
1361 {
1362 IOleLink *pOleLink;
1363 HRESULT hres1;
1364 hres1 = IUnknown_QueryInterface(pUnk, &IID_IOleLink, (void **)&pOleLink);
1365 if (SUCCEEDED(hres1))
1366 {
1367 FIXME("handle OLE link\n");
1368 IOleLink_Release(pOleLink);
1369 }
1370 }
1371
1372 if (FAILED(hres))
1373 {
1374 IUnknown_Release(pUnk);
1375 pUnk = NULL;
1376 }
1377
1378 *ppvObj = pUnk;
1379
1380 return hres;
1381 }
1382
1383 /***********************************************************************
1384 * OleSave [OLE32.@]
1385 */
1386 HRESULT WINAPI OleSave(
1387 LPPERSISTSTORAGE pPS,
1388 LPSTORAGE pStg,
1389 BOOL fSameAsLoad)
1390 {
1391 HRESULT hres;
1392 CLSID objectClass;
1393
1394 TRACE("(%p,%p,%x)\n", pPS, pStg, fSameAsLoad);
1395
1396 /*
1397 * First, we transfer the class ID (if available)
1398 */
1399 hres = IPersistStorage_GetClassID(pPS, &objectClass);
1400
1401 if (SUCCEEDED(hres))
1402 {
1403 WriteClassStg(pStg, &objectClass);
1404 }
1405
1406 /*
1407 * Then, we ask the object to save itself to the
1408 * storage. If it is successful, we commit the storage.
1409 */
1410 hres = IPersistStorage_Save(pPS, pStg, fSameAsLoad);
1411
1412 if (SUCCEEDED(hres))
1413 {
1414 IStorage_Commit(pStg,
1415 STGC_DEFAULT);
1416 }
1417
1418 return hres;
1419 }
1420
1421
1422 /******************************************************************************
1423 * OleLockRunning [OLE32.@]
1424 */
1425 HRESULT WINAPI OleLockRunning(LPUNKNOWN pUnknown, BOOL fLock, BOOL fLastUnlockCloses)
1426 {
1427 IRunnableObject* runnable = NULL;
1428 HRESULT hres;
1429
1430 TRACE("(%p,%x,%x)\n", pUnknown, fLock, fLastUnlockCloses);
1431
1432 hres = IUnknown_QueryInterface(pUnknown,
1433 &IID_IRunnableObject,
1434 (void**)&runnable);
1435
1436 if (SUCCEEDED(hres))
1437 {
1438 hres = IRunnableObject_LockRunning(runnable, fLock, fLastUnlockCloses);
1439
1440 IRunnableObject_Release(runnable);
1441
1442 return hres;
1443 }
1444
1445 return S_OK;
1446 }
1447
1448
1449 /**************************************************************************
1450 * Internal methods to manage the shared OLE menu in response to the
1451 * OLE***MenuDescriptor API
1452 */
1453
1454 /***
1455 * OLEMenu_Initialize()
1456 *
1457 * Initializes the OLEMENU data structures.
1458 */
1459 static void OLEMenu_Initialize(void)
1460 {
1461 }
1462
1463 /***
1464 * OLEMenu_UnInitialize()
1465 *
1466 * Releases the OLEMENU data structures.
1467 */
1468 static void OLEMenu_UnInitialize(void)
1469 {
1470 }
1471
1472 /*************************************************************************
1473 * OLEMenu_InstallHooks
1474 * Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
1475 *
1476 * RETURNS: TRUE if message hooks were successfully installed
1477 * FALSE on failure
1478 */
1479 static BOOL OLEMenu_InstallHooks( DWORD tid )
1480 {
1481 OleMenuHookItem *pHookItem;
1482
1483 /* Create an entry for the hook table */
1484 if ( !(pHookItem = HeapAlloc(GetProcessHeap(), 0,
1485 sizeof(OleMenuHookItem)) ) )
1486 return FALSE;
1487
1488 pHookItem->tid = tid;
1489 pHookItem->hHeap = GetProcessHeap();
1490 pHookItem->CallWndProc_hHook = NULL;
1491
1492 /* Install a thread scope message hook for WH_GETMESSAGE */
1493 pHookItem->GetMsg_hHook = SetWindowsHookExW( WH_GETMESSAGE, OLEMenu_GetMsgProc,
1494 0, GetCurrentThreadId() );
1495 if ( !pHookItem->GetMsg_hHook )
1496 goto CLEANUP;
1497
1498 /* Install a thread scope message hook for WH_CALLWNDPROC */
1499 pHookItem->CallWndProc_hHook = SetWindowsHookExW( WH_CALLWNDPROC, OLEMenu_CallWndProc,
1500 0, GetCurrentThreadId() );
1501 if ( !pHookItem->CallWndProc_hHook )
1502 goto CLEANUP;
1503
1504 /* Insert the hook table entry */
1505 pHookItem->next = hook_list;
1506 hook_list = pHookItem;
1507
1508 return TRUE;
1509
1510 CLEANUP:
1511 /* Unhook any hooks */
1512 if ( pHookItem->GetMsg_hHook )
1513 UnhookWindowsHookEx( pHookItem->GetMsg_hHook );
1514 if ( pHookItem->CallWndProc_hHook )
1515 UnhookWindowsHookEx( pHookItem->CallWndProc_hHook );
1516 /* Release the hook table entry */
1517 HeapFree(pHookItem->hHeap, 0, pHookItem );
1518
1519 return FALSE;
1520 }
1521
1522 /*************************************************************************
1523 * OLEMenu_UnInstallHooks
1524 * UnInstall thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
1525 *
1526 * RETURNS: TRUE if message hooks were successfully installed
1527 * FALSE on failure
1528 */
1529 static BOOL OLEMenu_UnInstallHooks( DWORD tid )
1530 {
1531 OleMenuHookItem *pHookItem = NULL;
1532 OleMenuHookItem **ppHook = &hook_list;
1533
1534 while (*ppHook)
1535 {
1536 if ((*ppHook)->tid == tid)
1537 {
1538 pHookItem = *ppHook;
1539 *ppHook = pHookItem->next;
1540 break;
1541 }
1542 ppHook = &(*ppHook)->next;
1543 }
1544 if (!pHookItem) return FALSE;
1545
1546 /* Uninstall the hooks installed for this thread */
1547 if ( !UnhookWindowsHookEx( pHookItem->GetMsg_hHook ) )
1548 goto CLEANUP;
1549 if ( !UnhookWindowsHookEx( pHookItem->CallWndProc_hHook ) )
1550 goto CLEANUP;
1551
1552 /* Release the hook table entry */
1553 HeapFree(pHookItem->hHeap, 0, pHookItem );
1554
1555 return TRUE;
1556
1557 CLEANUP:
1558 /* Release the hook table entry */
1559 HeapFree(pHookItem->hHeap, 0, pHookItem );
1560
1561 return FALSE;
1562 }
1563
1564 /*************************************************************************
1565 * OLEMenu_IsHookInstalled
1566 * Tests if OLEMenu hooks have been installed for a thread
1567 *
1568 * RETURNS: The pointer and index of the hook table entry for the tid
1569 * NULL and -1 for the index if no hooks were installed for this thread
1570 */
1571 static OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid )
1572 {
1573 OleMenuHookItem *pHookItem;
1574
1575 /* Do a simple linear search for an entry whose tid matches ours.
1576 * We really need a map but efficiency is not a concern here. */
1577 for (pHookItem = hook_list; pHookItem; pHookItem = pHookItem->next)
1578 {
1579 if ( tid == pHookItem->tid )
1580 return pHookItem;
1581 }
1582
1583 return NULL;
1584 }
1585
1586 /***********************************************************************
1587 * OLEMenu_FindMainMenuIndex
1588 *
1589 * Used by OLEMenu API to find the top level group a menu item belongs to.
1590 * On success pnPos contains the index of the item in the top level menu group
1591 *
1592 * RETURNS: TRUE if the ID was found, FALSE on failure
1593 */
1594 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos )
1595 {
1596 INT i, nItems;
1597
1598 nItems = GetMenuItemCount( hMainMenu );
1599
1600 for (i = 0; i < nItems; i++)
1601 {
1602 HMENU hsubmenu;
1603
1604 /* Is the current item a submenu? */
1605 if ( (hsubmenu = GetSubMenu(hMainMenu, i)) )
1606 {
1607 /* If the handle is the same we're done */
1608 if ( hsubmenu == hPopupMenu )
1609 {
1610 if (pnPos)
1611 *pnPos = i;
1612 return TRUE;
1613 }
1614 /* Recursively search without updating pnPos */
1615 else if ( OLEMenu_FindMainMenuIndex( hsubmenu, hPopupMenu, NULL ) )
1616 {
1617 if (pnPos)
1618 *pnPos = i;
1619 return TRUE;
1620 }
1621 }
1622 }
1623
1624 return FALSE;
1625 }
1626
1627 /***********************************************************************
1628 * OLEMenu_SetIsServerMenu
1629 *
1630 * Checks whether a popup menu belongs to a shared menu group which is
1631 * owned by the server, and sets the menu descriptor state accordingly.
1632 * All menu messages from these groups should be routed to the server.
1633 *
1634 * RETURNS: TRUE if the popup menu is part of a server owned group
1635 * FALSE if the popup menu is part of a container owned group
1636 */
1637 static BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor )
1638 {
1639 UINT nPos = 0, nWidth, i;
1640
1641 pOleMenuDescriptor->bIsServerItem = FALSE;
1642
1643 /* Don't bother searching if the popup is the combined menu itself */
1644 if ( hmenu == pOleMenuDescriptor->hmenuCombined )
1645 return FALSE;
1646
1647 /* Find the menu item index in the shared OLE menu that this item belongs to */
1648 if ( !OLEMenu_FindMainMenuIndex( pOleMenuDescriptor->hmenuCombined, hmenu, &nPos ) )
1649 return FALSE;
1650
1651 /* The group widths array has counts for the number of elements
1652 * in the groups File, Edit, Container, Object, Window, Help.
1653 * The Edit, Object & Help groups belong to the server object
1654 * and the other three belong to the container.
1655 * Loop through the group widths and locate the group we are a member of.
1656 */
1657 for ( i = 0, nWidth = 0; i < 6; i++ )
1658 {
1659 nWidth += pOleMenuDescriptor->mgw.width[i];
1660 if ( nPos < nWidth )
1661 {
1662 /* Odd elements are server menu widths */
1663 pOleMenuDescriptor->bIsServerItem = (i%2) ? TRUE : FALSE;
1664 break;
1665 }
1666 }
1667
1668 return pOleMenuDescriptor->bIsServerItem;
1669 }
1670
1671 /*************************************************************************
1672 * OLEMenu_CallWndProc
1673 * Thread scope WH_CALLWNDPROC hook proc filter function (callback)
1674 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1675 */
1676 static LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam)
1677 {
1678 LPCWPSTRUCT pMsg;
1679 HOLEMENU hOleMenu = 0;
1680 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1681 OleMenuHookItem *pHookItem = NULL;
1682 WORD fuFlags;
1683
1684 TRACE("%i, %04lx, %08lx\n", code, wParam, lParam );
1685
1686 /* Check if we're being asked to process the message */
1687 if ( HC_ACTION != code )
1688 goto NEXTHOOK;
1689
1690 /* Retrieve the current message being dispatched from lParam */
1691 pMsg = (LPCWPSTRUCT)lParam;
1692
1693 /* Check if the message is destined for a window we are interested in:
1694 * If the window has an OLEMenu property we may need to dispatch
1695 * the menu message to its active objects window instead. */
1696
1697 hOleMenu = GetPropW( pMsg->hwnd, prop_olemenuW );
1698 if ( !hOleMenu )
1699 goto NEXTHOOK;
1700
1701 /* Get the menu descriptor */
1702 pOleMenuDescriptor = GlobalLock( hOleMenu );
1703 if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1704 goto NEXTHOOK;
1705
1706 /* Process menu messages */
1707 switch( pMsg->message )
1708 {
1709 case WM_INITMENU:
1710 {
1711 /* Reset the menu descriptor state */
1712 pOleMenuDescriptor->bIsServerItem = FALSE;
1713
1714 /* Send this message to the server as well */
1715 SendMessageW( pOleMenuDescriptor->hwndActiveObject,
1716 pMsg->message, pMsg->wParam, pMsg->lParam );
1717 goto NEXTHOOK;
1718 }
1719
1720 case WM_INITMENUPOPUP:
1721 {
1722 /* Save the state for whether this is a server owned menu */
1723 OLEMenu_SetIsServerMenu( (HMENU)pMsg->wParam, pOleMenuDescriptor );
1724 break;
1725 }
1726
1727 case WM_MENUSELECT:
1728 {
1729 fuFlags = HIWORD(pMsg->wParam); /* Get flags */
1730 if ( fuFlags & MF_SYSMENU )
1731 goto NEXTHOOK;
1732
1733 /* Save the state for whether this is a server owned popup menu */
1734 else if ( fuFlags & MF_POPUP )
1735 OLEMenu_SetIsServerMenu( (HMENU)pMsg->lParam, pOleMenuDescriptor );
1736
1737 break;
1738 }
1739
1740 case WM_DRAWITEM:
1741 {
1742 LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT) pMsg->lParam;
1743 if ( pMsg->wParam != 0 || lpdis->CtlType != ODT_MENU )
1744 goto NEXTHOOK; /* Not a menu message */
1745
1746 break;
1747 }
1748
1749 default:
1750 goto NEXTHOOK;
1751 }
1752
1753 /* If the message was for the server dispatch it accordingly */
1754 if ( pOleMenuDescriptor->bIsServerItem )
1755 {
1756 SendMessageW( pOleMenuDescriptor->hwndActiveObject,
1757 pMsg->message, pMsg->wParam, pMsg->lParam );
1758 }
1759
1760 NEXTHOOK:
1761 if ( pOleMenuDescriptor )
1762 GlobalUnlock( hOleMenu );
1763
1764 /* Lookup the hook item for the current thread */
1765 if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1766 {
1767 /* This should never fail!! */
1768 WARN("could not retrieve hHook for current thread!\n" );
1769 return 0;
1770 }
1771
1772 /* Pass on the message to the next hooker */
1773 return CallNextHookEx( pHookItem->CallWndProc_hHook, code, wParam, lParam );
1774 }
1775
1776 /*************************************************************************
1777 * OLEMenu_GetMsgProc
1778 * Thread scope WH_GETMESSAGE hook proc filter function (callback)
1779 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1780 */
1781 static LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam)
1782 {
1783 LPMSG pMsg;
1784 HOLEMENU hOleMenu = 0;
1785 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1786 OleMenuHookItem *pHookItem = NULL;
1787 WORD wCode;
1788
1789 TRACE("%i, %04lx, %08lx\n", code, wParam, lParam );
1790
1791 /* Check if we're being asked to process a messages */
1792 if ( HC_ACTION != code )
1793 goto NEXTHOOK;
1794
1795 /* Retrieve the current message being dispatched from lParam */
1796 pMsg = (LPMSG)lParam;
1797
1798 /* Check if the message is destined for a window we are interested in:
1799 * If the window has an OLEMenu property we may need to dispatch
1800 * the menu message to its active objects window instead. */
1801
1802 hOleMenu = GetPropW( pMsg->hwnd, prop_olemenuW );
1803 if ( !hOleMenu )
1804 goto NEXTHOOK;
1805
1806 /* Process menu messages */
1807 switch( pMsg->message )
1808 {
1809 case WM_COMMAND:
1810 {
1811 wCode = HIWORD(pMsg->wParam); /* Get notification code */
1812 if ( wCode )
1813 goto NEXTHOOK; /* Not a menu message */
1814 break;
1815 }
1816 default:
1817 goto NEXTHOOK;
1818 }
1819
1820 /* Get the menu descriptor */
1821 pOleMenuDescriptor = GlobalLock( hOleMenu );
1822 if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1823 goto NEXTHOOK;
1824
1825 /* If the message was for the server dispatch it accordingly */
1826 if ( pOleMenuDescriptor->bIsServerItem )
1827 {
1828 /* Change the hWnd in the message to the active objects hWnd.
1829 * The message loop which reads this message will automatically
1830 * dispatch it to the embedded objects window. */
1831 pMsg->hwnd = pOleMenuDescriptor->hwndActiveObject;
1832 }
1833
1834 NEXTHOOK:
1835 if ( pOleMenuDescriptor )
1836 GlobalUnlock( hOleMenu );
1837
1838 /* Lookup the hook item for the current thread */
1839 if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1840 {
1841 /* This should never fail!! */
1842 WARN("could not retrieve hHook for current thread!\n" );
1843 return FALSE;
1844 }
1845
1846 /* Pass on the message to the next hooker */
1847 return CallNextHookEx( pHookItem->GetMsg_hHook, code, wParam, lParam );
1848 }
1849
1850 /***********************************************************************
1851 * OleCreateMenuDescriptor [OLE32.@]
1852 * Creates an OLE menu descriptor for OLE to use when dispatching
1853 * menu messages and commands.
1854 *
1855 * PARAMS:
1856 * hmenuCombined - Handle to the objects combined menu
1857 * lpMenuWidths - Pointer to array of 6 LONG's indicating menus per group
1858 *
1859 */
1860 HOLEMENU WINAPI OleCreateMenuDescriptor(
1861 HMENU hmenuCombined,
1862 LPOLEMENUGROUPWIDTHS lpMenuWidths)
1863 {
1864 HOLEMENU hOleMenu;
1865 OleMenuDescriptor *pOleMenuDescriptor;
1866 int i;
1867
1868 if ( !hmenuCombined || !lpMenuWidths )
1869 return 0;
1870
1871 /* Create an OLE menu descriptor */
1872 if ( !(hOleMenu = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT,
1873 sizeof(OleMenuDescriptor) ) ) )
1874 return 0;
1875
1876 pOleMenuDescriptor = GlobalLock( hOleMenu );
1877 if ( !pOleMenuDescriptor )
1878 return 0;
1879
1880 /* Initialize menu group widths and hmenu */
1881 for ( i = 0; i < 6; i++ )
1882 pOleMenuDescriptor->mgw.width[i] = lpMenuWidths->width[i];
1883
1884 pOleMenuDescriptor->hmenuCombined = hmenuCombined;
1885 pOleMenuDescriptor->bIsServerItem = FALSE;
1886 GlobalUnlock( hOleMenu );
1887
1888 return hOleMenu;
1889 }
1890
1891 /***********************************************************************
1892 * OleDestroyMenuDescriptor [OLE32.@]
1893 * Destroy the shared menu descriptor
1894 */
1895 HRESULT WINAPI OleDestroyMenuDescriptor(
1896 HOLEMENU hmenuDescriptor)
1897 {
1898 if ( hmenuDescriptor )
1899 GlobalFree( hmenuDescriptor );
1900 return S_OK;
1901 }
1902
1903 /***********************************************************************
1904 * OleSetMenuDescriptor [OLE32.@]
1905 * Installs or removes OLE dispatching code for the containers frame window.
1906 *
1907 * PARAMS
1908 * hOleMenu Handle to composite menu descriptor
1909 * hwndFrame Handle to containers frame window
1910 * hwndActiveObject Handle to objects in-place activation window
1911 * lpFrame Pointer to IOleInPlaceFrame on containers window
1912 * lpActiveObject Pointer to IOleInPlaceActiveObject on active in-place object
1913 *
1914 * RETURNS
1915 * S_OK - menu installed correctly
1916 * E_FAIL, E_INVALIDARG, E_UNEXPECTED - failure
1917 *
1918 * FIXME
1919 * The lpFrame and lpActiveObject parameters are currently ignored
1920 * OLE should install context sensitive help F1 filtering for the app when
1921 * these are non null.
1922 */
1923 HRESULT WINAPI OleSetMenuDescriptor(
1924 HOLEMENU hOleMenu,
1925 HWND hwndFrame,
1926 HWND hwndActiveObject,
1927 LPOLEINPLACEFRAME lpFrame,
1928 LPOLEINPLACEACTIVEOBJECT lpActiveObject)
1929 {
1930 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1931
1932 /* Check args */
1933 if ( !hwndFrame || (hOleMenu && !hwndActiveObject) )
1934 return E_INVALIDARG;
1935
1936 if ( lpFrame || lpActiveObject )
1937 {
1938 FIXME("(%p, %p, %p, %p, %p), Context sensitive help filtering not implemented!\n",
1939 hOleMenu,
1940 hwndFrame,
1941 hwndActiveObject,
1942 lpFrame,
1943 lpActiveObject);
1944 }
1945
1946 /* Set up a message hook to intercept the containers frame window messages.
1947 * The message filter is responsible for dispatching menu messages from the
1948 * shared menu which are intended for the object.
1949 */
1950
1951 if ( hOleMenu ) /* Want to install dispatching code */
1952 {
1953 /* If OLEMenu hooks are already installed for this thread, fail
1954 * Note: This effectively means that OleSetMenuDescriptor cannot
1955 * be called twice in succession on the same frame window
1956 * without first calling it with a null hOleMenu to uninstall */
1957 if ( OLEMenu_IsHookInstalled( GetCurrentThreadId() ) )
1958 return E_FAIL;
1959
1960 /* Get the menu descriptor */
1961 pOleMenuDescriptor = GlobalLock( hOleMenu );
1962 if ( !pOleMenuDescriptor )
1963 return E_UNEXPECTED;
1964
1965 /* Update the menu descriptor */
1966 pOleMenuDescriptor->hwndFrame = hwndFrame;
1967 pOleMenuDescriptor->hwndActiveObject = hwndActiveObject;
1968
1969 GlobalUnlock( hOleMenu );
1970 pOleMenuDescriptor = NULL;
1971
1972 /* Add a menu descriptor windows property to the frame window */
1973 SetPropW( hwndFrame, prop_olemenuW, hOleMenu );
1974
1975 /* Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC */
1976 if ( !OLEMenu_InstallHooks( GetCurrentThreadId() ) )
1977 return E_FAIL;
1978 }
1979 else /* Want to uninstall dispatching code */
1980 {
1981 /* Uninstall the hooks */
1982 if ( !OLEMenu_UnInstallHooks( GetCurrentThreadId() ) )
1983 return E_FAIL;
1984
1985 /* Remove the menu descriptor property from the frame window */
1986 RemovePropW( hwndFrame, prop_olemenuW );
1987 }
1988
1989 return S_OK;
1990 }
1991
1992 /******************************************************************************
1993 * IsAccelerator [OLE32.@]
1994 * Mostly copied from controls/menu.c TranslateAccelerator implementation
1995 */
1996 BOOL WINAPI IsAccelerator(HACCEL hAccel, int cAccelEntries, LPMSG lpMsg, WORD* lpwCmd)
1997 {
1998 LPACCEL lpAccelTbl;
1999 int i;
2000
2001 if(!lpMsg) return FALSE;
2002 if (!hAccel)
2003 {
2004 WARN_(accel)("NULL accel handle\n");
2005 return FALSE;
2006 }
2007 if((lpMsg->message != WM_KEYDOWN &&
2008 lpMsg->message != WM_SYSKEYDOWN &&
2009 lpMsg->message != WM_SYSCHAR &&
2010 lpMsg->message != WM_CHAR)) return FALSE;
2011 lpAccelTbl = HeapAlloc(GetProcessHeap(), 0, cAccelEntries * sizeof(ACCEL));
2012 if (NULL == lpAccelTbl)
2013 {
2014 return FALSE;
2015 }
2016 if (CopyAcceleratorTableW(hAccel, lpAccelTbl, cAccelEntries) != cAccelEntries)
2017 {
2018 WARN_(accel)("CopyAcceleratorTableW failed\n");
2019 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
2020 return FALSE;
2021 }
2022
2023 TRACE_(accel)("hAccel=%p, cAccelEntries=%d,"
2024 "msg->hwnd=%p, msg->message=%04x, wParam=%08lx, lParam=%08lx\n",
2025 hAccel, cAccelEntries,
2026 lpMsg->hwnd, lpMsg->message, lpMsg->wParam, lpMsg->lParam);
2027 for(i = 0; i < cAccelEntries; i++)
2028 {
2029 if(lpAccelTbl[i].key != lpMsg->wParam)
2030 continue;
2031
2032 if(lpMsg->message == WM_CHAR)
2033 {
2034 if(!(lpAccelTbl[i].fVirt & FALT) && !(lpAccelTbl[i].fVirt & FVIRTKEY))
2035 {
2036 TRACE_(accel)("found accel for WM_CHAR: ('%c')\n", LOWORD(lpMsg->wParam) & 0xff);
2037 goto found;
2038 }
2039 }
2040 else
2041 {
2042 if(lpAccelTbl[i].fVirt & FVIRTKEY)
2043 {
2044 INT mask = 0;
2045 TRACE_(accel)("found accel for virt_key %04lx (scan %04x)\n",
2046 lpMsg->wParam, HIWORD(lpMsg->lParam) & 0xff);
2047 if(GetKeyState(VK_SHIFT) & 0x8000) mask |= FSHIFT;
2048 if(GetKeyState(VK_CONTROL) & 0x8000) mask |= FCONTROL;
2049 if(GetKeyState(VK_MENU) & 0x8000) mask |= FALT;
2050 if(mask == (lpAccelTbl[i].fVirt & (FSHIFT | FCONTROL | FALT))) goto found;
2051 TRACE_(accel)("incorrect SHIFT/CTRL/ALT-state\n");
2052 }
2053 else
2054 {
2055 if(!(lpMsg->lParam & 0x01000000)) /* no special_key */
2056 {
2057 if((lpAccelTbl[i].fVirt & FALT) && (lpMsg->lParam & 0x20000000))
2058 { /* ^^ ALT pressed */
2059 TRACE_(accel)("found accel for Alt-%c\n", LOWORD(lpMsg->wParam) & 0xff);
2060 goto found;
2061 }
2062 }
2063 }
2064 }
2065 }
2066
2067 WARN_(accel)("couldn't translate accelerator key\n");
2068 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
2069 return FALSE;
2070
2071 found:
2072 if(lpwCmd) *lpwCmd = lpAccelTbl[i].cmd;
2073 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
2074 return TRUE;
2075 }
2076
2077 /***********************************************************************
2078 * ReleaseStgMedium [OLE32.@]
2079 */
2080 void WINAPI ReleaseStgMedium(
2081 STGMEDIUM* pmedium)
2082 {
2083 switch (pmedium->tymed)
2084 {
2085 case TYMED_HGLOBAL:
2086 {
2087 if ( (pmedium->pUnkForRelease==0) &&
2088 (pmedium->u.hGlobal!=0) )
2089 GlobalFree(pmedium->u.hGlobal);
2090 break;
2091 }
2092 case TYMED_FILE:
2093 {
2094 if (pmedium->u.lpszFileName!=0)
2095 {
2096 if (pmedium->pUnkForRelease==0)
2097 {
2098 DeleteFileW(pmedium->u.lpszFileName);
2099 }
2100
2101 CoTaskMemFree(pmedium->u.lpszFileName);
2102 }
2103 break;
2104 }
2105 case TYMED_ISTREAM:
2106 {
2107 if (pmedium->u.pstm!=0)
2108 {
2109 IStream_Release(pmedium->u.pstm);
2110 }
2111 break;
2112 }
2113 case TYMED_ISTORAGE:
2114 {
2115 if (pmedium->u.pstg!=0)
2116 {
2117 IStorage_Release(pmedium->u.pstg);
2118 }
2119 break;
2120 }
2121 case TYMED_GDI:
2122 {
2123 if ( (pmedium->pUnkForRelease==0) &&
2124 (pmedium->u.hBitmap!=0) )
2125 DeleteObject(pmedium->u.hBitmap);
2126 break;
2127 }
2128 case TYMED_MFPICT:
2129 {
2130 if ( (pmedium->pUnkForRelease==0) &&
2131 (pmedium->u.hMetaFilePict!=0) )
2132 {
2133 LPMETAFILEPICT pMP = GlobalLock(pmedium->u.hMetaFilePict);
2134 DeleteMetaFile(pMP->hMF);
2135 GlobalUnlock(pmedium->u.hMetaFilePict);
2136 GlobalFree(pmedium->u.hMetaFilePict);
2137 }
2138 break;
2139 }
2140 case TYMED_ENHMF:
2141 {
2142 if ( (pmedium->pUnkForRelease==0) &&
2143 (pmedium->u.hEnhMetaFile!=0) )
2144 {
2145 DeleteEnhMetaFile(pmedium->u.hEnhMetaFile);
2146 }
2147 break;
2148 }
2149 case TYMED_NULL:
2150 default:
2151 break;
2152 }
2153 pmedium->tymed=TYMED_NULL;
2154
2155 /*
2156 * After cleaning up, the unknown is released
2157 */
2158 if (pmedium->pUnkForRelease!=0)
2159 {
2160 IUnknown_Release(pmedium->pUnkForRelease);
2161 pmedium->pUnkForRelease = 0;
2162 }
2163 }
2164
2165 /***
2166 * OLEDD_Initialize()
2167 *
2168 * Initializes the OLE drag and drop data structures.
2169 */
2170 static void OLEDD_Initialize(void)
2171 {
2172 WNDCLASSW wndClass;
2173
2174 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
2175 wndClass.style = CS_GLOBALCLASS;
2176 wndClass.lpfnWndProc = OLEDD_DragTrackerWindowProc;
2177 wndClass.cbClsExtra = 0;
2178 wndClass.cbWndExtra = sizeof(TrackerWindowInfo*);
2179 wndClass.hCursor = 0;
2180 wndClass.hbrBackground = 0;
2181 wndClass.lpszClassName = OLEDD_DRAGTRACKERCLASS;
2182
2183 RegisterClassW (&wndClass);
2184 }
2185
2186 /***
2187 * OLEDD_DragTrackerWindowProc()
2188 *
2189 * This method is the WindowProcedure of the drag n drop tracking
2190 * window. During a drag n Drop operation, an invisible window is created
2191 * to receive the user input and act upon it. This procedure is in charge
2192 * of this behavior.
2193 */
2194
2195 #define DRAG_TIMER_ID 1
2196
2197 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(
2198 HWND hwnd,
2199 UINT uMsg,
2200 WPARAM wParam,
2201 LPARAM lParam)
2202 {
2203 switch (uMsg)
2204 {
2205 case WM_CREATE:
2206 {
2207 LPCREATESTRUCTA createStruct = (LPCREATESTRUCTA)lParam;
2208
2209 SetWindowLongPtrW(hwnd, 0, (LONG_PTR)createStruct->lpCreateParams);
2210 SetTimer(hwnd, DRAG_TIMER_ID, 50, NULL);
2211
2212 break;
2213 }
2214 case WM_TIMER:
2215 case WM_MOUSEMOVE:
2216 {
2217 OLEDD_TrackMouseMove((TrackerWindowInfo*)GetWindowLongPtrA(hwnd, 0));
2218 break;
2219 }
2220 case WM_LBUTTONUP:
2221 case WM_MBUTTONUP:
2222 case WM_RBUTTONUP:
2223 case WM_LBUTTONDOWN:
2224 case WM_MBUTTONDOWN:
2225 case WM_RBUTTONDOWN:
2226 {
2227 OLEDD_TrackStateChange((TrackerWindowInfo*)GetWindowLongPtrA(hwnd, 0));
2228 break;
2229 }
2230 case WM_DESTROY:
2231 {
2232 KillTimer(hwnd, DRAG_TIMER_ID);
2233 break;
2234 }
2235 }
2236
2237 /*
2238 * This is a window proc after all. Let's call the default.
2239 */
2240 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
2241 }
2242
2243 /***
2244 * OLEDD_TrackMouseMove()
2245 *
2246 * This method is invoked while a drag and drop operation is in effect.
2247 * it will generate the appropriate callbacks in the drop source
2248 * and drop target. It will also provide the expected feedback to
2249 * the user.
2250 *
2251 * params:
2252 * trackerInfo - Pointer to the structure identifying the
2253 * drag & drop operation that is currently
2254 * active.
2255 */
2256 static void OLEDD_TrackMouseMove(TrackerWindowInfo* trackerInfo)
2257 {
2258 HWND hwndNewTarget = 0;
2259 HRESULT hr = S_OK;
2260 POINT pt;
2261
2262 /*
2263 * Get the handle of the window under the mouse
2264 */
2265 pt.x = trackerInfo->curMousePos.x;
2266 pt.y = trackerInfo->curMousePos.y;
2267 hwndNewTarget = WindowFromPoint(pt);
2268
2269 /*
2270 * Every time, we re-initialize the effects passed to the
2271 * IDropTarget to the effects allowed by the source.
2272 */
2273 *trackerInfo->pdwEffect = trackerInfo->dwOKEffect;
2274
2275 /*
2276 * If we are hovering over the same target as before, send the
2277 * DragOver notification
2278 */
2279 if ( (trackerInfo->curDragTarget != 0) &&
2280 (trackerInfo->curTargetHWND == hwndNewTarget) )
2281 {
2282 IDropTarget_DragOver(trackerInfo->curDragTarget,
2283 trackerInfo->dwKeyState,
2284 trackerInfo->curMousePos,
2285 trackerInfo->pdwEffect);
2286 }
2287 else
2288 {
2289 /*
2290 * If we changed window, we have to notify our old target and check for
2291 * the new one.
2292 */
2293 if (trackerInfo->curDragTarget)
2294 IDropTarget_DragLeave(trackerInfo->curDragTarget);
2295
2296 /*
2297 * Make sure we're hovering over a window.
2298 */
2299 if (hwndNewTarget)
2300 {
2301 /*
2302 * Find-out if there is a drag target under the mouse
2303 */
2304 HWND next_target_wnd = hwndNewTarget;
2305
2306 trackerInfo->curTargetHWND = hwndNewTarget;
2307
2308 while (next_target_wnd && !is_droptarget(next_target_wnd))
2309 next_target_wnd = GetParent(next_target_wnd);
2310
2311 if (next_target_wnd) hwndNewTarget = next_target_wnd;
2312
2313 trackerInfo->curDragTargetHWND = hwndNewTarget;
2314 if(trackerInfo->curDragTarget) IDropTarget_Release(trackerInfo->curDragTarget);
2315 trackerInfo->curDragTarget = get_droptarget_pointer(hwndNewTarget);
2316
2317 /*
2318 * If there is, notify it that we just dragged-in
2319 */
2320 if (trackerInfo->curDragTarget)
2321 {
2322 hr = IDropTarget_DragEnter(trackerInfo->curDragTarget,
2323 trackerInfo->dataObject,
2324 trackerInfo->dwKeyState,
2325 trackerInfo->curMousePos,
2326 trackerInfo->pdwEffect);
2327
2328 /* failed DragEnter() means invalid target */
2329 if (hr != S_OK)
2330 {
2331 trackerInfo->curDragTargetHWND = 0;
2332 trackerInfo->curTargetHWND = 0;
2333 IDropTarget_Release(trackerInfo->curDragTarget);
2334 trackerInfo->curDragTarget = 0;
2335 }
2336 }
2337 }
2338 else
2339 {
2340 /*
2341 * The mouse is not over a window so we don't track anything.
2342 */
2343 trackerInfo->curDragTargetHWND = 0;
2344 trackerInfo->curTargetHWND = 0;
2345 if(trackerInfo->curDragTarget) IDropTarget_Release(trackerInfo->curDragTarget);
2346 trackerInfo->curDragTarget = 0;
2347 }
2348 }
2349
2350 /*
2351 * Now that we have done that, we have to tell the source to give
2352 * us feedback on the work being done by the target. If we don't
2353 * have a target, simulate no effect.
2354 */
2355 if (trackerInfo->curDragTarget==0)
2356 {
2357 *trackerInfo->pdwEffect = DROPEFFECT_NONE;
2358 }
2359
2360 hr = IDropSource_GiveFeedback(trackerInfo->dropSource,
2361 *trackerInfo->pdwEffect);
2362
2363 /*
2364 * When we ask for feedback from the drop source, sometimes it will
2365 * do all the necessary work and sometimes it will not handle it
2366 * when that's the case, we must display the standard drag and drop
2367 * cursors.
2368 */
2369 if (hr == DRAGDROP_S_USEDEFAULTCURSORS)
2370 {
2371 HCURSOR hCur;
2372
2373 if (*trackerInfo->pdwEffect & DROPEFFECT_MOVE)
2374 {
2375 hCur = LoadCursorW(hProxyDll, MAKEINTRESOURCEW(2));
2376 }
2377 else if (*trackerInfo->pdwEffect & DROPEFFECT_COPY)
2378 {
2379 hCur = LoadCursorW(hProxyDll, MAKEINTRESOURCEW(3));
2380 }
2381 else if (*trackerInfo->pdwEffect & DROPEFFECT_LINK)
2382 {
2383 hCur = LoadCursorW(hProxyDll, MAKEINTRESOURCEW(4));
2384 }
2385 else
2386 {
2387 hCur = LoadCursorW(hProxyDll, MAKEINTRESOURCEW(1));
2388 }
2389
2390 SetCursor(hCur);
2391 }
2392 }
2393
2394 /***
2395 * OLEDD_TrackStateChange()
2396 *
2397 * This method is invoked while a drag and drop operation is in effect.
2398 * It is used to notify the drop target/drop source callbacks when
2399 * the state of the keyboard or mouse button change.
2400 *
2401 * params:
2402 * trackerInfo - Pointer to the structure identifying the
2403 * drag & drop operation that is currently
2404 * active.
2405 */
2406 static void OLEDD_TrackStateChange(TrackerWindowInfo* trackerInfo)
2407 {
2408 /*
2409 * Ask the drop source what to do with the operation.
2410 */
2411 trackerInfo->returnValue = IDropSource_QueryContinueDrag(
2412 trackerInfo->dropSource,
2413 trackerInfo->escPressed,
2414 trackerInfo->dwKeyState);
2415
2416 /*
2417 * All the return valued will stop the operation except the S_OK
2418 * return value.
2419 */
2420 if (trackerInfo->returnValue!=S_OK)
2421 {
2422 /*
2423 * Make sure the message loop in DoDragDrop stops
2424 */
2425 trackerInfo->trackingDone = TRUE;
2426
2427 /*
2428 * Release the mouse in case the drop target decides to show a popup
2429 * or a menu or something.
2430 */
2431 ReleaseCapture();
2432
2433 /*
2434 * If we end-up over a target, drop the object in the target or
2435 * inform the target that the operation was cancelled.
2436 */
2437 if (trackerInfo->curDragTarget)
2438 {
2439 switch (trackerInfo->returnValue)
2440 {
2441 /*
2442 * If the source wants us to complete the operation, we tell
2443 * the drop target that we just dropped the object in it.
2444 */
2445 case DRAGDROP_S_DROP:
2446 if (*trackerInfo->pdwEffect != DROPEFFECT_NONE)
2447 IDropTarget_Drop(trackerInfo->curDragTarget,
2448 trackerInfo->dataObject,
2449 trackerInfo->dwKeyState,
2450 trackerInfo->curMousePos,
2451 trackerInfo->pdwEffect);
2452 else
2453 IDropTarget_DragLeave(trackerInfo->curDragTarget);
2454 break;
2455
2456 /*
2457 * If the source told us that we should cancel, fool the drop
2458 * target by telling it that the mouse left it's window.
2459 * Also set the drop effect to "NONE" in case the application
2460 * ignores the result of DoDragDrop.
2461 */
2462 case DRAGDROP_S_CANCEL:
2463 IDropTarget_DragLeave(trackerInfo->curDragTarget);
2464 *trackerInfo->pdwEffect = DROPEFFECT_NONE;
2465 break;
2466 }
2467 }
2468 }
2469 }
2470
2471 /***
2472 * OLEDD_GetButtonState()
2473 *
2474 * This method will use the current state of the keyboard to build
2475 * a button state mask equivalent to the one passed in the
2476 * WM_MOUSEMOVE wParam.
2477 */
2478 static DWORD OLEDD_GetButtonState(void)
2479 {
2480 BYTE keyboardState[256];
2481 DWORD keyMask = 0;
2482
2483 GetKeyboardState(keyboardState);
2484
2485 if ( (keyboardState[VK_SHIFT] & 0x80) !=0)
2486 keyMask |= MK_SHIFT;
2487
2488 if ( (keyboardState[VK_CONTROL] & 0x80) !=0)
2489 keyMask |= MK_CONTROL;
2490
2491 if ( (keyboardState[VK_LBUTTON] & 0x80) !=0)
2492 keyMask |= MK_LBUTTON;
2493
2494 if ( (keyboardState[VK_RBUTTON] & 0x80) !=0)
2495 keyMask |= MK_RBUTTON;
2496
2497 if ( (keyboardState[VK_MBUTTON] & 0x80) !=0)
2498 keyMask |= MK_MBUTTON;
2499
2500 return keyMask;
2501 }
2502
2503 /***
2504 * OLEDD_GetButtonState()
2505 *
2506 * This method will read the default value of the registry key in
2507 * parameter and extract a DWORD value from it. The registry key value
2508 * can be in a string key or a DWORD key.
2509 *
2510 * params:
2511 * regKey - Key to read the default value from
2512 * pdwValue - Pointer to the location where the DWORD
2513 * value is returned. This value is not modified
2514 * if the value is not found.
2515 */
2516
2517 static void OLEUTL_ReadRegistryDWORDValue(
2518 HKEY regKey,
2519 DWORD* pdwValue)
2520 {
2521 WCHAR buffer[20];
2522 DWORD cbData = sizeof(buffer);
2523 DWORD dwKeyType;
2524 LONG lres;
2525
2526 lres = RegQueryValueExW(regKey,
2527 emptyW,
2528 NULL,
2529 &dwKeyType,
2530 (LPBYTE)buffer,
2531 &cbData);
2532
2533 if (lres==ERROR_SUCCESS)
2534 {
2535 switch (dwKeyType)
2536 {
2537 case REG_DWORD:
2538 *pdwValue = *(DWORD*)buffer;
2539 break;
2540 case REG_EXPAND_SZ:
2541 case REG_MULTI_SZ:
2542 case REG_SZ:
2543 *pdwValue = (DWORD)strtoulW(buffer, NULL, 10);
2544 break;
2545 }
2546 }
2547 }
2548
2549 /******************************************************************************
2550 * OleDraw (OLE32.@)
2551 *
2552 * The operation of this function is documented literally in the WinAPI
2553 * documentation to involve a QueryInterface for the IViewObject interface,
2554 * followed by a call to IViewObject::Draw.
2555 */
2556 HRESULT WINAPI OleDraw(
2557 IUnknown *pUnk,
2558 DWORD dwAspect,
2559 HDC hdcDraw,
2560 LPCRECT lprcBounds)
2561 {
2562 HRESULT hres;
2563 IViewObject *viewobject;
2564
2565 hres = IUnknown_QueryInterface(pUnk,
2566 &IID_IViewObject,
2567 (void**)&viewobject);
2568
2569 if (SUCCEEDED(hres))
2570 {
2571 RECTL rectl;
2572
2573 rectl.left = lprcBounds->left;
2574 rectl.right = lprcBounds->right;
2575 rectl.top = lprcBounds->top;
2576 rectl.bottom = lprcBounds->bottom;
2577 hres = IViewObject_Draw(viewobject, dwAspect, -1, 0, 0, 0, hdcDraw, &rectl, 0, 0, 0);
2578
2579 IViewObject_Release(viewobject);
2580 return hres;
2581 }
2582 else
2583 {
2584 return DV_E_NOIVIEWOBJECT;
2585 }
2586 }
2587
2588 /***********************************************************************
2589 * OleTranslateAccelerator [OLE32.@]
2590 */
2591 HRESULT WINAPI OleTranslateAccelerator (LPOLEINPLACEFRAME lpFrame,
2592 LPOLEINPLACEFRAMEINFO lpFrameInfo, LPMSG lpmsg)
2593 {
2594 WORD wID;
2595
2596 TRACE("(%p,%p,%p)\n", lpFrame, lpFrameInfo, lpmsg);
2597
2598 if (IsAccelerator(lpFrameInfo->haccel,lpFrameInfo->cAccelEntries,lpmsg,&wID))
2599 return IOleInPlaceFrame_TranslateAccelerator(lpFrame,lpmsg,wID);
2600
2601 return S_FALSE;
2602 }
2603
2604 /******************************************************************************
2605 * OleCreate [OLE32.@]
2606 *
2607 */
2608 HRESULT WINAPI OleCreate(
2609 REFCLSID rclsid,
2610 REFIID riid,
2611 DWORD renderopt,
2612 LPFORMATETC pFormatEtc,
2613 LPOLECLIENTSITE pClientSite,
2614 LPSTORAGE pStg,
2615 LPVOID* ppvObj)
2616 {
2617 HRESULT hres;
2618 IUnknown * pUnk = NULL;
2619 IOleObject *pOleObject = NULL;
2620
2621 TRACE("(%s, %s, %d, %p, %p, %p, %p)\n", debugstr_guid(rclsid),
2622 debugstr_guid(riid), renderopt, pFormatEtc, pClientSite, pStg, ppvObj);
2623
2624 hres = CoCreateInstance(rclsid, 0, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER, riid, (LPVOID*)&pUnk);
2625
2626 if (SUCCEEDED(hres))
2627 hres = IStorage_SetClass(pStg, rclsid);
2628
2629 if (pClientSite && SUCCEEDED(hres))
2630 {
2631 hres = IUnknown_QueryInterface(pUnk, &IID_IOleObject, (LPVOID*)&pOleObject);
2632 if (SUCCEEDED(hres))
2633 {
2634 DWORD dwStatus;
2635 hres = IOleObject_GetMiscStatus(pOleObject, DVASPECT_CONTENT, &dwStatus);
2636 }
2637 }
2638
2639 if (SUCCEEDED(hres))
2640 {
2641 IPersistStorage * pPS;
2642 if (SUCCEEDED((hres = IUnknown_QueryInterface(pUnk, &IID_IPersistStorage, (LPVOID*)&pPS))))
2643 {
2644 TRACE("trying to set stg %p\n", pStg);
2645 hres = IPersistStorage_InitNew(pPS, pStg);
2646 TRACE("-- result 0x%08x\n", hres);
2647 IPersistStorage_Release(pPS);
2648 }
2649 }
2650
2651 if (pClientSite && SUCCEEDED(hres))
2652 {
2653 TRACE("trying to set clientsite %p\n", pClientSite);
2654 hres = IOleObject_SetClientSite(pOleObject, pClientSite);
2655 TRACE("-- result 0x%08x\n", hres);
2656 }
2657
2658 if (pOleObject)
2659 IOleObject_Release(pOleObject);
2660
2661 if (((renderopt == OLERENDER_DRAW) || (renderopt == OLERENDER_FORMAT)) &&
2662 SUCCEEDED(hres))
2663 {
2664 IRunnableObject *pRunnable;
2665 IOleCache *pOleCache;
2666 HRESULT hres2;
2667
2668 hres2 = IUnknown_QueryInterface(pUnk, &IID_IRunnableObject, (void **)&pRunnable);
2669 if (SUCCEEDED(hres2))
2670 {
2671 hres = IRunnableObject_Run(pRunnable, NULL);
2672 IRunnableObject_Release(pRunnable);
2673 }
2674
2675 if (SUCCEEDED(hres))
2676 {
2677 hres2 = IUnknown_QueryInterface(pUnk, &IID_IOleCache, (void **)&pOleCache);
2678 if (SUCCEEDED(hres2))
2679 {
2680 DWORD dwConnection;
2681 if (renderopt == OLERENDER_DRAW && !pFormatEtc) {
2682 FORMATETC pfe;
2683 pfe.cfFormat = 0;
2684 pfe.ptd = NULL;
2685 pfe.dwAspect = DVASPECT_CONTENT;
2686 pfe.lindex = -1;
2687 pfe.tymed = TYMED_NULL;
2688 hres = IOleCache_Cache(pOleCache, &pfe, ADVF_PRIMEFIRST, &dwConnection);
2689 }
2690 else
2691 hres = IOleCache_Cache(pOleCache, pFormatEtc, ADVF_PRIMEFIRST, &dwConnection);
2692 IOleCache_Release(pOleCache);
2693 }
2694 }
2695 }
2696
2697 if (FAILED(hres) && pUnk)
2698 {
2699 IUnknown_Release(pUnk);
2700 pUnk = NULL;
2701 }
2702
2703 *ppvObj = pUnk;
2704
2705 TRACE("-- %p\n", pUnk);
2706 return hres;
2707 }
2708
2709 /******************************************************************************
2710 * OleGetAutoConvert [OLE32.@]
2711 */
2712 HRESULT WINAPI OleGetAutoConvert(REFCLSID clsidOld, LPCLSID pClsidNew)
2713 {
2714 static const WCHAR wszAutoConvertTo[] = {'A','u','t','o','C','o','n','v','e','r','t','T','o',0};
2715 HKEY hkey = NULL;
2716 WCHAR buf[CHARS_IN_GUID];
2717 LONG len;
2718 HRESULT res = S_OK;
2719
2720 res = COM_OpenKeyForCLSID(clsidOld, wszAutoConvertTo, KEY_READ, &hkey);
2721 if (FAILED(res))
2722 goto done;
2723
2724 len = sizeof(buf);
2725 if (RegQueryValueW(hkey, NULL, buf, &len))
2726 {
2727 res = REGDB_E_KEYMISSING;
2728 goto done;
2729 }
2730 res = CLSIDFromString(buf, pClsidNew);
2731 done:
2732 if (hkey) RegCloseKey(hkey);
2733 return res;
2734 }
2735
2736 /******************************************************************************
2737 * OleSetAutoConvert [OLE32.@]
2738 */
2739 HRESULT WINAPI OleSetAutoConvert(REFCLSID clsidOld, REFCLSID clsidNew)
2740 {
2741 static const WCHAR wszAutoConvertTo[] = {'A','u','t','o','C','o','n','v','e','r','t','T','o',0};
2742 HKEY hkey = NULL;
2743 WCHAR szClsidNew[CHARS_IN_GUID];
2744 HRESULT res = S_OK;
2745
2746 TRACE("(%s,%s)\n", debugstr_guid(clsidOld), debugstr_guid(clsidNew));
2747
2748 res = COM_OpenKeyForCLSID(clsidOld, NULL, KEY_READ | KEY_WRITE, &hkey);
2749 if (FAILED(res))
2750 goto done;
2751 StringFromGUID2(clsidNew, szClsidNew, CHARS_IN_GUID);
2752 if (RegSetValueW(hkey, wszAutoConvertTo, REG_SZ, szClsidNew, (strlenW(szClsidNew)+1) * sizeof(WCHAR)))
2753 {
2754 res = REGDB_E_WRITEREGDB;
2755 goto done;
2756 }
2757
2758 done:
2759 if (hkey) RegCloseKey(hkey);
2760 return res;
2761 }
2762
2763 /******************************************************************************
2764 * OleDoAutoConvert [OLE32.@]
2765 */
2766 HRESULT WINAPI OleDoAutoConvert(LPSTORAGE pStg, LPCLSID pClsidNew)
2767 {
2768 FIXME("(%p,%p) : stub\n",pStg,pClsidNew);
2769 return E_NOTIMPL;
2770 }
2771
2772 /******************************************************************************
2773 * OleIsRunning [OLE32.@]
2774 */
2775 BOOL WINAPI OleIsRunning(LPOLEOBJECT object)
2776 {
2777 IRunnableObject *pRunnable;
2778 HRESULT hr;
2779 BOOL running;
2780
2781 TRACE("(%p)\n", object);
2782
2783 if (!object) return FALSE;
2784
2785 hr = IOleObject_QueryInterface(object, &IID_IRunnableObject, (void **)&pRunnable);
2786 if (FAILED(hr))
2787 return TRUE;
2788 running = IRunnableObject_IsRunning(pRunnable);
2789 IRunnableObject_Release(pRunnable);
2790 return running;
2791 }
2792
2793 /***********************************************************************
2794 * OleNoteObjectVisible [OLE32.@]
2795 */
2796 HRESULT WINAPI OleNoteObjectVisible(LPUNKNOWN pUnknown, BOOL bVisible)
2797 {
2798 TRACE("(%p, %s)\n", pUnknown, bVisible ? "TRUE" : "FALSE");
2799 return CoLockObjectExternal(pUnknown, bVisible, TRUE);
2800 }
2801
2802
2803 /***********************************************************************
2804 * OLE_FreeClipDataArray [internal]
2805 *
2806 * NOTES:
2807 * frees the data associated with an array of CLIPDATAs
2808 */
2809 static void OLE_FreeClipDataArray(ULONG count, CLIPDATA * pClipDataArray)
2810 {
2811 ULONG i;
2812 for (i = 0; i < count; i++)
2813 if (pClipDataArray[i].pClipData)
2814 CoTaskMemFree(pClipDataArray[i].pClipData);
2815 }
2816
2817 /***********************************************************************
2818 * PropSysAllocString [OLE32.@]
2819 * NOTES:
2820 * Basically a copy of SysAllocStringLen.
2821 */
2822 BSTR WINAPI PropSysAllocString(LPCOLESTR str)
2823 {
2824 DWORD bufferSize;
2825 DWORD* newBuffer;
2826 WCHAR* stringBuffer;
2827 int len;
2828
2829 if (!str) return 0;
2830
2831 len = lstrlenW(str);
2832 /*
2833 * Find the length of the buffer passed-in, in bytes.
2834 */
2835 bufferSize = len * sizeof (WCHAR);
2836
2837 /*
2838 * Allocate a new buffer to hold the string.
2839 * Don't forget to keep an empty spot at the beginning of the
2840 * buffer for the character count and an extra character at the
2841 * end for the NULL.
2842 */
2843 newBuffer = HeapAlloc(GetProcessHeap(), 0,
2844 bufferSize + sizeof(WCHAR) + sizeof(DWORD));
2845
2846 /*
2847 * If the memory allocation failed, return a null pointer.
2848 */
2849 if (newBuffer==0)
2850 return 0;
2851
2852 /*
2853 * Copy the length of the string in the placeholder.
2854 */
2855 *newBuffer = bufferSize;
2856
2857 /*
2858 * Skip the byte count.
2859 */
2860 newBuffer++;
2861
2862 memcpy(newBuffer, str, bufferSize);
2863
2864 /*
2865 * Make sure that there is a nul character at the end of the
2866 * string.
2867 */
2868 stringBuffer = (WCHAR*)newBuffer;
2869 stringBuffer[len] = '\0';
2870
2871 return stringBuffer;
2872 }
2873
2874 /***********************************************************************
2875 * PropSysFreeString [OLE32.@]
2876 * NOTES
2877 * Copy of SysFreeString.
2878 */
2879 void WINAPI PropSysFreeString(LPOLESTR str)
2880 {
2881 DWORD* bufferPointer;
2882
2883 /* NULL is a valid parameter */
2884 if(!str) return;
2885
2886 /*
2887 * We have to be careful when we free a BSTR pointer, it points to
2888 * the beginning of the string but it skips the byte count contained
2889 * before the string.
2890 */
2891 bufferPointer = (DWORD*)str;
2892
2893 bufferPointer--;
2894
2895 /*
2896 * Free the memory from its "real" origin.
2897 */
2898 HeapFree(GetProcessHeap(), 0, bufferPointer);
2899 }
2900
2901 /******************************************************************************
2902 * Check if a PROPVARIANT's type is valid.
2903 */
2904 static inline HRESULT PROPVARIANT_ValidateType(VARTYPE vt)
2905 {
2906 switch (vt)
2907 {
2908 case VT_EMPTY:
2909 case VT_NULL:
2910 case VT_I2:
2911 case VT_I4:
2912 case VT_R4:
2913 case VT_R8:
2914 case VT_CY:
2915 case VT_DATE:
2916 case VT_BSTR:
2917 case VT_ERROR:
2918 case VT_BOOL:
2919 case VT_DECIMAL:
2920 case VT_UI1:
2921 case VT_UI2:
2922 case VT_UI4:
2923 case VT_I8:
2924 case VT_UI8:
2925 case VT_LPSTR:
2926 case VT_LPWSTR:
2927 case VT_FILETIME:
2928 case VT_BLOB:
2929 case VT_STREAM:
2930 case VT_STORAGE:
2931 case VT_STREAMED_OBJECT:
2932 case VT_STORED_OBJECT:
2933 case VT_BLOB_OBJECT:
2934 case VT_CF:
2935 case VT_CLSID:
2936 case VT_I2|VT_VECTOR:
2937 case VT_I4|VT_VECTOR:
2938 case VT_R4|VT_VECTOR:
2939 case VT_R8|VT_VECTOR:
2940 case VT_CY|VT_VECTOR:
2941 case VT_DATE|VT_VECTOR:
2942 case VT_BSTR|VT_VECTOR:
2943 case VT_ERROR|VT_VECTOR:
2944 case VT_BOOL|VT_VECTOR:
2945 case VT_VARIANT|VT_VECTOR:
2946 case VT_UI1|VT_VECTOR:
2947 case VT_UI2|VT_VECTOR:
2948 case VT_UI4|VT_VECTOR:
2949 case VT_I8|VT_VECTOR:
2950 case VT_UI8|VT_VECTOR:
2951 case VT_LPSTR|VT_VECTOR:
2952 case VT_LPWSTR|VT_VECTOR:
2953 case VT_FILETIME|VT_VECTOR:
2954 case VT_CF|VT_VECTOR:
2955 case VT_CLSID|VT_VECTOR:
2956 return S_OK;
2957 }
2958 WARN("Bad type %d\n", vt);
2959 return STG_E_INVALIDPARAMETER;
2960 }
2961
2962 /***********************************************************************
2963 * PropVariantClear [OLE32.@]
2964 */
2965 HRESULT WINAPI PropVariantClear(PROPVARIANT * pvar) /* [in/out] */
2966 {
2967 HRESULT hr;
2968
2969 TRACE("(%p)\n", pvar);
2970
2971 if (!pvar)
2972 return S_OK;
2973
2974 hr = PROPVARIANT_ValidateType(pvar->vt);
2975 if (FAILED(hr))
2976 return hr;
2977
2978 switch(pvar->vt)
2979 {
2980 case VT_EMPTY:
2981 case VT_NULL:
2982 case VT_I2:
2983 case VT_I4:
2984 case VT_R4:
2985 case VT_R8:
2986 case VT_CY:
2987 case VT_DATE:
2988 case VT_ERROR:
2989 case VT_BOOL:
2990 case VT_DECIMAL:
2991 case VT_UI1:
2992 case VT_UI2:
2993 case VT_UI4:
2994 case VT_I8:
2995 case VT_UI8:
2996 case VT_FILETIME:
2997 break;
2998 case VT_STREAM:
2999 case VT_STREAMED_OBJECT:
3000 case VT_STORAGE:
3001 case VT_STORED_OBJECT:
3002 if (pvar->u.pStream)
3003 IUnknown_Release(pvar->u.pStream);
3004 break;
3005 case VT_CLSID:
3006 case VT_LPSTR:
3007 case VT_LPWSTR:
3008 /* pick an arbitrary typed pointer - we don't care about the type
3009 * as we are just freeing it */
3010 CoTaskMemFree(pvar->u.puuid);
3011 break;
3012 case VT_BLOB:
3013 case VT_BLOB_OBJECT:
3014 CoTaskMemFree(pvar->u.blob.pBlobData);
3015 break;
3016 case VT_BSTR:
3017 if (pvar->u.bstrVal)
3018 PropSysFreeString(pvar->u.bstrVal);
3019 break;
3020 case VT_CF:
3021 if (pvar->u.pclipdata)
3022 {
3023 OLE_FreeClipDataArray(1, pvar->u.pclipdata);
3024 CoTaskMemFree(pvar->u.pclipdata);
3025 }
3026 break;
3027 default:
3028 if (pvar->vt & VT_VECTOR)
3029 {
3030 ULONG i;
3031
3032 switch (pvar->vt & ~VT_VECTOR)
3033 {
3034 case VT_VARIANT:
3035 FreePropVariantArray(pvar->u.capropvar.cElems, pvar->u.capropvar.pElems);
3036 break;
3037 case VT_CF:
3038 OLE_FreeClipDataArray(pvar->u.caclipdata.cElems, pvar->u.caclipdata.pElems);
3039 break;
3040 case VT_BSTR:
3041 for (i = 0; i < pvar->u.cabstr.cElems; i++)
3042 PropSysFreeString(pvar->u.cabstr.pElems[i]);
3043 break;
3044 case VT_LPSTR:
3045 for (i = 0; i < pvar->u.calpstr.cElems; i++)
3046 CoTaskMemFree(pvar->u.calpstr.pElems[i]);
3047 break;
3048 case VT_LPWSTR:
3049 for (i = 0; i < pvar->u.calpwstr.cElems; i++)
3050 CoTaskMemFree(pvar->u.calpwstr.pElems[i]);
3051 break;
3052 }
3053 if (pvar->vt & ~VT_VECTOR)
3054 {
3055 /* pick an arbitrary VT_VECTOR structure - they all have the same
3056 * memory layout */
3057 CoTaskMemFree(pvar->u.capropvar.pElems);
3058 }
3059 }
3060 else
3061 WARN("Invalid/unsupported type %d\n", pvar->vt);
3062 }
3063
3064 ZeroMemory(pvar, sizeof(*pvar));
3065
3066 return S_OK;
3067 }
3068
3069 /***********************************************************************
3070 * PropVariantCopy [OLE32.@]
3071 */
3072 HRESULT WINAPI PropVariantCopy(PROPVARIANT *pvarDest, /* [out] */
3073 const PROPVARIANT *pvarSrc) /* [in] */
3074 {
3075 ULONG len;
3076 HRESULT hr;
3077
3078 TRACE("(%p, %p vt %04x)\n", pvarDest, pvarSrc, pvarSrc->vt);
3079
3080 hr = PROPVARIANT_ValidateType(pvarSrc->vt);
3081 if (FAILED(hr))
3082 return hr;
3083
3084 /* this will deal with most cases */
3085 *pvarDest = *pvarSrc;
3086
3087 switch(pvarSrc->vt)
3088 {
3089 case VT_EMPTY:
3090 case VT_NULL:
3091 case VT_I1:
3092 case VT_UI1:
3093 case VT_I2:
3094 case VT_UI2:
3095 case VT_BOOL:
3096 case VT_DECIMAL:
3097 case VT_I4:
3098 case VT_UI4:
3099 case VT_R4:
3100 case VT_ERROR:
3101 case VT_I8:
3102 case VT_UI8:
3103 case VT_R8:
3104 case VT_CY:
3105 case VT_DATE:
3106 case VT_FILETIME:
3107 break;
3108 case VT_STREAM:
3109 case VT_STREAMED_OBJECT:
3110 case VT_STORAGE:
3111 case VT_STORED_OBJECT:
3112 IUnknown_AddRef((LPUNKNOWN)pvarDest->u.pStream);
3113 break;
3114 case VT_CLSID:
3115 pvarDest->u.puuid = CoTaskMemAlloc(sizeof(CLSID));
3116 *pvarDest->u.puuid = *pvarSrc->u.puuid;
3117 break;
3118 case VT_LPSTR:
3119 len = strlen(pvarSrc->u.pszVal);
3120 pvarDest->u.pszVal = CoTaskMemAlloc((len+1)*sizeof(CHAR));
3121 CopyMemory(pvarDest->u.pszVal, pvarSrc->u.pszVal, (len+1)*sizeof(CHAR));
3122 break;
3123 case VT_LPWSTR:
3124 len = lstrlenW(pvarSrc->u.pwszVal);
3125 pvarDest->u.pwszVal = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
3126 CopyMemory(pvarDest->u.pwszVal, pvarSrc->u.pwszVal, (len+1)*sizeof(WCHAR));
3127 break;
3128 case VT_BLOB:
3129 case VT_BLOB_OBJECT:
3130 if (pvarSrc->u.blob.pBlobData)
3131 {
3132 len = pvarSrc->u.blob.cbSize;
3133 pvarDest->u.blob.pBlobData = CoTaskMemAlloc(len);
3134 CopyMemory(pvarDest->u.blob.pBlobData, pvarSrc->u.blob.pBlobData, len);
3135 }
3136 break;
3137 case VT_BSTR:
3138 pvarDest->u.bstrVal = PropSysAllocString(pvarSrc->u.bstrVal);
3139 break;
3140 case VT_CF:
3141 if (pvarSrc->u.pclipdata)
3142 {
3143 len = pvarSrc->u.pclipdata->cbSize - sizeof(pvarSrc->u.pclipdata->ulClipFmt);
3144 pvarDest->u.pclipdata = CoTaskMemAlloc(sizeof (CLIPDATA));
3145 pvarDest->u.pclipdata->cbSize = pvarSrc->u.pclipdata->cbSize;
3146 pvarDest->u.pclipdata->ulClipFmt = pvarSrc->u.pclipdata->ulClipFmt;
3147 pvarDest->u.pclipdata->pClipData = CoTaskMemAlloc(len);
3148 CopyMemory(pvarDest->u.pclipdata->pClipData, pvarSrc->u.pclipdata->pClipData, len);
3149 }
3150 break;
3151 default:
3152 if (pvarSrc->vt & VT_VECTOR)
3153 {
3154 int elemSize;
3155 ULONG i;
3156
3157 switch(pvarSrc->vt & ~VT_VECTOR)
3158 {
3159 case VT_I1: elemSize = sizeof(pvarSrc->u.cVal); break;
3160 case VT_UI1: elemSize = sizeof(pvarSrc->u.bVal); break;
3161 case VT_I2: elemSize = sizeof(pvarSrc->u.iVal); break;
3162 case VT_UI2: elemSize = sizeof(pvarSrc->u.uiVal); break;
3163 case VT_BOOL: elemSize = sizeof(pvarSrc->u.boolVal); break;
3164 case VT_I4: elemSize = sizeof(pvarSrc->u.lVal); break;
3165 case VT_UI4: elemSize = sizeof(pvarSrc->u.ulVal); break;
3166 case VT_R4: elemSize = sizeof(pvarSrc->u.fltVal); break;
3167 case VT_R8: elemSize = sizeof(pvarSrc->u.dblVal); break;
3168 case VT_ERROR: elemSize = sizeof(pvarSrc->u.scode); break;
3169 case VT_I8: elemSize = sizeof(pvarSrc->u.hVal); break;
3170 case VT_UI8: elemSize = sizeof(pvarSrc->u.uhVal); break;
3171 case VT_CY: elemSize = sizeof(pvarSrc->u.cyVal); break;
3172 case VT_DATE: elemSize = sizeof(pvarSrc->u.date); break;
3173 case VT_FILETIME: elemSize = sizeof(pvarSrc->u.filetime); break;
3174 case VT_CLSID: elemSize = sizeof(*pvarSrc->u.puuid); break;
3175 case VT_CF: elemSize = sizeof(*pvarSrc->u.pclipdata); break;
3176 case VT_BSTR: elemSize = sizeof(pvarSrc->u.bstrVal); break;
3177 case VT_LPSTR: elemSize = sizeof(pvarSrc->u.pszVal); break;
3178 case VT_LPWSTR: elemSize = sizeof(pvarSrc->u.pwszVal); break;
3179 case VT_VARIANT: elemSize = sizeof(*pvarSrc->u.pvarVal); break;
3180
3181 default:
3182 FIXME("Invalid element type: %ul\n", pvarSrc->vt & ~VT_VECTOR);
3183 return E_INVALIDARG;
3184 }
3185 len = pvarSrc->u.capropvar.cElems;
3186 pvarDest->u.capropvar.pElems = CoTaskMemAlloc(len * elemSize);
3187 if (pvarSrc->vt == (VT_VECTOR | VT_VARIANT))
3188 {
3189 for (i = 0; i < len; i++)
3190 PropVariantCopy(&pvarDest->u.capropvar.pElems[i], &pvarSrc->u.capropvar.pElems[i]);
3191 }
3192 else if (pvarSrc->vt == (VT_VECTOR | VT_CF))
3193 {
3194 FIXME("Copy clipformats\n");
3195 }
3196 else if (pvarSrc->vt == (VT_VECTOR | VT_BSTR))
3197 {
3198 for (i = 0; i < len; i++)
3199 pvarDest->u.cabstr.pElems[i] = PropSysAllocString(pvarSrc->u.cabstr.pElems[i]);
3200 }
3201 else if (pvarSrc->vt == (VT_VECTOR | VT_LPSTR))
3202 {
3203 size_t strLen;
3204 for (i = 0; i < len; i++)
3205 {
3206 strLen = lstrlenA(pvarSrc->u.calpstr.pElems[i]) + 1;
3207 pvarDest->u.calpstr.pElems[i] = CoTaskMemAlloc(strLen);
3208 memcpy(pvarDest->u.calpstr.pElems[i],
3209 pvarSrc->u.calpstr.pElems[i], strLen);
3210 }
3211 }
3212 else if (pvarSrc->vt == (VT_VECTOR | VT_LPWSTR))
3213 {
3214 size_t strLen;
3215 for (i = 0; i < len; i++)
3216 {
3217 strLen = (lstrlenW(pvarSrc->u.calpwstr.pElems[i]) + 1) *
3218 sizeof(WCHAR);
3219 pvarDest->u.calpstr.pElems[i] = CoTaskMemAlloc(strLen);
3220 memcpy(pvarDest->u.calpstr.pElems[i],
3221 pvarSrc->u.calpstr.pElems[i], strLen);
3222 }
3223 }
3224 else
3225 CopyMemory(pvarDest->u.capropvar.pElems, pvarSrc->u.capropvar.pElems, len * elemSize);
3226 }
3227 else
3228 WARN("Invalid/unsupported type %d\n", pvarSrc->vt);
3229 }
3230
3231 return S_OK;
3232 }
3233
3234 /***********************************************************************
3235 * FreePropVariantArray [OLE32.@]
3236 */
3237 HRESULT WINAPI FreePropVariantArray(ULONG cVariants, /* [in] */
3238 PROPVARIANT *rgvars) /* [in/out] */
3239 {
3240 ULONG i;
3241
3242 TRACE("(%u, %p)\n", cVariants, rgvars);
3243
3244 if (!rgvars)
3245 return E_INVALIDARG;
3246
3247 for(i = 0; i < cVariants; i++)
3248 PropVariantClear(&rgvars[i]);
3249
3250 return S_OK;
3251 }
3252
3253 /******************************************************************************
3254 * DllDebugObjectRPCHook (OLE32.@)
3255 * turns on and off internal debugging, pointer is only used on macintosh
3256 */
3257
3258 BOOL WINAPI DllDebugObjectRPCHook(BOOL b, void *dummy)
3259 {
3260 FIXME("stub\n");
3261 return TRUE;
3262 }
3263
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.