Quantcast
Channel: Common Language Runtime Internals and Architecture forum
Viewing all articles
Browse latest Browse all 1710

How to pass unmanaged C++ object to C# library and call methods on C++ objects from C# code...

$
0
0

I'm new to C++ and COM Interop and will appreciate your help in pointing me to samples/posts that achieve the objective of passing a unmanaged C++ object to C# library and calling methods on C++ object from C# library.

Earlier we used to have the implementation defined as below:

//C# delegate signature
public delegate void HandleMessage(MyCustomMessageTypeEnum msgEnum, MyCustomMessage msg);
//C# interface definition
public interface IMyCustomMessageProcessor
{
    void initialize(string clientId, [In, MarshalAs(UnmanagedType.FunctionPtr)] HandleMessage msgCallback);
}

The component has evolved since then and we need to perform additional new functionality other than just "HandleMessage" functionality. So instead of having several different delegates defined for the interacting with C++ and C# components and overloading the "initialize(string clientId, [In, MarshalAs(UnmanagedType.FunctionPtr)]HandleMessage msgCallback)" method signature, I'm thinking of defining another interface that will do "HandleMessage" and new additional functionality. So the "initialize" method call will take in an object that implements the "IMyNewAdditionalFunctionality" as a parameter as shown below:

//C# new additional functionality
public interface IMyNewAdditionalFunctionality
{
     void DoWork1(string test);
     void DoWork2(string test, MyCustomMessageTypeEnum msgEnum, int id);
     void HandleMessage(MyCustomMessageTypeEnum msgEnum, MyCustomMessage msg);
}

and now "IMyCustomMessageProcessor" will be changed to:

public interface IMyCustomMessageProcessor
{
    void initialize(string clientId, [What attribute definition do I need here for UnmanagedType??] IMyNewAdditionalFunctionality newAddedFunc);
}

I was wondering:
1. How do I pass unmanaged or managed object to the new "initialize" method.
2. Should I add several different delegates to "initialize" method or should I go with the interface "IMyNewAdditionalFunctionality" approach as defined above. What are pros/cons of each.
3. If somebody could point me to C++ sample code that will create objects of type IMyNewAdditionalFunctionality and IMyCustomMessageProcessor and pass in the "IMyNewAdditionalFunctionality" to "intialize" method would be great.


Viewing all articles
Browse latest Browse all 1710

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>