How to wrap unmanaged C++ code so as to use it from C#

L

Lonifasiko

I must use some unmanaged C++ code functionality from my C# managed
application. I do only have .cpp and .h files for that piece of code,
nothing more.

I understand I should wrap these files into a C++ project type so that
later I can reference this project's output form my managed
environment. Which project type should I choose? I assume the simplest
one, without any dependencies if possible.

I would really appreciate any help on this.

Thanks very much in advance.

Miguel
Blog: http://lonifasiko.blogspot.com
 
V

vvnraman

I must use some unmanaged C++ code functionality from my C# managed
application. I do only have .cpp and .h files for that piece of code,
nothing more.

I understand I should wrap these files into a C++ project type so that
later I can reference this project's output form my managed
environment. Which project type should I choose? I assume the simplest
one, without any dependencies if possible.

I would really appreciate any help on this.

Thanks very much in advance.

Miguel
Blog:http://lonifasiko.blogspot.com

Hi Lonifasiko
You have to chose to create a Class Library project.
The class library project will contain the .cpp and .h files which you
have.
When you build the project, a dll will be created in the <Project Dir>/
bin/debug folder.
Suppose the namespace you're using in this project is "cppdll".

Now to use this project in a managed app, say a C# app, Right click on
the managed app,
select "Add Reference".
Now select the Browse tab and locate the dll of the Class Library
project you created earlier and press open.
The Class Library project will be added as a reference in your managed
app.
Before you use any class from the Class Library project you'll have to
write "using cppdll", i.e. the namespace
to which the Class Library project belongs.

Hope this helps.
Tell me if you need an example project.
Prateek
 
L

Lonifasiko

Thanks, in fact, I was using the approach you describe vnraman.
Nevertheless, I'm having some problems when defining objects and
methods in the header file.

It's a struct type which is making me trouble......I've got a struct
type defined inside the C++ class and compiler tells me "a native type
cannot be nested within a managed type". Have a look at this:

public ref class Calculator
{
// public definitions ...........
private:
struct Params
{
double x, y;
Params(void);
Params(double _x, double _y);
};
}

Thanks very much in advance.

Miguel
Blog: http://lonifasiko.blogspot.com
 
L

Lonifasiko

Finally separated the unmanaged structure "struct" in another class
preceeded with #pragma unmanaged directive. That is, I finally access
from C# only one method of the managed C++ class, that internally uses
the other C++ unmanaged class. I understand I finally developed a kind
of wrapper.........

Thanks and regards.

Miguel
Blog: http://lonifasiko.blogspot.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top