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!!!

3
  • it won't work like this as far as I know, .Net dll created in C# lacks export table that should be added manually or with third-party utilities Commented Apr 5, 2017 at 18:39
  • third-party utilities? what kind of third-party utilities? Commented Apr 5, 2017 at 19:26
  • i've edited my answer. Disclaimer: I personally didn't use the NuGet mentioned but instead used some utility from github (I fail to find it now though). Commented Apr 6, 2017 at 8:08

1 Answer 1

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.

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

2 Comments

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 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.

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.