gpt4 book ai didi

c# - DllImport a c++ DLL in to a C# app, BYTE * p

转载 作者:搜寻专家 更新时间:2023-10-31 01:23:21 25 4
gpt4 key购买 nike

我在 C++ DLL 中有一个导出函数

// C++ DLL (Blarggg.dll)

extern "C"
{
USHORT ReadProperty( BYTE * messsage, USHORT length, BYTE * invokeID )
{
if( invokeID != NULL ) {
* invokeID = 10 ;
}
return 0;
}
}

我想让它对我的 C# 应用程序可用

// C# app 
[DllImport("Blarggg.dll")]
public static extern System.UInt16 ReadProperty(
/* [OUT] */ System.Byte[] message,
/* [IN] */ System.UInt16 length,
/* [OUT] */ System.Byte[] invokeID );


private void DoIt()
{
System.Byte[] message = new System.Byte[2000];
System.Byte[] InvokeID = new System.Byte[1];
System.UInt16 ret = ReadProperty( message, 2000, InvokeID ); // Error
}

问题是我不断收到以下错误消息。

An unhanded exception of type 'System.NullReferenceException' occurred in Blarggg.dll Additional information: Object reference not set to an instance of an object.

我正在使用 VS2008 构建 DLL 和 C# 应用程序。

我不是 C# 程序员。

我做错了什么?

最佳答案

我将你的代码直接粘贴到 VS2008 中,它在我的 32 位机器上运行完美(添加了一个 .def 文件来设置导出的名称)。你的 C++ 库绝对是一个纯 win32 项目吗?您提供的错误消息似乎暗示它引发了 CLR 异常。

关于c# - DllImport a c++ DLL in to a C# app, BYTE * p,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1603834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com