Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. COM
  4. Handling events with COM object as param

Handling events with COM object as param

Scheduled Pinned Locked Moved COM
comhelptutorial
6 Posts 4 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NotProfessional
    wrote on last edited by
    #1

    Hi, I am well done by responding COM events with simple data types( say VTS_I4). However, on topic that is not covered anywhere (and I spent days looking) is when the event has a COM object as a parameter. Say for instance in your sample that the addition is not on 2 integers but on 2 matrixes. The OnAdditionDone event would therefore pass a IMatrix * obj (VT_DISPATCH) pointer instead of an integer (VT_I4). How would you implement the event handler on the client side, and how would you actually start using the IMatrix pointer to access its members (tried everything I could think of, wither it doesn't compile or compiles but crashes, always get the 'first chance exception (OLEAUT32.DLL): 0xC0000005: Access Violation.). In other words, how to complete this: void CCoCaller::OnAdditionDone(IMatrix *result) { // How to implement this so that it works: BSTR str; result->get_Name(str); } MSDN gives 2 samples on how to implement IDispEventSimpleImpl and IDispEventImpl using Word and Excel veents, none of them show events that pass COM objects as parameters. Any help would be very useful. Thanks a lot :zzz::zzz::zzz:

    A V 2 Replies Last reply
    0
    • N NotProfessional

      Hi, I am well done by responding COM events with simple data types( say VTS_I4). However, on topic that is not covered anywhere (and I spent days looking) is when the event has a COM object as a parameter. Say for instance in your sample that the addition is not on 2 integers but on 2 matrixes. The OnAdditionDone event would therefore pass a IMatrix * obj (VT_DISPATCH) pointer instead of an integer (VT_I4). How would you implement the event handler on the client side, and how would you actually start using the IMatrix pointer to access its members (tried everything I could think of, wither it doesn't compile or compiles but crashes, always get the 'first chance exception (OLEAUT32.DLL): 0xC0000005: Access Violation.). In other words, how to complete this: void CCoCaller::OnAdditionDone(IMatrix *result) { // How to implement this so that it works: BSTR str; result->get_Name(str); } MSDN gives 2 samples on how to implement IDispEventSimpleImpl and IDispEventImpl using Word and Excel veents, none of them show events that pass COM objects as parameters. Any help would be very useful. Thanks a lot :zzz::zzz::zzz:

      A Offline
      A Offline
      Abhishek Srivastava
      wrote on last edited by
      #2

      Hi there , once i have tried the same also..and it worked fine. Suppose i have to pass IInterface instance to the client thru a Event method ThrowInter(IInterface *iFace) this is the code where i fired this event and passed the IInterface pointer. IInterface *iFace; CoCreateInstance(CLSID_Interface, NULL, CLSCTX_ALL, IID_IInterface, (void **) &iFace); Fire_ThrowInter(iFace); Now this is my client code where i caught the fired event and IInterface instance void CContainerDlgDlg::OnThrowInterTinterface1(LPDISPATCH interfac) { IInterface *iFace=reinterpret_cast(interfac); iFace->ShowMessage(); } but in client i have to include the header file of IInterface. and then simply reinterpret casting , i was able to access my IInterface methods and properties :) let me know if u r also doing the same :) Abhishek Srivastava Software Engg (VC++) India ,Noida Mobile no 9891492921 :)

      S N 3 Replies Last reply
      0
      • N NotProfessional

        Hi, I am well done by responding COM events with simple data types( say VTS_I4). However, on topic that is not covered anywhere (and I spent days looking) is when the event has a COM object as a parameter. Say for instance in your sample that the addition is not on 2 integers but on 2 matrixes. The OnAdditionDone event would therefore pass a IMatrix * obj (VT_DISPATCH) pointer instead of an integer (VT_I4). How would you implement the event handler on the client side, and how would you actually start using the IMatrix pointer to access its members (tried everything I could think of, wither it doesn't compile or compiles but crashes, always get the 'first chance exception (OLEAUT32.DLL): 0xC0000005: Access Violation.). In other words, how to complete this: void CCoCaller::OnAdditionDone(IMatrix *result) { // How to implement this so that it works: BSTR str; result->get_Name(str); } MSDN gives 2 samples on how to implement IDispEventSimpleImpl and IDispEventImpl using Word and Excel veents, none of them show events that pass COM objects as parameters. Any help would be very useful. Thanks a lot :zzz::zzz::zzz:

        V Offline
        V Offline
        Vi2
        wrote on last edited by
        #3

        I think that you should write BSTR str; result->get_Name( & str ); Or better CComBSTR str; result->get_Name( & str ); With best wishes, Vita

        1 Reply Last reply
        0
        • A Abhishek Srivastava

          Hi there , once i have tried the same also..and it worked fine. Suppose i have to pass IInterface instance to the client thru a Event method ThrowInter(IInterface *iFace) this is the code where i fired this event and passed the IInterface pointer. IInterface *iFace; CoCreateInstance(CLSID_Interface, NULL, CLSCTX_ALL, IID_IInterface, (void **) &iFace); Fire_ThrowInter(iFace); Now this is my client code where i caught the fired event and IInterface instance void CContainerDlgDlg::OnThrowInterTinterface1(LPDISPATCH interfac) { IInterface *iFace=reinterpret_cast(interfac); iFace->ShowMessage(); } but in client i have to include the header file of IInterface. and then simply reinterpret casting , i was able to access my IInterface methods and properties :) let me know if u r also doing the same :) Abhishek Srivastava Software Engg (VC++) India ,Noida Mobile no 9891492921 :)

          S Offline
          S Offline
          Steve S
          wrote on last edited by
          #4

          You really shouldn't do that, it's the kind of thing that gives COM a bad name. Better would be to get an IDispatch from the object and fire that, then use QueryInterface at the client end, or use IUnknown and do the same. If you remote (out of proc or off-machine) interfaces, then there is postively absolutely NO guarantee that your cast is safe, since a proxy/stub may well have only the same number of methods available in an IDispatch interface, and the rest of your interface's function table may/will be inadmissible. Steve S

          1 Reply Last reply
          0
          • A Abhishek Srivastava

            Hi there , once i have tried the same also..and it worked fine. Suppose i have to pass IInterface instance to the client thru a Event method ThrowInter(IInterface *iFace) this is the code where i fired this event and passed the IInterface pointer. IInterface *iFace; CoCreateInstance(CLSID_Interface, NULL, CLSCTX_ALL, IID_IInterface, (void **) &iFace); Fire_ThrowInter(iFace); Now this is my client code where i caught the fired event and IInterface instance void CContainerDlgDlg::OnThrowInterTinterface1(LPDISPATCH interfac) { IInterface *iFace=reinterpret_cast(interfac); iFace->ShowMessage(); } but in client i have to include the header file of IInterface. and then simply reinterpret casting , i was able to access my IInterface methods and properties :) let me know if u r also doing the same :) Abhishek Srivastava Software Engg (VC++) India ,Noida Mobile no 9891492921 :)

            N Offline
            N Offline
            NotProfessional
            wrote on last edited by
            #5

            The key is I do not have the source code for the COM nor the header though. So I cannot include the header. Thanks for your suggestions anyway.:wtf:

            1 Reply Last reply
            0
            • A Abhishek Srivastava

              Hi there , once i have tried the same also..and it worked fine. Suppose i have to pass IInterface instance to the client thru a Event method ThrowInter(IInterface *iFace) this is the code where i fired this event and passed the IInterface pointer. IInterface *iFace; CoCreateInstance(CLSID_Interface, NULL, CLSCTX_ALL, IID_IInterface, (void **) &iFace); Fire_ThrowInter(iFace); Now this is my client code where i caught the fired event and IInterface instance void CContainerDlgDlg::OnThrowInterTinterface1(LPDISPATCH interfac) { IInterface *iFace=reinterpret_cast(interfac); iFace->ShowMessage(); } but in client i have to include the header file of IInterface. and then simply reinterpret casting , i was able to access my IInterface methods and properties :) let me know if u r also doing the same :) Abhishek Srivastava Software Engg (VC++) India ,Noida Mobile no 9891492921 :)

              N Offline
              N Offline
              NotProfessional
              wrote on last edited by
              #6

              Thanks so much, Abhishek! I got it done, the trick is I need to declare the LPDISPATCH and convert to my desired COM refrence type explicitly. However, I declare the handler function the same as what is described in .tlh! I want to correct is that you don t have to get the COM header, what you need is simply import the dll and use the smart pointer.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups