📄 atlbase1.h
字号:
vt = VT_R4;
fltVal = fltSrc;
}
CComVariant(double dblSrc)
{
::VariantInit(this);
vt = VT_R8;
dblVal = dblSrc;
}
CComVariant(CY cySrc)
{
::VariantInit(this);
vt = VT_CY;
cyVal.Hi = cySrc.Hi;
cyVal.Lo = cySrc.Lo;
}
CComVariant(IDispatch* pSrc)
{
::VariantInit(this);
vt = VT_DISPATCH;
pdispVal = pSrc;
// Need to AddRef as VariantClear will Release
if (pdispVal != NULL)
pdispVal->AddRef();
}
CComVariant(IUnknown* pSrc)
{
::VariantInit(this);
vt = VT_UNKNOWN;
punkVal = pSrc;
// Need to AddRef as VariantClear will Release
if (punkVal != NULL)
punkVal->AddRef();
}
// Assignment Operators
public:
CComVariant& operator=(const CComVariant& varSrc)
{
InternalCopy(&varSrc);
return *this;
}
CComVariant& operator=(const VARIANT& varSrc)
{
InternalCopy(&varSrc);
return *this;
}
CComVariant& operator=(BSTR bstrSrc);
CComVariant& operator=(LPCOLESTR lpszSrc);
#ifndef OLE2ANSI
CComVariant& operator=(LPCSTR lpszSrc);
#endif
#if _MSC_VER>1020
CComVariant& operator=(bool bSrc);
#endif
CComVariant& operator=(int nSrc);
CComVariant& operator=(BYTE nSrc);
CComVariant& operator=(short nSrc);
CComVariant& operator=(long nSrc);
CComVariant& operator=(float fltSrc);
CComVariant& operator=(double dblSrc);
CComVariant& operator=(CY cySrc);
CComVariant& operator=(IDispatch* pSrc);
CComVariant& operator=(IUnknown* pSrc);
// Comparison Operators
public:
#if _MSC_VER>1020
bool operator==(const VARIANT& varSrc);
bool operator!=(const VARIANT& varSrc) {return !operator==(varSrc);}
#else
BOOL operator==(const VARIANT& varSrc);
BOOL operator!=(const VARIANT& varSrc) {return !operator==(varSrc);}
#endif
// Operations
public:
HRESULT Clear() { return ::VariantClear(this); }
HRESULT Copy(const VARIANT* pSrc) { return ::VariantCopy(this, const_cast<VARIANT*>(pSrc)); }
HRESULT Attach(VARIANT* pSrc);
HRESULT Detach(VARIANT* pDest);
HRESULT ChangeType(VARTYPE vtNew, const VARIANT* pSrc = NULL);
HRESULT WriteToStream(IStream* pStream);
HRESULT ReadFromStream(IStream* pStream);
// Implementation
public:
HRESULT InternalClear();
void InternalCopy(const VARIANT* pSrc);
};
/////////////////////////////////////////////////////////////////////////////
// GUID comparison
inline BOOL InlineIsEqualGUID(REFGUID rguid1, REFGUID rguid2)
{
return (
((PLONG) &rguid1)[0] == ((PLONG) &rguid2)[0] &&
((PLONG) &rguid1)[1] == ((PLONG) &rguid2)[1] &&
((PLONG) &rguid1)[2] == ((PLONG) &rguid2)[2] &&
((PLONG) &rguid1)[3] == ((PLONG) &rguid2)[3]);
}
inline BOOL InlineIsEqualUnknown(REFGUID rguid1)
{
return (
((PLONG) &rguid1)[0] == 0 &&
((PLONG) &rguid1)[1] == 0 &&
#ifdef _MAC
((PLONG) &rguid1)[2] == 0xC0000000 &&
((PLONG) &rguid1)[3] == 0x00000046);
#else
((PLONG) &rguid1)[2] == 0x000000C0 &&
((PLONG) &rguid1)[3] == 0x46000000);
#endif
}
/////////////////////////////////////////////////////////////////////////////
// Threading Model Support
class CComCriticalSection
{
public:
void Lock() {EnterCriticalSection(&m_sec);}
void Unlock() {LeaveCriticalSection(&m_sec);}
void Init() {InitializeCriticalSection(&m_sec);}
void Term() {DeleteCriticalSection(&m_sec);}
CRITICAL_SECTION m_sec;
};
class CComAutoCriticalSection
{
public:
void Lock() {EnterCriticalSection(&m_sec);}
void Unlock() {LeaveCriticalSection(&m_sec);}
CComAutoCriticalSection() {InitializeCriticalSection(&m_sec);}
~CComAutoCriticalSection() {DeleteCriticalSection(&m_sec);}
CRITICAL_SECTION m_sec;
};
class CComFakeCriticalSection
{
public:
void Lock() {}
void Unlock() {}
void Init() {}
void Term() {}
};
class CComMultiThreadModelNoCS
{
public:
static ULONG WINAPI Increment(LPLONG p) {return InterlockedIncrement(p);}
static ULONG WINAPI Decrement(LPLONG p) {return InterlockedDecrement(p);}
typedef CComFakeCriticalSection AutoCriticalSection;
typedef CComFakeCriticalSection CriticalSection;
typedef CComMultiThreadModelNoCS ThreadModelNoCS;
};
class CComMultiThreadModel
{
public:
static ULONG WINAPI Increment(LPLONG p) {return InterlockedIncrement(p);}
static ULONG WINAPI Decrement(LPLONG p) {return InterlockedDecrement(p);}
typedef CComAutoCriticalSection AutoCriticalSection;
typedef CComCriticalSection CriticalSection;
typedef CComMultiThreadModelNoCS ThreadModelNoCS;
};
class CComSingleThreadModel
{
public:
static ULONG WINAPI Increment(LPLONG p) {return ++(*p);}
static ULONG WINAPI Decrement(LPLONG p) {return --(*p);}
typedef CComFakeCriticalSection AutoCriticalSection;
typedef CComFakeCriticalSection CriticalSection;
typedef CComSingleThreadModel ThreadModelNoCS;
};
#ifndef _ATL_SINGLE_THREADED
#ifndef _ATL_APARTMENT_THREADED
#ifndef _ATL_FREE_THREADED
#define _ATL_FREE_THREADED
#endif
#endif
#endif
#if defined(_ATL_SINGLE_THREADED)
typedef CComSingleThreadModel CComObjectThreadModel;
typedef CComSingleThreadModel CComGlobalsThreadModel;
#elif defined(_ATL_APARTMENT_THREADED)
typedef CComSingleThreadModel CComObjectThreadModel;
typedef CComMultiThreadModel CComGlobalsThreadModel;
#else
typedef CComMultiThreadModel CComObjectThreadModel;
typedef CComMultiThreadModel CComGlobalsThreadModel;
#endif
/////////////////////////////////////////////////////////////////////////////
// CComModule
#define THREADFLAGS_APARTMENT 0x1
#define THREADFLAGS_BOTH 0x2
#define AUTPRXFLAG 0x4
struct _AtlCreateWndData
{
void* m_pThis;
DWORD m_dwThreadID;
_AtlCreateWndData* m_pNext;
};
class CComModule : public _ATL_MODULE
{
// Operations
public:
_AtlCreateWndData* m_pCreateWndList;
void AddCreateWndData(_AtlCreateWndData* pData, void* pObject);
void* ExtractCreateWndData();
void Init(_ATL_OBJMAP_ENTRY* p, HINSTANCE h)
{
cbSize = sizeof(_ATL_MODULE);
m_pCreateWndList = NULL;
AtlModuleInit(this, p, h);
}
void Term()
{
AtlModuleTerm(this);
}
LONG Lock() {return CComGlobalsThreadModel::Increment(&m_nLockCnt);}
LONG Unlock() {return CComGlobalsThreadModel::Decrement(&m_nLockCnt);}
LONG GetLockCount() {return m_nLockCnt;}
HINSTANCE GetModuleInstance() {return m_hInst;}
HINSTANCE GetResourceInstance() {return m_hInstResource;}
HINSTANCE GetTypeLibInstance() {return m_hInstTypeLib;}
// Registry support (helpers)
HRESULT RegisterTypeLib()
{
return AtlModuleRegisterTypeLib(this, NULL);
}
HRESULT RegisterTypeLib(LPCTSTR lpszIndex)
{
USES_CONVERSION;
return AtlModuleRegisterTypeLib(this, T2COLE(lpszIndex));
}
HRESULT RegisterServer(BOOL bRegTypeLib = FALSE, const CLSID* pCLSID = NULL)
{
return AtlModuleRegisterServer(this, bRegTypeLib, pCLSID);
}
HRESULT UnregisterServer(const CLSID* pCLSID = NULL)
{
return AtlModuleUnregisterServer(this, pCLSID);
}
// Resource-based Registration
HRESULT WINAPI UpdateRegistryFromResourceD(LPCTSTR lpszRes, BOOL bRegister,
struct _ATL_REGMAP_ENTRY* pMapEntries = NULL)
{
USES_CONVERSION;
return AtlModuleUpdateRegistryFromResourceD(this, T2COLE(lpszRes), bRegister,
pMapEntries);
}
HRESULT WINAPI UpdateRegistryFromResourceD(UINT nResID, BOOL bRegister,
struct _ATL_REGMAP_ENTRY* pMapEntries = NULL)
{
return AtlModuleUpdateRegistryFromResourceD(this,
(LPCOLESTR)MAKEINTRESOURCE(nResID), bRegister, pMapEntries);
}
#ifdef _ATL_STATIC_REGISTRY
// Statically linking to Registry Ponent
HRESULT WINAPI UpdateRegistryFromResourceS(LPCTSTR lpszRes, BOOL bRegister,
struct _ATL_REGMAP_ENTRY* pMapEntries = NULL);
HRESULT WINAPI UpdateRegistryFromResourceS(UINT nResID, BOOL bRegister,
struct _ATL_REGMAP_ENTRY* pMapEntries = NULL);
#endif //_ATL_STATIC_REGISTRY
// Standard Registration
HRESULT WINAPI UpdateRegistryClass(const CLSID& clsid, LPCTSTR lpszProgID,
LPCTSTR lpszVerIndProgID, UINT nDescID, DWORD dwFlags, BOOL bRegister);
HRESULT WINAPI RegisterClassHelper(const CLSID& clsid, LPCTSTR lpszProgID,
LPCTSTR lpszVerIndProgID, UINT nDescID, DWORD dwFlags);
HRESULT WINAPI UnregisterClassHelper(const CLSID& clsid, LPCTSTR lpszProgID,
LPCTSTR lpszVerIndProgID);
// Register/Revoke All Class Factories with the OS (EXE only)
HRESULT RegisterClassObjects(DWORD dwClsContext, DWORD dwFlags)
{
return AtlModuleRegisterClassObjects(this, dwClsContext, dwFlags);
}
HRESULT RevokeClassObjects()
{
return AtlModuleRevokeClassObjects(this);
}
// Obtain a Class Factory (DLL only)
HRESULT GetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
{
return AtlModuleGetClassObject(this, rclsid, riid, ppv);
}
// Only used in CComAutoThreadModule
HRESULT CreateInstance(void* /*pfnCreateInstance*/, REFIID /*riid*/, void** /*ppvObj*/)
{
return S_OK;
}
};
/////////////////////////////////////////////////////////////////////////////////////////////
// Thread Pooling classes
class _AtlAptCreateObjData
{
public:
_ATL_CREATORFUNC* pfnCreateInstance;
const IID* piid;
HANDLE hEvent;
LPSTREAM pStream;
HRESULT hRes;
};
class CComApartment
{
public:
static UINT ATL_CREATE_OBJECT;
static DWORD WINAPI _Apartment(void* pv)
{
return ((CComApartment*)pv)->Apartment();
}
DWORD Apartment()
{
CoInitialize(NULL);
MSG msg;
while(GetMessage(&msg, 0, 0, 0))
{
if (msg.message == ATL_CREATE_OBJECT)
{
_AtlAptCreateObjData* pdata = (_AtlAptCreateObjData*)msg.lParam;
IUnknown* pUnk = NULL;
pdata->hRes = pdata->pfnCreateInstance(NULL, IID_IUnknown, (void**)&pUnk);
if (SUCCEEDED(pdata->hRes))
pdata->hRes = CoMarshalInterThreadInterfaceInStream(*pdata->piid, pUnk, &pdata->pStream);
if (SUCCEEDED(pdata->hRes))
{
pUnk->Release();
ATLTRACE(_T("Object created on thread = %d\n"), GetCurrentThreadId());
}
SetEvent(pdata->hEvent);
}
DispatchMessage(&msg);
}
CoUninitialize();
return 0;
}
LONG Lock() {return CComGlobalsThreadModel::Increment(&m_nLockCnt);}
LONG Unlock(){return CComGlobalsThreadModel::Decrement(&m_nLockCnt);
}
LONG GetLockCount() {return m_nLockCnt;}
DWORD m_dwThreadID;
HANDLE m_hThread;
LONG m_nLockCnt;
};
class CComSimpleThreadAllocator
{
public:
CComSimpleThreadAllocator()
{
m_nThread = 0;
}
int GetThread(CComApartment* /*pApt*/, int nThreads)
{
if (++m_nThread == nThreads)
m_nThread = 0;
return m_nThread;
}
int m_nThread;
};
#if _MSC_VER>1020
template <class ThreadAllocator = CComSimpleThreadAllocator>
#else
template <class ThreadAllocator>
#endif
class CComAutoThreadModule : public CComModule
{
public:
void Init(_ATL_OBJMAP_ENTRY* p, HINSTANCE h, int nThreads = GetDefaultThreads());
~CComAutoThreadModule();
HRESULT CreateInstance(void* pfnCreateInstance, REFIID riid, void** ppvObj);
LONG Lock();
LONG Unlock();
DWORD dwThreadID;
int m_nThreads;
CComApartment* m_pApartments;
ThreadAllocator m_Allocator;
static int GetDefaultThreads()
{
SYSTEM_INFO si;
GetSystemInfo(&si);
return si.dwNumberOfProcessors * 4;
}
};
#ifdef _ATL_STATIC_REGISTRY
#define UpdateRegistryFromResource UpdateRegistryFromResourceS
#else
#define UpdateRegistryFromResource UpdateRegistryFromResourceD
#endif
/////////////////////////////////////////////////////////////////////////////
// CRegKey
class CRegKey
{
public:
CRegKey();
~CRegKey();
// Attributes
public:
operator HKEY() const;
HKEY m_hKey;
// Operations
public:
LONG SetValue(DWORD dwValue, LPCTSTR lpszValueName);
LONG QueryValue(DWORD& dwValue, LPCTSTR lpszValueName);
LONG QueryValue(LPTSTR szValue, LPCTSTR lpszValueName, DWORD* pdwCount);
LONG SetValue(LPCTSTR lpszValue, LPCTSTR lpszValueName = NULL);
LONG SetKeyValue(LPCTSTR lpszKeyName, LPCTSTR lpszValue, LPCTSTR lpszValueName = NULL);
static LONG WINAPI SetValue(HKEY hKeyParent, LPCTSTR lpszKeyName,
LPCTSTR lpszValue, LPCTSTR lpszValueName = NULL);
LONG Create(HKEY hKeyParent, LPCTSTR lpszKeyName,
LPTSTR lpszClass = REG_NONE, DWORD dwOptions = REG_OPTION_NON_VOLATILE,
REGSAM samDesired = KEY_ALL_ACCESS,
LPSECURITY_ATTRIBUTES lpSecAttr = NULL,
LPDWORD lpdwDisposition = NULL);
LONG Open(HKEY hKeyParent, LPCTSTR lpszKeyName,
REGSAM samDesired = KEY_ALL_ACCESS);
LONG Close();
HKEY Detach();
void Attach(HKEY hKey);
LONG DeleteSubKey(LPCTSTR lpszSubKey);
LONG RecurseDeleteKey(LPCTSTR lpszKey);
LONG DeleteValue(LPCTSTR lpszValue);
};
inline CRegKey::CRegKey()
{m_hKey = NULL;}
inline CRegKey::~CRegKey()
{Close();}
inline CRegKey::operator HKEY() const
{return m_hKey;}
inline HKEY CRegKey::Detach()
{
HKEY hKey = m_hKey;
m_hKey = NULL;
return hKey;
}
inline void CRegKey::Attach(HKEY hKey)
{
_ASSERTE(m_hKey == NULL);
m_hKey = hKey;
}
inline LONG CRegKey::DeleteSubKey(LPCTSTR lpszSubKey)
{
_ASSERTE(m_hKey != NULL);
return RegDeleteKey(m_hKey, lpszSubKey);
}
inline LONG CRegKey::DeleteValue(LPCTSTR lpszValue)
{
_ASSERTE(m_hKey != NULL);
return RegDeleteValue(m_hKey, (LPTSTR)lpszValue);
}
#pragma pack(pop)
#ifndef ATL_NO_NAMESPACE
}; //namespace ATL
using namespace ATL;
#endif
/////////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -