By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,923 Members | 2,023 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,923 IT Pros & Developers. It's quick & easy.

DeviceIoControl from C# ?

P: n/a
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;
}
Nov 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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" <Mi**********@discussions.microsoft.com> wrote in message
news:BC**********************************@microsof t.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;
}

Nov 17 '05 #2

P: n/a
Thanks very much for your quick reply...this looks like exactly what I need.

Regards,

Michael

"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" <Mi**********@discussions.microsoft.com> wrote in message
news:BC**********************************@microsof t.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;
}


Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.