1.使用QProcess执行cmd命令
QStringList args;
QString sCmd = QString("net stop mysql_psasp57");
args << "/c" << sCmd;
QProcess process;
process.execute("cmd.exe", args);
process.waitForFinished();
process.close();
2.调用ShellExecuteA函数
#include <Windows.h>
QString sCmd = QString("/c net stop mysql_psasp57");
ShellExecuteA(NULL, NULL, "cmd.exe", sCmd.toLocal8Bit().data(), NULL, SW_HIDE);
3.调用system函数,窗口不能隐藏
#include <stdlib.h>
QString sCmd = QString("cmd.exe /c net stop mysql_psasp57");
system(sCmd.toLocal8Bit().data());
4.封装函数执行cmd命令并获取结果
wstring Str2Wstr(string str)
{
unsigned len = str.size() * 2;// 预留字节数
setlocale(LC_CTYPE, ""); //必须调用此函数
wchar_t *p = new wchar_t[len];// 申请一段内存存放转