📄 comip.h
字号:
#endif
// Compare two pointers
//
template<typename _InterfacePtr> bool operator!=(_InterfacePtr p) throw(_com_error)
{
return !(operator==(p));
}
#ifndef __BORLANDC__
// Compare with other interface
//
template<> bool operator!=(Interface* p) throw(_com_error)
{
return !(operator==(p));
}
#endif
#ifndef __BORLANDC__
// Compares 2 _com_ptr_t's
//
template<> bool operator!=(_com_ptr_t& p) throw(_com_error)
{
return !(operator==(p));
}
#endif
#ifndef __BORLANDC__
// For comparison to NULL
//
template<> bool operator!=(int null) throw(_com_error)
{
return !(operator==(null));
}
#endif
// Compare two pointers
//
template<typename _InterfacePtr> bool operator<(_InterfacePtr p) throw(_com_error)
{
return _CompareUnknown(p) < 0;
}
// Compare two pointers
//
template<typename _InterfacePtr> bool operator>(_InterfacePtr p) throw(_com_error)
{
return _CompareUnknown(p) > 0;
}
// Compare two pointers
//
template<typename _InterfacePtr> bool operator<=(_InterfacePtr p) throw(_com_error)
{
return _CompareUnknown(p) <= 0;
}
// Compare two pointers
//
template<typename _InterfacePtr> bool operator>=(_InterfacePtr p) throw(_com_error)
{
return _CompareUnknown(p) >= 0;
}
// Provides error-checking Release()ing of this interface.
//
void Release() throw(_com_error)
{
if (m_pInterface == NULL) {
_com_issue_error(E_POINTER);
}
m_pInterface->Release();
m_pInterface = NULL;
}
// Provides error-checking AddRef()ing of this interface.
//
void AddRef() throw(_com_error)
{
if (m_pInterface == NULL) {
_com_issue_error(E_POINTER);
}
m_pInterface->AddRef();
}
// Another way to get the interface pointer without casting.
//
Interface* GetInterfacePtr() const throw()
{
return m_pInterface;
}
// Loads an interface for the provided CLSID.
// Returns an HRESULT. Any previous interface is released.
//
HRESULT CreateInstance(const CLSID& rclsid, IUnknown* pOuter = NULL, DWORD dwClsContext = CLSCTX_ALL) throw()
{
HRESULT hr;
_Release();
if (dwClsContext & (CLSCTX_LOCAL_SERVER | CLSCTX_REMOTE_SERVER)) {
IUnknown* pIUnknown;
hr = CoCreateInstance(rclsid, pOuter, dwClsContext, __uuidof(IUnknown), reinterpret_cast<void**>(&pIUnknown));
if (FAILED(hr)) {
return hr;
}
hr = OleRun(pIUnknown);
if (SUCCEEDED(hr)) {
hr = pIUnknown->QueryInterface(GetIID(), reinterpret_cast<void**>(&m_pInterface));
}
pIUnknown->Release();
}
else {
hr = CoCreateInstance(rclsid, pOuter, dwClsContext, GetIID(), reinterpret_cast<void**>(&m_pInterface));
}
return hr;
}
// Creates the class specified by clsidString. clsidString may
// contain a class id, or a prog id string.
//
HRESULT CreateInstance(LPOLESTR clsidString, IUnknown* pOuter = NULL, DWORD dwClsContext = CLSCTX_ALL) throw()
{
if (clsidString == NULL) {
return E_INVALIDARG;
}
CLSID clsid;
HRESULT hr;
if (clsidString[0] == '{') {
hr = CLSIDFromString(clsidString, &clsid);
}
else {
hr = CLSIDFromProgID(clsidString, &clsid);
}
if (FAILED(hr)) {
return hr;
}
return CreateInstance(clsid, pOuter, dwClsContext);
}
// Creates the class specified by SBCS clsidString. clsidString may
// contain a class id, or a prog id string.
//
HRESULT CreateInstance(LPCSTR clsidStringA, IUnknown* pOuter = NULL, DWORD dwClsContext = CLSCTX_ALL) throw()
{
if (clsidStringA == NULL) {
return E_INVALIDARG;
}
int size = lstrlenA(clsidStringA) + 1;
LPOLESTR clsidStringW = static_cast<LPOLESTR>(_alloca(size * 2));
clsidStringW[0] = '\0';
if (MultiByteToWideChar(CP_ACP, 0, clsidStringA, -1, clsidStringW, size) == 0) {
return HRESULT_FROM_WIN32(GetLastError());
}
return CreateInstance(clsidStringW, pOuter, dwClsContext);
}
// Attach to the active object specified by rclsid.
// Any previous interface is released.
//
HRESULT GetActiveObject(const CLSID& rclsid) throw()
{
_Release();
IUnknown* pIUnknown;
HRESULT hr = ::GetActiveObject(rclsid, NULL, &pIUnknown);
if (FAILED(hr)) {
return hr;
}
hr = pIUnknown->QueryInterface(GetIID(), reinterpret_cast<void**>(&m_pInterface));
if (FAILED(hr)) {
return hr;
}
pIUnknown->Release();
return hr;
}
// Attach to the active object specified by clsidString.
// First convert the LPOLESTR to a CLSID.
//
HRESULT GetActiveObject(LPOLESTR clsidString) throw()
{
if (clsidString == NULL) {
return E_INVALIDARG;
}
CLSID clsid;
HRESULT hr;
if (clsidString[0] == '{') {
hr = CLSIDFromString(clsidString, &clsid);
}
else {
hr = CLSIDFromProgID(clsidString, &clsid);
}
if (FAILED(hr)) {
return hr;
}
return GetActiveObject(clsid);
}
// Attach to the active object specified by clsidStringA.
// First convert the LPCSTR to a LPOLESTR.
//
HRESULT GetActiveObject(LPCSTR clsidStringA) throw()
{
if (clsidStringA == NULL) {
return E_INVALIDARG;
}
int size = lstrlenA(clsidStringA) + 1;
LPOLESTR clsidStringW = static_cast<LPOLESTR>(_alloca(size * 2));
clsidStringW[0] = '\0';
if (MultiByteToWideChar(CP_ACP, 0, clsidStringA, -1, clsidStringW, size) == 0) {
return HRESULT_FROM_WIN32(GetLastError());
}
return GetActiveObject(clsidStringW);
}
// Performs the QI for the specified IID and returns it in p.
// As with all QIs, the interface will be AddRef'd.
//
template<typename _InterfaceType> HRESULT QueryInterface(const IID& iid, _InterfaceType*& p) throw ()
{
if (m_pInterface != NULL) {
return m_pInterface->QueryInterface(iid, reinterpret_cast<void**>(&p));
}
return E_POINTER;
}
// Performs the QI for the specified IID and returns it in p.
// As with all QIs, the interface will be AddRef'd.
//
template<typename _InterfaceType> HRESULT QueryInterface(const IID& iid, _InterfaceType** p) throw()
{
return QueryInterface(iid, *p);
}
private:
// The Interface.
//
Interface* m_pInterface;
// Releases only if the interface is not null.
// The interface is not set to NULL.
//
void _Release() throw()
{
if (m_pInterface != NULL) {
m_pInterface->Release();
}
}
// AddRefs only if the interface is not NULL
//
void _AddRef() throw()
{
if (m_pInterface != NULL) {
m_pInterface->AddRef();
}
}
// Performs a QI on pUnknown for the interface type returned
// for this class. The interface is stored. If pUnknown is
// NULL, or the QI fails, E_NOINTERFACE is returned and
// _pInterface is set to NULL.
//
template<typename _InterfacePtr> HRESULT _QueryInterface(const _InterfacePtr& p) throw()
{
HRESULT hr;
// Can't QI NULL
//
if (p) {
// Query for this interface
//
Interface* pInterface;
hr = p->QueryInterface(GetIID(), reinterpret_cast<void**>(&pInterface));
if (FAILED(hr)) {
// If failed initialize interface to NULL and return HRESULT.
//
Attach(NULL);
return hr;
}
// Save the interface without AddRef()ing.
//
Attach(pInterface);
}
else {
operator=(static_cast<Interface*>(NULL));
hr = E_NOINTERFACE;
}
return hr;
}
// Compares the provided pointer with this by obtaining IUnknown interfaces
// for each pointer and then returning the difference.
//
template<typename _InterfacePtr> int _CompareUnknown(_InterfacePtr p) throw(_com_error)
{
IUnknown* pu1, *pu2;
if (m_pInterface != NULL) {
HRESULT hr = m_pInterface->QueryInterface(__uuidof(IUnknown), reinterpret_cast<void**>(&pu1));
if (FAILED(hr)) {
_com_issue_error(hr);
}
pu1->Release();
}
else {
pu1 = NULL;
}
if (p) {
HRESULT hr = p->QueryInterface(__uuidof(IUnknown), reinterpret_cast<void**>(&pu2));
if (FAILED(hr)) {
_com_issue_error(hr);
}
pu2->Release();
}
else {
pu2 = NULL;
}
return pu1 - pu2;
}
// Try to extract either IDispatch* or an IUnknown* from
// the VARIANT
//
HRESULT QueryStdInterfaces(const _variant_t& varSrc) throw()
{
if (V_VT(&varSrc) == VT_DISPATCH) {
return _QueryInterface(V_DISPATCH(&varSrc));
}
if (V_VT(&varSrc) == VT_UNKNOWN) {
return _QueryInterface(V_UNKNOWN(&varSrc));
}
// We have something other than an IUnknown or an IDispatch.
// Can we convert it to either one of these?
// Try IDispatch first
//
VARIANT varDest;
VariantInit(&varDest);
HRESULT hr = VariantChangeType(&varDest, const_cast<VARIANT*>(static_cast<const VARIANT*>(&varSrc)), 0, VT_DISPATCH);
if (SUCCEEDED(hr)) {
hr = _QueryInterface(V_DISPATCH(&varSrc));
}
if (FAILED(hr) && (hr == E_NOINTERFACE)) {
// That failed ... so try IUnknown
//
VariantInit(&varDest);
hr = VariantChangeType(&varDest, const_cast<VARIANT*>(static_cast<const VARIANT*>(&varSrc)), 0, VT_UNKNOWN);
if (SUCCEEDED(hr)) {
hr = _QueryInterface(V_UNKNOWN(&varSrc));
}
}
VariantClear(&varDest);
return hr;
}
};
// Reverse comparison operators for _com_ptr_t
//
template<typename _InterfaceType> bool operator==(int null, _com_ptr_t<_InterfaceType>& p) throw(_com_error)
{
if (null != 0) {
_com_issue_error(E_POINTER);
}
return p == NULL;
}
template<typename _Interface, typename _InterfacePtr> bool operator==(_Interface* i, _com_ptr_t<_InterfacePtr>& p) throw(_com_error)
{
return p == i;
}
template<typename _Interface> bool operator!=(int null, _com_ptr_t<_Interface>& p) throw(_com_error)
{
if (null != 0) {
_com_issue_error(E_POINTER);
}
return p != NULL;
}
template<typename _Interface, typename _InterfacePtr> bool operator!=(_Interface* i, _com_ptr_t<_InterfacePtr>& p) throw(_com_error)
{
return p != i;
}
template<typename _Interface> bool operator<(int null, _com_ptr_t<_Interface>& p) throw(_com_error)
{
if (null != 0) {
_com_issue_error(E_POINTER);
}
return p > NULL;
}
template<typename _Interface, typename _InterfacePtr> bool operator<(_Interface* i, _com_ptr_t<_InterfacePtr>& p) throw(_com_error)
{
return p > i;
}
template<typename _Interface> bool operator>(int null, _com_ptr_t<_Interface>& p) throw(_com_error)
{
if (null != 0) {
_com_issue_error(E_POINTER);
}
return p < NULL;
}
template<typename _Interface, typename _InterfacePtr> bool operator>(_Interface* i, _com_ptr_t<_InterfacePtr>& p) throw(_com_error)
{
return p < i;
}
template<typename _Interface> bool operator<=(int null, _com_ptr_t<_Interface>& p) throw(_com_error)
{
if (null != 0) {
_com_issue_error(E_POINTER);
}
return p >= NULL;
}
template<typename _Interface, typename _InterfacePtr> bool operator<=(_Interface* i, _com_ptr_t<_InterfacePtr>& p) throw(_com_error)
{
return p >= i;
}
template<typename _Interface> bool operator>=(int null, _com_ptr_t<_Interface>& p) throw(_com_error)
{
if (null != 0) {
_com_issue_error(E_POINTER);
}
return p <= NULL;
}
template<typename _Interface, typename _InterfacePtr> bool operator>=(_Interface* i, _com_ptr_t<_InterfacePtr>& p) throw(_com_error)
{
return p <= i;
}
#pragma warning(pop)
#pragma option pop /*P_O_Pop*/
#endif // _INC_COMIP
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -