public static bool UninstallDriver(out bool needRestart, out string failureReason) { failureReason = null; ulong wdfCallResult = 0; bool result = true; needRestart = false; string mydir; if (ExtractDriverFiles(out mydir)) { IntPtr wdfPreDeviceInstallPtr; IntPtr wdfPostDeviceInstallPtr; IntPtr wdfPreDeviceRemovePtr; IntPtr wdfPostDeviceRemovePtr; bool procAddressFailure; IntPtr hModule = GetCoinstallerFuncs(mydir, out wdfPreDeviceInstallPtr, out wdfPostDeviceInstallPtr, out wdfPreDeviceRemovePtr, out wdfPostDeviceRemovePtr, out procAddressFailure); if (!procAddressFailure) { // call WdfPreDeviceRemove WdfCoinstallInvoker preDevRemove = (WdfCoinstallInvoker)Marshal.GetDelegateForFunctionPointer(wdfPreDeviceRemovePtr, typeof(WdfCoinstallInvoker)); wdfCallResult = preDevRemove(Path.Combine(mydir, infFile), wdfInfSection); if (wdfCallResult != 0) { result = false; failureReason = string.Format("{0} result = 0x{1:X}", wdfPreDeviceRemove, wdfCallResult); } else { // run dpinst string inffile = Path.Combine(mydir, infFile); Process p = Process.Start(Path.Combine(mydir, dpinstFile), string.Format("/u \"{0}\" /d /q", inffile)); p.WaitForExit(); if (((p.ExitCode >> 24) & 0x40) == 0x40) { needRestart = true; } if (((p.ExitCode >> 24) & 0x80) == 0x80) { result = false; failureReason = string.Format("DPInst result = 0x{0:X}", p.ExitCode); } else { // call WdfPostDeviceRemove WdfCoinstallInvoker postDevRemove = (WdfCoinstallInvoker)Marshal.GetDelegateForFunctionPointer(wdfPostDeviceRemovePtr, typeof(WdfCoinstallInvoker)); wdfCallResult = postDevRemove(Path.Combine(mydir, infFile), wdfInfSection); if (wdfCallResult != 0) { result = false; failureReason = string.Format("{0} result = 0x{1:X}", wdfPostDeviceRemove, wdfCallResult); } } } } else { result = false; failureReason = "Error getting WDF coinstaller function addresses"; } // free coinstaller library FreeLibrary(hModule); // delete extracted files if (Directory.Exists(mydir)) { Directory.Delete(mydir, true); } } else { result = false; failureReason = "ExtractDriverFiles failed"; } return(result); }
// 卸载本软件驱动 public static bool UninstallDriver(out bool needRestart, out string failureReason) { failureReason = null; ulong wdfCallResult = 0; bool result = true; needRestart = false; string mydir; if (ExtractDriverFiles(out mydir)) { IntPtr wdfPreDeviceInstallPtr; IntPtr wdfPostDeviceInstallPtr; IntPtr wdfPreDeviceRemovePtr; IntPtr wdfPostDeviceRemovePtr; bool procAddressFailure; // 加载 WdfCoInstaller 函数 IntPtr hModule = GetCoinstallerFuncs(mydir, out wdfPreDeviceInstallPtr, out wdfPostDeviceInstallPtr, out wdfPreDeviceRemovePtr, out wdfPostDeviceRemovePtr, out procAddressFailure); if (!procAddressFailure) { // 调用 WdfPreDeviceRemove WdfCoinstallInvoker preDevRemove = (WdfCoinstallInvoker)Marshal.GetDelegateForFunctionPointer(wdfPreDeviceRemovePtr, typeof(WdfCoinstallInvoker)); wdfCallResult = preDevRemove(Path.Combine(mydir, infFile), wdfInfSection); if (wdfCallResult != 0) { result = false; failureReason = string.Format("{0} 处理结果 = 0x{1:X}", wdfPreDeviceRemove, wdfCallResult); } else { // 运行 Dpinst.exe 并等待 string inffile = Path.Combine(mydir, infFile); Process p = Process.Start(Path.Combine(mydir, dpinstFile), string.Format("/u \"{0}\" /d /q", inffile)); p.WaitForExit(); if (((p.ExitCode >> 24) & 0x40) == 0x40) needRestart = true; if (((p.ExitCode >> 24) & 0x80) == 0x80) { result = false; failureReason = string.Format("DPInst 处理结果 = 0x{0:X}", p.ExitCode); } else { // 调用 WdfPostDeviceRemove WdfCoinstallInvoker postDevRemove = (WdfCoinstallInvoker)Marshal.GetDelegateForFunctionPointer(wdfPostDeviceRemovePtr, typeof(WdfCoinstallInvoker)); wdfCallResult = postDevRemove(Path.Combine(mydir, infFile), wdfInfSection); if (wdfCallResult != 0) { result = false; failureReason = string.Format("{0} 处理结果 = 0x{1:X}", wdfPostDeviceRemove, wdfCallResult); } } } } else { result = false; failureReason = "获取 WdfCoInstaller 库文件函数路径失败,驱动未正常卸载..."; } // 释放 WdfCoInstaller 库 FreeLibrary(hModule); // 删除临时文件 if (Directory.Exists(mydir)) Directory.Delete(mydir, true); } else { result = false; failureReason = "解压驱动文件失败,驱动未能正常卸载..."; } return result; }