473,396 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

DeviceIoControl from C# ?

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
2 15515
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: ewoo | last post by:
I'm trying to write a wrapper in csharp to wrap DeviceIoControl() win32 method for IOCTL_VIDEO_QUERY_DISPLAY_BRIGHTNESS control code--without much luck. I've seen lots of examples out there for...
2
by: Jules Crown | last post by:
Everyone, greetings, newbee here. What I'm trying to do is to compress files on NTFS from C#. I've been fishing around for info and gathered the hereafter. I'd like to know what's wrong with...
3
by: Jacky | last post by:
Hi, I am trying to make network card interface with VB.NET 2002. I use DeviceIOControl-function. I have tried to define inbuffer and outbuffer using byte array and it's pointer. The second I...
1
by: Pixie | last post by:
I am trying to query the change journal using the deviceIOControl API. The API doesn't return an error, but all of the values in the output buffer are zero, and they shouldn't be. My code is below. I...
0
by: Pixie | last post by:
We are successfully getting a handle to a drive using createfile then using that handle to query the change journal using DeviceIOControl with the paramter FSCTL_QUERY_USN_DATA. However when we try...
1
by: Juan Pedro Gonzalez | last post by:
Helo, I'm having problems here with the input buffer.... Ive defined the API call as: <System.Runtime.InteropServices.DllImport("kernel32", SetLastError:=True)> _ Private Shared Function...
5
by: Lou | last post by:
is there a VB .NET way to use the API "DeviceIoControl"? -Lou
0
by: Andrew | last post by:
Hello I am trying to port some code and I am running into some issues I may or may not be able to solve on my own and would appreciate your help Basically I am trying to open the Tun Driver...
4
by: =?Utf-8?B?TWFyaW5h?= | last post by:
Does any know any sample of how to do a basic DeviceIoControl with something like IOCTL_BATTERY_GETSYSTEMPOWERSTATUSEX2 in C# I have been stuck all week :( and google doesnt yield anything of...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.