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

📄 client.cpp

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

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

	// IOutGoing
	HRESULT __stdcall GotMessage(int Message);

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

private:
	long m_cRef;
};

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

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

HRESULT CSink::QueryInterface(REFIID riid, void** ppv)
{
	if(riid == IID_IUnknown)
	{
		*ppv = (IUnknown*)this;
	}
	else if(riid == IID_IOutGoing)
	{
		cout << "Client: CSink::QueryInterface() for IOutGoing" << endl;
		*ppv = (IOutGoing*)this;
	}
	else 
	{
		*ppv = NULL;
		return E_NOINTERFACE;
	}
	AddRef();
	return S_OK;
}

HRESULT CSink::GotMessage(int Message)
{
	if(Message == (int)'b' || Message == (int)'B')
		PlaySound("BrockschmidtQuack", NULL, SND_RESOURCE|SND_ASYNC);
	cout << "CSink::GotMessage is " << (char)Message << endl;
	return S_OK;
}

void main()
{
	cout << "Client: Calling CoInitialize()" << endl;
	CoInitializeEx(NULL, COINIT_MULTITHREADED);
	
	IUnknown* pUnknown;
	cout << "Client: Calling CoCreateInstance()" << endl;
	CoCreateInstance(CLSID_InsideCOM, NULL, CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pUnknown);

	ISum* pSum;
	cout << "Client: Calling QueryInterface() for ISum on " << pUnknown << endl;
	HRESULT hr = pUnknown->QueryInterface(IID_ISum, (void**)&pSum);
	if(FAILED(hr))
		cout << "QueryInterface FAILED" << endl;

	int sum;
	pSum->Sum(17, 2, &sum);
	cout << "pSum->Sum(17, 2) = " << sum << endl;

	IConnectionPointContainer* pConnectionPointContainer;
	hr = pUnknown->QueryInterface(IID_IConnectionPointContainer, (void**)&pConnectionPointContainer);
	if(SUCCEEDED(hr))
	{
		IConnectionPoint* pConnectionPoint;
		hr = pConnectionPointContainer->FindConnectionPoint(IID_IOutGoing, &pConnectionPoint);
		cout << "FindConnectionPoint returns " << hr << " pConnectionPoint = " << pConnectionPoint << endl;

		CSink* mySink = new CSink;
		DWORD dwCookie;
		pConnectionPoint->Advise((IUnknown*)mySink, &dwCookie);

		cout << "Press any key to exit" << endl;
		_getch();

		pConnectionPoint->Unadvise(dwCookie);
		pConnectionPoint->Release();
		pConnectionPointContainer->Release();
	}

	cout << "Client: Calling Release() for pSum" << endl;
	hr = pSum->Release();

	cout << "Client: Calling Release() for pUnknown" << endl;
	hr = pUnknown->Release();

	cout << "Client: Calling CoUninitialize()" << endl;
	CoUninitialize();
}

⌨️ 快捷键说明

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