install/uninstall an .inf driver programmatically using C# .net

Viewed 24131

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

4 Answers

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.

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);

//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

Related