Link to home
Start Free TrialLog in
Avatar of lavitz
lavitzFlag for Poland

asked on

Wrap com interfaces


I need advice in question - is my atl+com project going in correct way.

I have to use some two Com interfaces. One of them I need to implement. So I created it by atl wizzard, adding Simple ATL objects and implement interface one of them. I decided to encapsulate/wrap them by making next simple atl object with two functions - run and stop
Run function do CoCreateInstance on some interfaces, set some options and other things.
I decided to do so for simplicity ( I dont know am i right).

At the end I have made windows dll project that create instance of my interface and fire run function.
Avatar of pgnatyuk
pgnatyuk
Flag of Israel image

I read your question twice and do not know what to say and where you need an advice.
Anyway you need a test application to verify your com. So begin from it and you will see what and how can be improved in your com - you will use these interfaces and will understand what is convenient and what has to be updated.

Few useful links, if you need:
CodeProject. ATL Under the Hood
http://www.codeproject.com/KB/atl/atl_underthehood_.aspx

CodeProject. Beginner's Tutorial: COM/ATL Simple Project
http://www.codeproject.com/KB/atl/com_atl.aspx
Avatar of lavitz

ASKER

Its hard to explain.
ok, I have Com app that works fine. It have implemented ISensNetwork interface. Now I dont known how to add this to other part of project. I mean, how to catch this moment where event occurs. I have idea to deliver some function or object that will be fired in event function(ConnectionMade). But then, the whole logic is inside com part. I need some solution that throw this event outside my Com interface


class CMySensNetwork2 (...CUT...)
{
public:
	(...CUT...)
public:


	// ISensNetwork Methods
public:
	STDMETHOD(ConnectionMade)(BSTR bstrConnection, unsigned long ulType, SENS_QOCINFO * lpQOCInfo)
	{
		Beep(50, 80);
// this function execute when event occur
		return E_NOTIMPL;
	}
	STDMETHOD(ConnectionMadeNoQOCInfo)(BSTR bstrConnection, unsigned long ulType)
	{
		return E_NOTIMPL;
	}
	STDMETHOD(ConnectionLost)(BSTR bstrConnection, unsigned long ulType)
	{

		return E_NOTIMPL;
	}
	STDMETHOD(DestinationReachable)(BSTR bstrDestination, BSTR bstrConnection, unsigned long ulType, SENS_QOCINFO * lpQOCInfo)
	{
		return E_NOTIMPL;
	}
	STDMETHOD(DestinationReachableNoQOCInfo)(BSTR bstrDestination, BSTR bstrConnection, unsigned long ulType)
	{
		return E_NOTIMPL;
	}
};



// ---- in main.cpp


int _tmain(int argc, _TCHAR* argv[])
{

	// TODO: Add your implementation code here

	IEventSubscription*         pIEventSubscription = 0;
	static IEventSystem*		pIEventSystem =0;
	static IMySensNetwork2*		pSubscriber;


	HRESULT hr = 0;

	hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);

	hr = CoCreateInstance(
		CLSID_MySensNetwork2,
							NULL,
							CLSCTX_SERVER,
							IID_IMySensNetwork2,
							(void**)&pSubscriber);

	//-----------------

	if (FAILED(hr))
	{
		return FALSE;
	}

	//----------------


	hr = CoCreateInstance( CLSID_CEventSystem, 0, CLSCTX_SERVER, IID_IEventSystem, (void**)&pIEventSystem);
	if (FAILED(hr))
	{
		return FALSE;
	}

	// Get a new IEventSubscription object.
		hr = CoCreateInstance(
								CLSID_CEventSubscription,
								NULL,
								CLSCTX_SERVER,
								IID_IEventSubscription,
								(LPVOID *) &pIEventSubscription
								);

		if (FAILED(hr))
		{		
			return FALSE;
		}

/*
		hr = pIEventSubscription->put_SubscriptionID(OLESTR("{cd1dcbd6-a14d-4823-a0d2-8473afde360f}"));
		if (FAILED(hr))
		{
			return FALSE;
		}
*/

		hr = pIEventSubscription->put_SubscriptionName(OLESTR("Connection 1"));
		if (FAILED(hr))
		{
			return FALSE;
		}

		hr = pIEventSubscription->put_MethodName(OLESTR("ConnectionLost"));
		if (FAILED(hr))
		{
			return FALSE;
		}

		//SENSGUID_EVENTCLASS_NETWORK
		hr = pIEventSubscription->put_EventClassID(OLESTR("{d5978620-5b9f-11d1-8dd2-00aa004abd5e}"));
		if (FAILED(hr))
		{
			return FALSE;
		}


		hr = pIEventSubscription->put_SubscriberInterface(pSubscriber);
		if (FAILED(hr))
		{
			return FALSE;
		}

		hr = pIEventSystem->Store(PROGID_EventSubscription, pIEventSubscription);
		if (FAILED(hr))
		{
			return FALSE;
		}

		pIEventSubscription->Release();
		pIEventSubscription = NULL;
		pSubscriber->Release();

		WaitMessage();

	return 0;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of pgnatyuk
pgnatyuk
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial