2

I'm a Linux C++ developer, and I need to write C++ dlls for windows, which is to be used in C# applications.

The problem is importing the DLLs into C#, which I have no idea how to do it. In my friends' project its probable that any kind of unmanaged dll will be used, and I'm charge of doing this :-D

I need to import all objects and functions in the DLL, and my search has led me to nothing more than DllImport and so.

Thanks so much for helps.

Haix64
  • 806
  • 1
  • 13
  • 21
  • possible duplicate of [C++/CLI Mixed Mode DLL Creation](http://stackoverflow.com/questions/2691325/c-cli-mixed-mode-dll-creation) – Hans Passant Apr 17 '12 at 04:29

5 Answers5

2

You can use C++/CLI as a wrapper for your unmanaged C++.For more info on C++/CLI and what it does you can use this link

http://www.functionx.com/cppcli

You can have a quick look at this

http://www.codeproject.com/Articles/19354/Quick-C-CLI-Learn-C-CLI-in-less-than-10-minutes#A8

manu_dilip_shah
  • 880
  • 14
  • 32
  • Thanks for the link. And does it mean that I must touch my C++ code to conform with CLI? I just compiled my C++ code again in VC++ with the clr option enabled, and I its possible now to import it in C# but I don't see it's contents there in object browser. What to do? – Haix64 Apr 17 '12 at 05:23
  • Export the class of c++ which you want to use in c# through dllexport and then use the lib file generated as an external dependency in your C++/CLI project.The make C++/CLI project as a dll and write wrapper class for your C++ code .Use this managed dll in your C# code – manu_dilip_shah Apr 17 '12 at 05:41
  • Well, I found the answer I was looking for, here: http://msdn.microsoft.com/en-us/library/ms173265(v=vs.80).aspx, and I can simply precede the class definition with **public** and access it in C#, but public doesn't work with functions, defined in global namespace besides classes. How may I do that? – Haix64 Apr 17 '12 at 06:16
  • Yes newbie, you're right. And I found the way to do it as I asked in another question after this one. Thanks anyway. – Haix64 Apr 17 '12 at 10:49
0

you want DllImport

There's a bunch of info here :-

http://msdn.microsoft.com/en-us/library/aa288468(v=vs.71).aspx

and lots lots more all over google, and many stackoverflow questions related to DllImport

Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
  • That's right, but DllImport doesn't import objects, and what am I supposed to do in case of dlls including objects with big definitions? lots of member functions, etc.? – Haix64 Apr 17 '12 at 04:24
  • then your better option is to go for compiling your C++ as CLR code. But even then, you have to do a bit of work as not all C++ basic types nicely interface with .NET. – Keith Nicholas Apr 17 '12 at 04:27
0

There are many ways to go about this. You can write a CLR dll in C++ which puts an interface that C# can directly "talk" to. This is a nice option cause you can keep native C++ still and not have all of your code be CLR based.

Do some searches for C++/CLI

You can also use dllimport and friends and create a standard dll.

Brian
  • 1,164
  • 1
  • 9
  • 27
0

Have you thought to keep the Dll's in C++ (port then recompiled under VS.NET) less effort than a C# port.

I have in the past made a shared memory DLL to allow LabVIEW (Windows7) and a Winforms C#.NET appl. to share data storage via this dll.

Ian Finlay
  • 121
  • 2
  • 9
0

Take a look at this tutorial. It shows two methods for accomplishing what you want: How to Marshal a C++ Class

Ove
  • 6,227
  • 2
  • 39
  • 68