C#, DeviceIoControl, and NDIS OIDs


Hi,

I have two problems in using DeviceIoControl in C#:
- At first, I want to read any data (=OIDs) from any NDIS network driver.
- Secondly, I want to set the values of any writable data (=OIDs), to something I want them to be, of any NDIS network driver.

I tried using of WMI but it only allows reading of data. It can't be used to set any NDIS OIDs. Well, at least I haven't found any example, book, or article that proves it. So, I switched to using DeviceIoControl that can be used to both get and set. The real problem is that there's no solid documentation of actually using it in C#. C/C++ has a lot of examples, of course, but how to do the same in C#

Currently I am able to import CreateFile, CloseHandle, and DeviceIoControl functions from kernel32.dll. I also get all network NDIS drivers of the system by using WMI and ManagementObjectSearcher. From the ManagementObjectSearcher I get an instance of ManagementObjectCollection. Looping through the collection I get instances of ManagementObject that give me the name of the drivers. Those names I use for CreateFile to get a handle to a driver. After that I want to read and set some OIDs of the drivers.

The problem is that I don't know how to set and use some of the parameters of the DeviceIoControl. The DeviceIoControl wants a device handle, IO control code, in buffer & size, out buffer & size, integer for return ed amount of bytes, and overlapped info. Specifically, the IO control code and the contents of the in buffer are troublesome. The documentation (MSDN, DDK, "Google") does not give any solid examples for using them.

For example, the IO control code should be constructed with a CTL_CODE macro in C/C++. In C#, the same code is probably translated as (not my code, took it from some example):

static uint CTL_CODE(uint deviceType, uint function, uint method, uint access)
{
    return ((deviceType) << 16) | ((access) << 14) | ((function) << 2) | (method);
}


I know the device type (FILE_DEVICE_NETWORK), the method (METHOD_BUFFERED or METHOD_NEITHER), and the access (FILE_READ_ACCESS or FILE_WRITE_ACCESS). But from where can I get the function codes ! Secondly, how can I translate FILE_DEVICE_NETWORK, METHOD_BUFFERED etc. from C/C++ to C# Are those constants defined somewhere in C#

More... are the function codes the same as OID codes Then where can I get a list of the the OID codes And then howabout the contents of in buffer Are there rules for filling it somewhere

And for the examples, here are a few OIDs that I would like to get and set (if writable):
MSNdis_MediaInUse
MSNdis_MediaConnectStatus
MSNdis_ReceiveBufferSize
MSNdis_TransmitBufferSize

How can I set those Do I need these somehow: IOCTL_NDISUIO_QUERY_OID_VALUE or IOCTL_NDISUIO_SET_OID_VALUE

~ Sand ~



Answer this question

C#, DeviceIoControl, and NDIS OIDs

  • C#, DeviceIoControl, and NDIS OIDs