Incorrect format Exception while trying to call C++ dll from C#

只愿长相守 提交于 2019-12-20 03:03:26

问题


I'm Using C# WPF.
I have a C++ test dll as follow:
.h:

extern "C" __delspec(dllexport) void TestMethod();

.cpp file:

extern "C"
{
    __delspec(dllexport) void TestMethod()
    {
        MessageBox(0, L"Test", L"Test", MB_ICONINFORMATION);
    }
}

C# Code:

[DllImport("DllTest.dll", EntryPoint = "TestMethod")]
public static extern void TestMethod();

And when i'm trying to call to TestMethod i got exception:

an attempt was made to load a program with an incorrect format

What i'm doing wrong?
Thanks!



回答1:


This seems to be 32bit/ 64 bit problem. Seems like your C++ dll and C# calling assembly are built for different platform targets. Try compiling both for the same platform (either x86 or x64) and then calling the function.



来源:https://stackoverflow.com/questions/36592848/incorrect-format-exception-while-trying-to-call-c-dll-from-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!