Help with DeviceIoControl PInvoke

  • Thread starter Juan Pedro Gonzalez
  • Start date
J

Juan Pedro Gonzalez

Helo,

I'm having problems here with the input buffer.... Ive defined the API call
as:

<System.Runtime.InteropServices.DllImport("kernel32", SetLastError:=True)> _
Private Shared Function DeviceIoControl(ByVal hDevice As IntPtr, ByVal
dwIoControlCode As Integer, ByVal lpInBuffer As IntPtr, ByVal nInBuffer As
Integer, ByRef lpOutBuffer As IntPtr, ByVal nOutBufferSize As Integer, ByRef
lbBytesResturned As Integer, ByVal lpOverlapped As IntPtr) As Boolean
End Function

<System.Runtime.InteropServices.DllImport("kernel32", SetLastError:=True)> _
Private Shared Function DeviceIoControl(ByVal hDevice As IntPtr, ByVal
dwIoControlCode As Integer, ByVal lpInBuffer As IntPtr, ByVal nInBuffer As
Integer, ByRef lpOutBuffer As Object, ByVal nOutBufferSize As Integer, ByRef
lbBytesResturned As Integer, ByVal lpOverlapped As IntPtr) As Boolean
End Function

Al right, use CreateFile to get a handle to my device, I issue

DeviceIoControl(Me.m_Handle, IOCTL_NDISUIO_BIND_WAIT, IntPtr.Zero, 0,
IntPtr.Zero, 0, nBytesReturned, IntPtr.Zero)

wich works fine, but when I can't figure how to query the device. For
example:

DeviceIoControl(Me.m_Handle, IOCTL_NDISUIO_QUERY_BINDING, p,
System.Runtime.Interop.Marshal.sizeof(p), oBuffer, Me.m_Buffer.Length,
dwBytesWritten, IntPtr.Zero)

(Where p is a structure). This fails all the time at, it seems to throw an
exception at System.Runtime.Interop.Marshal.sizeof(p), .

This code also exists in C# where the code is:

fixed (byte* buf = Buffer)
{
...
NDISUIO_QUERY_BINDING* p = (NDISUIO_QUERY_BINDING*)buf;
...
DeviceIoControl(DriverHandle, IOCTL_NDISUIO_QUERY_BINDING, new
IntPtr(p), sizeof(NDISUIO_QUERY_BINDING), new IntPtr(buf), Buffer.Length,
out dwBytesWritten, IntPtr.Zero)
...
}

I cann't manage to port this code to VB .NET and would be pleased to receive
some help on this issue. I'm having problems to get around those pointers...

Thanyou in advance,

Juan Pedro Gonzalez
 
D

Dragon

Hello Juan,

You may try to include an overloaded declare of API subroutine with
lpInBuffer and lpOutBuffer declared as NDISUIO_QUERY_BINDING, e.g:

~
Private Declare Function DeviceIoControl Lib "kernel32.dll" ( _
ByVal hDevice As IntPtr, _
ByVal dwIoControlCode As Integer, _
ByRef lpInBuffer As NDISUIO_QUERY_BINDING, _
ByVal nInBufferSize As Integer, _
ByRef lpOutBuffer As NDISUIO_QUERY_BINDING, _
ByVal nOutBufferSize As Integer, _
ByRef lpBytesReturned As Integer, _
ByVal lpOverlapped As IntPtr _
) As Boolean
~

and pass your structure into this method directly.
If this doesn't help please provide complete C# code.

Roman
 

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