Answered Implement C++ Callback in C#

  • Friday, August 31, 2012 4:15 PM
     
      Has Code

    Trying to implement the following C++ callback method in to C#: http://msdn.microsoft.com/en-us/library/windows/desktop/gg583871%28v=vs.85%29.aspx

    How do I implement the SourceReader pCallback pointer in C#? Should it be a pointer to the form? See the following pseudocode.

    public partial class myForm : Form, IMFSourceReaderCallback
    {
            int IMFSourceReaderCallback.OnReadSample()
            {            
                // Do something
            }
    
            int IMFSourceReaderCallback.OnEvent()
            {
                // Do something
            }
    
            int IMFSourceReaderCallback.OnFlush()
            {
                // Do something
            }
    
            int readMediaFile()
            {
                IMFSourceReader pReader = null;
    
                pCallback; // What should this be and where should it point to?
    
                // Do something
            }
    }

All Replies

  • Friday, August 31, 2012 4:55 PM
     
      Has Code

    Trying to implement the following C++ callback method in to C#: http://msdn.microsoft.com/en-us/library/windows/desktop/gg583871%28v=vs.85%29.aspx

    How do I implement the the callback object: SourceReader pCallback in C#? Should it be a pointer to the form handle? See the following pseudocode. Want to get in to the OnReadSample.

    public partial class myForm : Form, IMFSourceReaderCallback { int IMFSourceReaderCallback.OnReadSample() { // Do something } int IMFSourceReaderCallback.OnEvent() { // Do something } int IMFSourceReaderCallback.OnFlush() { // Do something } int readMediaFile() { IMFSourceReader pReader = null; pCallback; // What should this be and where should it point to?

    // Do something } }



  • Friday, August 31, 2012 5:02 PM
     
     
  • Friday, August 31, 2012 5:15 PM
    Moderator
     
     Answered

    You aren't actually using a callback in this case.  In order to get what you want you have to define a type that implements the specified callback interface.  Your form can't do that because the type has to be exposed to COM and it must implement the COM interface, not the .NET interface. 

    To get the COM interface in a .NET version your best bet is to import the appropriate typelib into your project.  That will generate the .NET version of the COM interface.  From there you'll create a type that implements the interface and is marked as COM visible, and has a class GUID and all the other stuff that is required for COM interop.  Please refer to MSDN where all the steps are listed.  Search for COM Interop.

    Once you've created your COM-visible type with the appropriate COM-defined interface then you can pass the instance of your type to the method and it'll work correctly.

    Michael Taylor - 8/31/2012
    http://msmvps.com/blogs/p3net

  • Friday, August 31, 2012 7:08 PM
     
     

    You aren't actually using a callback in this case.  In order to get what you want you have to define a type that implements the specified callback interface.  Your form can't do that because the type has to be exposed to COM and it must implement the COM interface, not the .NET interface. 

    To get the COM interface in a .NET version your best bet is to import the appropriate typelib into your project.  That will generate the .NET version of the COM interface.  From there you'll create a type that implements the interface and is marked as COM visible, and has a class GUID and all the other stuff that is required for COM interop.  Please refer to MSDN where all the steps are listed.  Search for COM Interop.

    Once you've created your COM-visible type with the appropriate COM-defined interface then you can pass the instance of your type to the method and it'll work correctly.

    Michael Taylor - 8/31/2012
    http://msmvps.com/blogs/p3net

    Would you be able to provide an example of how to create a type that implements a callback interface, please?
  • Saturday, September 01, 2012 5:21 PM
    Moderator
     
     

    As I already mentioned you just need to import the COM typelib into your project and the IDE will generate the necessary code.  But what I found online is that it is a difficult API to get working in .NET.  Here's a link to a posting about it: http://social.msdn.microsoft.com/forums/en-US/mediafoundationdevelopment/thread/1d9c32c4-e986-43c0-8adb-2544e5a9d196/.

    I recommend that you check out the wrapper class someone else has already written so you don't have to yourself unless you're very comfortable with COM and .NET interop.  I can't confirm how good the wrapper is but it will at least give you some good reference material on how to get things working: http://mfnet.sourceforge.net/

    Michael Taylor - 9/1/2012
    http://msmvps.com/blogs/p3net

  • Wednesday, September 05, 2012 6:28 PM
     
     

    As I already mentioned you just need to import the COM typelib into your project and the IDE will generate the necessary code.  But what I found online is that it is a difficult API to get working in .NET.  Here's a link to a posting about it: http://social.msdn.microsoft.com/forums/en-US/mediafoundationdevelopment/thread/1d9c32c4-e986-43c0-8adb-2544e5a9d196/.

    I recommend that you check out the wrapper class someone else has already written so you don't have to yourself unless you're very comfortable with COM and .NET interop.  I can't confirm how good the wrapper is but it will at least give you some good reference material on how to get things working: http://mfnet.sourceforge.net/

    Michael Taylor - 9/1/2012
    http://msmvps.com/blogs/p3net

    I am already using the wrapper but don't know how to create and assign pCallback.
  • Thursday, September 06, 2012 1:19 AM
    Moderator
     
     

    You'll create a COM-visible type that implements the specified COM interface.  Then you'll pass your object to the method in COM that will call your code back as it does its thing.  Or, just use the wrapper that somebody else already wrote.  That is the easiest approach.  COM callback interfaces aren't the easiest thing to work with so the wrapper is really the best way to go.  I strongly recommend that you consider the wrapper unless you are an expert in COM because debugging COM issues is not easy.

    Michael Taylor - 9/5/2012
    http://msmvps.com/blogs/p3net