Pass byte array from vb.net to unmanaged code

R

RGarg06

Hi,

I am trying to read a buffer from a file and then pass that buffer to a
C++ dll as follows. While debugging it tells me that byte array
received is a Bad Ptr. Any help will be appreciated.

VB code:

Dim bytes(511) As Byte
Dim stream As Stream
Dim bytesReadFromStream As Integer


stream = New FileStream("C:\abc.bak", FileMode.Open,
FileAccess.Read)

bytesReadFromStream = stream.Read(bytes, 0, 512)

While bytesReadFromStream > 0

SendBytesToFn(bytesReadFromStream , bytes)

bytesReadFromStream = stream.Read(bytes, 0, 512)

End While

stream.Close()



<System.Runtime.InteropServices.DllImport("MyDll.dll",
CharSet:=System.Runtime.InteropServices.CharSet.Auto)> _
Private Sub SendBytesToFn(ByVal bytesTransferred As Int64, ByVal
bytes() As Byte)

End Sub


C++ Code:

extern "C" MYDLL_API void SendBytesToFn(DWORD bytesTransferred, BYTE*
bytes)
{
// bytes here come as Bad Ptr

}
 
M

Mattias Sjögren

Private Sub SendBytesToFn(ByVal bytesTransferred As Int64, ByVal bytes() As Byte)
^^^^^

Should be an Int32 (or UInt32).


Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top