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