Search code examples
c#c++-climanaged-c++

Can a managed C++ assembly return an object to C#?


I'm writing a C# application that must read the video properties of video files. The only way I've found to do this is with the Microsoft Media Foundation which requires C++.

So far, I've made some progress:

  • I've created a managed C++ assembly which compiles to a DLL.
  • I can call it, with a parameter, from the C# code.
  • It executes and prints the video file properties.

What I would like to do next is have the DLL return an object of video properties (width, height, duration, etc.). Given I'm using C++ managed code, is there a simple way to define an object type and use it to pass data between C# and C++ or do I have to use the Marshal class?


Solution

  • Certainly! If you define a public object in your managed C++ (Also called C++/CLI):

    public ref class MyManagedClass{
    . . .
    }
    

    and then reference the dll from your c# project, you'll be able to use the object just like you had defined it in c#.