函數原型:
__drv_preferredFunction("CreateProcess","Deprecated. See MSDN for details")
WINBASEAPI
UINT
WINAPI
WinExec(__in LPCSTR lpCmdLine,__in UINT uCmdShow);
preferred : 更好的
__drv_preferredFunction("CreateProcess", "Deprecated. See MSDN for details")
?是 Windows 驅動開發中用于靜態代碼分析工具(如 PREfast for Drivers)的注解,主要作用是標記特定函數已被棄用,并推薦使用替代函數(此處為?CreateProcess
)。
WinExec
?是 Windows API 中的一個函數,主要用于執行外部應用程序或命令,但其設計初衷是為兼容早期的 ??16 位 Windows 系統??,現代開發中已被?CreateProcess
?取代。以下是其核心解析:
?? ??函數作用??
WinExec
?用于啟動一個外部可執行程序(.exe
)或命令行命令,并控制其窗口的顯示狀態。它通過簡單的參數實現快速調用,適合基礎場景,但功能較為有限。
示例1:
(1)
#include <windows.h>int main() {// 打開記事本(正常窗口)UINT ret = WinExec("notepad.exe", SW_SHOW);// 檢查返回值if (ret <= 31) {// 處理錯誤(例如文件未找到)}return 0;
}
(2)
#include <Windows.h>int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR nCmdLine,int iCmdShow) {WinExec("calc.exe", SW_SHOW);return 0;
}
功能:打開記事本
(3)
WinExec("Notepad.exe", SW_HIDE);
//會出現在任務管理器中,但不會顯示在任務欄。
示例2:
運行ScreenToGif.exe:
#include <Windows.h>
#include <stdlib.h>#include <iostream>
#include "tchar.h"
int main(int argc, char* argv[]) {int res = WinExec("E:\\ScreenToGif\\ScreenToGif.exe", SW_SHOW);std::cout << res<<'\n';if (res == 0) {std::cout << "系統內存或資源不足";}else if (res == ERROR_BAD_FORMAT) {std::cout << ".EXE文件格式無效";}else if (res == ERROR_FILE_NOT_FOUND) {std::cout << "指定的文件沒有找到";}else if (res == ERROR_PATH_NOT_FOUND) {std::cout << "指定的路徑沒有找到";}return 0;
}
?注意:路徑中是\\
同理:
打開網易詞典
int res = WinExec("E:\\網易有道詞典\\Dict\\YodaoDict.exe", SW_SHOW);
?打開指定的txt文件
int res = WinExec("notepad.exe D:\\2.txt", SW_SHOW);
這樣也可以:
std::string str("notepad.exe D:\\work\\版本\\1.0.1\\OfficeAssistant2.0\\Debug\\license.txt");int res = WinExec(str.c_str(), SW_SHOW);
這樣也可以:
QString str1("notepad.exe D:\\work\\learn_git\\Git\\2.txt");int res = WinExec(str1.toStdString().c_str(), SW_SHOW);
這樣也可以:
QString中文亂碼_qstring 中文_Coder-LiyG的博客-CSDN博客
QString str1=QString::fromLocal8Bit("notepad.exe D:\\work\\版本\\1.0.1\\OfficeAssistant2.0\\Debug\\license.txt");QByteArray by = str1.toLocal8Bit();int res = WinExec(str1.toLocal8Bit().constData(), SW_SHOW);