|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
DeviceIoControl from C# ?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; } 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; > } 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; > > } > > >
Other interesting topics
Suggestions to control concurrent users...
Closing a Form Fundamental question about bound controls debugging an application and using the Output window? C# program with repeating records printed in Crystal report open binary file and search hex string Overriding Properties Deep/Shallow copy when returning objects (DataTable) from methods Initialize Array c++ question |
|||||||||||||||||||||||