1

I'm trying to create a DLL exposing some static functions to use then in C.

Recently I read an article of Microsoft named "An Overview of Managed/Unmanaged Code Interoperability" and in this there is no a clear explanation on how to "Exposing a Managed API as a Flat API".

I installed this plugin to Visual Studio (https://www.nuget.org/packages/UnmanagedExports) but I still can't compile a project in C.

My C# project exposes a function like 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 a + b;
        }
    }
}

After building project, result these three 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 finally when I try to compile this file with:

gcc -o main.exe main.c -lcallcstest

Not work properly, files created by building the C# project are in the same folder as the main.c file.

Pleas any help!!!

jpolako
  • 11
  • 1

1 Answers1

1

One way to go: you may want to host CLR in your process. I would recommend against it though, because hosting is not the easiest procedure out there. Also it's often not really needed or you can use some slower methods to communicate with .Net code from unmanaged environment (for example, present your library as a local server and access it through network interfaces. As I see it that way you'll have ten times less work to do).

Or you could go with your original variant using utilities to help you like mentioned here.

Community
  • 1
  • 1
  • Don't see how it answers the question. OP already uses the tool mentioned in your link, but it does not work as expected for him, hence he asks for a fix. – Evk Apr 06 '17 at 09:35
  • @Evk the linked question also mentions some other tools that automate `.export` (which worked for me when I was doing the same). I do not claim that my answer ("you're better off not doing it at all or try some export utilities) is the best one possible or even good but it is an answer to "Please any help" request. If something works for the OP or not is up to OP and his/her luck. – Sergey.quixoticaxis.Ivanov Apr 06 '17 at 10:16