Reputation: 4614
I have a exported function in a c++ DLL
// C++ DLL (Blarggg.dll)
extern "C"
{
USHORT ReadProperty( BYTE * messsage, USHORT length, BYTE * invokeID )
{
if( invokeID != NULL ) {
* invokeID = 10 ;
}
return 0;
}
}
That I would like to make it available to my C# application
// 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
}
The problem is that I keep getting the following error message.
An unhanded exception of type 'System.NullReferenceException' occurred in Blarggg.dll Additional information: Object reference not set to an instance of an object.
I'm using VS2008 to build both the DLL and the C# application.
I'm not a C# programmer.
What am I doing wrong?
Upvotes: 2
Views: 5857
Reputation: 7894
Can you do this with C++ types?
I was under the impression that you could only DLLImport C dlls.
We use DLLImport with C++ Dll's just fine but we declare our external functions as
extern "C" __declspec(dllexport) ...
Have a look at this web page:
http://dotnetperls.com/dllimport-interop
Upvotes: 0
Reputation: 595295
Try this:
[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 );
}
Upvotes: 2
Reputation: 7426
I pasted your code directly into VS2008 and it runs perfectly on my 32-bit machine (added a .def file to set the exported name). Is your C++ library definitely a pure win32 project? The error message you gave seems to imply that it threw a CLR exception.
Upvotes: 2
Reputation: 28859
You might need to use the System.Runtime.InteropServices.Marshal class to convert between managed and unmanaged types.
Upvotes: 0