|
1 | | -### Driver 驱动 |
| 1 | +### Driver 驱动 |
| 2 | + |
| 3 | +#### (1)Windows平台 |
| 4 | + |
| 5 | +##### 驱动安装 |
| 6 | + |
| 7 | +本组件内置使用的驱动安装工具为微软自带的工具PnPutil.exe或使用setupapi.dll来实现。 |
| 8 | + |
| 9 | +驱动安装时需要注意的问题有两点: |
| 10 | + |
| 11 | +| 名称 | 说明 | |
| 12 | +| ---- | ---------------------------------- | |
| 13 | +| 安装 | 驱动证书安装,需要在驱动之前安装。 | |
| 14 | +| 版本 | 区分x86 , x64版本。 | |
| 15 | + |
| 16 | +**PnPUtil实现:** |
| 17 | + |
| 18 | +PnPUtil是一个命令行实用程序,它可以用来管理Windows的驱动程序商店。你可以使用它来添加、删除和列出驱动程序。 |
| 19 | + |
| 20 | +以下是如何使用PnPUtil来安装驱动程序的步骤: |
| 21 | + |
| 22 | +1. 打开命令提示符(以管理员身份)。 |
| 23 | + |
| 24 | +2. 导航到包含驱动程序的INF文件的目录。 |
| 25 | + |
| 26 | +3. 运行以下命令: |
| 27 | + |
| 28 | + `pnputil /add-driver <INF文件名>` |
| 29 | + |
| 30 | + 例如,如果你的INF文件名为`mydriver.inf`,那么你应该运行`pnputil /add-driver mydriver.inf`。 |
| 31 | + |
| 32 | +4. PnPUtil将会添加驱动程序到驱动程序商店,并尝试为任何匹配的设备安装驱动程序。 |
| 33 | + |
| 34 | +注意,PnPUtil需要管理员权限才能运行。 |
| 35 | + |
| 36 | +在C#中,你可以使用System.Diagnostics.Process类来运行PnPUtil。以下是一个例子: |
| 37 | + |
| 38 | +```c# |
| 39 | +using System.Diagnostics; |
| 40 | + |
| 41 | +public class Program |
| 42 | +{ |
| 43 | + public static void Main() |
| 44 | + { |
| 45 | + string infPath = "Path to your INF file"; |
| 46 | + |
| 47 | + Process process = new Process(); |
| 48 | + process.StartInfo.FileName = "pnputil.exe"; |
| 49 | + process.StartInfo.Arguments = "/add-driver " + infPath; |
| 50 | + process.StartInfo.Verb = "runas"; // 运行为管理员 |
| 51 | + process.Start(); |
| 52 | + |
| 53 | + process.WaitForExit(); |
| 54 | + } |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +**setupapi.dll实现:** |
| 61 | + |
| 62 | +```c# |
| 63 | +using System; |
| 64 | +using System.Runtime.InteropServices; |
| 65 | + |
| 66 | +public class Program |
| 67 | +{ |
| 68 | + // 定义 SetupCopyOEMInf 函数的 P/Invoke 签名 |
| 69 | + [DllImport("setupapi.dll", EntryPoint = "SetupCopyOEMInf", SetLastError = true)] |
| 70 | + public static extern bool SetupCopyOEMInf( |
| 71 | + string SourceInfFileName, |
| 72 | + string OEMSourceMediaLocation, |
| 73 | + int OEMSourceMediaType, |
| 74 | + int CopyStyle, |
| 75 | + string DestinationInfFileName, |
| 76 | + int DestinationInfFileNameSize, |
| 77 | + ref int RequiredSize, |
| 78 | + string DestinationInfFileNameComponent |
| 79 | + ); |
| 80 | + |
| 81 | + public static void Main() |
| 82 | + { |
| 83 | + string infPath = "Path to your INF file"; |
| 84 | + bool result = SetupCopyOEMInf(infPath, null, 0, 0, null, 0, ref int size, null); |
| 85 | + |
| 86 | + if (!result) |
| 87 | + { |
| 88 | + Console.WriteLine("Failed to install driver. Error code: " + Marshal.GetLastWin32Error()); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | +##### 驱动证书 |
| 97 | + |
| 98 | +本组件内置使用Windows的证书管理工具(CertMgr.exe)或者使用.NET框架中的X509Store类来实现。 |
| 99 | + |
| 100 | +**CertMgr.exe实现:** |
| 101 | + |
| 102 | +`CertMgr.exe` 是一个命令行工具,它是微软的.NET Framework的一部分。你可以在.NET Framework的安装目录中找到它。 |
| 103 | + |
| 104 | +对于大多数系统,它的位置通常是在以下目录之一: |
| 105 | + |
| 106 | +- `C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin` |
| 107 | +- `C:\Program Files\Microsoft SDKs\Windows\v7.0A\Bin` |
| 108 | + |
| 109 | +如果你找不到它,你可以使用Windows的搜索功能来搜索`CertMgr.exe`。 |
| 110 | + |
| 111 | +注意,`CertMgr.exe`是一个命令行工具,你需要在命令提示符或PowerShell中运行它。你也可以在你的C#代码中使用`System.Diagnostics.Process.Start()`方法来调用它。 |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | +**X509Store实现:** |
| 116 | + |
| 117 | +```c# |
| 118 | +using System; |
| 119 | +using System.Security.Cryptography.X509Certificates; |
| 120 | + |
| 121 | +public class Example |
| 122 | +{ |
| 123 | + public static void Main() |
| 124 | + { |
| 125 | + string CertificatePath = "Path to your certificate file"; |
| 126 | + |
| 127 | + // 创建一个新的X509证书实例 |
| 128 | + X509Certificate2 certificate = new X509Certificate2(CertificatePath); |
| 129 | + |
| 130 | + // 打开当前用户的个人证书存储区 |
| 131 | + X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser); |
| 132 | + |
| 133 | + // 将新证书添加到存储区 |
| 134 | + store.Open(OpenFlags.ReadWrite); |
| 135 | + store.Add(certificate); |
| 136 | + |
| 137 | + store.Close(); |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +``` |
| 142 | + |
0 commit comments