VirtualBox

source: vbox/trunk/include/VBox/com/defs.h@ 35517

Last change on this file since 35517 was 34655, checked in by vboxsync, 14 years ago

Glue: make sure that crashed XPCOM servers are reliably detected

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 20.4 KB
Line 
1/** @file
2 * MS COM / XPCOM Abstraction Layer:
3 * Common definitions
4 */
5
6/*
7 * Copyright (C) 2006-2010 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27#ifndef ___VBox_com_defs_h
28#define ___VBox_com_defs_h
29
30/* Make sure all the stdint.h macros are included - must come first! */
31#ifndef __STDC_LIMIT_MACROS
32# define __STDC_LIMIT_MACROS
33#endif
34#ifndef __STDC_CONSTANT_MACROS
35# define __STDC_CONSTANT_MACROS
36#endif
37
38#if defined (RT_OS_OS2)
39
40# if defined(RT_MAX) && RT_MAX != 22
41# undef RT_MAX
42# define REDEFINE_RT_MAX
43# endif
44# undef RT_MAX
45
46/* Make sure OS/2 Toolkit headers are pulled in to have BOOL/ULONG/etc. typedefs
47 * already defined in order to be able to redefine them using #define. */
48# define INCL_BASE
49# define INCL_PM
50# include <os2.h>
51
52/* OS/2 Toolkit defines TRUE and FALSE */
53# undef FALSE
54# undef TRUE
55
56/* */
57# undef RT_MAX
58# ifdef REDEFINE_RT_MAX
59# define RT_MAX(Value1, Value2) ( (Value1) >= (Value2) ? (Value1) : (Value2) )
60# endif
61
62#endif /* defined (RT_OS_OS2) */
63
64/* Include iprt/types.h (which also includes iprt/types.h) now to make sure iprt
65 * gets to stdint.h first, otherwise a system/xpcom header might beat us and
66 * we'll be without the macros that are optional in C++. */
67#include <iprt/types.h>
68
69#if !defined (VBOX_WITH_XPCOM)
70
71#if defined (RT_OS_WINDOWS)
72
73// Windows COM
74/////////////////////////////////////////////////////////////////////////////
75
76#include <objbase.h>
77#ifndef VBOX_COM_NO_ATL
78# include <atlbase.h>
79#include <atlcom.h>
80#endif
81
82#define NS_DECL_ISUPPORTS
83#define NS_IMPL_ISUPPORTS1_CI(a, b)
84
85/* these are XPCOM only, one for every interface implemented */
86#define NS_DECL_ISUPPORTS
87
88/** Returns @c true if @a rc represents a warning result code */
89#define SUCCEEDED_WARNING(rc) (SUCCEEDED (rc) && (rc) != S_OK)
90
91/** Tests is a COM result code indicates that the process implementing the
92 * interface is dead.
93 *
94 * COM status codes:
95 * 0x800706ba - RPC_S_SERVER_UNAVAILABLE. Killed before call was made.
96 * 0x800706be - RPC_S_CALL_FAILED. Killed after call was made.
97 * 0x800706bf - RPC_S_CALL_FAILED_DNE. Not observed, but should be matter of timing.
98 */
99#define FAILED_DEAD_INTERFACE(rc) \
100 ( (rc) == HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE) \
101 || (rc) == HRESULT_FROM_WIN32(RPC_S_CALL_FAILED) \
102 || (rc) == HRESULT_FROM_WIN32(RPC_S_CALL_FAILED_DNE) \
103 )
104
105/** Immutable BSTR string */
106typedef const OLECHAR *CBSTR;
107
108/** Input BSTR argument of interface method declaration. */
109#define IN_BSTR BSTR
110
111/** Input GUID argument of interface method declaration. */
112#define IN_GUID GUID
113/** Output GUID argument of interface method declaration. */
114#define OUT_GUID GUID*
115
116/** Makes the name of the getter interface function (n must be capitalized). */
117#define COMGETTER(n) get_##n
118/** Makes the name of the setter interface function (n must be capitalized). */
119#define COMSETTER(n) put_##n
120
121/**
122 * Declares an input safearray parameter in the COM method implementation. Also
123 * used to declare the COM attribute setter parameter. Corresponds to either of
124 * the following XIDL definitions:
125 * <pre>
126 * <param name="arg" ... dir="in" safearray="yes"/>
127 * ...
128 * <attribute name="arg" ... safearray="yes"/>
129 * </pre>
130 *
131 * The method implementation should use the com::SafeArray helper class to work
132 * with parameters declared using this define.
133 *
134 * @param aType Array element type.
135 * @param aArg Parameter/attribute name.
136 */
137#define ComSafeArrayIn(aType, aArg) SAFEARRAY **aArg
138
139/**
140 * Expands to @true if the given input safearray parameter is a "null pointer"
141 * which makes it impossible to use it for reading safearray data.
142 */
143#define ComSafeArrayInIsNull(aArg) ((aArg) == NULL || *(aArg) == NULL)
144
145/**
146 * Wraps the given parameter name to generate an expression that is suitable for
147 * passing the parameter to functions that take input safearray parameters
148 * declared using the ComSafeArrayIn marco.
149 *
150 * @param aArg Parameter name to wrap. The given parameter must be declared
151 * within the calling function using the ComSafeArrayIn macro.
152 */
153#define ComSafeArrayInArg(aArg) aArg
154
155/**
156 * Declares an output safearray parameter in the COM method implementation. Also
157 * used to declare the COM attribute getter parameter. Corresponds to either of
158 * the following XIDL definitions:
159 * <pre>
160 * <param name="arg" ... dir="out" safearray="yes"/>
161 * <param name="arg" ... dir="return" safearray="yes"/>
162 * ...
163 * <attribute name="arg" ... safearray="yes"/>
164 * </pre>
165 *
166 * The method implementation should use the com::SafeArray helper class to work
167 * with parameters declared using this define.
168 *
169 * @param aType Array element type.
170 * @param aArg Parameter/attribute name.
171 */
172#define ComSafeArrayOut(aType, aArg) SAFEARRAY **aArg
173
174/**
175 * Expands to @true if the given output safearray parameter is a "null pointer"
176 * which makes it impossible to use it for returning a safearray.
177 */
178#define ComSafeArrayOutIsNull(aArg) ((aArg) == NULL)
179
180/**
181 * Wraps the given parameter name to generate an expression that is suitable for
182 * passing the parameter to functions that take output safearray parameters
183 * declared using the ComSafeArrayOut marco.
184 *
185 * @param aArg Parameter name to wrap. The given parameter must be declared
186 * within the calling function using the ComSafeArrayOut macro.
187 */
188#define ComSafeArrayOutArg(aArg) aArg
189
190/**
191 * Version of ComSafeArrayIn for GUID.
192 * @param aArg Parameter name to wrap.
193 */
194#define ComSafeGUIDArrayIn(aArg) SAFEARRAY **aArg
195
196/**
197 * Version of ComSafeArrayInIsNull for GUID.
198 * @param aArg Parameter name to wrap.
199 */
200#define ComSafeGUIDArrayInIsNull(aArg) ComSafeArrayInIsNull (aArg)
201
202/**
203 * Version of ComSafeArrayInArg for GUID.
204 * @param aArg Parameter name to wrap.
205 */
206#define ComSafeGUIDArrayInArg(aArg) ComSafeArrayInArg (aArg)
207
208/**
209 * Version of ComSafeArrayOut for GUID.
210 * @param aArg Parameter name to wrap.
211 */
212#define ComSafeGUIDArrayOut(aArg) SAFEARRAY **aArg
213
214/**
215 * Version of ComSafeArrayOutIsNull for GUID.
216 * @param aArg Parameter name to wrap.
217 */
218#define ComSafeGUIDArrayOutIsNull(aArg) ComSafeArrayOutIsNull (aArg)
219
220/**
221 * Version of ComSafeArrayOutArg for GUID.
222 * @param aArg Parameter name to wrap.
223 */
224#define ComSafeGUIDArrayOutArg(aArg) ComSafeArrayOutArg (aArg)
225
226/**
227 * Returns the const reference to the IID (i.e., |const GUID &|) of the given
228 * interface.
229 *
230 * @param i interface class
231 */
232#define COM_IIDOF(I) _ATL_IIDOF(I)
233
234/**
235 * For using interfaces before including the interface definitions. This will
236 * deal with XPCOM using 'class' and COM using 'struct' when defining
237 * interfaces.
238 *
239 * @param I interface name.
240 */
241#define COM_STRUCT_OR_CLASS(I) struct I
242
243#else /* defined (RT_OS_WINDOWS) */
244
245#error "VBOX_WITH_XPCOM must be defined on a platform other than Windows!"
246
247#endif /* defined (RT_OS_WINDOWS) */
248
249#else /* !defined (VBOX_WITH_XPCOM) */
250
251// XPCOM
252/////////////////////////////////////////////////////////////////////////////
253
254#if defined (RT_OS_DARWIN) || (defined (QT_VERSION) && (QT_VERSION >= 0x040000))
255 /* CFBase.h defines these &
256 * qglobal.h from Qt4 defines these */
257# undef FALSE
258# undef TRUE
259#endif /* RT_OS_DARWIN || QT_VERSION */
260
261#include <nsID.h>
262
263#define ATL_NO_VTABLE
264#define DECLARE_CLASSFACTORY(a)
265#define DECLARE_CLASSFACTORY_SINGLETON(a)
266#define DECLARE_REGISTRY_RESOURCEID(a)
267#define DECLARE_NOT_AGGREGATABLE(a)
268#define DECLARE_PROTECT_FINAL_CONSTRUCT()
269#define BEGIN_COM_MAP(a)
270#define COM_INTERFACE_ENTRY(a)
271#define COM_INTERFACE_ENTRY2(a,b)
272#define END_COM_MAP() NS_DECL_ISUPPORTS
273
274#define HRESULT nsresult
275#define SUCCEEDED NS_SUCCEEDED
276#define FAILED NS_FAILED
277
278#define SUCCEEDED_WARNING(rc) (NS_SUCCEEDED (rc) && (rc) != NS_OK)
279
280#define FAILED_DEAD_INTERFACE(rc) ( (rc) == NS_ERROR_ABORT \
281 || (rc) == NS_ERROR_CALL_FAILED \
282 )
283
284#define IUnknown nsISupports
285
286#define BOOL PRBool
287#define BYTE PRUint8
288#define SHORT PRInt16
289#define USHORT PRUint16
290#define LONG PRInt32
291#define ULONG PRUint32
292#define LONG64 PRInt64
293#define ULONG64 PRUint64
294/* XPCOM has only 64bit floats */
295#define FLOAT PRFloat64
296#define DOUBLE PRFloat64
297
298#define FALSE PR_FALSE
299#define TRUE PR_TRUE
300
301#define OLECHAR wchar_t
302
303/* note: typedef to semantically match BSTR on Win32 */
304typedef PRUnichar *BSTR;
305typedef const PRUnichar *CBSTR;
306typedef BSTR *LPBSTR;
307
308/** Input BSTR argument the interface method declaration. */
309#define IN_BSTR CBSTR
310
311/**
312 * Type to define a raw GUID variable (for members use the com::Guid class
313 * instead).
314 */
315#define GUID nsID
316/** Input GUID argument the interface method declaration. */
317#define IN_GUID const nsID &
318/** Output GUID argument the interface method declaration. */
319#define OUT_GUID nsID **
320
321/** Makes the name of the getter interface function (n must be capitalized). */
322#define COMGETTER(n) Get##n
323/** Makes the name of the setter interface function (n must be capitalized). */
324#define COMSETTER(n) Set##n
325
326/* safearray input parameter macros */
327#define ComSafeArrayIn(aType, aArg) PRUint32 aArg##Size, aType *aArg
328#define ComSafeArrayInIsNull(aArg) ((aArg) == NULL)
329#define ComSafeArrayInArg(aArg) aArg##Size, aArg
330
331/* safearray output parameter macros */
332#define ComSafeArrayOut(aType, aArg) PRUint32 *aArg##Size, aType **aArg
333#define ComSafeArrayOutIsNull(aArg) ((aArg) == NULL)
334#define ComSafeArrayOutArg(aArg) aArg##Size, aArg
335
336/* safearray input parameter macros for GUID */
337#define ComSafeGUIDArrayIn(aArg) PRUint32 aArg##Size, const nsID **aArg
338#define ComSafeGUIDArrayInIsNull(aArg) ComSafeArrayInIsNull (aArg)
339#define ComSafeGUIDArrayInArg(aArg) ComSafeArrayInArg (aArg)
340
341/* safearray output parameter macros for GUID */
342#define ComSafeGUIDArrayOut(aArg) PRUint32 *aArg##Size, nsID ***aArg
343#define ComSafeGUIDArrayOutIsNull(aArg) ComSafeArrayOutIsNull (aArg)
344#define ComSafeGUIDArrayOutArg(aArg) ComSafeArrayOutArg (aArg)
345
346/* CLSID and IID for compatibility with Win32 */
347typedef nsCID CLSID;
348typedef nsIID IID;
349
350/* OLE error codes */
351#define S_OK ((nsresult) NS_OK)
352#define E_UNEXPECTED NS_ERROR_UNEXPECTED
353#define E_NOTIMPL NS_ERROR_NOT_IMPLEMENTED
354#define E_OUTOFMEMORY NS_ERROR_OUT_OF_MEMORY
355#define E_INVALIDARG NS_ERROR_INVALID_ARG
356#define E_NOINTERFACE NS_ERROR_NO_INTERFACE
357#define E_POINTER NS_ERROR_NULL_POINTER
358#define E_ABORT NS_ERROR_ABORT
359#define E_FAIL NS_ERROR_FAILURE
360/* Note: a better analog for E_ACCESSDENIED would probably be
361 * NS_ERROR_NOT_AVAILABLE, but we want binary compatibility for now. */
362#define E_ACCESSDENIED ((nsresult) 0x80070005L)
363
364#define STDMETHOD(a) NS_IMETHOD a
365#define STDMETHODIMP NS_IMETHODIMP
366
367#define COM_IIDOF(I) NS_GET_IID(I)
368
369#define COM_STRUCT_OR_CLASS(I) class I
370
371/* A few very simple ATL emulator classes to provide
372 * FinalConstruct()/FinalRelease() functionality on Linux. */
373
374class CComMultiThreadModel
375{
376};
377
378template <class Base> class CComObjectRootEx : public Base
379{
380public:
381 HRESULT FinalConstruct() { return S_OK; }
382 void FinalRelease() {}
383};
384
385template <class Base> class CComObject : public Base
386{
387public:
388 virtual ~CComObject() { this->FinalRelease(); }
389};
390
391/* helper functions */
392extern "C"
393{
394BSTR SysAllocString (const OLECHAR* sz);
395BSTR SysAllocStringByteLen (char *psz, unsigned int len);
396BSTR SysAllocStringLen (const OLECHAR *pch, unsigned int cch);
397void SysFreeString (BSTR bstr);
398int SysReAllocString (BSTR *pbstr, const OLECHAR *psz);
399int SysReAllocStringLen (BSTR *pbstr, const OLECHAR *psz, unsigned int cch);
400unsigned int SysStringByteLen (BSTR bstr);
401unsigned int SysStringLen (BSTR bstr);
402}
403
404/**
405 * 'Constructor' for the component class.
406 * This constructor, as opposed to NS_GENERIC_FACTORY_CONSTRUCTOR,
407 * assumes that the component class is derived from the CComObjectRootEx<>
408 * template, so it calls FinalConstruct() right after object creation
409 * and ensures that FinalRelease() will be called right before destruction.
410 * The result from FinalConstruct() is returned to the caller.
411 */
412#define NS_GENERIC_FACTORY_CONSTRUCTOR_WITH_RC(_InstanceClass) \
413static NS_IMETHODIMP \
414_InstanceClass##Constructor(nsISupports *aOuter, REFNSIID aIID, \
415 void **aResult) \
416{ \
417 nsresult rv; \
418 \
419 *aResult = NULL; \
420 if (NULL != aOuter) { \
421 rv = NS_ERROR_NO_AGGREGATION; \
422 return rv; \
423 } \
424 \
425 CComObject <_InstanceClass> *inst = new CComObject <_InstanceClass>(); \
426 if (NULL == inst) { \
427 rv = NS_ERROR_OUT_OF_MEMORY; \
428 return rv; \
429 } \
430 \
431 NS_ADDREF(inst); /* protect FinalConstruct() */ \
432 rv = inst->FinalConstruct(); \
433 if (NS_SUCCEEDED(rv)) \
434 rv = inst->QueryInterface(aIID, aResult); \
435 NS_RELEASE(inst); \
436 \
437 return rv; \
438}
439
440/**
441 * 'Constructor' that uses an existing getter function that gets a singleton.
442 * The getter function must have the following prototype:
443 * nsresult _GetterProc (_InstanceClass **inst)
444 * This constructor, as opposed to NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR,
445 * lets the getter function return a result code that is passed back to the
446 * caller that tries to instantiate the object.
447 * NOTE: assumes that getter does an AddRef - so additional AddRef is not done.
448 */
449#define NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR_WITH_RC(_InstanceClass, _GetterProc) \
450static NS_IMETHODIMP \
451_InstanceClass##Constructor(nsISupports *aOuter, REFNSIID aIID, \
452 void **aResult) \
453{ \
454 nsresult rv; \
455 \
456 _InstanceClass * inst = NULL; /* initialized to shut up gcc */ \
457 \
458 *aResult = NULL; \
459 if (NULL != aOuter) { \
460 rv = NS_ERROR_NO_AGGREGATION; \
461 return rv; \
462 } \
463 \
464 rv = _GetterProc(&inst); \
465 if (NS_FAILED(rv)) \
466 return rv; \
467 \
468 /* sanity check */ \
469 if (NULL == inst) \
470 return NS_ERROR_OUT_OF_MEMORY; \
471 \
472 /* NS_ADDREF(inst); */ \
473 if (NS_SUCCEEDED(rv)) { \
474 rv = inst->QueryInterface(aIID, aResult); \
475 } \
476 NS_RELEASE(inst); \
477 \
478 return rv; \
479}
480
481#endif /* !defined (VBOX_WITH_XPCOM) */
482
483/**
484 * Declares a wchar_t string literal from the argument.
485 * Necessary to overcome MSC / GCC differences.
486 * @param s expression to stringify
487 */
488#if defined (_MSC_VER)
489# define WSTR_LITERAL(s) L#s
490#elif defined (__GNUC__)
491# define WSTR_LITERAL(s) L""#s
492#else
493# error "Unsupported compiler!"
494#endif
495
496namespace com
497{
498
499// use this macro to implement scriptable interfaces
500#ifdef RT_OS_WINDOWS
501#define VBOX_SCRIPTABLE_IMPL(iface) \
502 public IDispatchImpl<iface, &IID_##iface, &LIBID_VirtualBox, \
503 kTypeLibraryMajorVersion, kTypeLibraryMinorVersion>
504
505#define VBOX_SCRIPTABLE_DISPATCH_IMPL(iface) \
506 STDMETHOD(QueryInterface)(REFIID riid , void **ppObj) \
507 { \
508 if (riid == IID_IUnknown) \
509 { \
510 *ppObj = (IUnknown*)this; \
511 AddRef(); \
512 return S_OK; \
513 } \
514 if (riid == IID_IDispatch) \
515 { \
516 *ppObj = (IDispatch*)this; \
517 AddRef(); \
518 return S_OK; \
519 } \
520 if (riid == IID_##iface) \
521 { \
522 *ppObj = (iface*)this; \
523 AddRef(); \
524 return S_OK; \
525 } \
526 *ppObj = NULL; \
527 return E_NOINTERFACE; \
528 }
529#else
530#define VBOX_SCRIPTABLE_IMPL(iface) \
531 public iface
532#define VBOX_SCRIPTABLE_DISPATCH_IMPL(iface)
533#endif
534
535
536} /* namespace com */
537
538#endif /* ___VBox_com_defs_h */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette