Home All Groups Group Topic Archive Search About

DeviceIoControl from C# ?

Author
8 Apr 2005 12:47 AM
Michael Allen
I would like to perform something similar to the below function in C# .NET.
The C++ below code is from a Microsoft DDK sample driver application.
Specifically, I would like to perform Device I/O Control from a .NET
application.

Is there any way to do this in C#. I have tried Googling to no avail.

Thanks,

Michael Allen

BOOLEAN
SendIoctlToControlDevice(
    IN PDEVICE_INFO deviceInfo)
{
    BOOLEAN result = FALSE;
    UINT bytes;      

    //
    // Open handle to the control device, if it's not already opened. Please
    // note that even a non-admin user can open handle to the device with
    // FILE_READ_ATTRIBUTES | SYNCHRONIZE DesiredAccess and send IOCTLs if
the
    // IOCTL is defined with FILE_ANY_ACCESS. So for better security avoid
    // specifying FILE_ANY_ACCESS in your IOCTL defintions.
    // If you open the device with GENERIC_READ, you can send IOCTL with
    // FILE_READ_DATA access rights. If you open the device with
GENERIC_WRITE,
    // you can sedn ioctl with FILE_WRITE_DATA access rights.
    //
    //

    if(deviceInfo->hControlDevice != INVALID_HANDLE_VALUE) {

        deviceInfo->hControlDevice = CreateFile (
            TEXT("\\\\.\\NETVMINI"),
            GENERIC_READ | GENERIC_WRITE,//FILE_READ_ATTRIBUTES | SYNCHRONIZE,
            FILE_SHARE_READ,
            NULL, // no SECURITY_ATTRIBUTES structure
            OPEN_EXISTING, // No special create flags
            FILE_ATTRIBUTE_NORMAL, // No special attributes
            NULL);

        if (INVALID_HANDLE_VALUE == deviceInfo->hControlDevice) {
            Display(TEXT("Failed to open the control device: %ws"),
                    deviceInfo->DeviceName);
            return result;
        }
    }       

    //
    // send ioclt requests
    //
    if(!DeviceIoControl (deviceInfo->hControlDevice,
          IOCTL_NETVMINI_READ_DATA,
          NULL, 0,
          NULL, 0,
          &bytes, NULL)) {
       Display(TEXT("Read IOCTL to %ws failed"), deviceInfo->DeviceName);   

    } else {
       Display(TEXT("Read IOCTL to %ws succeeded"), deviceInfo->DeviceName);
       result = TRUE;
    }

    if(!DeviceIoControl (deviceInfo->hControlDevice,
          IOCTL_NETVMINI_WRITE_DATA,
          NULL, 0,
          NULL, 0,
          &bytes, NULL)) {
       Display(TEXT("Write IOCTL to %ws failed"), deviceInfo->DeviceName);  

    } else {
       Display(TEXT("Write IOCTL to %ws succeeded"), deviceInfo->DeviceName);
       result = TRUE;
    }

    return result;
}

Author
8 Apr 2005 9:23 AM
Nassos
Hi Michael,
What your are trying is a bit difficult, i can give you a pointer on what to
search so, finde help on DllImport that allow you to access  OS SDK.
looks like that :
[System.Runtime.InteropServices.DllImport("Kernel32.dll",
SetLastError=true)]

public extern static int DeviceIoControl(IntPtr hDevice, uint IoControlCode,

IntPtr lpInBuffer, uint InBufferSize,

IntPtr lpOutBuffer, uint nOutBufferSize,

ref uint lpBytesReturned,

IntPtr lpOverlapped);

Hope that help

Show quoteHide quote
"Michael Allen" <MichaelAl***@discussions.microsoft.com> wrote in message
news:BC97B852-35D0-40AE-A9D7-4F079E09DB5A@microsoft.com...
>I would like to perform something similar to the below function in C# .NET.
> The C++ below code is from a Microsoft DDK sample driver application.
> Specifically, I would like to perform Device I/O Control from a .NET
> application.
>
> Is there any way to do this in C#. I have tried Googling to no avail.
>
> Thanks,
>
> Michael Allen
>
> BOOLEAN
> SendIoctlToControlDevice(
>    IN PDEVICE_INFO deviceInfo)
> {
>    BOOLEAN result = FALSE;
>    UINT bytes;
>
>    //
>    // Open handle to the control device, if it's not already opened.
> Please
>    // note that even a non-admin user can open handle to the device with
>    // FILE_READ_ATTRIBUTES | SYNCHRONIZE DesiredAccess and send IOCTLs if
> the
>    // IOCTL is defined with FILE_ANY_ACCESS. So for better security avoid
>    // specifying FILE_ANY_ACCESS in your IOCTL defintions.
>    // If you open the device with GENERIC_READ, you can send IOCTL with
>    // FILE_READ_DATA access rights. If you open the device with
> GENERIC_WRITE,
>    // you can sedn ioctl with FILE_WRITE_DATA access rights.
>    //
>    //
>
>    if(deviceInfo->hControlDevice != INVALID_HANDLE_VALUE) {
>
>        deviceInfo->hControlDevice = CreateFile (
>            TEXT("\\\\.\\NETVMINI"),
>            GENERIC_READ | GENERIC_WRITE,//FILE_READ_ATTRIBUTES |
> SYNCHRONIZE,
>            FILE_SHARE_READ,
>            NULL, // no SECURITY_ATTRIBUTES structure
>            OPEN_EXISTING, // No special create flags
>            FILE_ATTRIBUTE_NORMAL, // No special attributes
>            NULL);
>
>        if (INVALID_HANDLE_VALUE == deviceInfo->hControlDevice) {
>            Display(TEXT("Failed to open the control device: %ws"),
>                    deviceInfo->DeviceName);
>            return result;
>        }
>    }
>
>    //
>    // send ioclt requests
>    //
>    if(!DeviceIoControl (deviceInfo->hControlDevice,
>          IOCTL_NETVMINI_READ_DATA,
>          NULL, 0,
>          NULL, 0,
>          &bytes, NULL)) {
>       Display(TEXT("Read IOCTL to %ws failed"), deviceInfo->DeviceName);
>
>    } else {
>       Display(TEXT("Read IOCTL to %ws succeeded"),
> deviceInfo->DeviceName);
>       result = TRUE;
>    }
>
>    if(!DeviceIoControl (deviceInfo->hControlDevice,
>          IOCTL_NETVMINI_WRITE_DATA,
>          NULL, 0,
>          NULL, 0,
>          &bytes, NULL)) {
>       Display(TEXT("Write IOCTL to %ws failed"), deviceInfo->DeviceName);
>
>    } else {
>       Display(TEXT("Write IOCTL to %ws succeeded"),
> deviceInfo->DeviceName);
>       result = TRUE;
>    }
>
>    return result;
> }
Are all your drivers up to date? click for free checkup

