源码
#include <QCoreApplication>
#include <QProcess>
#include <QDebug>
#include <QTextCodec>
#include <windows.h>
#define _ST(str) QString::fromLocal8Bit(str)
#define DRIVER_NAME "yourself.inf"
QString executeCommand(const QString& command, const QStringList& arguments = QStringList()) {
// 构建 PowerShell 命令
QString powershellCommand = "Start-Process powershell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -Command \"& {" + command + "}\"' -Verb RunAs";
QProcess process;
process.start("powershell", QStringList() << "-NoProfile" << "-ExecutionPolicy" << "Bypass" << "-Command" << "& {" + powershellCommand + "}" << arguments);
// 等待进程完成
process.waitForFinished();
// 读取命令输出
QByteArray output = process.readAllStandardOutput();
// 将输出转换为 QString
return QString::fromLocal8Bit(output);
}
QString getCmdOutput(QString command, QStringList arguments) {
// 创建 QProcess 对象
QProcess process;
// 设置要执行的命令和参数
process.start(command, arguments);
QString strRes;
// 等待进程完成
if (process.waitForFinished()) {
// 读取命令输出
QByteArray output = process.readAllStandardOutput();
strRes = QString::fromLocal8Bit(output);
}
return strRes;
}
void printConsoleOutput(QString strInfo) {
QStringList lst = strInfo.split("\r\n");
for (int i = 0; i < lst.size(); ++i) {
qDebug() << lst[i];
}
}
QString getDriverPublishName(QString originalName) {
QString outputString = getCmdOutput("pnputil", { "/enum-drivers" });
QStringList lst = outputString.split("\r\n");
QString desStr;
for (int i = 0; i < lst.size(); ++i) {
if (lst[i].contains(originalName)) {
if (i > 0) {
desStr = lst[i - 1];
}
}
}
QString strPublishName;
lst = desStr.split(":");
if (lst.size() >= 2) {
strPublishName = lst.at(1).trimmed();
}
return strPublishName;
}
bool removeDriver(QString publishName) {
QString outputString = getCmdOutput("pnputil", { "/delete-driver", publishName, "/uninstall", "/force"});
if (outputString.isEmpty()) {
return false;
}
QStringList lst = outputString.split("\r\n");
QString desStr;
QString strPublishName;
lst = desStr.split(":");
if (lst.size() >= 2) {
strPublishName = lst.at(1).trimmed();
}
return true;
}
void removeDriverFile() {
// 要执行的PowerShell脚本文件路径
QString scriptPath = "delsun.ps1";
// 构建PowerShell命令
QString powerShellCommand = "powershell.exe";
QStringList arguments;
arguments << "-ExecutionPolicy" << "Bypass" << "-File" << scriptPath;
// 创建QProcess对象
QProcess process;
// 设置进程启动参数
process.setProgram(powerShellCommand);
process.setArguments(arguments);
// 启动进程
process.start();
// 等待进程完成
process.waitForFinished(-1);
QTextCodec* codec = QTextCodec::codecForName("GB2312"); // 或者使用 "UTF-8" 等合适的编码
// 输出进程输出和错误信息
printConsoleOutput(process.readAllStandardOutput());
printConsoleOutput(process.readAllStandardError());
}
int main(int argc, char* argv[]) {
QCoreApplication a(argc, argv);
// 获取
QString strPublishName = getDriverPublishName(DRIVER_NAME);
if (!strPublishName.isEmpty()) {
bool bRet = removeDriver(strPublishName);
if (bRet) {
// 删除对应驱动文件
removeDriverFile();
qDebug() << _ST("驱动已删除");
}
}
else {
qDebug() << _ST("获取驱动发布名称错误");
}
Sleep(2000);;
return 0;
// return a.exec();
}
ps1脚本
# Removeyourgige.ps1
#删除系统文件
Remove-Item -Path "C:\Windows\System32\drivers\yourgige.sys" -Force #GIGe驱动
Remove-Item -Path "C:\Windows\System32\drivers\yourUSB3.sys" -Force #usb驱动
# 删除指定路径下的文件夹
#获取所有权
takeown /f "C:\Windows\System32\DriverStore\FileRepository\yourgige.inf_amd64_46dde0fdb70279c4" /r /d y
icacls "C:\Windows\System32\DriverStore\FileRepository\yourgige.inf_amd64_46dde0fdb70279c4" /grant administrators:F /t
#删除
Remove-Item -Path "C:\Windows\System32\DriverStore\FileRepository\yourgige.inf_amd64_46dde0fdb70279c4" -Recurse -Force
Remove-Item -Path "C:\Windows\administrators\Recent\yourgige.inf" -Recurse -Force
Remove-Item -Path "C:\Windows\System32\drivers\yourgige.sys" -Recurse -Force
Remove-Item -Path "C:\Windows\System32\DriverStore\FileRepository\yourgige.inf_amd64_46dde0fdb70279c4\*" -Force
#删除所有sungige的文件
#Get-ChildItem -Path C:\ -Recurse -Filter yourgige* | Remove-Item -Force
#删除sungige注册表
Remove-Item -Path "HKLM:\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\yourgige" -Recurse