COM event client using #import
Does anyone know of a sample showing use (sinking?) of an event by a client using #import but not ATL?
I have tried to search, but unfortunately search engines don't recognize the # in #import. Also I don't know how to navigate the articles in this web site to find the relevant articles and as I said searching does not help. I find samples that don't use #import and I find samples that use ATL and/or MFC, but as I recall MFC does not help much for COM stuff that it does not have support for in the MFC libraries.
Yes, it has been a few years since I was in these forums. I was the first to get 10,000 posts, nearly all attempts to help others. I hope I can get some guidance now.
Re: COM event client using #import
This article covers the basics using only built-in compiler COM support stuff: http://www.codeproject.com/KB/COM/TEventHandler.aspx
Should at least give you better search terms to use :)
gg
1 Attachment(s)
Re: COM event client using #import
Attached please find the sample of handling COM events. The sample includes a trivial COM server which fires event back on calling COM method. The client uses no ATL features for implementing event sink, just regular COM smartpointers.
Re: COM event client using #import
Please also take into account, the sample does not provide complete error handling for the sake of code simplicity. And in a real project every _ASSERT(SUCCEEDED(hr)) has to be turned to if (SUCCEEDED/FAILED(hr)) clause.
Code:
int _tmain(int argc, _TCHAR* argv[])
{
CTestEventImpl sink;
ITestPtr ptest;
HRESULT hr = ptest.CreateInstance(__uuidof(Test));
if (SUCCEEDED(hr))
{
// find connection point
IConnectionPointPtr pcp;
IConnectionPointContainerPtr pcpc;
hr = ptest->QueryInterface(IID_IConnectionPointContainer, (void**)&pcpc);
_ASSERT(SUCCEEDED(hr));
hr = pcpc->FindConnectionPoint(__uuidof(_ITestEvent), &pcp);
_ASSERT(SUCCEEDED(hr));
// attach event sink
DWORD cookie;
hr = pcp->Advise(&sink, &cookie);
_ASSERT(SUCCEEDED(hr));
// call method
hr = ptest->Test1(_bstr_t("Hello COM Events World!\n"));
_ASSERT(SUCCEEDED(hr));
// clean up
hr = pcp->Unadvise(cookie);
_ASSERT(SUCCEEDED(hr));
}
return 0;
}