尝试从C#调用C ++ dll时格式不正确的异常 [英] Incorrect format Exception while trying to call C++ dll from C#

查看:206
本文介绍了尝试从C#调用C ++ dll时格式不正确的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#WPF.
我有一个C ++测试dll,如下所示:
.h:

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

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

.cpp文件:

.cpp file:

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

C#代码:

C# Code:

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

当我尝试调用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!


推荐答案

这似乎是32bit/ 64 bit问题.似乎您的C ++ dll和C#调用程序集是为不同的平台目标构建的.尝试为同一平台(x86或x64)编译两者,然后调用该函数.

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.

这篇关于尝试从C#调用C ++ dll时格式不正确的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