DeviceIoControl from C# ?

G

Guest

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;
}
 
N

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
 
G

Guest

Thanks very much for your quick reply...this looks like exactly what I need.

Regards,

Michael

Nassos said:
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 said:
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;
}
 

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