Home All Groups Group Topic Archive Search About

Pass byte array from vb.net to unmanaged code

Author
21 Jun 2006 6:06 PM
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

}

Author
21 Jun 2006 8:45 PM
Mattias Sjögren
>    Private Sub SendBytesToFn(ByVal bytesTransferred As Int64, ByVal bytes() As Byte)
                                                         ^^^^^

Should be an Int32 (or UInt32).


Mattias

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.