3

I have a small C# class (Gram.cs) and it is working with a 3rd party dll to operate a device. Now, I need to call some functions in this C# class using C++.

How can I do it?

I am using MS Visual Studio 2010 professional edition.

6
  • There is a question similar: stackoverflow.com/questions/778590/calling-c-sharp-code-from-c Commented May 10, 2013 at 18:25
  • 2
    Looking into your scenario don't you really need to call C++ function from the C# class? Commented May 10, 2013 at 18:26
  • @gustavodidomenico: no, C# from C++ Commented May 10, 2013 at 18:38
  • @pmni: How can I add the C# file into the project? Commented May 10, 2013 at 18:44
  • If C# class is small, and it deals with native dll, it might by simpler rewrite that class in C++, then integrate .Net with your app. Commented May 10, 2013 at 19:05

3 Answers 3

3

If C# class is small, and it deals with native dll, it might by simpler rewrite that class in C++, rather then integrate .Net into your application. In addition, integrating .Net will force whole .Net virtual machine to start just for processing your simple class.

Otherwise,

  • You could use COM interop: build an assembly based on your C# class and make it COM visible. Then you could use class as a COM-object.
  • You can use Managed C++ to make a wrapper between managed and unmanaged code.
  • You can reverse control flow, so C# code will call unmanaged functions. In this case you can export it from your binary and import from C# code with DllImport attribute.
Sign up to request clarification or add additional context in comments.

Comments

2

Normally you should make your c# code as COM visible from your c# project settings and use c# IJW regasm tool.

Look into http://msdn.microsoft.com/en-IN/library/ms173185.aspx

I had integrated c# into c++ using this approach few years ago.

You will be able to load your c# assembly as a COM component in your c++ code.

Comments

1

This might be what you're looking for (I've answered similiar question here before: how to : use c++ projects for windows phone (C#))

a) Load symbols directly from C library, for example:

using System;
using System.Runtime.InteropServices;
static class win32
{
    [DllImport("kernel32.dll")]
    public static extern IntPtr LoadLibrary(string dllToLoad);

    [DllImport("kernel32.dll")]
    public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);

    [DllImport("kernel32.dll")]
    public static extern bool FreeLibrary(IntPtr hModule);   
}

(this is taken from: http://www.andyrushton.co.uk/csharp-dynamic-loading-of-a-c-dll-at-run-time/ after brief googling)

You need to export the C++ interface methods as "C" for that, e.g.:

extern "C" __declspec( dllexport ) int MyFunc(long parm1);

(from MSDN: http://msdn.microsoft.com/en-us/library/wf2w9f6x.aspx)

b) Use a wrapper in C++/CLI to connect unmanaged C++ to managed C#:

here's a good example: http://www.codeproject.com/Articles/19354/Quick-C-CLI-Learn-C-CLI-in-less-than-10-minutes

Do note that the syntax is somewhat weird at first look, and not eveything can be used. However, what you gain is ability to use the C++ classes - something that exporting "C" prohibits you from doing.

2 Comments

Isn't this the exact reverse of what the question asked?
@JohnDemetriou that's correct, since it was a while ago I'm not sure why I answered it like this. Quick look through the comments to the question suggests someone mentioned rewriting the C#-interface-y part in C++, the calling that from C#, so that might be it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.