How to create a C# functions DLL and then use this functions in C? -


i'm trying create dll exposing static functions use in c.

recently read article of microsoft named "an overview of managed/unmanaged code interoperability" , in there no clear explanation on how "exposing managed api flat api".

i installed plugin visual studio (https://www.nuget.org/packages/unmanagedexports) still can't compile project in c.

my c# project exposes function this:

using rgiesecke.dllexport; using system.runtime.interopservices;  namespace libcallcstest {     public class class1     {         [dllexport("add", callingconvention = callingconvention.cdecl)]          public static int add(int a, int b)         {             return + b;         }     } } 

after building project, result these 3 files:

libcallcstest.dll libcallcstest.pdb libcallcstest.tlb 

my c code is:

#include <stdio.h> #include <stdlib.h>  int add(int, int);  int main(int argc, char** argv) {     int z = add(2,5);     printf("%d\n", z);     return (exit_success); } 

and when try compile file with:

gcc -o main.exe main.c -lcallcstest 

not work properly, files created building c# project in same folder main.c file.

pleas help!!!

one way go: may want host clr in process. recommend against though, because hosting not easiest procedure out there. it's not needed or can use slower methods communicate .net code unmanaged environment (for example, present library local server , access through network interfaces. see way you'll have ten times less work do).

or go original variant using utilities like mentioned here.


Comments