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

📄 local.cpp

📁 经验交流,从网上下载的好东西望大家分享
💻 CPP
字号:
// local.cpp
#define _WIN32_DCOM
#include <iostream.h>
#include <conio.h>
#include "Component\component.h" // Generated by MIDL
#include "registry.h"

HANDLE g_hEvent;

class CInsideCOM : public ISum, public IStdMarshalInfo
{
public:
	// IUnknown
	ULONG __stdcall AddRef();
	ULONG __stdcall Release();
	HRESULT __stdcall QueryInterface(REFIID riid, void** ppv);

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

	// IStdMarshalInfo
	HRESULT __stdcall GetClassForHandler(DWORD dwDestContext, void* pvDestContext, CLSID* pClsid);

	CInsideCOM();
	~CInsideCOM();

private:
	long m_cRef;
};


CInsideCOM::~CInsideCOM()
{ 
	cout << "Component: CInsideCOM::~CInsideCOM()" << endl;
	/*
	if(m_cRef != 0)
		cout << "Object deleted too early." << endl;
	
// As we are destructing, and releasing internal objects, we may be getting reentrant in to the Release 
//	and thus Destructor, to avoid that we protect the Destructor with an artifical increment of RefCount;
	m_cRef++;

	if(m_pStubMarshal)
	{
		// Following the rules of Aggregation, since we had kept an interface from Inner object, We Should AddRef() ourselves, 
		// and then call release on interface, otherwise, we will cause errorneous RefCount 
		AddRef();
		m_pStubMarshal->Release();
	}

	if(m_pStubUnk)
	{
		// Following the rules of Aggregation, since we had kept an interface from Inner object, We Should AddRef() ourselves, 
		// and then call release on interface, otherwise, we will cause errorneous RefCount 
		AddRef();
		m_pStubUnk->Release();
	}

	// Since we had artificially incremented the RefCount, to protect from getting reentrant during 
	// the destructor. Lets offset that by decrementing the RefCount.
	m_cRef--;
	*/
	SetEvent(g_hEvent);	// exit the application
}

CInsideCOM::CInsideCOM() : m_cRef(1)
{
	/*
	// There is a chance, that the AddRef/Release getting called during CoGetStdMarshalEx() dance
	// To protect from getting deleted during the constructor, We will artificially increment the reference counter.
	m_cRef++;

	m_pStubUnk = 0;

	HRESULT hr = CoGetStdMarshalEx((IUnknown*)(ISum*)this, SMEXF_SERVER, &m_pStubUnk);
	if(FAILED(hr))
	{
		cout << "FAILED  CoGetStdMarshalEx " << endl;
		m_pStubUnk = NULL;
	}
	else
	{
		hr = m_pStubUnk->QueryInterface(IID_IMarshal, (void**)&m_pStubMarshal);
		if(FAILED(hr))
			m_pStubMarshal = NULL;
		else
		// since we are keeping an interface from Inner object, 
		// we should Call Release on ourself following the rules of Aggregation. 
		// If we don't follow this rule, then Object will have errorneous reference count, 
			Release();
	}
	// Since we had artificially incremented the reference counter, to protect from getting deleted during 
	// the constructor. Lets offset that by decrementing the reference counter.
	m_cRef--;
	*/
}

ULONG CInsideCOM::AddRef()
{
	return ++m_cRef;
}

ULONG CInsideCOM::Release()
{
	if(--m_cRef != 0)
		return m_cRef;
	delete this;
	return 0;
}

HRESULT CInsideCOM::QueryInterface(REFIID riid, void** ppv)
{
	if(riid == IID_IUnknown || riid == IID_ISum)
	{
		cout << "Component: CInsideCOM::QueryInterface() for ISum or IUnknown returning " << this << endl;
		*ppv = (ISum*)this;
	}
	else if(riid == IID_IStdMarshalInfo)
	{
		cout << "Component: CInsideCOM::QueryInterface() for IStdMarshalInfo returning " << this << endl;
		*ppv = (IStdMarshalInfo*)this;
	}
	else
	{
		*ppv = NULL;
		return E_NOINTERFACE;
	}
	AddRef();
	return S_OK;
}

const CLSID CLSID_InsideCOMHandler = {0x11000006,0x0000,0x0000,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
HRESULT CInsideCOM::GetClassForHandler(DWORD dwDestContext, void* pvDestContext, CLSID* pClsid)
{
	cout << "IStdMarshalInfo::GetClassForHandler" << endl;
	*pClsid = CLSID_InsideCOMHandler;

//	CoGetStdMarshalEx(this, SMEXF_SERVER, &m_pUnknownInner);

	return NOERROR;
}

HRESULT CInsideCOM::Sum(int x, int y, int* retval)
{
	cout << "Component: CInsideCOM::Sum() " << 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 riid, void** ppv);
	HRESULT __stdcall LockServer(BOOL bLock);

	CFactory() : m_cRef(1) { }
	~CFactory() { cout << "CFactory::~CFactory" << endl; }

private:
	long m_cRef;
};

ULONG CFactory::AddRef()
{
	return ++m_cRef;
}

ULONG CFactory::Release()
{
	if(--m_cRef != 0)
		return m_cRef;
	delete this;
	return 0;
}

HRESULT CFactory::QueryInterface(REFIID riid, void** ppv)
{
	if(riid == IID_IUnknown || riid == IID_IClassFactory)
		*ppv = (IClassFactory*)this;
	else
	{
		*ppv = NULL;
		return E_NOINTERFACE;
	}
	AddRef();
	return S_OK;
}

HRESULT CFactory::CreateInstance(IUnknown* pUnknownOuter, REFIID riid, void** ppv)
{
	HRESULT hr;

	if(pUnknownOuter != NULL)
		return CLASS_E_NOAGGREGATION;

	CInsideCOM *pInsideCOM = new CInsideCOM;
	cout << "Component: CFactory::CreateInstance() " << pInsideCOM << endl;

	if(pInsideCOM == NULL)
		return E_OUTOFMEMORY;

	hr = pInsideCOM->QueryInterface(riid, ppv);
	pInsideCOM->Release();
	return hr;
}

HRESULT CFactory::LockServer(BOOL bLock)
{
	return S_OK;
}

void RegisterComponent()
{
	ITypeLib* pTypeLib;
	LoadTypeLibEx(L"component.exe", REGKIND_DEFAULT, &pTypeLib);
	pTypeLib->Release();
	RegisterServer("component.exe", CLSID_InsideCOM, "Inside COM Sample", "Component.InsideCOM", "Component.InsideCOM.1", NULL);
}

void CommandLineParameters(int argc, char** argv)
{
	RegisterComponent();
	if(argc < 2)
	{
		cout << "No parameter, but registered anyway" << endl;
		exit(false);
	}
	char* szToken = strtok(argv[1], "-/"); 
	if(_stricmp(szToken, "RegServer") == 0)
	{
		RegisterComponent();
		cout << "RegServer" << endl;
		exit(true);
	}
	if(_stricmp(szToken, "UnregServer") == 0)
	{
		UnRegisterTypeLib(LIBID_Component, 1, 0, LANG_NEUTRAL, SYS_WIN32);
		UnregisterServer(CLSID_InsideCOM, "Component.InsideCOM", "Component.InsideCOM.1");
		cout << "UnregServer" << endl;
		exit(true);
	}
	if(_stricmp(szToken, "Embedding") != 0)
	{
		cout << "Invalid parameter" << endl;
		exit(false);
	}
}

void main(int argc, char** argv)
{
	CommandLineParameters(argc, argv);

	cout << "Component: CoInitializeEx()" << endl;
	CoInitializeEx(NULL, COINIT_MULTITHREADED);

	IClassFactory *pClassFactory = new CFactory();

	cout << "Component: CoRegisterClassObject()" << endl;
	DWORD dwRegister;
	CoRegisterClassObject(CLSID_InsideCOM, pClassFactory, CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, &dwRegister);

	g_hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
	WaitForSingleObject(g_hEvent, INFINITE);

	CoRevokeClassObject(dwRegister);
	pClassFactory->Release();
	CoUninitialize();
	_getch();
}

⌨️ 快捷键说明

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