-1

I have two managed c++ classes . Which is used to communicate with i2C device. I need to use them in c#. i have 2 ".h" files which have source and declaration. Do I need to convert it to c# or if I am creating a DLL from that how can I do that?

code is sth like :

public ref class Class1 
{
   AnotherClass object = new Anotherclass();
   Method1();
   method2()
}

public ref class AnotherClass
{
}
msam
  • 4,259
  • 3
  • 19
  • 32
Zigma
  • 529
  • 6
  • 37

3 Answers3

1

Add the C++/CLI and the C# project into the same solution. Then add a reference from the C# project to the C++/CLI project

msam
  • 4,259
  • 3
  • 19
  • 32
1

The point of using a ref class is to let the C++/CLI compiler generate metadata for the class into the assembly. No need for a .h file, any .NET compiler can read that metadata and use that class.

Just add the reference in your C# project. Best done by having both the C++/CLI project and the C# project in the same solution so you can use a project reference. Right-click the C# project, Add Reference, Project tab.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • You mean add the c++ project to C#? it is a test app project with the source files included – Zigma Oct 01 '13 at 12:05
  • The output is an exe in the case of testapp. Then how could I get the metadata in c# (asking since i dont have any idea about this) – Zigma Oct 01 '13 at 12:09
  • Thats not I said about. The C++/CLI project is meant to be test ap . Adding that into the solution will do something fruitful? and what inside the C++/CLI project will be reffered to c# project – Zigma Oct 01 '13 at 12:28
  • Anyways.Project tab is empty – Zigma Oct 01 '13 at 12:31
  • I doubt you'll get anywhere until you figure out how to create a solution that has two projects. One of them your C++/CLI project that generates a DLL, the other a C# test app. So you'll actually test what you said you wanted to do in your question. This is a pretty basic task, there are many tutorials and introductory books that show you how to use Visual Studio. – Hans Passant Oct 01 '13 at 12:34
0

Simply write a wrapper of these functions in C++ (CLI), and it can then be visible and consumed by .NET by creating the relevant references in the project.

Dave
  • 8,163
  • 11
  • 67
  • 103
  • This wrapper thing is confusing(at least for this code). It uses objects and I need them above in my c# code – Zigma Oct 01 '13 at 12:06