3

I'm developing a Windows Application using C#. I wanted to run a .sys file (driver basically) at the backEnd as my Code makes use of certain functions to implement the output.

How can i write a code in C# ? If the .sys file is placed in "Bin" folder, it just does not run as I guess it can be made to run as a service.

Consider the name - "someDriver.sys" (I'm using Win7 OS here)

Can I run this .sys file as Service in Kernel mode? My application is a very simple one and does not have installer (I do not want to have it too). Can you pl help how I can tackle this situation where I need the presence of .sys file for my functions to work.

stack_pointer is EXTINCT
  • 2,263
  • 12
  • 49
  • 59
  • It is not clear from your question what you are trying to achieve, but note that [you cannot write Windows drivers in C#](http://stackoverflow.com/a/1001836/21567) (or any other managed language). – Christian.K Mar 16 '12 at 10:13
  • Basically, in my Windows application, I'm trying to import a .DLL (designed in C++) to achieve some functionality. But this particular DLL wants me to have a driver (.sys file) present and running and that I can use the functions in the DLL. Hence, the need for me to run the driver at the backend. – stack_pointer is EXTINCT Mar 16 '12 at 10:18
  • Why not install the driver with your app in an installer package? – spender Mar 16 '12 at 10:19
  • okay. my application is kinda tool where I do not want to have any installer and I've only an .exe (which user double clicks and makes use of the tool) – stack_pointer is EXTINCT Mar 16 '12 at 10:24
  • That doesn't sound like a service. Do you want an exe that the user double clicks to start, or do you want a service? – David Heffernan Mar 16 '12 at 10:46
  • EXE is my application (or tool). My appln has to get my driver(.sys) loaded in order to use its functionality as stated above. Hence, I decided to run the driver as a SERVICE so that my appln can function properly.Is this wrong? – stack_pointer is EXTINCT Mar 16 '12 at 10:48
  • You need to pinvoke OpenSCManager, OpenService to check if it is already running, CreateService with SERVICE_KERNEL_DRIVER if is not, StartService, CloseServiceHandle. – Hans Passant Mar 16 '12 at 12:59
  • Hi Hans, Could you pls provide a detailed answer instead of the comment? Thanks – stack_pointer is EXTINCT Mar 16 '12 at 14:18
  • 1
    A driver is not a service. Drivers run in kernel mode. Services run in user mode. – Raymond Chen Mar 16 '12 at 14:37
  • Raymond - I've edited my query. – stack_pointer is EXTINCT Mar 20 '12 at 17:47
  • Either way - you're going to need admin permissions to say the least to run such a thing on Win7. If you want to have a 'no-setup' app that's usually a problem. – NSGaga-mostly-inactive Mar 24 '12 at 13:30

2 Answers2

3

Have you tried to see how it's done in this article How to install driver dynamically or install driver as a service for Windows
You must adapt calls using interop.
Use this tools to help you in Interop definition : PInvoke Interop Assistant

Ajay
  • 18,086
  • 12
  • 59
  • 105
lsalamon
  • 7,998
  • 6
  • 50
  • 63
0

You can use the code from this answer and just change SERVICE_WIN32_OWN_PROCESS to SERVICE_KERNEL_DRIVER (0x00000001). Then you can just do

ServiceInsatller.InstallAndStart("yourServiceName", "Some Description", "C:\path\to\driver.sys");

This works for me.

Tomer
  • 3,149
  • 23
  • 26