I am having a problem with code when assigning values. I continue to get the above error on line 3. Where am I going wrong?
Dim Buffer As Byte() = New Byte(&H2710 - 1) {}
Dim numRef As Byte
numRef = Buffer
Dim num2 As Integer
Dim seemNum As UInt32 = Convert.ToUInt32(hexSeem, &H10)
If (seemNum <> 0) Then
num2 = Seem_Read(seemNum, recNum, startOff, bytes, numRef)
Else
num2 = Seem_Read(0, recNum, startOff, bytes, numRef)
End If
If num2 <> -1 Then
Dim buffer3 As Byte() = New Byte(num2 - 1) {}
Dim i As Integer
For i = 0 To num2 - 1
buffer3(i) = buffer(i)
Next i
Return buffer3
End If
Return Nothing
Thanks
Loading
AlanPosted Nov 4, 2008, 10:38 AM
Generally speaking, it's not easy (and sometimes virtually impossible) to translate unsafe code in C# into an equivalent form in VB.Net.
However, all the unsafe code is doing here is 'pinning' the byte array so it doesn't move in memory and then taking its address which it passes to the unmanaged function, Seem_Read(), so that it can fill in the data.
So, what I'd suggest you do from VB.Net is the following.
First change the signature of the unmanaged function so it takes an IntPtr rather than a Byte:
Public Declare Function Seem_Read Lib "p2kapi2.dll" (ByVal SeemNum As UInt32, ByVal RecordNum As UInt32, ByVal StartOff As UInt32, ByVal Bytes As UInt32, IntPtr ptrData) As Integer
Then change your VB function as follows (changed code highlighted):
Public Shared Function SeemRead(ByVal hexSeem As String, ByVal recNum As UInt32, ByVal startOff As UInt32, ByVal bytes As UInt32) As Byte()
Dim Buffer As Byte() = New Byte(&H2710 - 1) {}
Dim gch As GCHandle = GCHandle.Alloc(Buffer, GCHandleType.Pinned)
Dim numRef As IntPtr = gch.AddrOfPinnedObject()
Dim num2 As Integer
Dim seemNum As UInt32 = Convert.ToUInt32(hexSeem, &H10)
If (seemNum <> 0) Then
num2 = Seem_Read(seemNum, recNum, startOff, bytes, numRef)
Else
num2 = Seem_Read(0, recNum, startOff, bytes, numRef)
End If
If num2 <> -1 Then
Dim buffer3 As Byte() = New Byte(num2 - 1) {}
Dim i As Integer
For i = 0 To num2 - 1
buffer3(i) = buffer(i)
Next i
gch.Free()
Return buffer3
End If
gch.Free()
Return Nothing
End Function
CelldetPosted Nov 4, 2008, 8:02 AM
Public Declare Function Seem_Read Lib "p2kapi2.dll" (ByVal SeemNum As UInt32, ByVal RecordNum As UInt32, ByVal StartOff As UInt32, ByVal Bytes As UInt32, ByRef ptrData As Byte) As Integer
SeemNum - Seem cell number
RecordNum - Seem record number
StartOffset - Offset for seem reading from (0 - from cell beginning)
Bytes - Bytes count for reading (0 - read from beginning to end)
ptrData - pointer to a buffer for receiving seem cell data
If "Bytes" parameter not specified, then buffer length must be >= 2710h bytes
Returns count of read bytes or -1 on error.
Original code c#
Public Shared Function SeemRead(ByVal hexSeem As String, ByVal recNum As UInt32, ByVal startOff As UInt32, ByVal bytes As UInt32) As Byte()
Dim Buffer As Byte() = New Byte(&H2710 - 1) {}
Dim numRef As Integer
numRef = Buffer.Length
Dim num2 As Integer
Dim seemNum As UInt32 = Convert.ToUInt32(hexSeem, &H10)
If (seemNum <> 0) Then
num2 = Seem_Read(seemNum, recNum, startOff, bytes, numRef)
Else
num2 = Seem_Read(0, recNum, startOff, bytes, numRef)
End If
If num2 <> -1 Then
Dim buffer3 As Byte() = New Byte(num2 - 1) {}
Dim i As Integer
For i = 0 To num2 - 1
buffer3(i) = buffer(i)
Next i
Return buffer3
End If
Return Nothing
End Function
AlanPosted Nov 4, 2008, 5:36 AM
Well, as the error message says, numRef is declared to be a Byte variable and Buffer is a Byte array variable and so can't be converted to a single Byte.
You could prevent the error with:
Dim numRef As Byte()
numRef = Buffer
However, whilst I can't make much sense of the code, I suspect from its name and the way it's used that numRef may be the length of the Byte buffer. If that's the case, then you'd want:
Dim numRef As Integer
numRef = Buffer.Length