0

I'm trying to use some external C++ code in my C# (Unity) project. I decided to create a .dll file. My problem is when I try to run my program, it throws an exception at calling dll's function. My code looks something like this:

// Something.h

#ifndef SOMETHING_H
#define SOMETHING_H

extern "C"  __declspec(dllexport) int __stdcall f(int a, const char** b, char* c);

#endif
// Something.cpp

int f(int a, const char** b, const char* c) {
// some stuff
}
// Something.cs

using System.Runtime.InteropServices;

public class Something
{
    [DllImport("Something.dll", CallingConvention = CallingConvention.StdCall)]
    public static extern int f(
        int a,
        [In][MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPStr)] string[] b,
        [In][MarshalAs(UnmanagedType.LPStr)] string c
    );
}

I'm think that I'm doing something nasty with that string[] -> const char** conversion but i'm not sure because it's my first try with mixing languages by .dll or .so.

Here is the part of exception thrown:

System.EntryPointNotFoundException: f
  at (wrapper managed-to-native) Something.f(int,string[],string)

Thanks for any attention.

Best Regards,

Xavrax

Bharathi
  • 1,015
  • 13
  • 41
Xavrax
  • 68
  • 1
  • 5
  • 1
    may help https://stackoverflow.com/questions/3515270/entry-point-not-found-exception – Kevin Kouketsu Aug 16 '19 at 01:23
  • @KevinKouketsu Thank you so much. Dumpbin was exactly what I needed. There were some linking troubles that i didn't know that could apear. – Xavrax Aug 16 '19 at 01:57

1 Answers1

1

Your f declaration doesn't match your f implementation so they are separate functions. Change the type of c so that it is the same in both declaration and implementation.

You can help avoid this problem by putting extern "C" on both the declaration and implementation which will result in a compiler error if the arguments don't match:

extern "C"  int f(int a, const char** b, const char* c) {

results in:

error C2733: 'f': second C linkage of overloaded function not allowed
note: see declaration of 'f'

Note: this wont help if there is a typo in the function name though.

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60