Sync with trunk revision 63128.
[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 "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 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 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 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
1257 /*
1258 * Now, try and create the handler for the object
1259 */
1260 hres = CoCreateInstance(&storageInfo.clsid,
1261 NULL,
1262 CLSCTX_INPROC_HANDLER|CLSCTX_INPROC_SERVER,
1263 riid,
1264 (void**)&pUnk);
1265
1266 /*
1267 * If that fails, as it will most times, load the default
1268 * OLE handler.
1269 */
1270 if (FAILED(hres))
1271 {
1272 hres = OleCreateDefaultHandler(&storageInfo.clsid,
1273 NULL,
1274 riid,
1275 (void**)&pUnk);
1276 }
1277
1278 /*
1279 * If we couldn't find a handler... this is bad. Abort the whole thing.
1280 */
1281 if (FAILED(hres))
1282 return hres;
1283
1284 if (pClientSite)
1285 {
1286 hres = IUnknown_QueryInterface(pUnk, &IID_IOleObject, (void **)&pOleObject);
1287 if (SUCCEEDED(hres))
1288 {
1289 DWORD dwStatus;
1290 hres = IOleObject_GetMiscStatus(pOleObject, DVASPECT_CONTENT, &dwStatus);
1291 }
1292 }
1293
1294 /*
1295 * Initialize the object with its IPersistStorage interface.
1296 */
1297 hres = IUnknown_QueryInterface(pUnk, &IID_IPersistStorage, (void**)&persistStorage);
1298 if (SUCCEEDED(hres))
1299 {
1300 hres = IPersistStorage_Load(persistStorage, pStg);
1301
1302 IPersistStorage_Release(persistStorage);
1303 persistStorage = NULL;
1304 }
1305
1306 if (SUCCEEDED(hres) && pClientSite)
1307 /*
1308 * Inform the new object of its client site.
1309 */
1310 hres = IOleObject_SetClientSite(pOleObject, pClientSite);
1311
1312 /*
1313 * Cleanup interfaces used internally
1314 */
1315 if (pOleObject)
1316 IOleObject_Release(pOleObject);
1317
1318 if (SUCCEEDED(hres))
1319 {
1320 IOleLink *pOleLink;
1321 HRESULT hres1;
1322 hres1 = IUnknown_QueryInterface(pUnk, &IID_IOleLink, (void **)&pOleLink);
1323 if (SUCCEEDED(hres1))
1324 {
1325 FIXME("handle OLE link\n");
1326 IOleLink_Release(pOleLink);
1327 }
1328 }
1329
1330 if (FAILED(hres))
1331 {
1332 IUnknown_Release(pUnk);
1333 pUnk = NULL;
1334 }
1335
1336 *ppvObj = pUnk;
1337
1338 return hres;
1339 }
1340
1341 /***********************************************************************
1342 * OleSave [OLE32.@]
1343 */
1344 HRESULT WINAPI OleSave(
1345 LPPERSISTSTORAGE pPS,
1346 LPSTORAGE pStg,
1347 BOOL fSameAsLoad)
1348 {
1349 HRESULT hres;
1350 CLSID objectClass;
1351
1352 TRACE("(%p,%p,%x)\n", pPS, pStg, fSameAsLoad);
1353
1354 /*
1355 * First, we transfer the class ID (if available)
1356 */
1357 hres = IPersistStorage_GetClassID(pPS, &objectClass);
1358
1359 if (SUCCEEDED(hres))
1360 {
1361 WriteClassStg(pStg, &objectClass);
1362 }
1363
1364 /*
1365 * Then, we ask the object to save itself to the
1366 * storage. If it is successful, we commit the storage.
1367 */
1368 hres = IPersistStorage_Save(pPS, pStg, fSameAsLoad);
1369
1370 if (SUCCEEDED(hres))
1371 {
1372 IStorage_Commit(pStg,
1373 STGC_DEFAULT);
1374 }
1375
1376 return hres;
1377 }
1378
1379
1380 /******************************************************************************
1381 * OleLockRunning [OLE32.@]
1382 */
1383 HRESULT WINAPI OleLockRunning(LPUNKNOWN pUnknown, BOOL fLock, BOOL fLastUnlockCloses)
1384 {
1385 IRunnableObject* runnable = NULL;
1386 HRESULT hres;
1387
1388 TRACE("(%p,%x,%x)\n", pUnknown, fLock, fLastUnlockCloses);
1389
1390 hres = IUnknown_QueryInterface(pUnknown,
1391 &IID_IRunnableObject,
1392 (void**)&runnable);
1393
1394 if (SUCCEEDED(hres))
1395 {
1396 hres = IRunnableObject_LockRunning(runnable, fLock, fLastUnlockCloses);
1397
1398 IRunnableObject_Release(runnable);
1399
1400 return hres;
1401 }
1402
1403 return S_OK;
1404 }
1405
1406
1407 /**************************************************************************
1408 * Internal methods to manage the shared OLE menu in response to the
1409 * OLE***MenuDescriptor API
1410 */
1411
1412 /***
1413 * OLEMenu_Initialize()
1414 *
1415 * Initializes the OLEMENU data structures.
1416 */
1417 static void OLEMenu_Initialize(void)
1418 {
1419 }
1420
1421 /***
1422 * OLEMenu_UnInitialize()
1423 *
1424 * Releases the OLEMENU data structures.
1425 */
1426 static void OLEMenu_UnInitialize(void)
1427 {
1428 }
1429
1430 /*************************************************************************
1431 * OLEMenu_InstallHooks
1432 * Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
1433 *
1434 * RETURNS: TRUE if message hooks were successfully installed
1435 * FALSE on failure
1436 */
1437 static BOOL OLEMenu_InstallHooks( DWORD tid )
1438 {
1439 OleMenuHookItem *pHookItem;
1440
1441 /* Create an entry for the hook table */
1442 if ( !(pHookItem = HeapAlloc(GetProcessHeap(), 0,
1443 sizeof(OleMenuHookItem)) ) )
1444 return FALSE;
1445
1446 pHookItem->tid = tid;
1447 pHookItem->hHeap = GetProcessHeap();
1448 pHookItem->CallWndProc_hHook = NULL;
1449
1450 /* Install a thread scope message hook for WH_GETMESSAGE */
1451 pHookItem->GetMsg_hHook = SetWindowsHookExW( WH_GETMESSAGE, OLEMenu_GetMsgProc,
1452 0, GetCurrentThreadId() );
1453 if ( !pHookItem->GetMsg_hHook )
1454 goto CLEANUP;
1455
1456 /* Install a thread scope message hook for WH_CALLWNDPROC */
1457 pHookItem->CallWndProc_hHook = SetWindowsHookExW( WH_CALLWNDPROC, OLEMenu_CallWndProc,
1458 0, GetCurrentThreadId() );
1459 if ( !pHookItem->CallWndProc_hHook )
1460 goto CLEANUP;
1461
1462 /* Insert the hook table entry */
1463 pHookItem->next = hook_list;
1464 hook_list = pHookItem;
1465
1466 return TRUE;
1467
1468 CLEANUP:
1469 /* Unhook any hooks */
1470 if ( pHookItem->GetMsg_hHook )
1471 UnhookWindowsHookEx( pHookItem->GetMsg_hHook );
1472 if ( pHookItem->CallWndProc_hHook )
1473 UnhookWindowsHookEx( pHookItem->CallWndProc_hHook );
1474 /* Release the hook table entry */
1475 HeapFree(pHookItem->hHeap, 0, pHookItem );
1476
1477 return FALSE;
1478 }
1479
1480 /*************************************************************************
1481 * OLEMenu_UnInstallHooks
1482 * UnInstall thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC
1483 *
1484 * RETURNS: TRUE if message hooks were successfully installed
1485 * FALSE on failure
1486 */
1487 static BOOL OLEMenu_UnInstallHooks( DWORD tid )
1488 {
1489 OleMenuHookItem *pHookItem = NULL;
1490 OleMenuHookItem **ppHook = &hook_list;
1491
1492 while (*ppHook)
1493 {
1494 if ((*ppHook)->tid == tid)
1495 {
1496 pHookItem = *ppHook;
1497 *ppHook = pHookItem->next;
1498 break;
1499 }
1500 ppHook = &(*ppHook)->next;
1501 }
1502 if (!pHookItem) return FALSE;
1503
1504 /* Uninstall the hooks installed for this thread */
1505 if ( !UnhookWindowsHookEx( pHookItem->GetMsg_hHook ) )
1506 goto CLEANUP;
1507 if ( !UnhookWindowsHookEx( pHookItem->CallWndProc_hHook ) )
1508 goto CLEANUP;
1509
1510 /* Release the hook table entry */
1511 HeapFree(pHookItem->hHeap, 0, pHookItem );
1512
1513 return TRUE;
1514
1515 CLEANUP:
1516 /* Release the hook table entry */
1517 HeapFree(pHookItem->hHeap, 0, pHookItem );
1518
1519 return FALSE;
1520 }
1521
1522 /*************************************************************************
1523 * OLEMenu_IsHookInstalled
1524 * Tests if OLEMenu hooks have been installed for a thread
1525 *
1526 * RETURNS: The pointer and index of the hook table entry for the tid
1527 * NULL and -1 for the index if no hooks were installed for this thread
1528 */
1529 static OleMenuHookItem * OLEMenu_IsHookInstalled( DWORD tid )
1530 {
1531 OleMenuHookItem *pHookItem;
1532
1533 /* Do a simple linear search for an entry whose tid matches ours.
1534 * We really need a map but efficiency is not a concern here. */
1535 for (pHookItem = hook_list; pHookItem; pHookItem = pHookItem->next)
1536 {
1537 if ( tid == pHookItem->tid )
1538 return pHookItem;
1539 }
1540
1541 return NULL;
1542 }
1543
1544 /***********************************************************************
1545 * OLEMenu_FindMainMenuIndex
1546 *
1547 * Used by OLEMenu API to find the top level group a menu item belongs to.
1548 * On success pnPos contains the index of the item in the top level menu group
1549 *
1550 * RETURNS: TRUE if the ID was found, FALSE on failure
1551 */
1552 static BOOL OLEMenu_FindMainMenuIndex( HMENU hMainMenu, HMENU hPopupMenu, UINT *pnPos )
1553 {
1554 INT i, nItems;
1555
1556 nItems = GetMenuItemCount( hMainMenu );
1557
1558 for (i = 0; i < nItems; i++)
1559 {
1560 HMENU hsubmenu;
1561
1562 /* Is the current item a submenu? */
1563 if ( (hsubmenu = GetSubMenu(hMainMenu, i)) )
1564 {
1565 /* If the handle is the same we're done */
1566 if ( hsubmenu == hPopupMenu )
1567 {
1568 if (pnPos)
1569 *pnPos = i;
1570 return TRUE;
1571 }
1572 /* Recursively search without updating pnPos */
1573 else if ( OLEMenu_FindMainMenuIndex( hsubmenu, hPopupMenu, NULL ) )
1574 {
1575 if (pnPos)
1576 *pnPos = i;
1577 return TRUE;
1578 }
1579 }
1580 }
1581
1582 return FALSE;
1583 }
1584
1585 /***********************************************************************
1586 * OLEMenu_SetIsServerMenu
1587 *
1588 * Checks whether a popup menu belongs to a shared menu group which is
1589 * owned by the server, and sets the menu descriptor state accordingly.
1590 * All menu messages from these groups should be routed to the server.
1591 *
1592 * RETURNS: TRUE if the popup menu is part of a server owned group
1593 * FALSE if the popup menu is part of a container owned group
1594 */
1595 static BOOL OLEMenu_SetIsServerMenu( HMENU hmenu, OleMenuDescriptor *pOleMenuDescriptor )
1596 {
1597 UINT nPos = 0, nWidth, i;
1598
1599 pOleMenuDescriptor->bIsServerItem = FALSE;
1600
1601 /* Don't bother searching if the popup is the combined menu itself */
1602 if ( hmenu == pOleMenuDescriptor->hmenuCombined )
1603 return FALSE;
1604
1605 /* Find the menu item index in the shared OLE menu that this item belongs to */
1606 if ( !OLEMenu_FindMainMenuIndex( pOleMenuDescriptor->hmenuCombined, hmenu, &nPos ) )
1607 return FALSE;
1608
1609 /* The group widths array has counts for the number of elements
1610 * in the groups File, Edit, Container, Object, Window, Help.
1611 * The Edit, Object & Help groups belong to the server object
1612 * and the other three belong to the container.
1613 * Loop through the group widths and locate the group we are a member of.
1614 */
1615 for ( i = 0, nWidth = 0; i < 6; i++ )
1616 {
1617 nWidth += pOleMenuDescriptor->mgw.width[i];
1618 if ( nPos < nWidth )
1619 {
1620 /* Odd elements are server menu widths */
1621 pOleMenuDescriptor->bIsServerItem = i%2;
1622 break;
1623 }
1624 }
1625
1626 return pOleMenuDescriptor->bIsServerItem;
1627 }
1628
1629 /*************************************************************************
1630 * OLEMenu_CallWndProc
1631 * Thread scope WH_CALLWNDPROC hook proc filter function (callback)
1632 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1633 */
1634 static LRESULT CALLBACK OLEMenu_CallWndProc(INT code, WPARAM wParam, LPARAM lParam)
1635 {
1636 LPCWPSTRUCT pMsg;
1637 HOLEMENU hOleMenu = 0;
1638 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1639 OleMenuHookItem *pHookItem = NULL;
1640 WORD fuFlags;
1641
1642 TRACE("%i, %04lx, %08lx\n", code, wParam, lParam );
1643
1644 /* Check if we're being asked to process the message */
1645 if ( HC_ACTION != code )
1646 goto NEXTHOOK;
1647
1648 /* Retrieve the current message being dispatched from lParam */
1649 pMsg = (LPCWPSTRUCT)lParam;
1650
1651 /* Check if the message is destined for a window we are interested in:
1652 * If the window has an OLEMenu property we may need to dispatch
1653 * the menu message to its active objects window instead. */
1654
1655 hOleMenu = GetPropW( pMsg->hwnd, prop_olemenuW );
1656 if ( !hOleMenu )
1657 goto NEXTHOOK;
1658
1659 /* Get the menu descriptor */
1660 pOleMenuDescriptor = GlobalLock( hOleMenu );
1661 if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1662 goto NEXTHOOK;
1663
1664 /* Process menu messages */
1665 switch( pMsg->message )
1666 {
1667 case WM_INITMENU:
1668 {
1669 /* Reset the menu descriptor state */
1670 pOleMenuDescriptor->bIsServerItem = FALSE;
1671
1672 /* Send this message to the server as well */
1673 SendMessageW( pOleMenuDescriptor->hwndActiveObject,
1674 pMsg->message, pMsg->wParam, pMsg->lParam );
1675 goto NEXTHOOK;
1676 }
1677
1678 case WM_INITMENUPOPUP:
1679 {
1680 /* Save the state for whether this is a server owned menu */
1681 OLEMenu_SetIsServerMenu( (HMENU)pMsg->wParam, pOleMenuDescriptor );
1682 break;
1683 }
1684
1685 case WM_MENUSELECT:
1686 {
1687 fuFlags = HIWORD(pMsg->wParam); /* Get flags */
1688 if ( fuFlags & MF_SYSMENU )
1689 goto NEXTHOOK;
1690
1691 /* Save the state for whether this is a server owned popup menu */
1692 else if ( fuFlags & MF_POPUP )
1693 OLEMenu_SetIsServerMenu( (HMENU)pMsg->lParam, pOleMenuDescriptor );
1694
1695 break;
1696 }
1697
1698 case WM_DRAWITEM:
1699 {
1700 LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT) pMsg->lParam;
1701 if ( pMsg->wParam != 0 || lpdis->CtlType != ODT_MENU )
1702 goto NEXTHOOK; /* Not a menu message */
1703
1704 break;
1705 }
1706
1707 default:
1708 goto NEXTHOOK;
1709 }
1710
1711 /* If the message was for the server dispatch it accordingly */
1712 if ( pOleMenuDescriptor->bIsServerItem )
1713 {
1714 SendMessageW( pOleMenuDescriptor->hwndActiveObject,
1715 pMsg->message, pMsg->wParam, pMsg->lParam );
1716 }
1717
1718 NEXTHOOK:
1719 if ( pOleMenuDescriptor )
1720 GlobalUnlock( hOleMenu );
1721
1722 /* Lookup the hook item for the current thread */
1723 if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1724 {
1725 /* This should never fail!! */
1726 WARN("could not retrieve hHook for current thread!\n" );
1727 return 0;
1728 }
1729
1730 /* Pass on the message to the next hooker */
1731 return CallNextHookEx( pHookItem->CallWndProc_hHook, code, wParam, lParam );
1732 }
1733
1734 /*************************************************************************
1735 * OLEMenu_GetMsgProc
1736 * Thread scope WH_GETMESSAGE hook proc filter function (callback)
1737 * This is invoked from a message hook installed in OleSetMenuDescriptor.
1738 */
1739 static LRESULT CALLBACK OLEMenu_GetMsgProc(INT code, WPARAM wParam, LPARAM lParam)
1740 {
1741 LPMSG pMsg;
1742 HOLEMENU hOleMenu = 0;
1743 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1744 OleMenuHookItem *pHookItem = NULL;
1745 WORD wCode;
1746
1747 TRACE("%i, %04lx, %08lx\n", code, wParam, lParam );
1748
1749 /* Check if we're being asked to process a messages */
1750 if ( HC_ACTION != code )
1751 goto NEXTHOOK;
1752
1753 /* Retrieve the current message being dispatched from lParam */
1754 pMsg = (LPMSG)lParam;
1755
1756 /* Check if the message is destined for a window we are interested in:
1757 * If the window has an OLEMenu property we may need to dispatch
1758 * the menu message to its active objects window instead. */
1759
1760 hOleMenu = GetPropW( pMsg->hwnd, prop_olemenuW );
1761 if ( !hOleMenu )
1762 goto NEXTHOOK;
1763
1764 /* Process menu messages */
1765 switch( pMsg->message )
1766 {
1767 case WM_COMMAND:
1768 {
1769 wCode = HIWORD(pMsg->wParam); /* Get notification code */
1770 if ( wCode )
1771 goto NEXTHOOK; /* Not a menu message */
1772 break;
1773 }
1774 default:
1775 goto NEXTHOOK;
1776 }
1777
1778 /* Get the menu descriptor */
1779 pOleMenuDescriptor = GlobalLock( hOleMenu );
1780 if ( !pOleMenuDescriptor ) /* Bad descriptor! */
1781 goto NEXTHOOK;
1782
1783 /* If the message was for the server dispatch it accordingly */
1784 if ( pOleMenuDescriptor->bIsServerItem )
1785 {
1786 /* Change the hWnd in the message to the active objects hWnd.
1787 * The message loop which reads this message will automatically
1788 * dispatch it to the embedded objects window. */
1789 pMsg->hwnd = pOleMenuDescriptor->hwndActiveObject;
1790 }
1791
1792 NEXTHOOK:
1793 if ( pOleMenuDescriptor )
1794 GlobalUnlock( hOleMenu );
1795
1796 /* Lookup the hook item for the current thread */
1797 if ( !( pHookItem = OLEMenu_IsHookInstalled( GetCurrentThreadId() ) ) )
1798 {
1799 /* This should never fail!! */
1800 WARN("could not retrieve hHook for current thread!\n" );
1801 return FALSE;
1802 }
1803
1804 /* Pass on the message to the next hooker */
1805 return CallNextHookEx( pHookItem->GetMsg_hHook, code, wParam, lParam );
1806 }
1807
1808 /***********************************************************************
1809 * OleCreateMenuDescriptor [OLE32.@]
1810 * Creates an OLE menu descriptor for OLE to use when dispatching
1811 * menu messages and commands.
1812 *
1813 * PARAMS:
1814 * hmenuCombined - Handle to the objects combined menu
1815 * lpMenuWidths - Pointer to array of 6 LONG's indicating menus per group
1816 *
1817 */
1818 HOLEMENU WINAPI OleCreateMenuDescriptor(
1819 HMENU hmenuCombined,
1820 LPOLEMENUGROUPWIDTHS lpMenuWidths)
1821 {
1822 HOLEMENU hOleMenu;
1823 OleMenuDescriptor *pOleMenuDescriptor;
1824 int i;
1825
1826 if ( !hmenuCombined || !lpMenuWidths )
1827 return 0;
1828
1829 /* Create an OLE menu descriptor */
1830 if ( !(hOleMenu = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT,
1831 sizeof(OleMenuDescriptor) ) ) )
1832 return 0;
1833
1834 pOleMenuDescriptor = GlobalLock( hOleMenu );
1835 if ( !pOleMenuDescriptor )
1836 return 0;
1837
1838 /* Initialize menu group widths and hmenu */
1839 for ( i = 0; i < 6; i++ )
1840 pOleMenuDescriptor->mgw.width[i] = lpMenuWidths->width[i];
1841
1842 pOleMenuDescriptor->hmenuCombined = hmenuCombined;
1843 pOleMenuDescriptor->bIsServerItem = FALSE;
1844 GlobalUnlock( hOleMenu );
1845
1846 return hOleMenu;
1847 }
1848
1849 /***********************************************************************
1850 * OleDestroyMenuDescriptor [OLE32.@]
1851 * Destroy the shared menu descriptor
1852 */
1853 HRESULT WINAPI OleDestroyMenuDescriptor(
1854 HOLEMENU hmenuDescriptor)
1855 {
1856 if ( hmenuDescriptor )
1857 GlobalFree( hmenuDescriptor );
1858 return S_OK;
1859 }
1860
1861 /***********************************************************************
1862 * OleSetMenuDescriptor [OLE32.@]
1863 * Installs or removes OLE dispatching code for the containers frame window.
1864 *
1865 * PARAMS
1866 * hOleMenu Handle to composite menu descriptor
1867 * hwndFrame Handle to containers frame window
1868 * hwndActiveObject Handle to objects in-place activation window
1869 * lpFrame Pointer to IOleInPlaceFrame on containers window
1870 * lpActiveObject Pointer to IOleInPlaceActiveObject on active in-place object
1871 *
1872 * RETURNS
1873 * S_OK - menu installed correctly
1874 * E_FAIL, E_INVALIDARG, E_UNEXPECTED - failure
1875 *
1876 * FIXME
1877 * The lpFrame and lpActiveObject parameters are currently ignored
1878 * OLE should install context sensitive help F1 filtering for the app when
1879 * these are non null.
1880 */
1881 HRESULT WINAPI OleSetMenuDescriptor(
1882 HOLEMENU hOleMenu,
1883 HWND hwndFrame,
1884 HWND hwndActiveObject,
1885 LPOLEINPLACEFRAME lpFrame,
1886 LPOLEINPLACEACTIVEOBJECT lpActiveObject)
1887 {
1888 OleMenuDescriptor *pOleMenuDescriptor = NULL;
1889
1890 /* Check args */
1891 if ( !hwndFrame || (hOleMenu && !hwndActiveObject) )
1892 return E_INVALIDARG;
1893
1894 if ( lpFrame || lpActiveObject )
1895 {
1896 FIXME("(%p, %p, %p, %p, %p), Context sensitive help filtering not implemented!\n",
1897 hOleMenu,
1898 hwndFrame,
1899 hwndActiveObject,
1900 lpFrame,
1901 lpActiveObject);
1902 }
1903
1904 /* Set up a message hook to intercept the containers frame window messages.
1905 * The message filter is responsible for dispatching menu messages from the
1906 * shared menu which are intended for the object.
1907 */
1908
1909 if ( hOleMenu ) /* Want to install dispatching code */
1910 {
1911 /* If OLEMenu hooks are already installed for this thread, fail
1912 * Note: This effectively means that OleSetMenuDescriptor cannot
1913 * be called twice in succession on the same frame window
1914 * without first calling it with a null hOleMenu to uninstall
1915 */
1916 if ( OLEMenu_IsHookInstalled( GetCurrentThreadId() ) )
1917 return E_FAIL;
1918
1919 /* Get the menu descriptor */
1920 pOleMenuDescriptor = GlobalLock( hOleMenu );
1921 if ( !pOleMenuDescriptor )
1922 return E_UNEXPECTED;
1923
1924 /* Update the menu descriptor */
1925 pOleMenuDescriptor->hwndFrame = hwndFrame;
1926 pOleMenuDescriptor->hwndActiveObject = hwndActiveObject;
1927
1928 GlobalUnlock( hOleMenu );
1929 pOleMenuDescriptor = NULL;
1930
1931 /* Add a menu descriptor windows property to the frame window */
1932 SetPropW( hwndFrame, prop_olemenuW, hOleMenu );
1933
1934 /* Install thread scope message hooks for WH_GETMESSAGE and WH_CALLWNDPROC */
1935 if ( !OLEMenu_InstallHooks( GetCurrentThreadId() ) )
1936 return E_FAIL;
1937 }
1938 else /* Want to uninstall dispatching code */
1939 {
1940 /* Uninstall the hooks */
1941 if ( !OLEMenu_UnInstallHooks( GetCurrentThreadId() ) )
1942 return E_FAIL;
1943
1944 /* Remove the menu descriptor property from the frame window */
1945 RemovePropW( hwndFrame, prop_olemenuW );
1946 }
1947
1948 return S_OK;
1949 }
1950
1951 /******************************************************************************
1952 * IsAccelerator [OLE32.@]
1953 * Mostly copied from controls/menu.c TranslateAccelerator implementation
1954 */
1955 BOOL WINAPI IsAccelerator(HACCEL hAccel, int cAccelEntries, LPMSG lpMsg, WORD* lpwCmd)
1956 {
1957 LPACCEL lpAccelTbl;
1958 int i;
1959
1960 if(!lpMsg) return FALSE;
1961 if (!hAccel)
1962 {
1963 WARN_(accel)("NULL accel handle\n");
1964 return FALSE;
1965 }
1966 if((lpMsg->message != WM_KEYDOWN &&
1967 lpMsg->message != WM_SYSKEYDOWN &&
1968 lpMsg->message != WM_SYSCHAR &&
1969 lpMsg->message != WM_CHAR)) return FALSE;
1970 lpAccelTbl = HeapAlloc(GetProcessHeap(), 0, cAccelEntries * sizeof(ACCEL));
1971 if (NULL == lpAccelTbl)
1972 {
1973 return FALSE;
1974 }
1975 if (CopyAcceleratorTableW(hAccel, lpAccelTbl, cAccelEntries) != cAccelEntries)
1976 {
1977 WARN_(accel)("CopyAcceleratorTableW failed\n");
1978 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
1979 return FALSE;
1980 }
1981
1982 TRACE_(accel)("hAccel=%p, cAccelEntries=%d,"
1983 "msg->hwnd=%p, msg->message=%04x, wParam=%08lx, lParam=%08lx\n",
1984 hAccel, cAccelEntries,
1985 lpMsg->hwnd, lpMsg->message, lpMsg->wParam, lpMsg->lParam);
1986 for(i = 0; i < cAccelEntries; i++)
1987 {
1988 if(lpAccelTbl[i].key != lpMsg->wParam)
1989 continue;
1990
1991 if(lpMsg->message == WM_CHAR)
1992 {
1993 if(!(lpAccelTbl[i].fVirt & FALT) && !(lpAccelTbl[i].fVirt & FVIRTKEY))
1994 {
1995 TRACE_(accel)("found accel for WM_CHAR: ('%c')\n", LOWORD(lpMsg->wParam) & 0xff);
1996 goto found;
1997 }
1998 }
1999 else
2000 {
2001 if(lpAccelTbl[i].fVirt & FVIRTKEY)
2002 {
2003 INT mask = 0;
2004 TRACE_(accel)("found accel for virt_key %04lx (scan %04x)\n",
2005 lpMsg->wParam, HIWORD(lpMsg->lParam) & 0xff);
2006 if(GetKeyState(VK_SHIFT) & 0x8000) mask |= FSHIFT;
2007 if(GetKeyState(VK_CONTROL) & 0x8000) mask |= FCONTROL;
2008 if(GetKeyState(VK_MENU) & 0x8000) mask |= FALT;
2009 if(mask == (lpAccelTbl[i].fVirt & (FSHIFT | FCONTROL | FALT))) goto found;
2010 TRACE_(accel)("incorrect SHIFT/CTRL/ALT-state\n");
2011 }
2012 else
2013 {
2014 if(!(lpMsg->lParam & 0x01000000)) /* no special_key */
2015 {
2016 if((lpAccelTbl[i].fVirt & FALT) && (lpMsg->lParam & 0x20000000))
2017 { /* ^^ ALT pressed */
2018 TRACE_(accel)("found accel for Alt-%c\n", LOWORD(lpMsg->wParam) & 0xff);
2019 goto found;
2020 }
2021 }
2022 }
2023 }
2024 }
2025
2026 WARN_(accel)("couldn't translate accelerator key\n");
2027 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
2028 return FALSE;
2029
2030 found:
2031 if(lpwCmd) *lpwCmd = lpAccelTbl[i].cmd;
2032 HeapFree(GetProcessHeap(), 0, lpAccelTbl);
2033 return TRUE;
2034 }
2035
2036 /***********************************************************************
2037 * ReleaseStgMedium [OLE32.@]
2038 */
2039 void WINAPI ReleaseStgMedium(
2040 STGMEDIUM* pmedium)
2041 {
2042 switch (pmedium->tymed)
2043 {
2044 case TYMED_HGLOBAL:
2045 {
2046 if ( (pmedium->pUnkForRelease==0) &&
2047 (pmedium->u.hGlobal!=0) )
2048 GlobalFree(pmedium->u.hGlobal);
2049 break;
2050 }
2051 case TYMED_FILE:
2052 {
2053 if (pmedium->u.lpszFileName!=0)
2054 {
2055 if (pmedium->pUnkForRelease==0)
2056 {
2057 DeleteFileW(pmedium->u.lpszFileName);
2058 }
2059
2060 CoTaskMemFree(pmedium->u.lpszFileName);
2061 }
2062 break;
2063 }
2064 case TYMED_ISTREAM:
2065 {
2066 if (pmedium->u.pstm!=0)
2067 {
2068 IStream_Release(pmedium->u.pstm);
2069 }
2070 break;
2071 }
2072 case TYMED_ISTORAGE:
2073 {
2074 if (pmedium->u.pstg!=0)
2075 {
2076 IStorage_Release(pmedium->u.pstg);
2077 }
2078 break;
2079 }
2080 case TYMED_GDI:
2081 {
2082 if ( (pmedium->pUnkForRelease==0) &&
2083 (pmedium->u.hBitmap!=0) )
2084 DeleteObject(pmedium->u.hBitmap);
2085 break;
2086 }
2087 case TYMED_MFPICT:
2088 {
2089 if ( (pmedium->pUnkForRelease==0) &&
2090 (pmedium->u.hMetaFilePict!=0) )
2091 {
2092 LPMETAFILEPICT pMP = GlobalLock(pmedium->u.hMetaFilePict);
2093 DeleteMetaFile(pMP->hMF);
2094 GlobalUnlock(pmedium->u.hMetaFilePict);
2095 GlobalFree(pmedium->u.hMetaFilePict);
2096 }
2097 break;
2098 }
2099 case TYMED_ENHMF:
2100 {
2101 if ( (pmedium->pUnkForRelease==0) &&
2102 (pmedium->u.hEnhMetaFile!=0) )
2103 {
2104 DeleteEnhMetaFile(pmedium->u.hEnhMetaFile);
2105 }
2106 break;
2107 }
2108 case TYMED_NULL:
2109 default:
2110 break;
2111 }
2112 pmedium->tymed=TYMED_NULL;
2113
2114 /*
2115 * After cleaning up, the unknown is released
2116 */
2117 if (pmedium->pUnkForRelease!=0)
2118 {
2119 IUnknown_Release(pmedium->pUnkForRelease);
2120 pmedium->pUnkForRelease = 0;
2121 }
2122 }
2123
2124 /***
2125 * OLEDD_Initialize()
2126 *
2127 * Initializes the OLE drag and drop data structures.
2128 */
2129 static void OLEDD_Initialize(void)
2130 {
2131 WNDCLASSW wndClass;
2132
2133 ZeroMemory (&wndClass, sizeof(WNDCLASSW));
2134 wndClass.style = CS_GLOBALCLASS;
2135 wndClass.lpfnWndProc = OLEDD_DragTrackerWindowProc;
2136 wndClass.cbClsExtra = 0;
2137 wndClass.cbWndExtra = sizeof(TrackerWindowInfo*);
2138 wndClass.hCursor = 0;
2139 wndClass.hbrBackground = 0;
2140 wndClass.lpszClassName = OLEDD_DRAGTRACKERCLASS;
2141
2142 RegisterClassW (&wndClass);
2143 }
2144
2145 /***
2146 * OLEDD_DragTrackerWindowProc()
2147 *
2148 * This method is the WindowProcedure of the drag n drop tracking
2149 * window. During a drag n Drop operation, an invisible window is created
2150 * to receive the user input and act upon it. This procedure is in charge
2151 * of this behavior.
2152 */
2153
2154 #define DRAG_TIMER_ID 1
2155
2156 static LRESULT WINAPI OLEDD_DragTrackerWindowProc(
2157 HWND hwnd,
2158 UINT uMsg,
2159 WPARAM wParam,
2160 LPARAM lParam)
2161 {
2162 switch (uMsg)
2163 {
2164 case WM_CREATE:
2165 {
2166 LPCREATESTRUCTA createStruct = (LPCREATESTRUCTA)lParam;
2167
2168 SetWindowLongPtrW(hwnd, 0, (LONG_PTR)createStruct->lpCreateParams);
2169 SetTimer(hwnd, DRAG_TIMER_ID, 50, NULL);
2170
2171 break;
2172 }
2173 case WM_TIMER:
2174 case WM_MOUSEMOVE:
2175 case WM_LBUTTONUP:
2176 case WM_MBUTTONUP:
2177 case WM_RBUTTONUP:
2178 case WM_LBUTTONDOWN:
2179 case WM_MBUTTONDOWN:
2180 case WM_RBUTTONDOWN:
2181 {
2182 OLEDD_TrackStateChange((TrackerWindowInfo*)GetWindowLongPtrA(hwnd, 0));
2183 break;
2184 }
2185 case WM_DESTROY:
2186 {
2187 KillTimer(hwnd, DRAG_TIMER_ID);
2188 break;
2189 }
2190 }
2191
2192 /*
2193 * This is a window proc after all. Let's call the default.
2194 */
2195 return DefWindowProcW (hwnd, uMsg, wParam, lParam);
2196 }
2197
2198 /***
2199 * OLEDD_TrackStateChange()
2200 *
2201 * This method is invoked while a drag and drop operation is in effect.
2202 *
2203 * params:
2204 * trackerInfo - Pointer to the structure identifying the
2205 * drag & drop operation that is currently
2206 * active.
2207 */
2208 static void OLEDD_TrackStateChange(TrackerWindowInfo* trackerInfo)
2209 {
2210 HWND hwndNewTarget = 0;
2211 HRESULT hr = S_OK;
2212 POINT pt;
2213
2214 /*
2215 * Get the handle of the window under the mouse
2216 */
2217 pt.x = trackerInfo->curMousePos.x;
2218 pt.y = trackerInfo->curMousePos.y;
2219 hwndNewTarget = WindowFromPoint(pt);
2220
2221 trackerInfo->returnValue = IDropSource_QueryContinueDrag(trackerInfo->dropSource,
2222 trackerInfo->escPressed,
2223 trackerInfo->dwKeyState);
2224
2225 /*
2226 * Every time, we re-initialize the effects passed to the
2227 * IDropTarget to the effects allowed by the source.
2228 */
2229 *trackerInfo->pdwEffect = trackerInfo->dwOKEffect;
2230
2231 /*
2232 * If we are hovering over the same target as before, send the
2233 * DragOver notification
2234 */
2235 if ( (trackerInfo->curDragTarget != 0) &&
2236 (trackerInfo->curTargetHWND == hwndNewTarget) )
2237 {
2238 IDropTarget_DragOver(trackerInfo->curDragTarget,
2239 trackerInfo->dwKeyState,
2240 trackerInfo->curMousePos,
2241 trackerInfo->pdwEffect);
2242 *trackerInfo->pdwEffect &= trackerInfo->dwOKEffect;
2243 }
2244 else
2245 {
2246 /*
2247 * If we changed window, we have to notify our old target and check for
2248 * the new one.
2249 */
2250 if (trackerInfo->curDragTarget)
2251 IDropTarget_DragLeave(trackerInfo->curDragTarget);
2252
2253 /*
2254 * Make sure we're hovering over a window.
2255 */
2256 if (hwndNewTarget)
2257 {
2258 /*
2259 * Find-out if there is a drag target under the mouse
2260 */
2261 HWND next_target_wnd = hwndNewTarget;
2262
2263 trackerInfo->curTargetHWND = hwndNewTarget;
2264
2265 while (next_target_wnd && !is_droptarget(next_target_wnd))
2266 next_target_wnd = GetParent(next_target_wnd);
2267
2268 if (next_target_wnd) hwndNewTarget = next_target_wnd;
2269
2270 trackerInfo->curDragTargetHWND = hwndNewTarget;
2271 if(trackerInfo->curDragTarget) IDropTarget_Release(trackerInfo->curDragTarget);
2272 trackerInfo->curDragTarget = get_droptarget_pointer(hwndNewTarget);
2273
2274 /*
2275 * If there is, notify it that we just dragged-in
2276 */
2277 if (trackerInfo->curDragTarget)
2278 {
2279 hr = IDropTarget_DragEnter(trackerInfo->curDragTarget,
2280 trackerInfo->dataObject,
2281 trackerInfo->dwKeyState,
2282 trackerInfo->curMousePos,
2283 trackerInfo->pdwEffect);
2284 *trackerInfo->pdwEffect &= trackerInfo->dwOKEffect;
2285
2286 /* failed DragEnter() means invalid target */
2287 if (hr != S_OK)
2288 {
2289 trackerInfo->curDragTargetHWND = 0;
2290 trackerInfo->curTargetHWND = 0;
2291 IDropTarget_Release(trackerInfo->curDragTarget);
2292 trackerInfo->curDragTarget = 0;
2293 }
2294 }
2295 }
2296 else
2297 {
2298 /*
2299 * The mouse is not over a window so we don't track anything.
2300 */
2301 trackerInfo->curDragTargetHWND = 0;
2302 trackerInfo->curTargetHWND = 0;
2303 if(trackerInfo->curDragTarget) IDropTarget_Release(trackerInfo->curDragTarget);
2304 trackerInfo->curDragTarget = 0;
2305 }
2306 }
2307
2308 /*
2309 * Now that we have done that, we have to tell the source to give
2310 * us feedback on the work being done by the target. If we don't
2311 * have a target, simulate no effect.
2312 */
2313 if (trackerInfo->curDragTarget==0)
2314 {
2315 *trackerInfo->pdwEffect = DROPEFFECT_NONE;
2316 }
2317
2318 hr = IDropSource_GiveFeedback(trackerInfo->dropSource,
2319 *trackerInfo->pdwEffect);
2320
2321 /*
2322 * When we ask for feedback from the drop source, sometimes it will
2323 * do all the necessary work and sometimes it will not handle it
2324 * when that's the case, we must display the standard drag and drop
2325 * cursors.
2326 */
2327 if (hr == DRAGDROP_S_USEDEFAULTCURSORS)
2328 {
2329 HCURSOR hCur;
2330
2331 if (*trackerInfo->pdwEffect & DROPEFFECT_MOVE)
2332 {
2333 hCur = LoadCursorW(hProxyDll, MAKEINTRESOURCEW(2));
2334 }
2335 else if (*trackerInfo->pdwEffect & DROPEFFECT_COPY)
2336 {
2337 hCur = LoadCursorW(hProxyDll, MAKEINTRESOURCEW(3));
2338 }
2339 else if (*trackerInfo->pdwEffect & DROPEFFECT_LINK)
2340 {
2341 hCur = LoadCursorW(hProxyDll, MAKEINTRESOURCEW(4));
2342 }
2343 else
2344 {
2345 hCur = LoadCursorW(hProxyDll, MAKEINTRESOURCEW(1));
2346 }
2347
2348 SetCursor(hCur);
2349 }
2350
2351 /*
2352 * All the return valued will stop the operation except the S_OK
2353 * return value.
2354 */
2355 if (trackerInfo->returnValue!=S_OK)
2356 {
2357 /*
2358 * Make sure the message loop in DoDragDrop stops
2359 */
2360 trackerInfo->trackingDone = TRUE;
2361
2362 /*
2363 * Release the mouse in case the drop target decides to show a popup
2364 * or a menu or something.
2365 */
2366 ReleaseCapture();
2367
2368 /*
2369 * If we end-up over a target, drop the object in the target or
2370 * inform the target that the operation was cancelled.
2371 */
2372 if (trackerInfo->curDragTarget)
2373 {
2374 switch (trackerInfo->returnValue)
2375 {
2376 /*
2377 * If the source wants us to complete the operation, we tell
2378 * the drop target that we just dropped the object in it.
2379 */
2380 case DRAGDROP_S_DROP:
2381 if (*trackerInfo->pdwEffect != DROPEFFECT_NONE)
2382 trackerInfo->returnValue = IDropTarget_Drop(trackerInfo->curDragTarget,
2383 trackerInfo->dataObject,
2384 trackerInfo->dwKeyState,
2385 trackerInfo->curMousePos,
2386 trackerInfo->pdwEffect);
2387 else
2388 IDropTarget_DragLeave(trackerInfo->curDragTarget);
2389 break;
2390
2391 /*
2392 * If the source told us that we should cancel, fool the drop
2393 * target by telling it that the mouse left its window.
2394 * Also set the drop effect to "NONE" in case the application
2395 * ignores the result of DoDragDrop.
2396 */
2397 case DRAGDROP_S_CANCEL:
2398 IDropTarget_DragLeave(trackerInfo->curDragTarget);
2399 *trackerInfo->pdwEffect = DROPEFFECT_NONE;
2400 break;
2401 }
2402 }
2403 }
2404 }
2405
2406 /***
2407 * OLEDD_GetButtonState()
2408 *
2409 * This method will use the current state of the keyboard to build
2410 * a button state mask equivalent to the one passed in the
2411 * WM_MOUSEMOVE wParam.
2412 */
2413 static DWORD OLEDD_GetButtonState(void)
2414 {
2415 BYTE keyboardState[256];
2416 DWORD keyMask = 0;
2417
2418 GetKeyboardState(keyboardState);
2419
2420 if ( (keyboardState[VK_SHIFT] & 0x80) !=0)
2421 keyMask |= MK_SHIFT;
2422
2423 if ( (keyboardState[VK_CONTROL] & 0x80) !=0)
2424 keyMask |= MK_CONTROL;
2425
2426 if ( (keyboardState[VK_LBUTTON] & 0x80) !=0)
2427 keyMask |= MK_LBUTTON;
2428
2429 if ( (keyboardState[VK_RBUTTON] & 0x80) !=0)
2430 keyMask |= MK_RBUTTON;
2431
2432 if ( (keyboardState[VK_MBUTTON] & 0x80) !=0)
2433 keyMask |= MK_MBUTTON;
2434
2435 return keyMask;
2436 }
2437
2438 /***
2439 * OLEDD_GetButtonState()
2440 *
2441 * This method will read the default value of the registry key in
2442 * parameter and extract a DWORD value from it. The registry key value
2443 * can be in a string key or a DWORD key.
2444 *
2445 * params:
2446 * regKey - Key to read the default value from
2447 * pdwValue - Pointer to the location where the DWORD
2448 * value is returned. This value is not modified
2449 * if the value is not found.
2450 */
2451
2452 static void OLEUTL_ReadRegistryDWORDValue(
2453 HKEY regKey,
2454 DWORD* pdwValue)
2455 {
2456 WCHAR buffer[20];
2457 DWORD cbData = sizeof(buffer);
2458 DWORD dwKeyType;
2459 LONG lres;
2460
2461 lres = RegQueryValueExW(regKey,
2462 emptyW,
2463 NULL,
2464 &dwKeyType,
2465 (LPBYTE)buffer,
2466 &cbData);
2467
2468 if (lres==ERROR_SUCCESS)
2469 {
2470 switch (dwKeyType)
2471 {
2472 case REG_DWORD:
2473 *pdwValue = *(DWORD*)buffer;
2474 break;
2475 case REG_EXPAND_SZ:
2476 case REG_MULTI_SZ:
2477 case REG_SZ:
2478 *pdwValue = (DWORD)strtoulW(buffer, NULL, 10);
2479 break;
2480 }
2481 }
2482 }
2483
2484 /******************************************************************************
2485 * OleDraw (OLE32.@)
2486 *
2487 * The operation of this function is documented literally in the WinAPI
2488 * documentation to involve a QueryInterface for the IViewObject interface,
2489 * followed by a call to IViewObject::Draw.
2490 */
2491 HRESULT WINAPI OleDraw(
2492 IUnknown *pUnk,
2493 DWORD dwAspect,
2494 HDC hdcDraw,
2495 LPCRECT rect)
2496 {
2497 HRESULT hres;
2498 IViewObject *viewobject;
2499
2500 if (!pUnk) return E_INVALIDARG;
2501
2502 hres = IUnknown_QueryInterface(pUnk,
2503 &IID_IViewObject,
2504 (void**)&viewobject);
2505 if (SUCCEEDED(hres))
2506 {
2507 hres = IViewObject_Draw(viewobject, dwAspect, -1, 0, 0, 0, hdcDraw, (RECTL*)rect, 0, 0, 0);
2508 IViewObject_Release(viewobject);
2509 return hres;
2510 }
2511 else
2512 return DV_E_NOIVIEWOBJECT;
2513 }
2514
2515 /***********************************************************************
2516 * OleTranslateAccelerator [OLE32.@]
2517 */
2518 HRESULT WINAPI OleTranslateAccelerator (LPOLEINPLACEFRAME lpFrame,
2519 LPOLEINPLACEFRAMEINFO lpFrameInfo, LPMSG lpmsg)
2520 {
2521 WORD wID;
2522
2523 TRACE("(%p,%p,%p)\n", lpFrame, lpFrameInfo, lpmsg);
2524
2525 if (IsAccelerator(lpFrameInfo->haccel,lpFrameInfo->cAccelEntries,lpmsg,&wID))
2526 return IOleInPlaceFrame_TranslateAccelerator(lpFrame,lpmsg,wID);
2527
2528 return S_FALSE;
2529 }
2530
2531 /******************************************************************************
2532 * OleCreate [OLE32.@]
2533 *
2534 */
2535 HRESULT WINAPI OleCreate(
2536 REFCLSID rclsid,
2537 REFIID riid,
2538 DWORD renderopt,
2539 LPFORMATETC pFormatEtc,
2540 LPOLECLIENTSITE pClientSite,
2541 LPSTORAGE pStg,
2542 LPVOID* ppvObj)
2543 {
2544 HRESULT hres;
2545 IUnknown * pUnk = NULL;
2546 IOleObject *pOleObject = NULL;
2547
2548 TRACE("(%s, %s, %d, %p, %p, %p, %p)\n", debugstr_guid(rclsid),
2549 debugstr_guid(riid), renderopt, pFormatEtc, pClientSite, pStg, ppvObj);
2550
2551 hres = CoCreateInstance(rclsid, 0, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER, riid, (LPVOID*)&pUnk);
2552
2553 if (SUCCEEDED(hres))
2554 hres = IStorage_SetClass(pStg, rclsid);
2555
2556 if (pClientSite && SUCCEEDED(hres))
2557 {
2558 hres = IUnknown_QueryInterface(pUnk, &IID_IOleObject, (LPVOID*)&pOleObject);
2559 if (SUCCEEDED(hres))
2560 {
2561 DWORD dwStatus;
2562 hres = IOleObject_GetMiscStatus(pOleObject, DVASPECT_CONTENT, &dwStatus);
2563 }
2564 }
2565
2566 if (SUCCEEDED(hres))
2567 {
2568 IPersistStorage * pPS;
2569 if (SUCCEEDED((hres = IUnknown_QueryInterface(pUnk, &IID_IPersistStorage, (LPVOID*)&pPS))))
2570 {
2571 TRACE("trying to set stg %p\n", pStg);
2572 hres = IPersistStorage_InitNew(pPS, pStg);
2573 TRACE("-- result 0x%08x\n", hres);
2574 IPersistStorage_Release(pPS);
2575 }
2576 }
2577
2578 if (pClientSite && SUCCEEDED(hres))
2579 {
2580 TRACE("trying to set clientsite %p\n", pClientSite);
2581 hres = IOleObject_SetClientSite(pOleObject, pClientSite);
2582 TRACE("-- result 0x%08x\n", hres);
2583 }
2584
2585 if (pOleObject)
2586 IOleObject_Release(pOleObject);
2587
2588 if (((renderopt == OLERENDER_DRAW) || (renderopt == OLERENDER_FORMAT)) &&
2589 SUCCEEDED(hres))
2590 {
2591 IRunnableObject *pRunnable;
2592 IOleCache *pOleCache;
2593 HRESULT hres2;
2594
2595 hres2 = IUnknown_QueryInterface(pUnk, &IID_IRunnableObject, (void **)&pRunnable);
2596 if (SUCCEEDED(hres2))
2597 {
2598 hres = IRunnableObject_Run(pRunnable, NULL);
2599 IRunnableObject_Release(pRunnable);
2600 }
2601
2602 if (SUCCEEDED(hres))
2603 {
2604 hres2 = IUnknown_QueryInterface(pUnk, &IID_IOleCache, (void **)&pOleCache);
2605 if (SUCCEEDED(hres2))
2606 {
2607 DWORD dwConnection;
2608 if (renderopt == OLERENDER_DRAW && !pFormatEtc) {
2609 FORMATETC pfe;
2610 pfe.cfFormat = 0;
2611 pfe.ptd = NULL;
2612 pfe.dwAspect = DVASPECT_CONTENT;
2613 pfe.lindex = -1;
2614 pfe.tymed = TYMED_NULL;
2615 hres = IOleCache_Cache(pOleCache, &pfe, ADVF_PRIMEFIRST, &dwConnection);
2616 }
2617 else
2618 hres = IOleCache_Cache(pOleCache, pFormatEtc, ADVF_PRIMEFIRST, &dwConnection);
2619 IOleCache_Release(pOleCache);
2620 }
2621 }
2622 }
2623
2624 if (FAILED(hres) && pUnk)
2625 {
2626 IUnknown_Release(pUnk);
2627 pUnk = NULL;
2628 }
2629
2630 *ppvObj = pUnk;
2631
2632 TRACE("-- %p\n", pUnk);
2633 return hres;
2634 }
2635
2636 /******************************************************************************
2637 * OleGetAutoConvert [OLE32.@]
2638 */
2639 HRESULT WINAPI OleGetAutoConvert(REFCLSID clsidOld, LPCLSID pClsidNew)
2640 {
2641 static const WCHAR wszAutoConvertTo[] = {'A','u','t','o','C','o','n','v','e','r','t','T','o',0};
2642 HKEY hkey = NULL;
2643 WCHAR buf[CHARS_IN_GUID];
2644 LONG len;
2645 HRESULT res = S_OK;
2646
2647 res = COM_OpenKeyForCLSID(clsidOld, wszAutoConvertTo, KEY_READ, &hkey);
2648 if (FAILED(res))
2649 goto done;
2650
2651 len = sizeof(buf);
2652 if (RegQueryValueW(hkey, NULL, buf, &len))
2653 {
2654 res = REGDB_E_KEYMISSING;
2655 goto done;
2656 }
2657 res = CLSIDFromString(buf, pClsidNew);
2658 done:
2659 if (hkey) RegCloseKey(hkey);
2660 return res;
2661 }
2662
2663 /******************************************************************************
2664 * OleSetAutoConvert [OLE32.@]
2665 */
2666 HRESULT WINAPI OleSetAutoConvert(REFCLSID clsidOld, REFCLSID clsidNew)
2667 {
2668 static const WCHAR wszAutoConvertTo[] = {'A','u','t','o','C','o','n','v','e','r','t','T','o',0};
2669 HKEY hkey = NULL;
2670 WCHAR szClsidNew[CHARS_IN_GUID];
2671 HRESULT res = S_OK;
2672
2673 TRACE("(%s,%s)\n", debugstr_guid(clsidOld), debugstr_guid(clsidNew));
2674
2675 res = COM_OpenKeyForCLSID(clsidOld, NULL, KEY_READ | KEY_WRITE, &hkey);
2676 if (FAILED(res))
2677 goto done;
2678 StringFromGUID2(clsidNew, szClsidNew, CHARS_IN_GUID);
2679 if (RegSetValueW(hkey, wszAutoConvertTo, REG_SZ, szClsidNew, (strlenW(szClsidNew)+1) * sizeof(WCHAR)))
2680 {
2681 res = REGDB_E_WRITEREGDB;
2682 goto done;
2683 }
2684
2685 done:
2686 if (hkey) RegCloseKey(hkey);
2687 return res;
2688 }
2689
2690 /******************************************************************************
2691 * OleDoAutoConvert [OLE32.@]
2692 */
2693 HRESULT WINAPI OleDoAutoConvert(LPSTORAGE pStg, LPCLSID pClsidNew)
2694 {
2695 WCHAR *user_type_old, *user_type_new;
2696 CLIPFORMAT cf;
2697 STATSTG stat;
2698 CLSID clsid;
2699 HRESULT hr;
2700
2701 TRACE("(%p, %p)\n", pStg, pClsidNew);
2702
2703 *pClsidNew = CLSID_NULL;
2704 if(!pStg)
2705 return E_INVALIDARG;
2706 hr = IStorage_Stat(pStg, &stat, STATFLAG_NONAME);
2707 if(FAILED(hr))
2708 return hr;
2709
2710 *pClsidNew = stat.clsid;
2711 hr = OleGetAutoConvert(&stat.clsid, &clsid);
2712 if(FAILED(hr))
2713 return hr;
2714
2715 hr = IStorage_SetClass(pStg, &clsid);
2716 if(FAILED(hr))
2717 return hr;
2718
2719 hr = ReadFmtUserTypeStg(pStg, &cf, &user_type_old);
2720 if(FAILED(hr)) {
2721 cf = 0;
2722 user_type_new = NULL;
2723 }
2724
2725 hr = OleRegGetUserType(&clsid, USERCLASSTYPE_FULL, &user_type_new);
2726 if(FAILED(hr))
2727 user_type_new = NULL;
2728
2729 hr = WriteFmtUserTypeStg(pStg, cf, user_type_new);
2730 CoTaskMemFree(user_type_new);
2731 if(FAILED(hr))
2732 {
2733 CoTaskMemFree(user_type_old);
2734 IStorage_SetClass(pStg, &stat.clsid);
2735 return hr;
2736 }
2737
2738 hr = SetConvertStg(pStg, TRUE);
2739 if(FAILED(hr))
2740 {
2741 WriteFmtUserTypeStg(pStg, cf, user_type_old);
2742 IStorage_SetClass(pStg, &stat.clsid);
2743 }
2744 else
2745 *pClsidNew = clsid;
2746 CoTaskMemFree(user_type_old);
2747 return hr;
2748 }
2749
2750 /******************************************************************************
2751 * OleIsRunning [OLE32.@]
2752 */
2753 BOOL WINAPI OleIsRunning(LPOLEOBJECT object)
2754 {
2755 IRunnableObject *pRunnable;
2756 HRESULT hr;
2757 BOOL running;
2758
2759 TRACE("(%p)\n", object);
2760
2761 if (!object) return FALSE;
2762
2763 hr = IOleObject_QueryInterface(object, &IID_IRunnableObject, (void **)&pRunnable);
2764 if (FAILED(hr))
2765 return TRUE;
2766 running = IRunnableObject_IsRunning(pRunnable);
2767 IRunnableObject_Release(pRunnable);
2768 return running;
2769 }
2770
2771 /***********************************************************************
2772 * OleNoteObjectVisible [OLE32.@]
2773 */
2774 HRESULT WINAPI OleNoteObjectVisible(LPUNKNOWN pUnknown, BOOL bVisible)
2775 {
2776 TRACE("(%p, %s)\n", pUnknown, bVisible ? "TRUE" : "FALSE");
2777 return CoLockObjectExternal(pUnknown, bVisible, TRUE);
2778 }
2779
2780
2781 /***********************************************************************
2782 * OLE_FreeClipDataArray [internal]
2783 *
2784 * NOTES:
2785 * frees the data associated with an array of CLIPDATAs
2786 */
2787 static void OLE_FreeClipDataArray(ULONG count, CLIPDATA * pClipDataArray)
2788 {
2789 ULONG i;
2790 for (i = 0; i < count; i++)
2791 if (pClipDataArray[i].pClipData)
2792 CoTaskMemFree(pClipDataArray[i].pClipData);
2793 }
2794
2795 /***********************************************************************
2796 * PropSysAllocString [OLE32.@]
2797 * NOTES
2798 * Forward to oleaut32.
2799 */
2800 BSTR WINAPI PropSysAllocString(LPCOLESTR str)
2801 {
2802 return SysAllocString(str);
2803 }
2804
2805 /***********************************************************************
2806 * PropSysFreeString [OLE32.@]
2807 * NOTES
2808 * Forward to oleaut32.
2809 */
2810 void WINAPI PropSysFreeString(LPOLESTR str)
2811 {
2812 SysFreeString(str);
2813 }
2814
2815 /******************************************************************************
2816 * Check if a PROPVARIANT's type is valid.
2817 */
2818 static inline HRESULT PROPVARIANT_ValidateType(VARTYPE vt)
2819 {
2820 switch (vt)
2821 {
2822 case VT_EMPTY:
2823 case VT_NULL:
2824 case VT_I1:
2825 case VT_I2:
2826 case VT_I4:
2827 case VT_I8:
2828 case VT_R4:
2829 case VT_R8:
2830 case VT_CY:
2831 case VT_DATE:
2832 case VT_BSTR:
2833 case VT_ERROR:
2834 case VT_BOOL:
2835 case VT_DECIMAL:
2836 case VT_UI1:
2837 case VT_UI2:
2838 case VT_UI4:
2839 case VT_UI8:
2840 case VT_INT:
2841 case VT_UINT:
2842 case VT_LPSTR:
2843 case VT_LPWSTR:
2844 case VT_FILETIME:
2845 case VT_BLOB:
2846 case VT_STREAM:
2847 case VT_STORAGE:
2848 case VT_STREAMED_OBJECT:
2849 case VT_STORED_OBJECT:
2850 case VT_BLOB_OBJECT:
2851 case VT_CF:
2852 case VT_CLSID:
2853 case VT_I1|VT_VECTOR:
2854 case VT_I2|VT_VECTOR:
2855 case VT_I4|VT_VECTOR:
2856 case VT_I8|VT_VECTOR:
2857 case VT_R4|VT_VECTOR:
2858 case VT_R8|VT_VECTOR:
2859 case VT_CY|VT_VECTOR:
2860 case VT_DATE|VT_VECTOR:
2861 case VT_BSTR|VT_VECTOR:
2862 case VT_ERROR|VT_VECTOR:
2863 case VT_BOOL|VT_VECTOR:
2864 case VT_VARIANT|VT_VECTOR:
2865 case VT_UI1|VT_VECTOR:
2866 case VT_UI2|VT_VECTOR:
2867 case VT_UI4|VT_VECTOR:
2868 case VT_UI8|VT_VECTOR:
2869 case VT_LPSTR|VT_VECTOR:
2870 case VT_LPWSTR|VT_VECTOR:
2871 case VT_FILETIME|VT_VECTOR:
2872 case VT_CF|VT_VECTOR:
2873 case VT_CLSID|VT_VECTOR:
2874 return S_OK;
2875 }
2876 WARN("Bad type %d\n", vt);
2877 return STG_E_INVALIDPARAMETER;
2878 }
2879
2880 /***********************************************************************
2881 * PropVariantClear [OLE32.@]
2882 */
2883 HRESULT WINAPI PropVariantClear(PROPVARIANT * pvar) /* [in/out] */
2884 {
2885 HRESULT hr;
2886
2887 TRACE("(%p)\n", pvar);
2888
2889 if (!pvar)
2890 return S_OK;
2891
2892 hr = PROPVARIANT_ValidateType(pvar->vt);
2893 if (FAILED(hr))
2894 {
2895 memset(pvar, 0, sizeof(*pvar));
2896 return hr;
2897 }
2898
2899 switch(pvar->vt)
2900 {
2901 case VT_EMPTY:
2902 case VT_NULL:
2903 case VT_I1:
2904 case VT_I2:
2905 case VT_I4:
2906 case VT_I8:
2907 case VT_R4:
2908 case VT_R8:
2909 case VT_CY:
2910 case VT_DATE:
2911 case VT_ERROR:
2912 case VT_BOOL:
2913 case VT_DECIMAL:
2914 case VT_UI1:
2915 case VT_UI2:
2916 case VT_UI4:
2917 case VT_UI8:
2918 case VT_INT:
2919 case VT_UINT:
2920 case VT_FILETIME:
2921 break;
2922 case VT_STREAM:
2923 case VT_STREAMED_OBJECT:
2924 case VT_STORAGE:
2925 case VT_STORED_OBJECT:
2926 if (pvar->u.pStream)
2927 IStream_Release(pvar->u.pStream);
2928 break;
2929 case VT_CLSID:
2930 case VT_LPSTR:
2931 case VT_LPWSTR:
2932 /* pick an arbitrary typed pointer - we don't care about the type
2933 * as we are just freeing it */
2934 CoTaskMemFree(pvar->u.puuid);
2935 break;
2936 case VT_BLOB:
2937 case VT_BLOB_OBJECT:
2938 CoTaskMemFree(pvar->u.blob.pBlobData);
2939 break;
2940 case VT_BSTR:
2941 PropSysFreeString(pvar->u.bstrVal);
2942 break;
2943 case VT_CF:
2944 if (pvar->u.pclipdata)
2945 {
2946 OLE_FreeClipDataArray(1, pvar->u.pclipdata);
2947 CoTaskMemFree(pvar->u.pclipdata);
2948 }
2949 break;
2950 default:
2951 if (pvar->vt & VT_VECTOR)
2952 {
2953 ULONG i;
2954
2955 switch (pvar->vt & ~VT_VECTOR)
2956 {
2957 case VT_VARIANT:
2958 FreePropVariantArray(pvar->u.capropvar.cElems, pvar->u.capropvar.pElems);
2959 break;
2960 case VT_CF:
2961 OLE_FreeClipDataArray(pvar->u.caclipdata.cElems, pvar->u.caclipdata.pElems);
2962 break;
2963 case VT_BSTR:
2964 for (i = 0; i < pvar->u.cabstr.cElems; i++)
2965 PropSysFreeString(pvar->u.cabstr.pElems[i]);
2966 break;
2967 case VT_LPSTR:
2968 for (i = 0; i < pvar->u.calpstr.cElems; i++)
2969 CoTaskMemFree(pvar->u.calpstr.pElems[i]);
2970 break;
2971 case VT_LPWSTR:
2972 for (i = 0; i < pvar->u.calpwstr.cElems; i++)
2973 CoTaskMemFree(pvar->u.calpwstr.pElems[i]);
2974 break;
2975 }
2976 if (pvar->vt & ~VT_VECTOR)
2977 {
2978 /* pick an arbitrary VT_VECTOR structure - they all have the same
2979 * memory layout */
2980 CoTaskMemFree(pvar->u.capropvar.pElems);
2981 }
2982 }
2983 else
2984 {
2985 WARN("Invalid/unsupported type %d\n", pvar->vt);
2986 hr = STG_E_INVALIDPARAMETER;
2987 }
2988 }
2989
2990 memset(pvar, 0, sizeof(*pvar));
2991 return hr;
2992 }
2993
2994 /***********************************************************************
2995 * PropVariantCopy [OLE32.@]
2996 */
2997 HRESULT WINAPI PropVariantCopy(PROPVARIANT *pvarDest, /* [out] */
2998 const PROPVARIANT *pvarSrc) /* [in] */
2999 {
3000 ULONG len;
3001 HRESULT hr;
3002
3003 TRACE("(%p, %p vt %04x)\n", pvarDest, pvarSrc, pvarSrc->vt);
3004
3005 hr = PROPVARIANT_ValidateType(pvarSrc->vt);
3006 if (FAILED(hr))
3007 return hr;
3008
3009 /* this will deal with most cases */
3010 *pvarDest = *pvarSrc;
3011
3012 switch(pvarSrc->vt)
3013 {
3014 case VT_EMPTY:
3015 case VT_NULL:
3016 case VT_I1:
3017 case VT_UI1:
3018 case VT_I2:
3019 case VT_UI2:
3020 case VT_BOOL:
3021 case VT_DECIMAL:
3022 case VT_I4:
3023 case VT_UI4:
3024 case VT_R4:
3025 case VT_ERROR:
3026 case VT_I8:
3027 case VT_UI8:
3028 case VT_INT:
3029 case VT_UINT:
3030 case VT_R8:
3031 case VT_CY:
3032 case VT_DATE:
3033 case VT_FILETIME:
3034 break;
3035 case VT_STREAM:
3036 case VT_STREAMED_OBJECT:
3037 case VT_STORAGE:
3038 case VT_STORED_OBJECT:
3039 if (pvarDest->u.pStream)
3040 IStream_AddRef(pvarDest->u.pStream);
3041 break;
3042 case VT_CLSID:
3043 pvarDest->u.puuid = CoTaskMemAlloc(sizeof(CLSID));
3044 *pvarDest->u.puuid = *pvarSrc->u.puuid;
3045 break;
3046 case VT_LPSTR:
3047 if (pvarSrc->u.pszVal)
3048 {
3049 len = strlen(pvarSrc->u.pszVal);
3050 pvarDest->u.pszVal = CoTaskMemAlloc((len+1)*sizeof(CHAR));
3051 CopyMemory(pvarDest->u.pszVal, pvarSrc->u.pszVal, (len+1)*sizeof(CHAR));
3052 }
3053 break;
3054 case VT_LPWSTR:
3055 if (pvarSrc->u.pwszVal)
3056 {
3057 len = lstrlenW(pvarSrc->u.pwszVal);
3058 pvarDest->u.pwszVal = CoTaskMemAlloc((len+1)*sizeof(WCHAR));
3059 CopyMemory(pvarDest->u.pwszVal, pvarSrc->u.pwszVal, (len+1)*sizeof(WCHAR));
3060 }
3061 break;
3062 case VT_BLOB:
3063 case VT_BLOB_OBJECT:
3064 if (pvarSrc->u.blob.pBlobData)
3065 {
3066 len = pvarSrc->u.blob.cbSize;
3067 pvarDest->u.blob.pBlobData = CoTaskMemAlloc(len);
3068 CopyMemory(pvarDest->u.blob.pBlobData, pvarSrc->u.blob.pBlobData, len);
3069 }
3070 break;
3071 case VT_BSTR:
3072 pvarDest->u.bstrVal = PropSysAllocString(pvarSrc->u.bstrVal);
3073 break;
3074 case VT_CF:
3075 if (pvarSrc->u.pclipdata)
3076 {
3077 len = pvarSrc->u.pclipdata->cbSize - sizeof(pvarSrc->u.pclipdata->ulClipFmt);
3078 pvarDest->u.pclipdata = CoTaskMemAlloc(sizeof (CLIPDATA));
3079 pvarDest->u.pclipdata->cbSize = pvarSrc->u.pclipdata->cbSize;
3080 pvarDest->u.pclipdata->ulClipFmt = pvarSrc->u.pclipdata->ulClipFmt;
3081 pvarDest->u.pclipdata->pClipData = CoTaskMemAlloc(len);
3082 CopyMemory(pvarDest->u.pclipdata->pClipData, pvarSrc->u.pclipdata->pClipData, len);
3083 }
3084 break;
3085 default:
3086 if (pvarSrc->vt & VT_VECTOR)
3087 {
3088 int elemSize;
3089 ULONG i;
3090
3091 switch(pvarSrc->vt & ~VT_VECTOR)
3092 {
3093 case VT_I1: elemSize = sizeof(pvarSrc->u.cVal); break;
3094 case VT_UI1: elemSize = sizeof(pvarSrc->u.bVal); break;
3095 case VT_I2: elemSize = sizeof(pvarSrc->u.iVal); break;
3096 case VT_UI2: elemSize = sizeof(pvarSrc->u.uiVal); break;
3097 case VT_BOOL: elemSize = sizeof(pvarSrc->u.boolVal); break;
3098 case VT_I4: elemSize = sizeof(pvarSrc->u.lVal); break;
3099 case VT_UI4: elemSize = sizeof(pvarSrc->u.ulVal); break;
3100 case VT_R4: elemSize = sizeof(pvarSrc->u.fltVal); break;
3101 case VT_R8: elemSize = sizeof(pvarSrc->u.dblVal); break;
3102 case VT_ERROR: elemSize = sizeof(pvarSrc->u.scode); break;
3103 case VT_I8: elemSize = sizeof(pvarSrc->u.hVal); break;
3104 case VT_UI8: elemSize = sizeof(pvarSrc->u.uhVal); break;
3105 case VT_CY: elemSize = sizeof(pvarSrc->u.cyVal); break;
3106 case VT_DATE: elemSize = sizeof(pvarSrc->u.date); break;
3107 case VT_FILETIME: elemSize = sizeof(pvarSrc->u.filetime); break;
3108 case VT_CLSID: elemSize = sizeof(*pvarSrc->u.puuid); break;
3109 case VT_CF: elemSize = sizeof(*pvarSrc->u.pclipdata); break;
3110 case VT_BSTR: elemSize = sizeof(pvarSrc->u.bstrVal); break;
3111 case VT_LPSTR: elemSize = sizeof(pvarSrc->u.pszVal); break;
3112 case VT_LPWSTR: elemSize = sizeof(pvarSrc->u.pwszVal); break;
3113 case VT_VARIANT: elemSize = sizeof(*pvarSrc->u.pvarVal); break;
3114
3115 default:
3116 FIXME("Invalid element type: %ul\n", pvarSrc->vt & ~VT_VECTOR);
3117 return E_INVALIDARG;
3118 }
3119 len = pvarSrc->u.capropvar.cElems;
3120 pvarDest->u.capropvar.pElems = len ? CoTaskMemAlloc(len * elemSize) : NULL;
3121 if (pvarSrc->vt == (VT_VECTOR | VT_VARIANT))
3122 {
3123 for (i = 0; i < len; i++)
3124 PropVariantCopy(&pvarDest->u.capropvar.pElems[i], &pvarSrc->u.capropvar.pElems[i]);
3125 }
3126 else if (pvarSrc->vt == (VT_VECTOR | VT_CF))
3127 {
3128 FIXME("Copy clipformats\n");
3129 }
3130 else if (pvarSrc->vt == (VT_VECTOR | VT_BSTR))
3131 {
3132 for (i = 0; i < len; i++)
3133 pvarDest->u.cabstr.pElems[i] = PropSysAllocString(pvarSrc->u.cabstr.pElems[i]);
3134 }
3135 else if (pvarSrc->vt == (VT_VECTOR | VT_LPSTR))
3136 {
3137 size_t strLen;
3138 for (i = 0; i < len; i++)
3139 {
3140 strLen = lstrlenA(pvarSrc->u.calpstr.pElems[i]) + 1;
3141 pvarDest->u.calpstr.pElems[i] = CoTaskMemAlloc(strLen);
3142 memcpy(pvarDest->u.calpstr.pElems[i],
3143 pvarSrc->u.calpstr.pElems[i], strLen);
3144 }
3145 }
3146 else if (pvarSrc->vt == (VT_VECTOR | VT_LPWSTR))
3147 {
3148 size_t strLen;
3149 for (i = 0; i < len; i++)
3150 {
3151 strLen = (lstrlenW(pvarSrc->u.calpwstr.pElems[i]) + 1) *
3152 sizeof(WCHAR);
3153 pvarDest->u.calpstr.pElems[i] = CoTaskMemAlloc(strLen);
3154 memcpy(pvarDest->u.calpstr.pElems[i],
3155 pvarSrc->u.calpstr.pElems[i], strLen);
3156 }
3157 }
3158 else
3159 CopyMemory(pvarDest->u.capropvar.pElems, pvarSrc->u.capropvar.pElems, len * elemSize);
3160 }
3161 else
3162 WARN("Invalid/unsupported type %d\n", pvarSrc->vt);
3163 }
3164
3165 return S_OK;
3166 }
3167
3168 /***********************************************************************
3169 * FreePropVariantArray [OLE32.@]
3170 */
3171 HRESULT WINAPI FreePropVariantArray(ULONG cVariants, /* [in] */
3172 PROPVARIANT *rgvars) /* [in/out] */
3173 {
3174 ULONG i;
3175
3176 TRACE("(%u, %p)\n", cVariants, rgvars);
3177
3178 if (!rgvars)
3179 return E_INVALIDARG;
3180
3181 for(i = 0; i < cVariants; i++)
3182 PropVariantClear(&rgvars[i]);
3183
3184 return S_OK;
3185 }
3186
3187 /******************************************************************************
3188 * DllDebugObjectRPCHook (OLE32.@)
3189 * turns on and off internal debugging, pointer is only used on macintosh
3190 */
3191
3192 BOOL WINAPI DllDebugObjectRPCHook(BOOL b, void *dummy)
3193 {
3194 FIXME("stub\n");
3195 return TRUE;
3196 }