VC 呼叫外部可執行程式

2021-08-26 04:07:41 字數 2546 閱讀 5048

方法一:

winexec( "notepad.exe,sw_show ");

方法二:

shellexecute(null, "open ", "notepad.exe ",null,null,sw_showmaximized)

方法三:

startupinfo si;

::zeromemory(&si,sizeof(startupinfo));

si.cb=sizeof(startupinfo);

process_information pi;

if(::createprocess(null,_t( "notepad.exe "),null,null,false,normal_priority_class,null,null,&si,&pi))

等於 0

error_file_not_found = 2;

error_path_not_found = 3;

error_bad_format = 11;

//ucmdshow 引數可選值:

sw_hide = 0;

sw_shownormal = 1;

sw_normal = 1;

sw_showminimized = 2;

sw_showmaximized = 3;

sw_maximize = 3;

sw_shownoactivate = 4;

sw_show = 5;

sw_minimize = 6;

sw_showminnoactive = 7;

sw_showna = 8;

sw_restore = 9;

sw_showdefault = 10;

sw_max = 10;

其中以winexec最為簡單,

shellexecute

winexec靈活一些,

createprocess最為複雜。

winexec

有兩個引數,前乙個指定路徑,後乙個指定顯示方式。

shellexecute

可以指定工作目錄,並且還可以尋找檔案的關聯直接開啟不用載入與檔案關聯的應用程式,shellexecute還可以開啟網頁,啟動相應的郵件關聯傳送郵件等等。

createprocess

一共有十個引數,不過大部分都可以用null代替,它可以指定程序的安全屬性,繼承資訊,類的優先順序等等。如果我們要得到足夠多的關於新的程序的資訊,控制新的程序的細節屬性,若要達到這些目的,就需要使用createprocess函式了。具體用法如下:

這個函式最簡單,只有兩個引數,原型如下:

uint winexec(

lpcstr lpcmdline, // 命令路徑

uint ucmdshow // 顯示方式,共有11種,具體可以查閱msdn的showwindow函式 );

使用方法如下:

winexec("notepad.exe", sw_show); // 開啟記事本

winexec("d:\\program files\\test\\test.exe",sw_showmaximized); // 以最大化的方式開啟test.exe(注意檔名的大小寫也必須完全一樣)

需要注意的是若用 sw_showmaxmized 方式去載入乙個無最大化按鈕的程式,譬如calc (計算器),就不會出現正常的窗體,但是已經被加到任務列表裡了。

原型如下:

hinstance shellexecute(

hwnd hwnd, //父視窗控制代碼

lpctstr lpoperation, //操作, 開啟方式"edit","explore","open","find","print","null"

lpctstr lpfile, //檔名,前面可加路徑

lpctstr lpparameters, //引數

lpctstr lpdirectory, //預設資料夾

int nshowcmd //顯示方式 );

使用方法如下:

shellexecute(null,"open","c:\\test.txt",null,null,sw_shownormal); // 開啟c:\test.txt 檔案

shellexecute(null, "open", "", null, null, sw_shownormal); // 開啟網頁www.google.com

shellexecute(null,"explore", "d:\\c++",null,null,sw_shownormal); // 開啟目錄d:\c++

shellexecute(null,"print","c:\\test.txt",null,null, sw_hide); // 列印檔案c:\test.txt

注意:shellexecute不支援定向輸出。

原型如下:

VC 呼叫外部可執行程式

方法一 winexec notepad.exe,sw show 方法二 shellexecute null,open notepad.exe null,null,sw showmaximized 方法三 startupinfo si zeromemory si,sizeof startupinfo ...

Linux 呼叫可執行程式

3.使用execl 常用方法 二 system函式 在c c 程式中,經常需要呼叫其它的程式來先成某項任務,例如其它的c c 程式 作業系統命令或shell指令碼,c c 提供了exec函式族和system函式來實現這個功能。exec函式族提供了乙個在程序中啟動另乙個程式執行的方法。它可以根據指定的...

在程式中如何呼叫可執行程式。

第乙個程式 呼叫winexec 函式,但是開啟應用程式後,得手動關閉,沒有自行結束的方法。include include include int main 第二個程式 呼叫creatprocess 函式,它可以用terminateprocess 函式使之關閉,還可以使之延遲時間。include in...