轉自http://www.cnblogs.com/project/archive/2009/10/22/1588015.html
關于三個SDK函數: WinExec, ShellExecute,CreateProcess 的其他注意事項:
【1】定義頭文件
必須定義以下頭文件:
#include <windows.h>
【2】定義路徑
C++中所表示的路徑要用 " \\ "而不是平常所用的" \ ",所以以上三個函數表示路徑都為: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 );
你可以傳入一命令,啟動某個程序。如"ping www.vccode.com", "YourExe"等等。不過這里有幾點要值得注意:
(1)、他不會立即返回,直到你啟動的程序執行完成。
(2)、如果你啟動是windows程序,它仍然會啟動一個控制臺,這就給人感覺太差勁了,但如果本身是控制臺的,而且又需要等待它的完成,那這將是比較好的選擇。
(3)、它的返回值代表是否執行成功以及程序的退出碼。
(4)、不能運行*.txt文件或"www.baidu.com"
二、WinExec
UINT WinExec(
LPCSTR lpCmdLine, //命令行
UINT uCmdShow //窗口樣式
);
這個API與API:system同樣的使用簡單,同用是使用命令行型式。
不過它與API:system相比,有幾個優點:
(1)、它將啟動了一個新進程,并且立即返回,因此你的程序無需等待。
(2)、它的多了一個參數:uCmdShow,通過它你可以一定程度上控件窗體的顯示,比如讓它后臺運行而不顯示出來。
(3)、它無論啟動控制臺程序還是windows程序都只做你想要做的事。
不足之處:
(1)、它完全與本進程脫離,無法做些必要的控制。
(2)、無法得知啟動的程序是否退出。
(3)、得不到啟動的程序的退出碼。
(4)、不能運行*.txt文件或"www.baidu.com"
三、ShellExecute
HINSTANCE ShellExecute(
HWND hwnd,
LPCTSTR lpOperation,
LPCTSTR lpFile,
LPCTSTR lpParameters,
LPCTSTR lpDirectory,
INT nShowCmd
);
它也有WinExec同樣的缺點。
它雖然傳回一個HINSTANCE,但他并不是真正的句柄,我們僅能拿它來做一些錯誤值檢查。
但它的功能比前兩者更強大,它執行系統的Shell命令。
1、2中如果傳入“XX.txt”,它們將不能成功執行,ShellExecute卻能很好地執行,它將啟動一個默認的文字處理程序來打開它。
1、2中如果傳入“www.vccode.com”,將不能成功執行,而ShellExecute卻能很好地執行,它將啟動一個默認瀏覽器來打開這個網站。
四、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
);
往往看到這個函數就讓人生畏,它參數多,而且參數類型也如此陌生。是的,正是因為如此它才功能強大!
但不要怕,作為一般使用,非常簡單!下面便是一個簡單的例子(啟動記事本):
STARTUPINFO StartInfo;
PROCESS_INFORMATION pinfo;
//對程序的啟動信息不作任何設定,全部清0
memset(&StartInfo,0,sizeof(STARTUPINFO));
StartInfo.cb = sizeof(STARTUPINFO);//設定結構的大小
BOOL ret=CreateProcess(
NULL, //啟動程序路徑名
"notepad.exe", //參數(當exeName為NULL時,可將命令放入參數前)
NULL, //使用默認進程安全屬性
NULL, //使用默認線程安全屬性
FALSE, //句柄不繼承
NORMAL_PRIORITY_CLASS, //使用正常優先級
NULL, //使用父進程的環境變量
NULL, //指定工作目錄
&StartInfo, //子進程主窗口如何顯示
&pinfo); //用于存放新進程的返回信息
這樣在創建成功這后我們就可以從pinfo中找到它的:進程句柄,線程句柄,進程ID,線程ID
在附件源碼中演示了進程序的啟動,停止。
實際上我們可以通過很多方式如內存共享、父進程窗體句體傳入仍后從消息中獲得子進程窗體句柄等,來實現更多的控制。
想很好地掌握CreateProcess,可參見人民郵電出版社出版的<< Windows系統編程 >>,它的“進程”部份作了很詳盡的說明。
例程:
#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不能打開網站或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的第二個參數為你想執行的操作(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).
*/