Home » VB.Net

AccessViolationException - Calling Unmanaged Code - Byref...

Hi there

I was calling this C++ function from vb and i got the error "Attempted to read or write protected memory..."

Here's the code:
The prototype of the API is:

voidWINAPIAvzGetImage(unsigned short DeviceID, unsigned char *Image,unsigned short FingerOn)

DeviceID = Device ID
Image = Data Of The Image
FingerOn = Is always 1

So, in vb, i tried calling it using:

Declare Auto Sub AvzGetImage Lib "AvzScanner.dll" (ByVal DID As UShort, ByRef img() As Byte, ByVal finger As UShort)
Dim img(280 * 280 - 1) as Byte
AvzGetImage(0, img, 1)



However, it would return me the stated error. Anyone any idea? =D



1 Answer Found


Answer 1

Based upon the C++ signature I end up with the following (using P/Invoke Interop Assistant ):

Partial Public Class NativeMethods
 '''Return Type: void
 '''DeviceID: unsigned short
 '''Image: unsigned char*
 '''FingerOn: unsigned short
 <System.Runtime.InteropServices.DllImportAttribute("<Unknown>", EntryPoint:="AvzGetImage")> _
 Public Shared Sub AvzGetImage(ByVal DeviceID As UShort, ByVal Image As System.IntPtr, ByVal FingerOn As UShort)
 End Sub
End Class


If the signature is correct (I don't know what Image is returning) I'm assuming that Marshal.PtrToStringAuto will get you the data:


Dim StringBuffer As String = Marshal.PtrToStringAuto(Image)

Paul ~~~~ Microsoft MVP (Visual Basic)


<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Sharepoint   |   Azure