Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 826
Navaneeth Avatar asked Jan 09 '10 05:01

Navaneeth


People also ask

How do I uninstall an .INF file?

The user can then right click on the inf file shown in the list and view the content in the richedit box on the right bottom of the screen or open it in Notepad by right clicking on the particular entry in listview. Once the user is sure that the inf can be uninstalled, they can right click and select Uninstall.

How do I manually install an inf driver?

Right-Click InstallIn Windows Explorer, select and hold (or right-click) the INF file name. A shortcut menu will appear. Select Install.

Why can't I install an INF file?

inf drivers that were displaying the inf file you selected does not support this method of installation error by using Device Manager. This might work if the INF driver doesn't have the required install parameters to run conventionally (Right-click > Install) but it's configured to be installed through Device Manager.


2 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.

like image 129
Eilon Avatar answered Sep 21 '22 18:09

Eilon


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");
    }
like image 37
Ravian Avatar answered Sep 21 '22 18:09

Ravian