轉(zhuǎn)自http://www.cnblogs.com/project/archive/2009/10/22/1588015.html
關(guān)于三個(gè)SDK函數(shù): WinExec, ShellExecute,CreateProcess 的其他注意事項(xiàng):
【1】定義頭文件
必須定義以下頭文件:
#include <windows.h>
【2】定義路徑
C++中所表示的路徑要用 " \\ "而不是平常所用的" \ ",所以以上三個(gè)函數(shù)表示路徑都為:disk:\\Directory\\...\\File name
WinExec("D:\\Program Files\\Test\\Test.exe",SW_SHOWMAXIMIZED);
ShellExecute(NULL,"open","C:\\Test.txt",NULL,NULL,SW_SHOWNORMAL);
一、system
int system( const char *command );
你可以傳入一命令,啟動(dòng)某個(gè)程序。如"ping www.vccode.com", "YourExe"等等。不過這里有幾點(diǎn)要值得注意:
(1)、他不會(huì)立即返回,直到你啟動(dòng)的程序執(zhí)行完成。
(2)、如果你啟動(dòng)是windows程序,它仍然會(huì)啟動(dòng)一個(gè)控制臺,這就給人感覺太差勁了,但如果本身是控制臺的,而且又需要等待它的完成,那這將是比較好的選擇。
(3)、它的返回值代表是否執(zhí)行成功以及程序的退出碼。
(4)、不能運(yùn)行*.txt文件或"www.baidu.com"
二、WinExec
UINT WinExec(
LPCSTR lpCmdLine, //命令行
UINT uCmdShow //窗口樣式
);
這個(gè)API與API:system同樣的使用簡單,同用是使用命令行型式。
不過它與API:system相比,有幾個(gè)優(yōu)點(diǎn):
(1)、它將啟動(dòng)了一個(gè)新進(jìn)程,并且立即返回,因此你的程序無需等待。
(2)、它的多了一個(gè)參數(shù):uCmdShow,通過它你可以一定程度上控件窗體的顯示,比如讓它后臺運(yùn)行而不顯示出來。
(3)、它無論啟動(dòng)控制臺程序還是windows程序都只做你想要做的事。
不足之處:
(1)、它完全與本進(jìn)程脫離,無法做些必要的控制。
(2)、無法得知啟動(dòng)的程序是否退出。
(3)、得不到啟動(dòng)的程序的退出碼。
(4)、不能運(yùn)行*.txt文件或"www.baidu.com"
三、ShellExecute
HINSTANCE ShellExecute(
HWND hwnd,
LPCTSTR lpOperation,
LPCTSTR lpFile,
LPCTSTR lpParameters,
LPCTSTR lpDirectory,
INT nShowCmd
);
它也有WinExec同樣的缺點(diǎn)。
它雖然傳回一個(gè)HINSTANCE,但他并不是真正的句柄,我們僅能拿它來做一些錯(cuò)誤值檢查。
但它的功能比前兩者更強(qiáng)大,它執(zhí)行系統(tǒng)的Shell命令。
1、2中如果傳入“XX.txt”,它們將不能成功執(zhí)行,ShellExecute卻能很好地執(zhí)行,它將啟動(dòng)一個(gè)默認(rèn)的文字處理程序來打開它。
1、2中如果傳入“www.vccode.com”,將不能成功執(zhí)行,而ShellExecute卻能很好地執(zhí)行,它將啟動(dòng)一個(gè)默認(rèn)瀏覽器來打開這個(gè)網(wǎng)站。
四、CreateProcess
BOOL CreateProcess(
LPCTSTR lpApplicationName, // name of executable module
LPTSTR lpCommandLine, // command line string
LPSECURITY_ATTRIBUTES lpProcessAttributes, // SD
LPSECURITY_ATTRIBUTES lpThreadAttributes, // SD
BOOL bInheritHandles, // handle inheritance option
DWORD dwCreationFlags, // creation flags
LPVOID lpEnvironment, // new environment block
LPCTSTR lpCurrentDirectory, // current directory name
LPSTARTUPINFO lpStartupInfo, // startup information
LPPROCESS_INFORMATION lpProcessInformation // process information
);
往往看到這個(gè)函數(shù)就讓人生畏,它參數(shù)多,而且參數(shù)類型也如此陌生。是的,正是因?yàn)槿绱怂殴δ軓?qiáng)大!
但不要怕,作為一般使用,非常簡單!下面便是一個(gè)簡單的例子(啟動(dòng)記事本):
STARTUPINFO StartInfo;
PROCESS_INFORMATION pinfo;
//對程序的啟動(dòng)信息不作任何設(shè)定,全部清0
memset(&StartInfo,0,sizeof(STARTUPINFO));
StartInfo.cb = sizeof(STARTUPINFO);//設(shè)定結(jié)構(gòu)的大小
BOOL ret=CreateProcess(
NULL, //啟動(dòng)程序路徑名
"notepad.exe", //參數(shù)(當(dāng)exeName為NULL時(shí),可將命令放入?yún)?shù)前)
NULL, //使用默認(rèn)進(jìn)程安全屬性
NULL, //使用默認(rèn)線程安全屬性
FALSE, //句柄不繼承
NORMAL_PRIORITY_CLASS, //使用正常優(yōu)先級
NULL, //使用父進(jìn)程的環(huán)境變量
NULL, //指定工作目錄
&StartInfo, //子進(jìn)程主窗口如何顯示
&pinfo); //用于存放新進(jìn)程的返回信息
這樣在創(chuàng)建成功這后我們就可以從pinfo中找到它的:進(jìn)程句柄,線程句柄,進(jìn)程ID,線程ID
在附件源碼中演示了進(jìn)程序的啟動(dòng),停止。
實(shí)際上我們可以通過很多方式如內(nèi)存共享、父進(jìn)程窗體句體傳入仍后從消息中獲得子進(jìn)程窗體句柄等,來實(shí)現(xiàn)更多的控制。
想很好地掌握CreateProcess,可參見人民郵電出版社出版的<< Windows系統(tǒng)編程 >>,它的“進(jìn)程”部份作了很詳盡的說明。
例程:
#include<windows.h>
void main()
{
HWND handle;
printf("Function <WinExec>:\nIt can run a cmd command,but can`t open *.txt and \"www.*.*\"\n");
printf("Please press Enter go on\n");
getchar();
WinExec("mspaint.exe",SW_SHOWNOACTIVATE);
/*winexec不能打開網(wǎng)站或txt文件*/
printf("Function <ShellExecute>:\nIt can run a cmd command to open file or web\n\n");
getchar();
printf("Open a txt file\n");
ShellExecute(NULL,"open","C:\\test.txt",NULL,NULL,SW_MINIMIZE);
getchar();
printf("Open a web\n");
ShellExecute(NULL,NULL,"www.baidu.com",NULL,NULL,SW_SHOWNA);
getchar();
printf("Run a cmd command:ping www.sina.com\n");
ShellExecute(NULL, NULL, "ping", "sina.com", NULL, SW_SHOWNORMAL);
getchar();
printf("打開目錄\n");
ShellExecute(NULL, "open", "c:", NULL, NULL, SW_SHOWNORMAL);
getchar();
printf("瀏覽目錄\n");
ShellExecute(NULL, "explore", "c:", NULL, NULL, SW_SHOWNORMAL);
getchar();
printf("文件屬性\n");
ShellExecute(handle,"properties","C:\\test.txt",NULL,NULL,SW_MINIMIZE);
printf("%s",handle);
/*shellExecute的第二個(gè)參數(shù)為你想執(zhí)行的操作(edit,explore,find,open,print,properties),也可為NULL*/
}
/*
SW_HIDE Hides the window and passes activation to another window.
SW_MINIMIZE Minimizes the specified window and activates the top-level window in the system's list.
SW_RESTORE Activates and displays a window. If the window is minimized or maximized, Windows restores it to its original size and position (same as SW_SHOWNORMAL).
SW_SHOW Activates a window and displays it in its current size and position.
SW_SHOWMAXIMIZED Activates a window and displays it as a maximized window.
SW_SHOWMINIMIZED Activates a window and displays it as an icon.
SW_SHOWMINNOACTIVE Displays a window as an icon. The window that is currently active remains active.
SW_SHOWNA Displays a window in its current state. The window that is currently active remains active.
SW_SHOWNOACTIVATE Displays a window in its most recent size and position. The window that is currently active remains active.
SW_SHOWNORMAL Activates and displays a window. If the window is minimized or maximized, Windows restores it to its original size and position (same as SW_RESTORE).
*/