How to Call a .Net Assembly from C/C++

How do I call a .NET assembly from C/C++?

[Guid("123565C4-C5FA-4512-A560-1D47F9FDFA20")]
public interface IConfig
{
[DispId(1)]
string Destination{ get; }

[DispId(2)]
void Unserialize();

[DispId(3)]
void Serialize();
}

[ComVisible(true)]
[Guid("12AC8095-BD27-4de8-A30B-991940666927")]
[ClassInterface(ClassInterfaceType.None)]
public sealed class Config : IConfig
{
public Config()
{
}

public string Destination
{
get { return ""; }
}

public void Serialize()
{
}

public void Unserialize()
{
}
}

After that, you need to regasm your assembly. Regasm will add the necessary registry entries to allow your .NET component to be see as a COM Component. After, you can call your .NET Component in C++ in the same way as any other COM component.

Calling C# from C

There is more than just COM interop if you want to call into managed code from C or C++. The are also the following lesser known methods (taken from MSDN FAQ):

How do I call a .NET assembly from native Visual C++?

There are basically four methods to
call .NET assembly from native VC++ code:

  1. CLR Hosting API: Native VC++ module calls CLR Hosting APIs to host CLR, load and call the .NET assembly (sample code: CppHostCLR).

  2. COM Interop: If the .NET assembly can be exposed as a COM component, native VC++ module can call into the .NET assembly through .NET – COM interop (sample code: CppCOMClient).

  3. Reverse PInvoke: The managed code calls native code passing a delegate that the native code can call back (sample code: CSPInvokeDll).

  4. C++/CLI: If the module containing native VC++ code is allowed to enable CLR, the native VC++ code can call
    .NET assembly directly (sample code: Consuming C# Library in native C or C++ using C++/CLI)

Accessing a .NET Assembly from classic ASP

Another thing to check: make sure your .Net assembly is set to be COM Visible.

How to reuse a .Net assembly from a pure C application

I assume your C# class is a static class. You need to create an interop layer in C++/CLI before you can use it in pure C. Create a C++/CLI class to wrap your C# class. Once that is done use the export function to export the specific C functions. C++/CLI will manage the interop on your behalf. The rule of thumb is if you class/function has any CLI it WILL BE CLI. So your extern functions should only return native datatypes.

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

Here is an article to help you get started. It converts C++ to C# but the process is reversed in your case. CodeProject Unfortunately there is no convenient reverse PInvoke for pure C.

Unfortunately I have never gone from C# to C. It sounds like an interesting project. Good luck!

Ok If you have not figured it out yet i have a quick sample for you.

C# CSLibrary.Math.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSLibrary
{
static public class Math
{
static public int Add(int a, int b)
{
int c = a + b;
return c;
}
}
}

Cpp/C++ Project CPPCLibrary.h (Compiled with C++/CLI Option with project dependencies)

#pragma once

using namespace System;

extern "C" __declspec( dllexport ) int MathAdd(int a, int b)
{
return CSLibrary::Math::Add(a, b);
}

C Project CTest.c (Compiled as C Code)

#include "stdafx.h"
#pragma comment(lib, "../Debug/CPPCLILibrary.lib")

extern __declspec( dllimport ) int MathAdd(int a, int b);

int _tmain(int argc, _TCHAR* argv[])
{
int answer = MathAdd(10, 32);
_tprintf(_T("%d\n"), answer);
return 0;
}

All files are in the same solution but different projects. I have confirmed this has worked. I hope this helps anyone who comes across it.

Cheers!

How to call .NET (C#) code from a native C(++) DLL?

While the link provided by SLaks to the Unmanaged Exportsutility might or might not work, we used a similar tool (from a different source) here and due to problems with signed executables have abandoned this approach.

We have concluded the following and will do this in the future:

The correct way to make .NET callbacks available to pure native modules is to write a C++/CLR project that does the upcalls to to .NET assembly and exports a native interface.

[ .NET ] -> ----------- -> [ C(++) ]  ... via DllImport Attribute

[ .NET ] <- [ C++/CLR ] <- [ C(++) ] ... "default" .NET interface + "default" native interface

Best and simple way to call .net assemblies in java

Check is this response is helpful..
Calling .net assembly from java jvm crashes..

Came across this interesting site (may not be directly helpful to you)...

dotnetfromjava.dev.java.net



Related Topics



Leave a reply



Submit