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

4

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

Ohmmeter answered 9/1, 2010 at 5:50 Comment(0)
A
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.

Amphetamine answered 9/1, 2010 at 6:3 Comment(7)
I was looking for a .net equivalent for this native API.Ohmmeter
There isn't one. You have to P/Invoke it.Amphetamine
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.Amphetamine
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...Crispy
On windows 10, you need to add CharSet = CharSet.Unicode to the DllImport() callBricker
@JamieCockburn How do I add it to the call? I tried the same statement but I get "identifier expected" for the = symbolJapheth
@Japheth put that inside the DllImport() like this: [DllImport(......., CharSet = CharSet.Unicode)]. You can look up "C# attribute syntax" to find more about this syntax.Amphetamine
A
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");
    }
Ailurophobe answered 29/2, 2016 at 10:11 Comment(3)
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).Burchette
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.Flournoy
driverPath should be "\"" + driverPath + "\"" to count for the INF file having any spaces in it.Flournoy
F
2

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);
Flournoy answered 10/11, 2021 at 22:30 Comment(2)
That worked perfectly for me, thanx! Any idea on how to UNinstall a driver in the same way?Luau
No problem. You have to ask a new question for that. I'm sure there's a few on here that deal with that.Flournoy
T
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

Tritanopia answered 5/8, 2022 at 20:10 Comment(1)
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.Isacco

© 2022 - 2024 — McMap. All rights reserved.