Author
8 Apr 2005 3:37 PM
Michael Allen
Thanks very much for your quick reply...this looks like exactly what I need.

Regards,

Michael

Show quoteHide quote
"Nassos" wrote:

> Hi Michael,
> What your are trying is a bit difficult, i can give you a pointer on what to
> search so, finde help on DllImport that allow you to access  OS SDK.
> looks like that :
> [System.Runtime.InteropServices.DllImport("Kernel32.dll",
> SetLastError=true)]
>
> public extern static int DeviceIoControl(IntPtr hDevice, uint IoControlCode,
>
> IntPtr lpInBuffer, uint InBufferSize,
>
> IntPtr lpOutBuffer, uint nOutBufferSize,
>
> ref uint lpBytesReturned,
>
> IntPtr lpOverlapped);
>
> Hope that help
>
> "Michael Allen" <MichaelAl***@discussions.microsoft.com> wrote in message
> news:BC97B852-35D0-40AE-A9D7-4F079E09DB5A@microsoft.com...
> >I would like to perform something similar to the below function in C# .NET.
> > The C++ below code is from a Microsoft DDK sample driver application.
> > Specifically, I would like to perform Device I/O Control from a .NET
> > application.
> >
> > Is there any way to do this in C#. I have tried Googling to no avail.
> >
> > Thanks,
> >
> > Michael Allen
> >
> > BOOLEAN
> > SendIoctlToControlDevice(
> >    IN PDEVICE_INFO deviceInfo)
> > {
> >    BOOLEAN result = FALSE;
> >    UINT bytes;
> >
> >    //
> >    // Open handle to the control device, if it's not already opened.
> > Please
> >    // note that even a non-admin user can open handle to the device with
> >    // FILE_READ_ATTRIBUTES | SYNCHRONIZE DesiredAccess and send IOCTLs if
> > the
> >    // IOCTL is defined with FILE_ANY_ACCESS. So for better security avoid
> >    // specifying FILE_ANY_ACCESS in your IOCTL defintions.
> >    // If you open the device with GENERIC_READ, you can send IOCTL with
> >    // FILE_READ_DATA access rights. If you open the device with
> > GENERIC_WRITE,
> >    // you can sedn ioctl with FILE_WRITE_DATA access rights.
> >    //
> >    //
> >
> >    if(deviceInfo->hControlDevice != INVALID_HANDLE_VALUE) {
> >
> >        deviceInfo->hControlDevice = CreateFile (
> >            TEXT("\\\\.\\NETVMINI"),
> >            GENERIC_READ | GENERIC_WRITE,//FILE_READ_ATTRIBUTES |
> > SYNCHRONIZE,
> >            FILE_SHARE_READ,
> >            NULL, // no SECURITY_ATTRIBUTES structure
> >            OPEN_EXISTING, // No special create flags
> >            FILE_ATTRIBUTE_NORMAL, // No special attributes
> >            NULL);
> >
> >        if (INVALID_HANDLE_VALUE == deviceInfo->hControlDevice) {
> >            Display(TEXT("Failed to open the control device: %ws"),
> >                    deviceInfo->DeviceName);
> >            return result;
> >        }
> >    }
> >
> >    //
> >    // send ioclt requests
> >    //
> >    if(!DeviceIoControl (deviceInfo->hControlDevice,
> >          IOCTL_NETVMINI_READ_DATA,
> >          NULL, 0,
> >          NULL, 0,
> >          &bytes, NULL)) {
> >       Display(TEXT("Read IOCTL to %ws failed"), deviceInfo->DeviceName);
> >
> >    } else {
> >       Display(TEXT("Read IOCTL to %ws succeeded"),
> > deviceInfo->DeviceName);
> >       result = TRUE;
> >    }
> >
> >    if(!DeviceIoControl (deviceInfo->hControlDevice,
> >          IOCTL_NETVMINI_WRITE_DATA,
> >          NULL, 0,
> >          NULL, 0,
> >          &bytes, NULL)) {
> >       Display(TEXT("Write IOCTL to %ws failed"), deviceInfo->DeviceName);
> >
> >    } else {
> >       Display(TEXT("Write IOCTL to %ws succeeded"),
> > deviceInfo->DeviceName);
> >       result = TRUE;
> >    }
> >
> >    return result;
> > }
>
>
>

Bookmark and Share