0

I am generating a dll using c++-cli. The class in the dll looks like this:

ref class Locator

{
public:
Locator(int HP) : m_HP(HP) { } 
~Locator() 
{ } 
Dictionary<String^, array< Byte >^>^ Locate(Dictionary<String^, String^>^ imgParms) 
{ .....  }

private:
int m_HP;

How can I expose it to use it in c#? The class and the members are not being exposed in c#

fmvpsenior
  • 197
  • 7
  • 22
  • If this were a "normal" C/C++ program, you'd use Interop. Since this is a C++-CLI (managed C++) program, you should already *have* assemblies. Just reference them in your C# client. PS: I'm honestly curious why anybody would want to use C++ CLI (in contrast to VB.Net or C# for .Net, or standard C++ for portability and efficiency)? It's always baffled me... – paulsm4 Jul 10 '12 at 20:05
  • 1
    That should be `ref class Locator`. – Danny Varod Jul 10 '12 at 20:05
  • 3
    paulsm4: Sometimes, it's a matter of having native libraries to interop with, and writing the right P/Invoke and ComImport magic can be more difficult than wrapping the whole shebang in a C++/CLI `ref class`. :) – Rytmis Jul 10 '12 at 20:11
  • I have to use C/C++ because I am using OpenCV for image processing but I'll try the ref – fmvpsenior Jul 10 '12 at 20:11
  • So I added ref to class locator and now it doesn't compile: error C2664: 'cvReleaseCapture' : cannot convert parameter 1 from 'cli::interior_ptr' to 'CvCapture **' 1> with 1> [ 1> Type=CvCapture * 1> ] 1> Cannot convert a managed type to an unmanaged type – fmvpsenior Jul 10 '12 at 20:55
  • It compiles now but the class still cannot be seen in c# :/ – fmvpsenior Jul 10 '12 at 21:56

3 Answers3

4

You need to use the public keyword to make managed types visible to consumers of the assembly. Try

public ref class Locator { ... };
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
0

A question I asked a few months ago, regarding a similar issue :

How to use a C callback from C#?

Here are a few extra hints in my answer about the problem :

Calling C++ DLL from C++ works, but not from C#

I read you're using OpenCV, do you know Emgu CV: OpenCV in .NET (C#, VB, C++ and more) ? :-)

Community
  • 1
  • 1
aybe
  • 15,516
  • 9
  • 57
  • 105
-1

C++/CLI generates standard .NET managed assembly. That's the whole point of C++/CLI => compile an existing C++ code into a managed assembly without rewriting it to a CLS language.

So simply reference this assembly as any other .NET assembly in your consuming project. And then consume:

var imgParms = new Dictionary<string, string> { { "foo", "bar" }, { "baz", "bazinga" } };
var locator = new Locator(123);
var result = locator.Locate(imgParams);
// TODO: do something with the result dictionary
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928