sventevit
sventevit

Reputation: 4816

Problems with data types from external dll

I use Philip's MfRc500.dll for communicating with RFID chips. It contains a function for reading data, which is defined like this:

signed char Mf500PiccRead(unsigned char addr, unsigned char * data)

I use it in VB.NET like this:

Private Declare Function RFID_PiccRead Lib "MfRc500.dll" Alias "_Mf500PiccRead@8" (ByVal addr As UShort, ByRef value As Long) As Short

and then a wrapper:

Public Function PiccRead(ByVal addr As UShort, ByRef value As Long) As Short
    Return RFID_PiccRead(addr, value)
End Function

The second parameter in function Mf500PiccRead can returns "16 byte data block", so my long data type is too small. If I use byte() instead of long, then i get ExecutionEngineException Exception.

Which data type should I use and how?

Upvotes: 3

Views: 539

Answers (3)

Hans Passant
Hans Passant

Reputation: 941990

The last argument really is a Byte(). ExecutionEngineException indicates that the garbage collected heap got corrupted. Be sure to pass an initialized array that's large enough to contain the response:

    Dim buffer(666) As Byte
    Dim retval As SByte = RFID_PiccRead(42, Buffer)
...
  Private Declare Function RFID_PiccRead Lib "MfRc500.dll" Alias "_Mf500PiccRead@8" _
     (ByVal addr As Byte, Byval buffer() As Byte) As SByte

Upvotes: 3

MarkJ
MarkJ

Reputation: 30408

A char is equivalent to a Byte so some other parts of your declaration are also wrong.

I recommend you use the PInvoke Interop Assistant to automatically convert the C header into VB.Net PInvoke declarations.

However I don't have that installed here, so just for fun I've attempted a manual conversion. In any case, the assistant isn't psychic, so it won't know the second argument can return 16 bytes, and you'll need to do a little manual tweaking anyway.

Private Declare Function RFID_PiccRead Lib "MfRc500.dll" _ 
  Alias "_Mf500PiccRead@8" (ByVal addr As Byte, ByRef value() As Byte) As Byte 

Make sure to pass a 16-byte array for the second parameter and I think it should work.

Upvotes: 1

Zyon
Zyon

Reputation: 361

try and use an Int or Integer instead of a short.

Private Declare Function RFID_PiccRead Lib "MfRc500.dll" Alias "_Mf500PiccRead@8" (ByVal addr As UShort, ByRef value As Long) As Int or use Integer

Public Function PiccRead(ByVal addr As UShort, ByRef value As Long) As Int o r Integer Return RFID_PiccRead(addr, value) End Function

Upvotes: 0

Related Questions