19

I am making an application using c#.net. It contains a filesystem minifilter driver also. I want to install and uninstall this driver programmatically using c# .net. Normally i can install this using the .INF file (by right click + press install).but I want to install this programmatically. There is an SDK function InstallHinfSection() for installing the .inf driver . I am looking for a .net equivalent for this function.

Regards

Navaneeth

Navaneeth
  • 607
  • 2
  • 9
  • 19

4 Answers4

29

Try something like this:

using System.Runtime.InteropServices;

[DllImport("Setupapi.dll", EntryPoint="InstallHinfSection", CallingConvention=CallingConvention.StdCall)]
public static extern void InstallHinfSection(
    [In] IntPtr hwnd,
    [In] IntPtr ModuleHandle,
    [In, MarshalAs(UnmanagedType.LPWStr)] string CmdLineBuffer,
    int nCmdShow);

Then to call it:

InstallHinfSection(IntPtr.Zero, IntPtr.Zero, "my path", 0);

I generated most of this signature using the P/Invoke Signature Generator.

The full details of this method and its parameters are on MSDN. According to MSDN the first parameter can be null, the second one must be null, and the last parameter must be 0. You only have to pass in the string parameter.

Eilon
  • 25,582
  • 3
  • 84
  • 102
  • I was looking for a .net equivalent for this native API. – Navaneeth Jan 09 '10 at 06:07
  • There isn't one. You have to P/Invoke it. – Eilon Jan 09 '10 at 06:09
  • 5
    I should clarify: The .NET Framework does not include a managed code version of this API. The .NET Framework has very few APIs that wrap low-level Win32 APIs such as driver installation APIs. By declaring a P/Invoke method you're directly calling the native Win32 API from managed code. – Eilon Jan 09 '10 at 06:20
  • This doesn't work for me, for some reason... (windows 10, x64, visual studio 2015) - doesn't give an error or nothing, just doesn't really install the inf file... – Maverick Meerkat Jul 18 '17 at 12:18
  • 1
    On windows 10, you need to add `CharSet = CharSet.Unicode` to the `DllImport()` call – Jamie Cockburn Jul 11 '18 at 13:03
  • @JamieCockburn How do I add it to the call? I tried the same statement but I get "identifier expected" for the = symbol – Macindows May 03 '20 at 10:48
  • 2
    @Macindows put that inside the `DllImport()` like this: `[DllImport(......., CharSet = CharSet.Unicode)]`. You can look up "C# attribute syntax" to find more about this syntax. – Eilon May 04 '20 at 22:10
7

This simple code worked for me

    private void driverInstall()
    {

        var process = new Process();
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.CreateNoWindow = true;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.RedirectStandardError = true;
        process.StartInfo.FileName = "cmd.exe";

        process.StartInfo.Arguments = "/c C:\\Windows\\System32\\InfDefaultInstall.exe " + driverPath; // where driverPath is path of .inf file
        process.Start();
        process.WaitForExit();
        process.Dispose();
        MessageBox.Show(@"Driver has been installed");
    }
Ravian
  • 141
  • 1
  • 8
  • Works perfect from a console app. Although, i can't get it to run from a Windows Service running as SYSTEM. (win service is x86, the driver itself is x64, not sure if that matters). – Darksody Feb 06 '21 at 16:55
  • Hi, I'm getting an error. The parameter is incorrect. This appears as a message box when I click on yes to the INF install UAC dialog. The INF file installs perfectly fine via the right click>Install method. – Ste Nov 09 '21 at 20:28
  • `driverPath` should be `"\"" + driverPath + "\""` to count for the INF file having any spaces in it. – Ste Nov 09 '21 at 21:00
1

Same as @Ravians answer only I've added an ExitCode to check if the user has pressed yes or no to the dialog. 0 been that they pressed yes and 1 for no.

It also fixes the issue for the file path of the inf file when it has spaces.

public static readonly string windowsPath = Environment.GetFolderPath(Environment.SpecialFolder.Windows);
public static bool driversInstalledSuccessfully;
public static readonly string driverPath = @"C:/Path to/Driver.inf";

private static void DriverInstall(string driverFile)
{

    var process = new Process();
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.CreateNoWindow = true;
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.RedirectStandardError = true;
    process.StartInfo.FileName = "cmd.exe";
    process.StartInfo.Arguments = "/c " + windowsPath + "\\System32\\InfDefaultInstall.exe " + "\"" + driverPath + "\""; // where driverPath is path of .inf file
    process.Start();
    process.WaitForExit();

    if (process.ExitCode == 0)
    {
        Debug.WriteLine("Successfully Installed");
        driversInstalledSuccessfully = true;
    }
    else
    {
        Debug.WriteLine("Big Problemo");
        driversInstalledSuccessfully = false;
    }
    process.Dispose();

} // End DriverInstall

Called via

DriverInstall(driverPath);
Ste
  • 1,729
  • 1
  • 17
  • 27
  • That worked perfectly for me, thanx! Any idea on how to UNinstall a driver in the same way? – tombobadil Mar 16 '22 at 08:53
  • No problem. You have to ask a new question for that. I'm sure there's a few on here that deal with that. – Ste Mar 16 '22 at 13:29
0

//public static readonly string windowsPath = Environment.GetFolderPath(Environment.SpecialFolder.Windows); //public static bool driversInstalledSuccessfully; //public static readonly string driverPath = Environment.CurrentDirectory + "Driver\64bit\u4.inf";

    private static void DriverInstall(string driverFile)
    {

        var process = new Process();
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.CreateNoWindow = true;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.RedirectStandardError = true;
        process.StartInfo.FileName = "cmd.exe";
        process.StartInfo.Arguments = "/c C:\\Windows\\System32\\InfDefaultInstall.exe " + driverPath + "\""; // where driverPath is path of .inf file
        process.Start();
        process.WaitForExit();

        if (process.ExitCode == 0)
        {
            Debug.WriteLine("Successfully Installed");
            driversInstalledSuccessfully = true;
        }
        else
        {
            Debug.WriteLine("Big Problemo");
            driversInstalledSuccessfully = false;
        }
        process.Dispose();

    }

//it gave error the parameter is incorrect

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 09 '22 at 12:11