Unable to find an entry point when calling C++ dll in C#

前端 未结 3 2203
感情败类
感情败类 2020-12-15 18:48

I am trying to learn P/Invoke, so I created a simple dll in C++

KingFucs.h:

namespace KingFuncs
{
    class KingFuncs
    {
    public:
        stati         


        
相关标签:
3条回答
  • 2020-12-15 19:02

    You need to use extern "C" when you export your function so that you suppress C++ name mangling. And you also should not try to p/invoke to members of a class. Use free functions instead:

    extern "C" {
        __declspec(dllexport) int GiveMeNumber(int i)
        {
            return i;
        }
    }
    

    On the managed side your DllImport attribute is all wrong. Don't use SetLastError which is for Win32 APIs only. Don't bother setting CharSet if there are not text parameters. No need for ExactSpelling. And the calling convention is presumably Cdecl.

    [DllImport("KingFuncDll.dll", CallingConvention=CallingConvention.Cdecl)]
    public static extern int GiveMeNumber(int i);
    
    0 讨论(0)
  • 2020-12-15 19:06

    The entry point name of a dll file is given in .exp file which is found in debug folder where other source files are present. If dumpbin doesn't work you can try this.

    0 讨论(0)
  • 2020-12-15 19:18

    The problem is that you are declaring the C++ "function" inside a C++ class and are telling P/Invoke to use StdCall.

    Try to declare a C++ function outside a class and and export it like you did. Then your code should work.

    If you really must have a C++ function inside a class, take a look at CallingConvention.ThisCall. But then you are responsible for creating your unmanaged class instance and pass it as the first parameter of your P/Invoke call

    0 讨论(0)
提交回复
热议问题