⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 container.cpp

📁 经验交流,从网上下载的好东西望大家分享
💻 CPP
字号:
// container.cpp
#include <iostream.h>
#include "Container\container.h" // Generated by MIDL
#include "registry.h" // Add This!!!

const CLSID CLSID_Container = {0x10000012,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01}};
const CLSID CLSID_InsideCOM = {0x10000002,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01}};

long g_cComponents = 0;
long g_cServerLocks = 0;

class CContainer : public IMultiply, public ISum
{
public:
	// IUnknown
	ULONG __stdcall AddRef();
	ULONG __stdcall Release();
	HRESULT __stdcall QueryInterface(REFIID iid, void** ppv);

	// ISum
	HRESULT __stdcall Sum(int x, int y, int* retval);

	// IMultiply
	HRESULT __stdcall Multiply(int x, int y, int* retval);

	HRESULT Init();
	CContainer();
	~CContainer();

private:
	ULONG m_cRef;
	ISum* m_pSum;
};

CContainer::CContainer() : m_cRef(1), m_pSum(NULL)
{
	g_cComponents++;
}

CContainer::~CContainer()
{
	cout << "Container: CContainer::~CContainer()" << endl;
	g_cComponents--;
	m_pSum->Release();
}

// This code goes in an Init method because a constructor cannot return an error code
HRESULT CContainer::Init()
{
	return CoCreateInstance(CLSID_InsideCOM, NULL, CLSCTX_INPROC_SERVER, IID_ISum, (void**)&m_pSum);
}

ULONG CContainer::AddRef()
{
	cout << "Container: CContainer::AddRef() m_cRef = " << m_cRef + 1 << endl;
	return ++m_cRef;
}

ULONG CContainer::Release()
{
	cout << "Container: CContainer::Release() m_cRef = " << m_cRef - 1 << endl;
	if(--m_cRef != 0)
		return m_cRef;
	delete this;
	return 0;
}

HRESULT CContainer::QueryInterface(REFIID riid, void** ppv)
{
	if(riid == IID_IUnknown)
	{
		cout << "Container: CContainer::QueryInterface() for IUnknown returning " << this << endl;
//		*ppv = reinterpret_cast<IUnknown*>(this);
//		*ppv = static_cast<ISum*>(this);
		*ppv = (IMultiply*)this;
	}
	else if(riid == IID_ISum)
	{
		cout << "Container: CContainer::QueryInterface() for ISum returning " << this << endl;
		*ppv = (ISum*)this;
	}
	else if(riid == IID_IMultiply)
	{
		cout << "Container: CContainer::QueryInterface() for ISum returning " << this << endl;
		*ppv = (IMultiply*)this;
	}
	else 
	{
		*ppv = NULL;
		return E_NOINTERFACE;
	}
	AddRef();
	return S_OK;
}

HRESULT CContainer::Sum(int x, int y, int* retval)
{
	cout << "Container: CContainer::Sum() " << x << " + " << y << " = " << x + y << endl;
	// Delegate this call to our contained object
	return m_pSum->Sum(x, y, retval);
}

HRESULT CContainer::Multiply(int x, int y, int* retval)
{
	cout << "Container: CContainer::Multiply() " << x << " * " << y << " = " << x * y << endl;
	*retval = x * y;
	return S_OK;
}

class CFactory : public IClassFactory
{
public:
	// IUnknown
	ULONG __stdcall AddRef();
	ULONG __stdcall Release();
	HRESULT __stdcall QueryInterface(REFIID iid, void** ppv);

	// IClassFactory
	HRESULT __stdcall CreateInstance(IUnknown *pUnknownOuter, REFIID iid, void** ppv);
	HRESULT __stdcall LockServer(BOOL bLock);

	CFactory() : m_cRef(1) { }
	~CFactory() { }

private:
	ULONG m_cRef;
};

ULONG CFactory::AddRef()
{
	cout << "Container: CFactory::AddRef() m_cRef = " << m_cRef + 1 << endl;
	return ++m_cRef;
}

ULONG CFactory::Release()
{
	cout << "Container: CFactory::Release() m_cRef = " << m_cRef - 1 << endl;
	if(--m_cRef != 0)
		return m_cRef;
	delete this;
	return 0;
}

HRESULT CFactory::QueryInterface(REFIID iid, void** ppv)
{
	if((iid == IID_IUnknown) || (iid == IID_IClassFactory))
	{
		cout << "Container: CFactory::QueryInteface() for IUnknown or IClassFactory " << this << endl;
		*ppv = (IClassFactory *)this;
	}
	else
	{
		*ppv = NULL;
		return E_NOINTERFACE;
	}
	AddRef();
	return S_OK;
}

HRESULT CFactory::CreateInstance(IUnknown *pUnknownOuter, REFIID riid, void** ppv)
{
	if(pUnknownOuter != NULL)
		return CLASS_E_NOAGGREGATION;

	CContainer *pContainer = new CContainer();
	if(pContainer == NULL)
		return E_OUTOFMEMORY;

	pContainer->Init();

	// QueryInterface probably for IID_IUnknown
	HRESULT hr = pContainer->QueryInterface(riid, ppv);
	pContainer->Release();
	return hr;
}

HRESULT CFactory::LockServer(BOOL bLock)
{
	if(bLock)
		g_cServerLocks++;
	else
		g_cServerLocks--;
	return S_OK;
}

HRESULT __stdcall DllCanUnloadNow()
{
	cout << "Container: DllCanUnloadNow() " << (g_cServerLocks == 0 && g_cComponents == 0 ? "Yes" : "No") << endl;
	if(g_cServerLocks == 0 && g_cComponents == 0)
		return S_OK;
	else
		return S_FALSE;
}

HRESULT __stdcall DllGetClassObject(REFCLSID clsid, REFIID iid, void** ppv)
{
	cout << "Container: DllGetClassObject" << endl;
	
	if(clsid != CLSID_Container)
		return CLASS_E_CLASSNOTAVAILABLE;

	CFactory* pFactory = new CFactory;
	if(pFactory == NULL)
		return E_OUTOFMEMORY;

	// QueryInterface probably for IClassFactory
	HRESULT hr = pFactory->QueryInterface(iid, ppv);
	pFactory->Release();
	return hr;
}

HRESULT __stdcall DllRegisterServer()
{
	return RegisterServer("container.dll", CLSID_Container, "Inside COM+ Containment Sample", "Component.Container", "Component.Container.1", NULL);
}

HRESULT __stdcall DllUnregisterServer()
{
	return UnregisterServer(CLSID_Container, "Component.Container", "Component.Container.1");
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -