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

📄 local.cpp

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

HANDLE g_hEvent;

class CInsideCOM : 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);

	CInsideCOM();
	~CInsideCOM();

private:
	long m_cRef;
};

void InitiateComponentShutdown()
{
	cout << "InitiateComponentShutdown()" << endl;
	SetEvent(g_hEvent);
}

CInsideCOM::CInsideCOM() : m_cRef(1)
{
	CoAddRefServerProcess();
}

CInsideCOM::~CInsideCOM()
{
	cout << "Component: CInsideCOM::~CInsideCOM()" << endl;
	if(CoReleaseServerProcess() == 0)
		InitiateComponentShutdown();
}

ULONG CInsideCOM::AddRef()
{
	cout << "Component: CInsideCOM::AddRef() m_cRef = " << m_cRef + 1 << endl;
	return InterlockedIncrement(&m_cRef);
}

ULONG CInsideCOM::Release()
{
	cout << "Component: CInsideCOM::Release() m_cRef = " << m_cRef - 1 << endl;
	unsigned cRef = InterlockedDecrement(&m_cRef);
	if(cRef != 0)
		return cRef;
	delete this;
	return 0;
}

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

HRESULT CInsideCOM::Sum(int x, int y, int *retval)
{
	*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() { cout << "Component: CFactory::~CFactory()" << endl; }

private:
	long m_cRef;
};

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

ULONG CFactory::Release()
{
	cout << "Component: CFactory::Release() m_cRef = " << m_cRef - 1 << endl;
	unsigned cRef = InterlockedDecrement(&m_cRef);
	if(cRef != 0)
		return cRef;
	delete this;
	return 0;
}

HRESULT CFactory::QueryInterface(REFIID iid, void** ppv)
{
	if((iid == IID_IUnknown) || (iid == IID_IClassFactory))
	{
		cout << "Component: 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 iid, void **ppv)
{
	if(pUnknownOuter != NULL)
		return CLASS_E_NOAGGREGATION;

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

	if(pInsideCOM == NULL)
		return E_OUTOFMEMORY;

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

HRESULT CFactory::LockServer(BOOL bLock)
{
	if(bLock)
		CoAddRefServerProcess();
	else
		if(CoReleaseServerProcess() == 0)
			InitiateComponentShutdown();
	return S_OK;
}

void RegisterComponent()
{
	ITypeLib* pTypeLib;
	HRESULT hr = 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);

	CoInitializeEx(NULL, COINIT_MULTITHREADED);
	g_hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

	DWORD dwRegister;
	IClassFactory *pIFactory = new CFactory();
	CoRegisterClassObject(CLSID_InsideCOM, pIFactory, CLSCTX_LOCAL_SERVER, REGCLS_SUSPENDED|REGCLS_MULTIPLEUSE, &dwRegister);
	CoResumeClassObjects();

	WaitForSingleObject(g_hEvent, INFINITE);
	CloseHandle(g_hEvent);

	CoRevokeClassObject(dwRegister);
	pIFactory->Release();
	CoUninitialize();
}

⌨️ 快捷键说明

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