Parameters in DeviceIoControl in VB.NET

  • Thread starter Thread starter Jacky
  • Start date Start date
J

Jacky

Hi,

I am trying to make network card interface with VB.NET 2002.

I use DeviceIOControl-function. I have tried to define inbuffer and
outbuffer using byte array and it's pointer. The second I tried
AllocHGlobal and Copy.

They works nice though the old app set these parameters to heap and VB.NET
to programs reserved memory area.

I get windows error 31.

Any suggestion??

Thanks in Advance,
Jacky
 
Hello,

Jacky said:
I am trying to make network card interface with VB.NET 2002.

I use DeviceIOControl-function. I have tried to define inbuffer and
outbuffer using byte array and it's pointer. The second I tried
AllocHGlobal and Copy.

They works nice though the old app set these parameters to heap
and VB.NET to programs reserved memory area.

I get windows error 31.

Posting code will make finding the error easier.
 
This C code works:


UCHAR inBuffer[1024];
UCHAR outBuffer[1024];
....

inBuffer[0] = 0x01;
inBuffer[1] = 0x01;
inBuffer[2] = 0x01;
inBuffer[3] = 0x01;

outBuffer[0] = 0x00;
outBuffer[1] = 0x00;
outBuffer[2] = 0x00;
outBuffer[3] = 0x01;

if (DeviceIoControl(handle,
157330,
(LPVOID) &inBuffer[0],
sizeof(inBuffer),
(LPVOID) &outBuffer[0],
sizeof(outBuffer),
&dwBytesReturned,
NULL))

But VB.Net not. It do not bush buffers to stack

<DllImport("kernel32.dll", SetLastError:=True)> _
Private Function DeviceIoControl(ByVal deviceHandle As IntPtr, _
ByVal ioControlCode As Int32, _
ByVal inBuffer As IntPtr, _
ByVal inBufferSize As Int32, _
<Out()> ByVal outBuffer As IntPtr, _
ByVal outBufferSize As Int32, _
ByRef bytesReturned As Int32, _
ByVal overlapped As IntPtr) As Boolean
End Function
....
Dim bufferIn() As Byte = New Byte(3) {}
Dim bufferOut() As Byte = New Byte(3) {}
....
bufferIn(0) = &H1
bufferIn(1) = &H1
bufferIn(2) = &H1
bufferIn(3) = &H1
bufferOut(0) = &H0
bufferOut(1) = &H0
bufferOut(2) = &H0
bufferOut(3) = &H1

Dim inPtr As IntPtr = Marshal.AllocHGlobal(4)
Marshal.Copy(bufferIn, 0, inPtr, 4)

Dim outPtr As IntPtr = Marshal.AllocHGlobal(4)
Marshal.Copy(bufferOut, 0, outPtr, 4)

ok = DeviceIoControl(handle, _
&H170002, _
inPtr, _
bufferIn.Length, _
outPtr, _
bufferOut.Length, _
ret, _
IntPtr.Zero)
 
Back
Top