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