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

📄 local.cpp

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

long g_cComponents = 0;
long g_cServerLocks = 0;
HANDLE g_hEvent;

// Category identifier for Arithmetic objects
CATID CATID_Math = {0x10000010,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01}};

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() : m_cRef(0) { g_cComponents++; }
	~CInsideCOM() { cout << "Component: CInsideCOM::~CInsideCOM()" << endl, g_cComponents--; }

private:
	long m_cRef;
};

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

ULONG __stdcall CInsideCOM::Release()
{
	cout << "Component: CInsideCOM::Release() m_cRef = " << m_cRef - 1 << endl;
	if(--m_cRef == 0)
	{
		SetEvent(g_hEvent);	// ADD THIS!!!
		delete this;
		return 0;
	}
	return m_cRef;
}

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

HRESULT __stdcall 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 iid, void** ppv);
	HRESULT __stdcall LockServer(BOOL bLock);

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

private:
	long m_cRef;
};

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

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

HRESULT __stdcall 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 __stdcall 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;

	// QueryInterface probably for IID_IUNKNOWN
	return pInsideCOM->QueryInterface(iid, ppv);
}

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

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

	// Instantiate COM's implementation of component categories
	ICatRegister* pCatRegister;
	CoCreateInstance(CLSID_StdComponentCategoriesMgr, NULL, CLSCTX_INPROC_SERVER, IID_ICatRegister, (void**)&pCatRegister);

	// Set up the CATEGORYINFO structure
	CATEGORYINFO catinfo;
	catinfo.catid = CATID_Math;
	catinfo.lcid = 0x0409;
	wcsncpy(catinfo.szDescription, L"Arithmetic Objects", 128);

	// Install the component category
	pCatRegister->RegisterCategories(1, &catinfo);

	// Register InsideCOM as an Arithmetic object
	CATID rgcatid[1];
	rgcatid[0] = CATID_Math;
	pCatRegister->RegisterClassImplCategories(CLSID_InsideCOM, 1, rgcatid);

	// Release COM's implementation of component categories
	pCatRegister->Release();

	// Instantiate COM's implementation of component categories
	// This time ask for ICatInformation
	ICatInformation* pCatInformation;
	CoCreateInstance(CLSID_StdComponentCategoriesMgr, NULL, CLSCTX_INPROC_SERVER, IID_ICatInformation, (void**)&pCatInformation);

	// Get an enumerator for all CLSIDs that are Arithmetic objects
	IEnumCLSID* pEnumCLSID;
	pCatInformation->EnumClassesOfCategories(1, &CATID_Math, 0, NULL, &pEnumCLSID);

	// Release the ICatInformation interface pointer
	pCatInformation->Release();

	// Loop through the enumerator
	CLSID clsid = CLSID_NULL;
	DWORD fetched = 0;
	while(true)
	{
		// Get the next CLSID
		pEnumCLSID->Next(1, &clsid, &fetched);
		if(fetched == 0)
			break;

		// Convert the CLSID to a string for display
		char buffer[39];
		OLECHAR ppsz[39];
		StringFromGUID2(clsid, ppsz, 39);
		WideCharToMultiByte(CP_ACP, 0, ppsz, 39, buffer, 39, NULL, NULL);

		// Print out the CLSIDs of the objects found
		cout << "CLSID that supports the Arithmetic Object category is " << buffer << endl;
	}

	// Release the enumerator
	pEnumCLSID->Release();
}

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");

		// Instantiate COM's implementation of component categories
		ICatRegister* pCatRegister;
		CoCreateInstance(CLSID_StdComponentCategoriesMgr, NULL, CLSCTX_INPROC_SERVER, IID_ICatRegister, (void**)&pCatRegister);

		// Delete the component category
		pCatRegister->UnRegisterCategories(1, &CATID_Math);

		// Unregister InsideCOM as no longer being an Arithmetic object
		CATID rgcatid[1];
		rgcatid[0] = CATID_Math;
		pCatRegister->UnRegisterClassImplCategories(CLSID_InsideCOM, 1, rgcatid);

		// Release COM's implementation of component categories
		pCatRegister->Release();

		cout << "UnregServer" << endl;
		exit(true);
	}
	if(_stricmp(szToken, "Embedding") != 0)
	{
		cout << "Invalid parameter" << endl;
		exit(false);
	}
}

void main(int argc, char** argv)
{
	cout << "Component: CoInitializeEx()" << endl;
	CoInitializeEx(NULL, COINIT_MULTITHREADED);

	CommandLineParameters(argc, argv);

	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();
}

⌨️ 快捷键说明

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