473,395 Members | 1,668 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,395 software developers and data experts.

Is C# support load device driver?

I wrote a simple virtual device driver int15.sys, Is C# support load the
device driver from AP?
Nov 16 '05 #1
12 13716
Steve wrote:
I wrote a simple virtual device driver int15.sys, Is C# support load the
device driver from AP?


Aren't drivers used by the OS (windows) ? So how did you plan to use this
driver?

Frans.

--
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET Blog: http://weblogs.asp.net/fbouma
Microsoft C# MVP
Nov 16 '05 #2
Nope, yoou can interact directly with them vice the DeviceIOControl API. I've done this in the past to write to area of the physical disk that win32 doesn't support.

So I guess to be able to interact with a device driver from C# you'd have to P/Invoke to DeviceIOControl

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<xn***************@msnews.microsoft.com>

Steve wrote:
I wrote a simple virtual device driver int15.sys, Is C# support load the
device driver from AP?


Aren't drivers used by the OS (windows) ? So how did you plan to use this
driver?

Frans.

--
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET Blog: http://weblogs.asp.net/fbouma
Microsoft C# MVP

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.766 / Virus Database: 513 - Release Date: 17/09/2004

[microsoft.public.dotnet.languages.csharp]
Nov 16 '05 #3
Yes, you are right, after I load device driver, I can use DeviceIOControl, I
wrote a virtual device driver, int15h.sys,
the purpose of this device driver is provide a interface to BIOS int15h, we
also modify the BIOS int15h, support
wireless/bluetooth power on/off, AP through virtual device driver, can power
on/off PC system wireless/bluetooth.

I need to load int15h.sys before I use DeviceIOControl, in C++, I found some
API
int SCManager = OpenSCManager(null, null, SC_MANAGER_ALL_ACCESS);
GetServiceName(Path, Driver, DeviceName);
int dwStatus = DriverInstall(SCManager, Path, Driver);
int dwStatus1 = DriverStart(SCManager, Driver);
can load the driver, my question is "Can C# support load device driver?"
"Richard Blewett [DevelopMentor]" wrote:
Nope, yoou can interact directly with them vice the DeviceIOControl API. I've done this in the past to write to area of the physical disk that win32 doesn't support.

So I guess to be able to interact with a device driver from C# you'd have to P/Invoke to DeviceIOControl

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<xn***************@msnews.microsoft.com>

Steve wrote:
> I wrote a simple virtual device driver int15.sys, Is C# support load the
> device driver from AP?


Aren't drivers used by the OS (windows) ? So how did you plan to use this
driver?

Frans.

--
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET Blog: http://weblogs.asp.net/fbouma
Microsoft C# MVP

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.766 / Virus Database: 513 - Release Date: 17/09/2004

[microsoft.public.dotnet.languages.csharp]

Nov 16 '05 #4
Can you not use CreateFile to open the device?

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<EA**********************************@microsoft.co m>

Yes, you are right, after I load device driver, I can use DeviceIOControl, I
wrote a virtual device driver, int15h.sys,
the purpose of this device driver is provide a interface to BIOS int15h, we
also modify the BIOS int15h, support
wireless/bluetooth power on/off, AP through virtual device driver, can power
on/off PC system wireless/bluetooth.

I need to load int15h.sys before I use DeviceIOControl, in C++, I found some
API
int SCManager = OpenSCManager(null, null, SC_MANAGER_ALL_ACCESS);
GetServiceName(Path, Driver, DeviceName);
int dwStatus = DriverInstall(SCManager, Path, Driver);
int dwStatus1 = DriverStart(SCManager, Driver);
can load the driver, my question is "Can C# support load device driver?"
"Richard Blewett [DevelopMentor]" wrote:
Nope, yoou can interact directly with them vice the DeviceIOControl API. I've done this in the past to write to area of the physical disk that win32 doesn't support.

So I guess to be able to interact with a device driver from C# you'd have to P/Invoke to DeviceIOControl

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<xn***************@msnews.microsoft.com>

Steve wrote:
I wrote a simple virtual device driver int15.sys, Is C# support load the
device driver from AP?


Aren't drivers used by the OS (windows) ? So how did you plan to use this
driver?

Frans.

--
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET Blog: http://weblogs.asp.net/fbouma
Microsoft C# MVP

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.766 / Virus Database: 513 - Release Date: 17/09/2004

[microsoft.public.dotnet.languages.csharp]


---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.766 / Virus Database: 513 - Release Date: 17/09/2004

[microsoft.public.dotnet.languages.csharp]
Nov 16 '05 #5
Yes, before DeviceIOControl, use CreateFile to get a handle.
if not support load driver in C#, is anyone know the dllentry for
GetServiceName
DriverInstall
DriverStart
For I only found OpenSCManager from MSDN
[DllImport("advapi32.dll")]
int SCManager = OpenSCManager(null, null, SC_MANAGER_ALL_ACCESS);

"Richard Blewett [DevelopMentor]" wrote:
Can you not use CreateFile to open the device?

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<EA**********************************@microsoft.co m>

Yes, you are right, after I load device driver, I can use DeviceIOControl, I
wrote a virtual device driver, int15h.sys,
the purpose of this device driver is provide a interface to BIOS int15h, we
also modify the BIOS int15h, support
wireless/bluetooth power on/off, AP through virtual device driver, can power
on/off PC system wireless/bluetooth.

I need to load int15h.sys before I use DeviceIOControl, in C++, I found some
API
int SCManager = OpenSCManager(null, null, SC_MANAGER_ALL_ACCESS);
GetServiceName(Path, Driver, DeviceName);
int dwStatus = DriverInstall(SCManager, Path, Driver);
int dwStatus1 = DriverStart(SCManager, Driver);
can load the driver, my question is "Can C# support load device driver?"
"Richard Blewett [DevelopMentor]" wrote:
> Nope, yoou can interact directly with them vice the DeviceIOControl API. I've done this in the past to write to area of the physical disk that win32 doesn't support.
>
> So I guess to be able to interact with a device driver from C# you'd have to P/Invoke to DeviceIOControl
>
> Regards
>
> Richard Blewett - DevelopMentor
> http://staff.develop.com/richardb/weblog
>
> nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<xn***************@msnews.microsoft.com>
>
> Steve wrote:
>
> > I wrote a simple virtual device driver int15.sys, Is C# support load the
> > device driver from AP?

>
> Aren't drivers used by the OS (windows) ? So how did you plan to use this
> driver?
>
> Frans.
>
> --
> Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
> My .NET Blog: http://weblogs.asp.net/fbouma
> Microsoft C# MVP
>
> ---
> Incoming mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.766 / Virus Database: 513 - Release Date: 17/09/2004
>
>
>
> [microsoft.public.dotnet.languages.csharp]
>


---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.766 / Virus Database: 513 - Release Date: 17/09/2004

[microsoft.public.dotnet.languages.csharp]

Nov 16 '05 #6
Richard Blewett [DevelopMentor] wrote:
Nope, yoou can interact directly with them vice the DeviceIOControl API.
I've done this in the past to write to area of the physical disk that win32
doesn't support.

So I guess to be able to interact with a device driver from C# you'd have
to P/Invoke to DeviceIOControl
(just to make it clear to me, not to nittpick ;))
But isn't this just a wrapper around Windows' device manager? There is no
way you will be able to poke into hardware without a kernel space module, as
everything is virtualized: you can't simply throw an interrupt or set an
address to a value to change some hardware's internal settings (at least
that's what I know of it).

Frans.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<xn0dnlt0
z3*******@msnews.microsoft.com>

Steve wrote:
> I wrote a simple virtual device driver int15.sys, Is C# support load the
> device driver from AP?


Aren't drivers used by the OS (windows) ? So how did you plan to use this
driver?

--
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET Blog: http://weblogs.asp.net/fbouma
Microsoft C# MVP
Nov 16 '05 #7
DeviceIOControl is simply a way of passing "op-codes" to a kernel mode device driver and retrieving the results. You obviously can't *write* a device driver in C# but you can interrogate a device driver via pinvoke.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<xn***************@msnews.microsoft.com>

Richard Blewett [DevelopMentor] wrote:
Nope, yoou can interact directly with them vice the DeviceIOControl API.
I've done this in the past to write to area of the physical disk that win32
doesn't support.

So I guess to be able to interact with a device driver from C# you'd have
to P/Invoke to DeviceIOControl
(just to make it clear to me, not to nittpick ;))
But isn't this just a wrapper around Windows' device manager? There is no
way you will be able to poke into hardware without a kernel space module, as
everything is virtualized: you can't simply throw an interrupt or set an
address to a value to change some hardware's internal settings (at least
that's what I know of it).

Frans.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<xn0dnlt0
z3*******@msnews.microsoft.com>

Steve wrote:
I wrote a simple virtual device driver int15.sys, Is C# support load the
device driver from AP?


Aren't drivers used by the OS (windows) ? So how did you plan to use this
driver?

--
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET Blog: http://weblogs.asp.net/fbouma
Microsoft C# MVP

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.766 / Virus Database: 513 - Release Date: 17/09/2004

[microsoft.public.dotnet.languages.csharp]
Nov 16 '05 #8

"Steve" <St***@discussions.microsoft.com> wrote in message
news:D4**********************************@microsof t.com...
Yes, before DeviceIOControl, use CreateFile to get a handle.
if not support load driver in C#, is anyone know the dllentry for
GetServiceName
DriverInstall
DriverStart
For I only found OpenSCManager from MSDN
[DllImport("advapi32.dll")]
int SCManager = OpenSCManager(null, null, SC_MANAGER_ALL_ACCESS);

Drivers are no different than services in Windows, they are controled
(loaded, unloaded etc..) by the SCM, but before you can do this they need to
be installed/registered into the SC database (Registry). You can do this
from C# using the System.Management classes (and the WMI class
Win32_BaseService).
First you have to create a Win32_BaseService class describing your driver
(supplying stuff like PathName="\Mydrivers\MyDriver.sys") by calling
"Create" on the Win32_BaseService WMI Class.
Once you have this (registration) done you can call any of the methods like
"StartService", "StopService", "ChangeService" on an instance of this WMI
class.

Willy.
Nov 16 '05 #9
All,

Thanks for the help about this issue.

Willy,

I think you answer my question, for I am new in C#, I will try figure out
how to implement based on your suggestion, really appreciated.

"Willy Denoyette [MVP]" wrote:

"Steve" <St***@discussions.microsoft.com> wrote in message
news:D4**********************************@microsof t.com...
Yes, before DeviceIOControl, use CreateFile to get a handle.
if not support load driver in C#, is anyone know the dllentry for
GetServiceName
DriverInstall
DriverStart
For I only found OpenSCManager from MSDN
[DllImport("advapi32.dll")]
int SCManager = OpenSCManager(null, null, SC_MANAGER_ALL_ACCESS);

Drivers are no different than services in Windows, they are controled
(loaded, unloaded etc..) by the SCM, but before you can do this they need to
be installed/registered into the SC database (Registry). You can do this
from C# using the System.Management classes (and the WMI class
Win32_BaseService).
First you have to create a Win32_BaseService class describing your driver
(supplying stuff like PathName="\Mydrivers\MyDriver.sys") by calling
"Create" on the Win32_BaseService WMI Class.
Once you have this (registration) done you can call any of the methods like
"StartService", "StopService", "ChangeService" on an instance of this WMI
class.

Willy.

Nov 16 '05 #10
Richard Blewett [DevelopMentor] wrote:
DeviceIOControl is simply a way of passing "op-codes" to a kernel mode
device driver and retrieving the results. You obviously can't write a
device driver in C# but you can interrogate a device driver via pinvoke.
ok thanks for the info, Richard :)

FB

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<xn0dnn7g
y4*******@msnews.microsoft.com>

Richard Blewett [DevelopMentor] wrote:
> Nope, yoou can interact directly with them vice the DeviceIOControl API.
> I've done this in the past to write to area of the physical disk that

win32 > doesn't support.
>
> So I guess to be able to interact with a device driver from C# you'd have
> to P/Invoke to DeviceIOControl


(just to make it clear to me, not to nittpick ;))
But isn't this just a wrapper around Windows' device manager? There is no
way you will be able to poke into hardware without a kernel space module,
as everything is virtualized: you can't simply throw an interrupt or set an
address to a value to change some hardware's internal settings (at least
that's what I know of it).

Frans.
>
> Regards
>
> Richard Blewett - DevelopMentor
> http://staff.develop.com/richardb/weblog
>
>
>

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<xn0dnlt0
z3*******@msnews.microsoft.com> >
> Steve wrote:
>
> > I wrote a simple virtual device driver int15.sys, Is C# support load

the > > device driver from AP?
>
> Aren't drivers used by the OS (windows) ? So how did you plan to use this
> driver?

Nov 16 '05 #11

"Steve" <St***@discussions.microsoft.com> wrote in message
news:82**********************************@microsof t.com...
All,

Thanks for the help about this issue.

Willy,

I think you answer my question, for I am new in C#, I will try figure out
how to implement based on your suggestion, really appreciated.

"Willy Denoyette [MVP]" wrote:


Steve,

I would never use C# for this, but here's a sample class that illustrates
how to manage driver loading/unloading using both PInvoke interop and WMI.

Note this is no production code, it's only meant to illustrate how to:
- load a driver.
- start the driver
- open the device
- stop the driver
- unload the driver
using C#.
usage:
Win32Driver driver = new Win32Driver(driverName,
@"c:\\folder\\mydriver.sys");
if (driver.LoadDeviceDriver()){
IntPtr handle = driver.OpenDevice();
// use device using ....DeviceIoControl(handle,....) see class code for
signature
}
//unload when done
driver.UnloadDeviceDriver();


// the class file
public sealed class Win32Driver : IDisposable
{
string driverName;
string execPath;
IntPtr fileHandle;
public Win32Driver(string driver, string driverExecPath)
{
this.driverName = driver;
this.execPath = driverExecPath;
}
~Win32Driver()
{
// BUG - should never rely on finalizer to clean-up unmanaged resources
Dispose();
}
private void CloseStuff()
{
if(fileHandle != INVALID_HANDLE_VALUE)
{
fileHandle = INVALID_HANDLE_VALUE;
CloseHandle(fileHandle);
}
}

public void Dispose()
{
CloseStuff();
GC.SuppressFinalize(this);
}

private readonly static IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
private const int STANDARD_RIGHTS_REQUIRED = 0x000F0000;
private const int SC_MANAGER_CONNECT = 0x0001;
private const int SC_MANAGER_CREATE_SERVICE = 0x0002;
private const int SC_MANAGER_ENUMERATE_SERVICE = 0x0004;
private const int SC_MANAGER_LOCK = 0x0008;
private const int SC_MANAGER_QUERY_LOCK_STATUS = 0x0010;
private const int SC_MANAGER_MODIFY_BOOT_CONFIG =0x0020;
private const int SC_MANAGER_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED |
SC_MANAGER_CONNECT |
SC_MANAGER_CREATE_SERVICE |
SC_MANAGER_ENUMERATE_SERVICE |
SC_MANAGER_LOCK |
SC_MANAGER_QUERY_LOCK_STATUS |
SC_MANAGER_MODIFY_BOOT_CONFIG;

private const int SERVICE_QUERY_CONFIG = 0x0001;
private const int SERVICE_CHANGE_CONFIG = 0x0002;
private const int SERVICE_QUERY_STATUS = 0x0004;
private const int SERVICE_ENUMERATE_DEPENDENTS = 0x0008;
private const int SERVICE_START = 0x0010;
private const int SERVICE_STOP = 0x0020;
private const int SERVICE_PAUSE_CONTINUE = 0x0040;
private const int SERVICE_INTERROGATE = 0x0080;
private const int SERVICE_USER_DEFINED_CONTROL = 0x0100;

private const int SERVICE_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED
|
SERVICE_QUERY_CONFIG |
SERVICE_CHANGE_CONFIG |
SERVICE_QUERY_STATUS |
SERVICE_ENUMERATE_DEPENDENTS |
SERVICE_START |
SERVICE_STOP |
SERVICE_PAUSE_CONTINUE |
SERVICE_INTERROGATE |
SERVICE_USER_DEFINED_CONTROL;

private const int SERVICE_DEMAND_START = 0x00000003;
private const int SERVICE_KERNEL_DRIVER = 0x00000001;
private const int SERVICE_ERROR_NORMAL = 0x00000001;

private const uint GENERIC_READ = 0x80000000;
private const uint FILE_SHARE_READ = 1;
private const uint FILE_SHARE_WRITE = 2;
private const uint OPEN_EXISTING = 3;
private const uint IOCTL_SHOCKMGR_READ_ACCELEROMETER_DATA = 0x733fc;
private const int FACILITY_WIN32 = unchecked((int)0x80070000);
private IntPtr handle = INVALID_HANDLE_VALUE;

[DllImport("advapi32", SetLastError = true)]
internal static extern IntPtr OpenSCManager(string machineName, string
databaseName, uint dwDesiredAccess);
[DllImport("advapi32", SetLastError = true)]
internal static extern IntPtr CreateService(IntPtr hSCManager, string
serviceName, string displayName,
uint dwDesiredAccess, uint serviceType, uint startType, uint
errorControl,
string lpBinaryPathName, string lpLoadOrderGroup, string lpdwTagId,
string lpDependencies,
string lpServiceStartName, string lpPassword);

[DllImport("advapi32")]
internal static extern bool CloseServiceHandle(IntPtr handle);
[DllImport("kernel32", SetLastError = true)]
internal static extern IntPtr CreateFile(string lpFileName, uint
dwDesiredAccess, uint dwShareMode, IntPtr lpSecurityAttributes, uint
dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile);

[DllImport("kernel32")]
internal static extern void CloseHandle(IntPtr handle);

[DllImport("kernel32", SetLastError = true)]
private static extern bool DeviceIoControl(IntPtr hDevice, uint
dwIoControlCode, IntPtr lpInBuffer, uint nInBufferSize, IntPtr lpOutBuffer,
uint nOutBufferSize, ref uint lpBytesReturned, IntPtr lpOverlapped);
internal bool LoadDeviceDriver()
{
IntPtr scHandle = OpenSCManager(null, null, SC_MANAGER_ALL_ACCESS);
if (scHandle != INVALID_HANDLE_VALUE)
{
IntPtr hService = CreateService(scHandle, this.driverName,
this.driverName, SERVICE_ALL_ACCESS
, SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START,SERVICE_ERROR_NORMAL
,execPath, null, null, null, null, null);
if (hService != IntPtr.Zero)
{
CloseServiceHandle(hService); // close both handles
CloseServiceHandle(scHandle);
// Start the driver using System.Management (WMI)
if (ExecuteSCMOperationOnDriver(this.driverName, "StartService") == 0)
return true;
}
else
if (Marshal.GetLastWin32Error()== 1073) // Driver/Service already in DB
{
CloseServiceHandle(scHandle);
// Start the driver using System.Management (WMI)
if (ExecuteSCMOperationOnDriver(this.driverName, "StartService") == 0)
return true;
}
Marshal.ThrowExceptionForHR(HRESULT_FROM_WIN32(Mar shal.GetLastWin32Error()));
}
return false;
}

internal bool UnloadDeviceDriver()
{
int ret = 0;
if (fileHandle != IntPtr.Zero && fileHandle != INVALID_HANDLE_VALUE)
{
CloseHandle(fileHandle);
}
if ((ret = ExecuteSCMOperationOnDriver(driverName, "StopService")) == 0)
{
ret = ExecuteSCMOperationOnDriver(driverName, "Delete");
}
if (ret != 0)
{
return false;
}
return true;
}

private static int ExecuteSCMOperationOnDriver(string driverName, string
operation)
{
ManagementPath path = new ManagementPath();
path.Server = ".";
path.NamespacePath = @"root\CIMV2";
path.RelativePath = @"Win32_BaseService.Name='" + driverName +"'";
using(ManagementObject o = new ManagementObject(path))
{
ManagementBaseObject outParams = o.InvokeMethod(operation,
null, null);
return Convert.ToInt32(outParams.Properties["ReturnValue"].Value);
}
}
internal IntPtr OpenDevice()
{
fileHandle = CreateFile("\\\\.\\" + driverName, GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0,
IntPtr.Zero);
if(handle == INVALID_HANDLE_VALUE)
{
Marshal.ThrowExceptionForHR(HRESULT_FROM_WIN32(Mar shal.GetLastWin32Error()));
}
return fileHandle;
}
private static int HRESULT_FROM_WIN32(int x)
{
return x <= 0 ? x : ((x & 0x0000FFFF) | FACILITY_WIN32);
}
}
Nov 16 '05 #12
Willy,

Thanks a lot, I tried your solution and used
ServiceController[] scServices;
scServices = ServiceController.GetDevices();

I can see my driver is loaded.

B. RGDS
Steve
"Willy Denoyette [MVP]" wrote:

"Steve" <St***@discussions.microsoft.com> wrote in message
news:82**********************************@microsof t.com...
All,

Thanks for the help about this issue.

Willy,

I think you answer my question, for I am new in C#, I will try figure out
how to implement based on your suggestion, really appreciated.

"Willy Denoyette [MVP]" wrote:


Steve,

I would never use C# for this, but here's a sample class that illustrates
how to manage driver loading/unloading using both PInvoke interop and WMI.

Note this is no production code, it's only meant to illustrate how to:
- load a driver.
- start the driver
- open the device
- stop the driver
- unload the driver
using C#.
usage:
Win32Driver driver = new Win32Driver(driverName,
@"c:\\folder\\mydriver.sys");
if (driver.LoadDeviceDriver()){
IntPtr handle = driver.OpenDevice();
// use device using ....DeviceIoControl(handle,....) see class code for
signature
}
//unload when done
driver.UnloadDeviceDriver();


// the class file
public sealed class Win32Driver : IDisposable
{
string driverName;
string execPath;
IntPtr fileHandle;
public Win32Driver(string driver, string driverExecPath)
{
this.driverName = driver;
this.execPath = driverExecPath;
}
~Win32Driver()
{
// BUG - should never rely on finalizer to clean-up unmanaged resources
Dispose();
}
private void CloseStuff()
{
if(fileHandle != INVALID_HANDLE_VALUE)
{
fileHandle = INVALID_HANDLE_VALUE;
CloseHandle(fileHandle);
}
}

public void Dispose()
{
CloseStuff();
GC.SuppressFinalize(this);
}

private readonly static IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
private const int STANDARD_RIGHTS_REQUIRED = 0x000F0000;
private const int SC_MANAGER_CONNECT = 0x0001;
private const int SC_MANAGER_CREATE_SERVICE = 0x0002;
private const int SC_MANAGER_ENUMERATE_SERVICE = 0x0004;
private const int SC_MANAGER_LOCK = 0x0008;
private const int SC_MANAGER_QUERY_LOCK_STATUS = 0x0010;
private const int SC_MANAGER_MODIFY_BOOT_CONFIG =0x0020;
private const int SC_MANAGER_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED |
SC_MANAGER_CONNECT |
SC_MANAGER_CREATE_SERVICE |
SC_MANAGER_ENUMERATE_SERVICE |
SC_MANAGER_LOCK |
SC_MANAGER_QUERY_LOCK_STATUS |
SC_MANAGER_MODIFY_BOOT_CONFIG;

private const int SERVICE_QUERY_CONFIG = 0x0001;
private const int SERVICE_CHANGE_CONFIG = 0x0002;
private const int SERVICE_QUERY_STATUS = 0x0004;
private const int SERVICE_ENUMERATE_DEPENDENTS = 0x0008;
private const int SERVICE_START = 0x0010;
private const int SERVICE_STOP = 0x0020;
private const int SERVICE_PAUSE_CONTINUE = 0x0040;
private const int SERVICE_INTERROGATE = 0x0080;
private const int SERVICE_USER_DEFINED_CONTROL = 0x0100;

private const int SERVICE_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED
|
SERVICE_QUERY_CONFIG |
SERVICE_CHANGE_CONFIG |
SERVICE_QUERY_STATUS |
SERVICE_ENUMERATE_DEPENDENTS |
SERVICE_START |
SERVICE_STOP |
SERVICE_PAUSE_CONTINUE |
SERVICE_INTERROGATE |
SERVICE_USER_DEFINED_CONTROL;

private const int SERVICE_DEMAND_START = 0x00000003;
private const int SERVICE_KERNEL_DRIVER = 0x00000001;
private const int SERVICE_ERROR_NORMAL = 0x00000001;

private const uint GENERIC_READ = 0x80000000;
private const uint FILE_SHARE_READ = 1;
private const uint FILE_SHARE_WRITE = 2;
private const uint OPEN_EXISTING = 3;
private const uint IOCTL_SHOCKMGR_READ_ACCELEROMETER_DATA = 0x733fc;
private const int FACILITY_WIN32 = unchecked((int)0x80070000);
private IntPtr handle = INVALID_HANDLE_VALUE;

[DllImport("advapi32", SetLastError = true)]
internal static extern IntPtr OpenSCManager(string machineName, string
databaseName, uint dwDesiredAccess);
[DllImport("advapi32", SetLastError = true)]
internal static extern IntPtr CreateService(IntPtr hSCManager, string
serviceName, string displayName,
uint dwDesiredAccess, uint serviceType, uint startType, uint
errorControl,
string lpBinaryPathName, string lpLoadOrderGroup, string lpdwTagId,
string lpDependencies,
string lpServiceStartName, string lpPassword);

[DllImport("advapi32")]
internal static extern bool CloseServiceHandle(IntPtr handle);
[DllImport("kernel32", SetLastError = true)]
internal static extern IntPtr CreateFile(string lpFileName, uint
dwDesiredAccess, uint dwShareMode, IntPtr lpSecurityAttributes, uint
dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile);

[DllImport("kernel32")]
internal static extern void CloseHandle(IntPtr handle);

[DllImport("kernel32", SetLastError = true)]
private static extern bool DeviceIoControl(IntPtr hDevice, uint
dwIoControlCode, IntPtr lpInBuffer, uint nInBufferSize, IntPtr lpOutBuffer,
uint nOutBufferSize, ref uint lpBytesReturned, IntPtr lpOverlapped);
internal bool LoadDeviceDriver()
{
IntPtr scHandle = OpenSCManager(null, null, SC_MANAGER_ALL_ACCESS);
if (scHandle != INVALID_HANDLE_VALUE)
{
IntPtr hService = CreateService(scHandle, this.driverName,
this.driverName, SERVICE_ALL_ACCESS
, SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START,SERVICE_ERROR_NORMAL
,execPath, null, null, null, null, null);
if (hService != IntPtr.Zero)
{
CloseServiceHandle(hService); // close both handles
CloseServiceHandle(scHandle);
// Start the driver using System.Management (WMI)
if (ExecuteSCMOperationOnDriver(this.driverName, "StartService") == 0)
return true;
}
else
if (Marshal.GetLastWin32Error()== 1073) // Driver/Service already in DB
{
CloseServiceHandle(scHandle);
// Start the driver using System.Management (WMI)
if (ExecuteSCMOperationOnDriver(this.driverName, "StartService") == 0)
return true;
}
Marshal.ThrowExceptionForHR(HRESULT_FROM_WIN32(Mar shal.GetLastWin32Error()));
}
return false;
}

internal bool UnloadDeviceDriver()
{
int ret = 0;
if (fileHandle != IntPtr.Zero && fileHandle != INVALID_HANDLE_VALUE)
{
CloseHandle(fileHandle);
}
if ((ret = ExecuteSCMOperationOnDriver(driverName, "StopService")) == 0)
{
ret = ExecuteSCMOperationOnDriver(driverName, "Delete");
}
if (ret != 0)
{
return false;
}
return true;
}

private static int ExecuteSCMOperationOnDriver(string driverName, string
operation)
{
ManagementPath path = new ManagementPath();
path.Server = ".";
path.NamespacePath = @"root\CIMV2";
path.RelativePath = @"Win32_BaseService.Name='" + driverName +"'";
using(ManagementObject o = new ManagementObject(path))
{
ManagementBaseObject outParams = o.InvokeMethod(operation,
null, null);
return Convert.ToInt32(outParams.Properties["ReturnValue"].Value);
}
}
internal IntPtr OpenDevice()
{
fileHandle = CreateFile("\\\\.\\" + driverName, GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0,
IntPtr.Zero);
if(handle == INVALID_HANDLE_VALUE)
{
Marshal.ThrowExceptionForHR(HRESULT_FROM_WIN32(Mar shal.GetLastWin32Error()));
}
return fileHandle;
}
private static int HRESULT_FROM_WIN32(int x)
{
return x <= 0 ? x : ((x & 0x0000FFFF) | FACILITY_WIN32);
}
}

