在C#应用程序中使用标准C ++ dll [英] Use a standard C++ dll in c# application

查看:114
本文介绍了在C#应用程序中使用标准C ++ dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我有一个用C ++编写的dll,想在C#中使用(一个常见问题).
我正在尝试进行测试以了解如何使用dll.
因此,我用以下几行简单的代码在C ++中创建了一个新的标准dll:

Hi I have a dll written in C++ that I want to use in C# (a common problem).
I am trying to make a test to understand how to use the dll.
So I created a new standard dll in C++ with these simple lines:

extern "C" __declspec (dllexport) short __stdcall TestFunction(short i){
    return i+123;
}



然后,我尝试使用以下代码在C#应用程序中使用此测试功能:



Then I am trying to use this test function in an C# application using the code:

[DllImport("Dll.dll")]
static extern short TestFunction(short i);



然后



and then

short x=3;
x=TestFunction(x);



已编译!
但是我得到了这个错误运行时:

发生了类型为"System.BadImageFormatException"的未处理的异常

我认为问题在于该dll无法托管,但我必须将其用作标准C ++且未托管.对吧?
有人知道能达到我的目标吗?
谢谢您的帮助.



Compiled!
But I got this error runtime:

An unhandled exception of type ''System.BadImageFormatException'' occurred

I think the problem is that the dll is not managed, but I have to use it as standard C++ and unmanaged. Right?
Anyone knows hot to reach my target?
Thank you for any help

推荐答案

这似乎是您在64位OS上运行.NET应用程序,而DLL是32位. 如果这是问题所在,则可以通过在项目设置中将.NET应用程序定位到x86来解决.
This looks like you are running your .NET application on 64bit OS and your DLL is 32bits.
If this is the issue you can fix it by targetting your .NET application to x86 in project settings.


这篇关于在C#应用程序中使用标准C ++ dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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