在vc中調用黑屏下的cmd命令目前知道4種方法,分別是system、WinExec,ShellExecute和CreateProcess四個命令.下面簡單介紹一下
1.在VC中調用DOS命令時,可以用函數system("DOS命令"),如我們經常見的命令system("pause"),當然也可以執行別的應用程序,比如 system("F://lame//lame.exe F://lame//a.wav F://lame//a.mp3")。只要在cmd下執行的在這裏應該也沒問題,只是注意打開文件或保存文件時若不指定絕對路徑,將默認是當前程序的路徑,也可以使用絕對路徑。
2.WinExec多了個顯示窗口方式,在MSDN中函數爲:
UINT WinExec( LPCSTR lpCmdLine, UINT uCmdShow );
WinExec主要運行EXE文件。如:WinExec("notepad.exe", SW_SHOW); WinExec默認路徑爲程序所在的路徑,所以一般使用絕對路徑。
現在舉例如下: WinExec("F://lame//lame.exe F://lame//a.wav F://lame//a.mp3", SW_HIDE); 對於多條命令用"&&"連接,第二個參數是隱藏CMD窗口的意思,還可以爲其它的函數: nCmdShow Specifies how the CWnd is to be shown. It must be one of the following values: SW_HIDE Hides this window and passes activation to another window.
SW_MINIMIZE Minimizes the window and activates the top-level window in the system's list. SW_RESTORE Activates and displays the window. If the window is minimized or maximized, Windows restores it to its original size and position.
SW_SHOW Activates the window and displays it in its current size and position. SW_SHOWMAXIMIZED Activates the window and displays it as a maximized window. SW_SHOWMINIMIZED Activates the window and displays it as an icon. SW_SHOWMINNOACTIVE Displays the window as an icon. The window that is currently active remains active.
SW_SHOWNA Displays the window in its current state. The window that is currently active remains active.
SW_SHOWNOACTIVATE Displays the window in its most recent size and position. The window that is currently active remains active.
SW_SHOWNORMAL Activates and displays the window. If the window is minimized or maximized, Windows restores it to its original size and position.
3.ShellExecute ShellExecute不僅可以運行EXE文件,也可以運行已經關聯的文件。 ShellExecute的一個參數爲窗口句柄,第二個參數爲處理方式如“open”“edit”“find”等,第三個參數爲命令,第四個爲命令參數,第五個爲默認文件夾,第六個爲控制檯顯示方式,一般命令參數只寫命令,具體位置在默認文件夾處指定。對於一般的控制檯程序,可以這樣調用ShellExecute,
ShellExecute(NULL,"open","calc.exe",NULL,NULL, SW_SHOW ); 其中exe命令或者爲系統的,或者爲某個文件夾下的應用程序,這時需要在第5個參數指定應用程序位置文件夾。
下面引用關於ShellExecute的幾個問答。
Q: 如何打開一個應用程序?
ShellExecute(this->m_hWnd,"open","calc.exe","","", SW_SHOW ); 或 ShellExecute(this->m_hWnd,"open","notepad.exe", "c://MyLog.log","",SW_SHOW ); 正如您所看到的,我並沒有傳遞程序的完整路徑。
Q: 如何打開一個同系統程序相關連的文檔?
ShellExecute(this->m_hWnd,"open", "c://abc.txt","","",SW_SHOW ); Q: 如何打開一個網頁? ShellExecute(this->m_hWnd,"open", "http://www.google.com","","", SW_SHOW );
Q: 如何激活相關程序,發送EMAIL?
ShellExecute(this->m_hWnd,"open", "mailto:[email protected]","","", SW_SHOW );
Q: 如何用系統打印機打印文檔?
ShellExecute(this->m_hWnd,"print", "c://abc.txt","","", SW_HIDE);
Q: 如何用系統查找功能來查找指定文件?
ShellExecute(m_hWnd,"find","d://nish", NULL,NULL,SW_SHOW);
Q: 如何啓動一個程序,直到它運行結束?
(打開一個程序,直到它關閉再回到主程序)
SHELLEXECUTEINFO ShExecInfo = {0};
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL; ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = "c://MyProgram.exe";
ShExecInfo.lpParameters = "";
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_SHOW;
ShExecInfo.hInstApp = NULL;
ShellExecuteEx(&ShExecInfo);
WaitForSingleObject(ShExecInfo.hProcess,INFINITE);
或: PROCESS_INFORMATION ProcessInfo;
STARTUPINFO StartupInfo; //This is an [in] parameter
ZeroMemory(&StartupInfo, sizeof(StartupInfo));
StartupInfo.cb = sizeof StartupInfo; //Only compulsory field if(CreateProcess("c://winnt//notepad.exe", NULL, NULL,NULL,FALSE,0,NULL, NULL,&StartupInfo,&ProcessInfo))
{
WaitForSingleObject(ProcessInfo.hProcess,INFINITE);
CloseHandle(ProcessInfo.hThread);
CloseHandle(ProcessInfo.hProcess);
}
else
{
MessageBox("The process could not be started...");
}
Q: 如何顯示文件或文件夾的屬性?
SHELLEXECUTEINFO ShExecInfo ={0};
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_INVOKEIDLIST ;
ShExecInfo.hwnd = NULL; ShExecInfo.lpVerb = "properties";
ShExecInfo.lpFile = "c://"; //can be a file as well ShExecInfo.lpParameters = ""; ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_SHOW;
ShExecInfo.hInstApp = NULL;
ShellExecuteEx(&ShExecInfo);
4.CreateProcess因爲使用複雜,比較少用。
BOOL CMSChartuseDlg::RunCMD(LPCTSTR pszCommand)
{
// TCHAR szAppName[_MAX_PATH] = TEXT("fexplore.exe");//資源管理器程序 TCHAR szAppName[60] = TEXT("c://windows//system32//cmd.exe");//資源管理器程序 PROCESS_INFORMATION pi;
STARTUPINFO si;
ZeroMemory(&si,sizeof(si));
ZeroMemory(&pi,sizeof(pi));
si.cb=sizeof(si);
si.dwFlags=STARTF_USESHOWWINDOW;
si.wShowWindow=SW_HIDE; if(CreateProcess(szAppName,(char*)pszCommand,NULL,NULL,NULL, 0,NULL,NULL,&si,&pi)==0)
{
LPVOID lpMsgBuf; FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM, NULL,GetLastError(),MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), // Default language (LPTSTR)&lpMsgBuf,0,NULL);
CString strMsg;
strMsg.Format("執行軟件時出錯:%s",(char*)lpMsgBuf);
LocalFree(lpMsgBuf); MessageBox(strMsg);
return FALSE;
}
else{
// system(pszCommand);
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
// WaitForSingleObject(pi.hProcess,INFINITE);
return TRUE;
}
}
承接前面博客“vc調用cmd命令總結”,列出以下常用windows命令,從網上找的,僅供查找。
附:一些windows的系統命令:
winver---------檢查Windows版本
wmimgmt.msc----打開windows管理體系結構(WMI)
wupdmgr--------windows更新程序
wscript--------windows腳本宿主設置
write----------寫字板
winmsd---------系統信息
wiaacmgr-------掃描儀和照相機嚮導
winchat--------XP自帶局域網聊天
mem.exe--------顯示內存使用情況
Msconfig.exe---系統配置實用程序
mplayer2-------簡易widnows media player
mspaint--------畫圖板
mstsc----------遠程桌面連接
mplayer2-------媒體播放機
magnify--------放大鏡實用程序
mmc------------打開控制檯
mobsync--------同步命令 dxdiag---------檢查DirectX信息
drwtsn32------ 系統醫生
devmgmt.msc--- 設備管理器
dfrg.msc-------磁盤碎片整理程序
diskmgmt.msc---磁盤管理實用程序
dcomcnfg-------打開系統組件服務
ddeshare-------打開DDE共享設置
dvdplay--------DVD播放器
net stop messenger-----停止信使服務
net start messenger----開始信使服務
notepad--------打開記事本
nslookup-------網絡管理的工具嚮導
ntbackup-------系統備份和還原
narrator-------屏幕“講述人”
ntmsmgr.msc----移動存儲管理器
ntmsoprq.msc---移動存儲管理員*作請求
netstat -an----(TC)命令檢查接口
syncapp--------創建一個公文包
sysedit--------系統配置編輯器
sigverif-------文件簽名驗證程序
sndrec32-------錄音機
shrpubw--------創建共享文件夾
secpol.msc-----本地安全策略
syskey---------系統加密,一旦加密就不能解開,保護windows xp系統的雙重密碼 services.msc---本地服務設置
Sndvol32-------音量控制程序
sfc.exe--------系統文件檢查器
sfc /scannow---windows文件保護
tsshutdn-------60秒倒計時關機命令
tourstart------xp簡介(安裝完成後出現的漫遊xp程序)
taskmgr--------任務管理器
eventvwr-------事件查看器
eudcedit-------造字程序
explorer-------打開資源管理器
packager-------對象包裝程序
perfmon.msc----計算機性能監測程序
progman--------程序管理器
regedit.exe----註冊表
rsop.msc-------組策略結果集
regedt32-------註冊表編輯器
rononce -p ----15秒關機
regsvr32 /u *.dll----停止dll文件運行
regsvr32 /u zipfldr.dll------取消ZIP支持
cmd.exe--------CMD命令提示符
chkdsk.exe-----Chkdsk磁盤檢查
certmgr.msc----證書管理實用程序
calc-----------啓動計算器
charmap--------啓動字符映射表
cliconfg-------SQL SERVER 客戶端網絡實用程序
Clipbrd--------剪貼板查看器
conf-----------啓動
netmeeting compmgmt.msc---計算機管理
cleanmgr-------好東西整理
ciadv.msc------索引服務程序
osk------------打開屏幕鍵盤
odbcad32-------ODBC數據源管理器
oobe/msoobe /a----檢查XP是否激活
lusrmgr.msc----本機用戶和組
logoff---------註銷命令
iexpress-------木馬捆綁工具,系統自帶
Nslookup-------IP地址偵測器
fsmgmt.msc-----共享文件夾管理器
utilman--------輔助工具管理器
gpedit.msc-----組策略
xp下運行命令大全. $Systemroot$Documents and Settingsusername 目錄下 appwiz.cpl------------添加刪除程序
control userpasswords2--------用戶帳戶設置
cleanmgr-------垃圾整理
CMD--------------命令提示符可以當作是 Windows 的一個附件,Ping,Convert 這些不能在圖形環境下使用的功能要藉助它來完成。
cmd------jview察看Java虛擬機版本。
command.com------調用的則是系統內置的 NTVDM,一個 DOS虛擬機。它完全是一個類似 Virtual PC 的虛擬環境,和系統本身聯繫不大。當我們在命令提示符下運行 DOS 程序時,實際上也 是自動轉移到 NTVDM虛擬機下,和 CMD 本身沒什麼關係。
calc-----------啓動計算器
chkdsk.exe-----Chkdsk磁盤檢查
compmgmt.msc---計算機管理
conf-----------啓動 netmeeting control
userpasswords2-----User Account 權限設置
devmgmt.msc--- 設備管理器
diskmgmt.msc---磁盤管理實用程序
dfrg.msc-------磁盤碎片整理程序
drwtsn32------ 系統醫生
dvdplay--------啓動Media Player
dxdiag-----------DirectX Diagnostic Tool
gpedit.msc-------組策略編輯器
gpupdate /target:computer /force 強制刷新組策略
eventvwr.exe-----事件查看器
explorer-------打開資源管理器
logoff---------註銷命令
lusrmgr.msc----本機用戶和組 msinfo32---------系統信息
msconfig---------系統配置實用程序
net start (servicename)----啓動該服務
net stop (servicename)-----停止該服務
notepad--------打開記事本
nusrmgr.cpl-------同control userpasswords,打開用戶帳戶控制面板
Nslookup-------IP地址偵測器 oobe/msoobe /a----檢查XP是否激活
perfmon.msc----計算機性能監測程序
progman--------程序管理器
regedit----------註冊表編輯器
regedt32-------註冊表編輯器 regsvr32 /u *.dll----停止dll文件運行
route print------查看路由表 rononce -p ----15秒關機
rsop.msc-------組策略結果集
rundll32.exe rundll32.exe %Systemroot%System32shimgvw.dll,
ImageView_Fullscreen----啓動一個空白的Windows 圖片和傳真查看器
secpol.msc--------本地安全策略
services.msc---本地服務設置
sfc /scannow-----啓動系統文件檢查器
sndrec32-------錄音機
taskmgr-----任務管理器(適用於2000/xp/2003)
tsshutdn-------60秒倒計時關機命令
winchat--------XP自帶局域網聊天
winmsd---------系統信息
winver-----顯示About Windows 窗口 Windows XP的關機是由Shutdown.exe程序來控制的,位於Windows/System32文件夾中。如果想讓Windows 2000也實現同樣的效果,可以把Shutdown.exe複製到系統目錄下。 比如你的電腦要在22:00關機,可以選擇“開始→運行”,輸入“at 22:00 Shutdown -s”,這樣,到了22點電腦就會出現“系統關機”對話框,默認有30秒鐘的倒計時並提示你保存工作。如果你想以倒計時的方式關機,可以輸入“Shutdown.exe -s -t 3600”,這裏表示60 分鐘後自動關機,“3600”代表60分鐘。 設置好自動關機後,如果想取消的話,可以在運行中輸入“shutdown -a”。另外輸入“shutdown -i”,則可以打開設置自動關機對話框,對自動關機進行設置。 Shutdown.exe的參數,每個都具有特定的用途,執行每一個都會產生不同的效果,比如 “-s”就表示關閉本地計算機,“-a”表示取消關機操作,下面列出了更多參數, 可以在Shutdown.exe中按需使用。 -f:強行關閉應用程序 -m //計算機名:控制遠程計算機 -i:顯示圖形用戶界面,但必須是Shutdown的第一個選項 -l:註銷當前用戶 -r:關機並重啓 -t 時間:設置關機倒計時 -c "消息內容":輸入關機對話框中的消息內容(不能超127個字符) 有時候,我們需要定時關閉計算機,下面介紹一個在Windows XP下實現定時關機的簡單方法。 指定系統在22分鐘後自動關閉:點擊“開始→運行”,在“打開”中輸入命令“Shutdown -s -t 1320”(注意:引號不輸入,參數之間有空格,1320的單位是秒),單擊“確定” 按鈕即可。 指定系統在某個時間(比如12:00)自動關閉:在“打開”中輸入命令 “at 12:00 Shutdown -s”即可。 取消定時關機:在“打開”中輸入命令“Shutdown -a”即可。