11

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;
      }
    }
}
runfastman
  • 927
  • 2
  • 11
  • 31
  • It does not create a DLL, it *modifies* a DLL. So that did not happen. One nasty trap is that the Nuget package will not install correctly when you don't run VS elevated, failure is silent. Right-click the VS shortcut and select "Run as Administrator". – Hans Passant Dec 22 '15 at 15:27
  • I am running as Admin, and it shows "RGiesecke.DllExport.Metadata" in the References. How do I tell if it tries to modify the dll? – runfastman Dec 22 '15 at 15:32

3 Answers3

14

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.

runfastman
  • 927
  • 2
  • 11
  • 31
1

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.

theotheraussie
  • 495
  • 1
  • 4
  • 14
-1

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)

vlad
  • 184
  • 2
  • 5