【问题标题】:install/uninstall an .inf driver programmatically using C# .net使用 C# .net 以编程方式安装/卸载 .inf 驱动程序
【发布时间】:2011-01-03 04:48:42
【问题描述】:

我正在使用 c#.net 制作应用程序。它还包含一个文件系统微过滤驱动程序。我想使用 c# .net 以编程方式安装和卸载此驱动程序。通常我可以使用 .INF 文件安装它(通过右键单击 + 按安装)。但我想以编程方式安装它。有一个 SDK 函数 InstallHinfSection() 用于安装 .inf 驱动程序。我正在寻找此功能的 .net 等效项。

问候

纳瓦尼斯

【问题讨论】:

    标签: c# .net installation driver


    【解决方案1】:

    试试这样的:

    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,第一个参数可以为空,第二个必须为空,最后一个参数必须为0。你只需要传入字符串参数。

    【讨论】:

    • 我正在为这个原生 API 寻找一个 .net 等价物。
    • 没有。你必须 P/Invoke 它。
    • 我应该澄清一下:.NET Framework 不包含此 API 的托管代码版本。 .NET Framework 有很少的 API 封装了低级 Win32 API,例如驱动程序安装 API。通过声明 P/Invoke 方法,您可以直接从托管代码调用本机 Win32 API。
    • 在 Windows 10 上,您需要将 CharSet = CharSet.Unicode 添加到 DllImport() 调用中
    • @Macindows 将其放入 DllImport() 中,如下所示:[DllImport(......., CharSet = CharSet.Unicode)]。您可以查找“C# 属性语法”以了解有关此语法的更多信息。
    【解决方案2】:

    这个简单的代码对我有用

        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,不知道有没有关系)。
    • 您好,我遇到了一个错误。参数不正确。当我在 INF 安装 UAC 对话框中单击“是”时,这将显示为一个消息框。通过右键单击>安装方法,INF 文件安装得非常好。
    • driverPath 应该是 "\"" + driverPath + "\"" 以计算其中包含任何空格的 INF 文件。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2023-03-16
      • 2011-09-20
      • 2013-11-15
      • 1970-01-01
      • 2012-02-01
      • 1970-01-01
      • 2014-04-06
      • 1970-01-01
      • 2012-05-05
      相关资源
      最近更新 更多