No functions in C# DLL with RGiesecke.DllExport

2020-03-01 18:46发布

问题:

I am trying to make a DLL in C# for use in a couple other languages. I found RGiesecke's DllExport but it doesn't seem to work. It builds just fine and makes a dll, but when I open it in Dependency Walker it doesn't show any functions, and my calling code can't find them either.

I created a new "Class Library" project (VS 2013) and then installed "Unmanaged Exports (DllExport for .Net)" from NuGet. Are there any project settings I need?

Here is my code.

using System;
using System.Collections.Generic;
using System.Text;
using RGiesecke.DllExport;

namespace ToolServiceDLL
{
    public class Class1
    {
      [DllExport("addUp", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
      public static double addUp(double num1, double num2)
      {
        return num1 + num2;
      }

      [DllExport("get5", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
      public static int get5()
      {
        return 5;
      }
    }
}

回答1:

I found the problem. It has it in the RGiesecke Documentation, but I missed it. In the project settings->Build->Platform target: you can not have it set to "Any CPU". You must have it set to x64 or x86 depending on if you want to use it in a a 64 or 32 bit application.



回答2:

I had a similar problem, but had already set the platform target to x64 and had the following error:

The name 'CallingConvention' does not exist in the current context

I found adding the using directive System.Runtime.InteropServices resolve the problem.



回答3:

This is a good question and worked for me. Just took a bit more time than it should due to the Python side where I made a mistake. Here is the working code in Python3 import sys import clr sys.path.insert(0,'C:\\your_path\\Py2NetComm\\bin\\Debug') clr.AddReference("Py2NetComm") import ToolServiceDLL as p2n ret = p2n.Class1.addUp(2,3) print("addUp:", ret)



标签: c# dll unmanaged