[英]How to Interop between C# and C++
I'm attempting a very basic interoperation between the 2 languages. 我正在尝试两种语言之间的非常基本的互操作。 I basically have some performance intensive code I wanna handle in C++ and then get the result back to my application.
我基本上有一些性能密集的代码,我想用C ++处理,然后将结果返回给我的应用程序。
All is going to be compiled in Visual Studio. 所有都将在Visual Studio中编译。
i have chosen int to be the input and output type since marshalling can be a bit wonky and not really what i'm dealing with. 我选择int作为输入和输出类型,因为编组可能有点不稳定而不是我正在处理的事情。
C++ i have: C ++我有:
#include "stdafx.h" // default from vs2013, no idea what it is
_declspec(dllexport) int Diu(int p) {
return p * 2;
}
C# i have: C#我有:
using System;
namespace Interop {
public class Program{
[System.Runtime.InteropServices.DllImport("Hardworker.dll")]
public static extern int Diu(int p);
private static void Main(string[] args) {
Console.WriteLine(Diu(2));
}
}
}
So it's a pretty basic example. 所以这是一个非常基本的例子。 But I'm getting the exception:
但是我得到了例外:
An unhandled exception of type 'System.BadImageFormatException' occurred in Interop.exe
Interop.exe中发生了未处理的“System.BadImageFormatException”类型异常
Additional information: An attempt was made to load a program with an incorrect format.
其他信息:尝试加载格式不正确的程序。 (Exception from HRESULT: 0x8007000B)
(HRESULT异常:0x8007000B)
The C++ project is created as a Console Application > Dll in the create dialog. C ++项目在创建对话框中创建为Console Application> Dll。 I checked the C++ dll in the disassembler and i can see a Diu as an exported symbol.
我在反汇编程序中检查了C ++ dll,我可以看到Diu作为导出符号。
Uh. 呃。 What did I miss about the setting up interop?
关于设置互操作,我错过了什么?
When you get this error: HRESULT: 0x8007000B
is caused by paltform incompatibility. 当您收到此错误时:
HRESULT: 0x8007000B
是由paltform不兼容引起的。
Check that that your compiler profile are set to the same platform( x86
, x64
or AnyCPU
). 检查您的编译器配置文件是否设置为相同的平台(
x86
, x64
或AnyCPU
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.