How to fix this unmanaged exports trouble using De

2019-08-07 14:29发布

问题:

I came across Unmanaged exports technique in this anwser on SO.

  • I can manage myself to have simplest project working on SharpDevelop, albeit I don't have a good command of C#.
  • I want to use the excellent Gavaghan.Geodesy (.Net/C#) library written by Mike Gavaghan.
  • I want to avoid interoperability through COM.
  • I definitely use Delphi. I only want to harness the library with it.

I managed to build the following code :

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

namespace DelphiNET
{
    internal static class UnmanagedExports
{
    [DllExport("add", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
    public static int add(int left, int right) {
        return left + right;            
        }

    }
}

I probed it with Delphi but was in vain.

I checked the output dll with a PE editor but It turned out to have no export data at all.

I miss something and I'm confused.

Edit:

Please - Help me fix the trouble

回答1:

I managed to get this to work like this:

  1. Download the C# Project template. This requires Visual Studio but you should be fine with the Express version.
  2. Create a new project based on the template.
  3. Build that project.
  4. Load the DLL in Dependency Walker to see that it does indeed export the function. I succeeded in calling the function from Delphi.

Note that the path to find the DLL is bin\Debug\x86. There is a DLL in bin\Debug but this has no exports. Perhaps that's what you have been looking at.