0

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.

Simone Margaritelli
  • 4,584
  • 10
  • 45
  • 70
  • I guess the fully managed option is [ServiceInstaller](http://msdn.microsoft.com/en-us/library/system.serviceprocess.serviceinstaller.aspx) but I doubt that will allow you to install a kernel driver... – rene Dec 29 '12 at 19:22
  • i don't think i can use it ... i already use this class for another service ( fully managed ) and it works flawlessy, but i can't find any documentation on how to specify a kernel driver file to the installer or something similar ... – Simone Margaritelli Dec 29 '12 at 19:25
  • The code you have is then the only alternative unfortunately – rene Dec 29 '12 at 19:27
  • i see ... well, never say never, maybe someone will give me a solution :D – Simone Margaritelli Dec 29 '12 at 19:29
  • It is certainly worth asking, and who knows what features .Net 7 brings ;-) – rene Dec 29 '12 at 19:33

0 Answers0