使用C# .net编程自动安装/卸载.inf驱动程序

19
我正在使用c#.net制作应用程序。 它还包含一个文件系统minifilter驱动程序。 我想使用c# .net以编程方式安装和卸载此驱动程序。 通常,我可以使用.INF文件进行安装(右键单击+按“安装”)。但我想以编程方式安装它。有一个SDK函数InstallHinfSection()可用于安装.INF驱动程序。我正在寻找这个函数的.net等效函数。
4个回答

29

尝试像这样:

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);
然后调用它:
InstallHinfSection(IntPtr.Zero, IntPtr.Zero, "my path", 0);

我使用P/Invoke Signature Generator生成了大部分的签名。

此方法及其参数的完整详细信息,请参阅MSDN。根据MSDN ,第一个参数可以为null,第二个参数必须为null,最后一个参数必须为0。您只需要传递字符串参数即可。


我正在寻找一个与这个本地API等价的.NET替代方案。 - Navaneeth
5
我需要澄清一下:.NET Framework中没有包含此API的托管代码版本。.NET Framework中很少有API可以包装低级Win32 API,例如驱动程序安装API。声明P/Invoke方法时,您直接从托管代码中调用本机Win32 API。 - Eilon
这在我的电脑上不起作用,没有任何提示... (Windows 10,x64,Visual Studio 2015)- 没有错误提示,也没有安装inf文件... - Maverick Meerkat
2
在Windows 10上,您需要在DllImport()调用中添加CharSet = CharSet.Unicode - Jamie Cockburn
3
@Macindows 将其放入 DllImport() 中,如下所示:[DllImport(......., CharSet = CharSet.Unicode)]。您可以查找“C# 属性语法”以了解更多关于该语法的信息。 - Eilon
显示剩余2条评论

7
这段简单的代码对我很有用。
    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");
    }

可以在控制台应用程序中完美运行。但是,我无法让它在作为SYSTEM运行的Windows服务中运行。(win服务是x86,驱动程序本身是x64,不确定是否有影响)。 - Darksody
嗨,我遇到了一个错误。参数不正确。当我点击“是”以安装UAC对话框时,这会出现为消息框。通过右键单击>安装方法,INF文件可以完美地安装。 - Ste
driverPath 应该是 "\"" + driverPath + "\"",以便考虑到 INF 文件中有任何空格。 - Ste

1
与@Ravians的答案相同,只是我添加了一个ExitCode来检查用户是否按下对话框中的“是”或“否”按钮。0表示他们按下了“是”,1表示按下了“否”。
它还修复了inf文件路径中包含空格时的问题。
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

通过调用

DriverInstall(driverPath);

那对我来说完美地运作了,谢谢!有没有想法如何以同样的方式卸载驱动程序? - tombobadil
没问题。你需要提出一个新的问题。我相信这里有一些与此相关的问题。 - Ste

0

//公共静态只读字符串windowsPath = Environment.GetFolderPath(Environment.SpecialFolder.Windows); //公共静态布尔型driversInstalledSuccessfully; //公共静态只读字符串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();

    }

//它报错了,参数不正确


你的回答可以通过提供更多支持信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的答案是正确的。您可以在帮助中心找到有关如何编写良好答案的更多信息。 - Community

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接