2

I am trying to create an example of exporting a simple function from a managed C# class library and use it in an unmanaged C++ console application.

I'm using Robert Giesecke's Unmanaged Exports for this. (IDE is Visiual Studio 2017)

My Code on the managed side of things is:

using RGiesecke.DllExport;
using System.Runtime.InteropServices;

namespace ManagedCodeDll
{
    public class Calculator
    {
        [DllExport(CallingConvention = CallingConvention.Cdecl)]
        static public int Add(int a, int b)
        {
            return a + b;
        }           
    }

}

The Build Platform target is set to x86.

But when building the project no .lib file is created. Only the dll itself.

From what I understand is that I need the lib file for the linker setting on the unmanaged side.

What am I doing wrong? Can someone help me?

  • 1
    "Build Platform" is not the one that matters in a C# project. The important one is Project > Properties > Compile tab, "Platform target". You must change the default of AnyCPU to either x86 or x64 to get the utility to do anything. Do this both for the Debug and the Release configuration. – Hans Passant Nov 27 '18 at 09:53
  • Under Project > Properties > There is no Compile tab. I have set the platform target in the Build tab to x84 as well as the Platform under Configuration to x86. Still no .lib file – CasualNobody Nov 27 '18 at 10:04
  • 1
    Right, Build tab. The order in which you do this matters, when you change Platform then you have to back again and modify the Build tab. A C# project should have only one platform (AnyCPU) and two configurations (Debug and Release). Platform only really matters for C++ projects, it uses different compilers for different platforms. It was retained in C#, you might want to build both flavors of unmanaged exports, but causes more confusion than help. – Hans Passant Nov 27 '18 at 10:36
  • Ok. But Platform target is set to x86. I checked all setting multible times. It still wont create a lib file. – CasualNobody Nov 27 '18 at 10:45
  • 2
    The only other flaw I ever noticed is that you must run VS elevated to let the nuget package installer do its job. It modifies the project file and fails some times for very mysterious reasons. Open the .csproj file in Notepad and verify that at the very bottom it has an Import for RGiesecke.DllExport.targets. – Hans Passant Nov 27 '18 at 10:52
  • Did you ever get it to work? I think VS2017 and unmanagedexports just don't get along – Gaspa79 Jul 25 '19 at 20:22

0 Answers0