CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266

    Question 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.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  2. #2
    Join Date
    Nov 2003
    Posts
    1,902

    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

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    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.
    Attached Files Attached Files
    Last edited by Igor Vartanov; May 27th, 2011 at 04:09 AM.
    Best regards,
    Igor

  4. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    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;
    }
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured