简体   繁体   English

如何在C#项目中调用WIN32 DLL-EntryPointNotFoundException

[英]How to call WIN32 DLL in C# project - EntryPointNotFoundException

I have created a WIN32 DLL project and its dllmain.cpp is as follows; 我创建了一个WIN32 DLL项目,它的dllmain.cpp如下;

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

_declspec(dllexport) float RGBCompare()
{
    return 100;
}

My target is to call method RGBCompare from a C# project and as per rule I have mentioned dllexport tag before it. 我的目标是从C#项目中调用方法RGBCompare,按照规则,我之前提到了dllexport标记。

On the other side in C# project I have defined an entry point as follows; 在C#项目的另一端,我定义了一个入口点,如下所示:

namespace LogoFinderWrapper
{
    public class LogoFinder
    {
        [DllImport("LogoIdentifier.dll", EntryPoint = "RGBCompare")]
        private static extern float Api_RGBCompare();


        public static float RGBCompare()
        {
            return Api_RGBCompare();
        }
    }
}

When I call DLL it raises exception System.EntryPointNotFoundException. 当我调用DLL时,它引发异常System.EntryPointNotFoundException。

Please could any one help me in this regard? 请有人在这方面帮助我吗?

Your native code is C++ and the name is mangled before export. 您的本机代码是C ++,并且在导出之前对名称进行了修改。 Possible solutions: 可能的解决方案:

  1. Use the mangled name in the EntryPoint parameter. EntryPoint参数中使用错误的名称。 Find out the mangled name with dumpbin or Dependency Viewer. 使用dumpbin或Dependency Viewer找出混乱的名称。
  2. Use a .def file rather than __declspec(dllexport) to control which functions are exported. 使用.def文件而不是__declspec(dllexport)来控制导出哪些函数。
  3. Suppress mangling with extern "C" in your C++ source code. 禁止在C ++源代码中使用extern "C"进行改写。

The final option would look like this: 最终选项如下所示:

extern "C" 
{
    __declspec(dllexport) float RGBCompare()
    {
        return 100;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM