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