vs2022+QT+powershell删除驱动

源码

#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

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值