0

I'm trying to Marshal this C++ test DLL with C interface which works when calling from Python using CFFI:

#define AS_TYPE(Type, Obj) reinterpret_cast<Type *>(Obj)
#define AS_CTYPE(Type, Obj) reinterpret_cast<const Type *>(Obj)

struct mytype_s;
typedef struct mytype_s mytype_t;

namespace TestDLL {
    class Test {
    public:
        Test() { return; }
        ~Test() { return; }

        int test_function() const { return 314159; }
    };
}

mytype_t *test_new() {
    return AS_TYPE(mytype_t, new TestDLL::Test());
}

int test_function(const mytype_t *mytype) {
    return AS_CTYPE(TestDLL::Test, mytype)->test_function();
}

From C# I tried using an IntPtr for mytype_t which had worked for me with structures in the past. The C# code:

[DllImport(dllPath, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr test_new();
[DllImport(dllPath, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern int test_function(IntPtr mytype);

IntPtr mytype;
mytype = test_new();
Console.WriteLine("Alloc complete");

int result;
result = test_function(mytype);
Console.WriteLine("test: {0}", result);

But I get the error:

Alloc complete

Unhandled Exception: System.EntryPointNotFoundException: Unable to find an entry point named 'test_function' in DLL

Is this related to how I marshalled the mytype_t or a more complex issue with passing the object from C++ to C#?

Thanks

port80
  • 131
  • 1
  • 1
  • 3
  • 1
    Cause is likely non-C name mangling in your DLL exports (you are using a C++ compiler, after all). Declare your exported functions as unmangled DLL exports and you should be fine. (https://stackoverflow.com/questions/538134/exporting-functions-from-a-dll-with-dllexport) –  Jun 19 '19 at 14:32
  • Here is link https://learn.microsoft.com/en-us/cpp/build/exporting-from-a-dll-using-declspec-dllexport?view=vs-2019 – auslander Jun 19 '19 at 14:32
  • I'm using "define TEST_API __declspec(dllexport)". Other functions (e.g. that simply return an int) are working fine. – port80 Jun 19 '19 at 15:59
  • I think it's working but I'm not sure what changed. – port80 Jun 19 '19 at 20:50

0 Answers0