我正在使用c#.net制作应用程序。 它还包含一个文件系统minifilter驱动程序。 我想使用c# .net以编程方式安装和卸载此驱动程序。 通常,我可以使用.INF文件进行安装(右键单击+按“安装”)。但我想以编程方式安装它。有一个SDK函数InstallHinfSection()可用于安装.INF驱动程序。我正在寻找这个函数的.net等效函数。
尝试像这样:
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。您只需要传递字符串参数即可。
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");
}
driverPath
应该是 "\"" + driverPath + "\""
,以便考虑到 INF 文件中有任何空格。 - SteExitCode
来检查用户是否按下对话框中的“是”或“否”按钮。0
表示他们按下了“是”,1
表示按下了“否”。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);
//公共静态只读字符串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();
}
//它报错了,参数不正确
DllImport()
调用中添加CharSet = CharSet.Unicode
。 - Jamie CockburnDllImport()
中,如下所示:[DllImport(......., CharSet = CharSet.Unicode)]
。您可以查找“C# 属性语法”以了解更多关于该语法的信息。 - Eilon