title: Handle C# COM events in C++
body: <p>I have managed to create a C# COM object with events. Please find code below,</p>
  
  <pre><code>    [Guid("1212674-38748-45434")]
      public interface ICalculator
      {
          int Add(int Num1, int Num2);
      }
  
      [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
      [Guid("3453674234-84444-84784")]
      public interface ICalculatorEvents
      {
          [DispId(1)]
          void Completed(int Result);
      }
  
      [ClassInterface(ClassInterfaceType.None)]
      [ComSourceInterfaces(typeof(ICalculatorEvents))]
      [Guid("87457845-945u48-4954")]
      public class Calculator : ICalculator
      {
          public delegate void CompletedDelegate(int result);
          public event CompletedDelegate Completed;
          public Add(int Num1, int Num2)
          {
              int Result = Num1 + Num2;
              if(Completed != null)
                  Completed(Result);
          }
      }
  </code></pre>
  
  <p>I have imported this COM object in a C++ console application and able to call the 'Add()' method. I am not sure how to handle 'Completed' event in my C++ application. Can you please advise on this? I am looking to display the result value in console whenever this event occurs. </p>
  
  <p>Please find C++ application's code below. The Event 'Completed' never gets handled here. This goes into an infinite loop.</p>
  
  <pre><code>    #import "Calculator.tlb"
      using namespace Calculator;
      int Flag = 0;
      class HandleEvent : public ICalculatorEvent
      {
          public:
              HandleEvent(void);
              ~HandleEvent(void);
              HRESULT __stdcall QueryInterface(const IID &amp;, void **);
              ULONG __stdcall AddRef(void) { return 1; }
              ULONG __stdcall Release(void) { return 1; }
              HRESULT __stdcall Completed(int Result);
      };
  
      HandleEvent::HandleEvent(void)
      {
      }
  
      HRESULT HandleEvent::Completed(int Result)
      {
          printf("Addition Completed, Result: %d", Result);
          Flag = 1;
      }
  
      HRESULT HandleEvent::QueryInterface(const IID &amp; iid,void ** pp)
      {
          if (iid == __uuidof(ICalculatorEvent) || iid == __uuidof(IUnknown))
          {
              *pp = this;
              AddRef();
              return S_OK;
          }
          return E_NOINTERFACE;
      }
  
      int _tmain(int argc, _TCHAR* argv[])
      {
          CoInitialize(NULL);
          Flag = 0;
          ICalculatorPtr pCalc(__uuidof(Calculator));
          pCalc-&gt;Add(5, 6);
  
          do
          {
          }while(Flag == 0);
  
          CoUninitialize ();
          return 0;
      }
  </code></pre>
  
  <p>Thanks in advance.</p>