Nov 16 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Wouter van Ooijen | last post by:
I want to use Python to interface with an USB HID device (not a keyboard or mouse, just something that uses the HID driver to avoid the need for a specific driver). Is this possible in pure Python...
4
by: dalewz | last post by:
Hi, Could sb kindly answer my following questions: Plan: I am trying to find a language to build a GUI to communicate with our device via serial port. Questions: 1. which language (VC++...
2
by: nbhalala | last post by:
Hello Friends, Hi Myself Naresh (B.E. Comp. Eng) from Mumbai... I'm a Linux Device Driver Writer... I would like to learn writing Driver of "USB Devices"...BUT before that I'm presently working...
2
by: Claus Konrad | last post by:
Hi Anybody got an impression whether .NET 2.0 supports USB ports? I know that the Serial (COM) and parallel ports (LPT1) are included, but how is the story on USB ports? /Claus
7
by: Ritu | last post by:
Hi All, Can any body please tell me how i can write a device driver using CSharp. Thanks, Ritu
0
by: am | last post by:
Hi, I have an USB hardware device that came with some (poor) software. I would like to write my own software to use the hardware in dotnet. There are no COM dll's to reference from the...
7
by: Nuno Magalhaes | last post by:
I have a problem loading a DLL file that is exactly in the same directory of the executable. The DLL is not in .NET format but can be accessed through P/Invoke. This never happened to me. My...
1
by: =?Utf-8?B?15DXldeo158=?= | last post by:
I have recently installed my windows XP, the process went smoothly, but I had encounterd some drivers issues after the installation. I managed to solve the ethernet adapter driver issue, but I...
2
by: Steve | last post by:
Hi All I have a POS program (Windows app) . I am using VB.net 2005 Pro The OPOS drivers freezes my program if Parallel port is configured for the Receipt printer and the Printer is not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.