Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

124 lines
1.8 KiB

// Copyright (c) 1997-1999 Microsoft Corporation
//
// COM utility code
//
// 2-3-99 sburns
#ifndef COMSTUFF_HPP_INCLUDED
#define COMSTUFF_HPP_INCLUDED
// Ensures that the type of the pointer to receive the interface pointer
// matches the IID of the interface. If it doesn't, the static_cast will
// cause a compiler error.
//
// Example:
// IFoo* fooptr = 0;
// HRESULT hr = punk->QueryInterface(QI_PARAMS(IFoo, &fooptr));
//
// From Box, D. Essential COM. pp 60-61. Addison-Wesley. ISBN 0-201-63446-5
#define QI_PARAMS(Interface, ppvExpression) \
IID_##Interface, reinterpret_cast<void**>(static_cast<Interface**>(ppvExpression))
namespace Burnslib
{
// A BSTR wrapper that frees itself upon destruction.
//
// From Box, D. Essential COM. pp 80-81. Addison-Wesley. ISBN 0-201-63446-5
class AutoBstr
{
public:
explicit
AutoBstr(const String& s)
:
bstr(::SysAllocString(const_cast<wchar_t*>(s.c_str())))
{
}
explicit
AutoBstr(const wchar_t* s)
:
bstr(::SysAllocString(s))
{
}
~AutoBstr()
{
::SysFreeString(bstr);
bstr = 0;
}
operator BSTR () const
{
return bstr;
}
private:
BSTR bstr;
};
class AutoCoInitialize
{
public:
AutoCoInitialize()
{
hr = ::CoInitialize(0);
}
// uncomment if this ever becomes necessary
// AutoCoInitialize(DWORD dwCoInit)
// {
// hr = ::CoInitializeEx(0, dwCoInit);
// }
~AutoCoInitialize()
{
if (SUCCEEDED(hr))
{
::CoUninitialize();
}
}
HRESULT
Result() const
{
return hr;
}
private:
HRESULT hr;
};
} // namespace Burnslib
#endif // COMSTUFF_HPP_INCLUDED