I have a C/C# mixed project composed by a kernel driver written in C and an application written in C#. Right now, to create the kernel driver service and run it, i'm using the native APIs OpenSCManager and CreateService with the interop services, the snippet of code is:
string sDriverFileName = @"c:\path\of\my\driver.sys";
IntPtr hServiceManager = IntPtr.Zero;
IntPtr hService = IntPtr.Zero;
if (System.IO.File.Exists(sDriverFileName) == false)
throw new IOException( string.Format( "Driver file '{0}' does not exist.", sDriverFileName ) );
hServiceManager = WIN32.OpenSCManager(IntPtr.Zero, IntPtr.Zero, WIN32.SC_MANAGER_ALL_ACCESS);
if (hServiceManager == IntPtr.Zero)
throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not open service manager.");
hService = WIN32.CreateService
(
hServiceManager,
"DriverServiceName",
"Driver Service Display Name",
WIN32.SERVICE_ACCESS.SERVICE_ALL_ACCESS,
WIN32.SERVICE_TYPE.SERVICE_KERNEL_DRIVER,
WIN32.SERVICE_START.SERVICE_DEMAND_START,
WIN32.SERVICE_ERROR.SERVICE_ERROR_NORMAL,
sDriverFileName,
IntPtr.Zero,
IntPtr.Zero,
IntPtr.Zero,
IntPtr.Zero,
IntPtr.Zero
);
But i'd like ( if possible ) to make this fully managed having only the driver written in C and relying on system API/natives/whatever.
I know how to use the ServiceController to manage ( start/stop/etc ) an already installed windows service, but i have no clue on how to use it ( or any other class ) to create a service for a kernel driver.
Thanks for your help.