0

I have some existing C++ classes (including methods and #define constants, as well as a couple of extra C-style functions) that I would like to use in a new C# application. What's the best way to do this? I'd rather have everything linked into the one exe, so no DLLs if possible, but just linking into class libraries.

What's the best way to do this? Is it fairly straight forward, or am I better off porting my original code to C#?

2 Answers 2

3

As far as I know, your best bet may be to convert your code to C++/CLI, if you want to put them into your project/solution.

If you want to just call those C++ code as an external library, you can use P/Invoke. But my experience with it has been painful. Other than fairly trivial stuff, it has a steep learning curve. But there are really knowledgeable people regarding that subject on SO (like Hans Passant) that can help you.

Of course, you always have the option to rewrite everything in C#.

Sign up to request clarification or add additional context in comments.

5 Comments

I'm in the process of creating a C++ Class library under the CLR, because I assumed this was a good first step. The modules I'm working with were never organised as a .lib, so it seems logical to take this step and build this with the CLR.Will this be enough to get it all playing happily with C#?
@user1961169: Since you are starting to create I feel Managed C++ should be the best option. As, You can mix native and managed code within a single .dll, which is called a mixed-mode assembly. This is done by compiling some of the C++ files using /clr and some using normal C++
@user1961169, I'm sorry I can't help you much with that. I barely have any experience with C++, and no experience with C++/CLI. I can't do more than just looking for a link on Google and point you to it, which is something you can do. And one more note, if you decide to use your C++ code as an external library, you should check out swig.org (SWIG). It simplifies the process a lot AFAIK. I haven't used it personally but I have had a friend who used it with great success.
Couldn't manage to call C functions directly, but I managed to call some of the C-style functions by building a C++ wrapper class to encapsulate them in the class library, and another wrapper class in C# program to massage the data types, as my functions use char * and byte * parameters, which C# really doesn't like.
I have never had any problem using things like char* and byte* parameters. Where exactly are you having problems with? For example if your char* is a string, it can directly be passed to your C++ code as strings are blittable types.
1

You should be able to use PInvoke/DLLIMPORT to call any function in your C, C++.

Update 1:

I had a success in calling C function, but not C++ class/methods.

You would have to use Managed C++ to do this. This article should help..

Update 2: Rearranging as suggested by David.

For purely C style implementation you could use P/Invoke. Reference you could use LibGit2Sharp project. Which consume a native C.exe in C#. Here are the DllImport Calls Native Methods

Note: Here, you would have to ship the exe, it cannot be compiled into any other .Net assemblies.

11 Comments

is pinovke applicable to C++ class instances? say CPPClass a=new CPPClass; a.SomeMethod();? How to use dllimport for non-C style calls?
You might have to use __declspec(dllexport) on the c++ method.. msdn.microsoft.com/en-us/library/ms235636(v=vs.100).aspx
your link seems off topic, I am asking on C# side, how to use dllimport or pInvoke to create an instance of unmanaged object instance, and then call its method?
@David: It was to show how to create a reusable C++ library. Which can then be consumed using C#. In other words, only if you create a C++ function with _declspec only then C# can access it using P/Invoke..
I had a success in calling C function, but not C++ class/methods. Your comments still did not clearly answer this. Would you mind offering some links or examples on how to do this on C# SIDE, how to consume an unmanaged instance in C#, preferably in your updated answer? Thanks!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.