📄 comstuff.h
字号:
// comstuff.h
//
// @author christian oetterli
//
#ifndef __COMSTUFF_H__
#define __COMSTUFF_H__
// handy classes/methods for dealing with com support classes.
// microsoft com support classes.
#include <comdef.h>
// remove _com_util namespace.
using namespace _com_util;
// remove underlines.
typedef _com_error com_error;
typedef _variant_t variant;
typedef _bstr_t bstr;
#define com_issue_error _com_issue_error
// prefere those versions.
#define uuid_of __uuidof
#define checkError CheckError
// casts bstr to CString.
// @param s bstr to convert.
// @return casted string.
#define asCString(s) (BSTR)(bstr)s
// casts CString to bstr.
// @param cs CString to convert.
// @return casted string.
#define asBSTR(cs) (LPCWSTR)cs
// handlers to protect com methods.
#define tryIt try
#define catchIt catch (com_error &err) { return CComError(err); }
// check if variant is missing; i.e. has not been provided as a parameter to a method.
// @param v variant to check.
// @return true if it is.
inline bool isMissing(const variant &v)
{
return VT_ERROR == v.vt && DISP_E_PARAMNOTFOUND == v.lVal;
}
// check if variant is an object.
// @param v variant to check.
// @return true if it is.
inline bool isObject(const variant &v)
{
return VT_DISPATCH == v.vt || VT_UNKNOWN == v.vt;
}
// check if variant is null.
// @param v variant to check.
// @return true if it is.
inline bool isNull(const variant &v)
{
return VT_NULL == v.vt;
}
// check if pointer is null.
// @param p pointer to check.
// @return true if is.
inline bool isNull(IUnknownPtr p)
{
return 0 == p.GetInterfacePtr();
}
// check if variant is empty.
// @param v variant to check.
// @return true if it is.
inline bool isEmpty(const variant &v)
{
return VT_EMPTY == v.vt;
}
// throws an E_POINTER com_error if pointer is null.
// @param pointer to check.
template <class T> void checkPointer(T pointer)
{
if (0 == pointer)
{
com_issue_error(E_POINTER);
}
}
// class that enables passing an issued com_error to the caller.
class CComError
{
public:
// initializes this from com_error.
// @param newErr error to pass back to caller.
CComError(const com_error &newErr)
: err(newErr)
{
// retrieve error info...
IErrorInfoPtr e = err.ErrorInfo();
if (! isNull(e))
{
// ...and place it in our thread.
SetErrorInfo(0, e);
}
}
// @return err.
com_error getErr() const
{
return err;
}
// returns error code.
// @return hresult.
operator HRESULT() const
{
return getErr().Error();
}
protected:
com_error err;
};
// returns the VARIANT representation of bool b.
// @param b bool to convert.
// @return VARIANT_BOOL version.
inline VARIANT_BOOL asVariantBool(bool b)
{
return (b) ? VARIANT_TRUE : VARIANT_FALSE;
}
#endif //__COMSTUFF_H__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -