2

我目前正在使用 setup.dll 以编程方式安装过滤器驱动程序。以下是我的代码:

    protected bool INFSetup(string path_to_inf, bool Install)
    {
        string exe = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "rundll32.exe");
        if (File.Exists(exe) && File.Exists(path_to_inf))
        {
            try
            {
                Process proc = new Process();
                proc.EnableRaisingEvents = true;
                string FileName = exe;
                string Arguments = @"SETUPAPI.DLL,InstallHinfSection " + (Install ? "DefaultInstall" : "DefaultUninstall") + " 128 " + path_to_inf;
                Debug.Writeline("Executing: '" + FileName + "' with arguments: " + Arguments);
                ProcessStartInfo StartInfo = new ProcessStartInfo(FileName, Arguments);
                StartInfo.CreateNoWindow = true;
                StartInfo.UseShellExecute = false;
                proc.StartInfo = StartInfo;
                if (proc.Start())
                {
                    if (proc.WaitForExit(10000))
                    {
                        return (proc.ExitCode == 0);
                    }
                    else
                    {
                        Debug.Writeline("INFSetup: proc.WaitForExit() returned false");
                    }
                }
                else
                {
                    Debug.Writeline("INFSetup: proc.Start() returned false");
                }
            }
            catch (Exception e)
            {
                Debug.Writeline("Caught Execption while installing INF: " + e.ToString());
                return false;
            }
        }
        return false;
    }

虽然代码工作正常,但我想知道是否有办法对本机 Win32 调用做同样的事情?如果有人有示例 c# 代码会很棒吗?谢谢亨利

4

1 回答 1

0

正如rundll命令行所暗示InstallHinfSection的,它也是从 SETUPAPI.DLL 导出的,因此是 p/invokable。MSFT bod 在这里发布了 ap/invoke 签名:

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);
于 2013-11-05T10:30:10.313 回答