can't call C++ function from C#

2019-08-24 18:14发布

问题:

I have a DLL with a set of functions. The DLL was used with "themidia" to make it safe.

When I try to call the functions, C# spits out errors due to the functions names.

[DllImport("safety.dll", CallingConvention=CallingConvention.StdCall, ExactSpelling=true)]
private static extern IntPtr _encryptLogin@8(string string_0, string string_1);

If I remove the @8 and remove ExactSpelling=true, it just returns an exception saying no entry point.

What exactly am I doing wrong?

回答1:

Remove the "@", and in your attribute add EntryPoint="_encryptLogin@8"



回答2:

As an alternative to specifying EntryPoint as rfmodulator suggested, you can use extern "C" in your C++ source, which will make the exported function names the same as their names in your C++ source.

C++ compiler normally mangles the names of functions, so that you can have overloaded functions (functions with the same name bu different parameters).