DllImport c++DLL在C#应用程序,BYTE*p
题
我有一个出口功能在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
}
问题是,我不断收到以下的错误信息。
一个unhanded异常的类型系统。NullReferenceException'发生在Blarggg.dll 额外信息:目的参考,不设置为一个象的实例。
我使用的VS2008建立两DLL和C#应用程序。
我不C#程序员。
我做错了什么?
解决方案
我粘贴你的代码直接进入VS2008和它的运行完全可以在我的32位的机(加。def文件设置的出口名)。是你C++库绝对是一个纯粹的win32项目?错误消息你给似乎意味着它扔了一个CLR例外。
其他提示
试试这个:
[DllImport("Blarggg.dll", CallingConvention := CallingConvention.Cdecl)]
public static extern System.UInt16 ReadProperty(
/* [IN] */ System.Byte[] message,
/* [IN] */ System.UInt16 length,
/* [OUT] */ out System.Byte invokeID );
private void DoIt()
{
System.Byte[] message = new System.Byte[2000];
System.Byte InvokeID;
System.UInt16 ret = ReadProperty( message, 2000, out InvokeID );
}
你可能需要使用 系统。运行时间。InteropServices.元帅类 之间的转换管理的和不受管理的类型。
你可以做这个用C++的类型?
我的印象是,你只能DLLImport C dll。
我们使用DLLImport用C++Dll的只是好的,但我们宣布我们的外部职能
extern "C" __declspec(dllexport) ...
看看这个网页:
不隶属于 StackOverflow