0
votes

I've been working recently in COM interop thing.Read some Good books about it C++/CLI in Action By Nishant Shivakumar. I also went through this link by Srinivas wherein he has explained beautifully how to Consume C# libraryin native C++ using C++/CLI.I downloaded the code present at the bottom of the page..Read it..And loved it.My application is loosely based on it. I have a question though in that example(which you can download it there). In C# Library "Worker.cs",If I have a method in the Worker class with a signature like this::

public void GetStudent(ManagedStudent student,int ){....}

FYI:: I need the object parameter because I would be accessing a method from that class in the Worker Class.

In C++/CLI wrapper project, NativeInterface.h, I wanted to export a method which gets this managed object as a parameter unlike the example.

__declspec dllexport void GetStudent(ManagedStudent^ obj)

What is the equivalent of an object in C++/CLI?

And later I want to access this method by importing this method by passing an object in my native Win32 Application/DLL. I am including the "NativeInterface.h" file in my native dll/App . But an error comes while building the dll/App..managed targeted code requires \clr option.

FYI:: 1. My WIN32 dll/app is being compiled with no \clr support and my wrapper with \clr support 2. I believe the error comes because of the handle used as a parameter? I want to access UnmanagedStudent(Object obj) in native app/dll; Is that possible

1
You could define your ManagedStudent as a COM Object so every language in the world supporting COM can access it quite naturally. - Simon Mourier

1 Answers

0
votes

It's possible, but you can't pass a .NET object to a native DLL, which is a native C++ code... The equivalent of an .NET object in native C++ is C++ object... but this is like asking a C# to understand Native or PHP, or.... So, there are several approaches.

Don't use C++ managed wrapper. Instead using Marsahling and C# interoperability directly call into native C++, in 1 of 2 ways:

  1. Pass the object fields as a long list of parameters to the C++ and re-construct the object inside native C++. A bit "not nice" approach but very efficient and easy to debug.
  2. You can also pass the object as a class pointer to the C++ native directly, you need first to create the right marshaling in C#. See here: http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal(v=vs.110).aspx

Use C++ Interop (which you are doing...). You need to compile with Mixed mode! See this nice example: http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/f2de75c5-50a6-41ac-9ddf-694a8c0ce75b/ BUT... you need to understand the basics and know how to compile it: http://msdn.microsoft.com/en-us/library/x0w2664k.aspx

Interoperability is a complex issue... and I try to write a short answer, please revert to me for further clarifications....