天天看點

C/C++中調用外部程式

http://hi.baidu.com/ekepptbfluehlwe/item/321109e59ac745b52f140b57

C語言:

1._execl函數家庭

Each function in this family loads and executes a new process:

The letter at the end of the function name determines the variation.

_exec function suffix Description

e

envp, array of pointers to environment settings, is passed to the new process.

l

Command-line arguments are passed individually to _exec function. Typically used when the number of parameters to the new process is known in advance.

p

PATH environment variable is used to find the file to execute.

v

argv, array of pointers to command-line arguments, is passed to _exec. Typically used when the number of parameters to the new process is variable.

2._spawnl函數家庭

Each of the _spawn functions creates and executes a new process:

e、l、p、v 的含義同1.

3.system

用法:system函數是執行shell指令,在windows下就是将指令交給DOS或cmd.exe去執行。如果要改變c:\windows\下檔案名為myfile.txt檔案為隻讀屬性,可以執行:

system("attrib +r c:\windows\myfile.txt");

注:用VC++編寫源程式時,system應該寫為System

總結:

相同點:三者都可以建立一個新程序;

不同點:_execl與system建立一個新程序後不傳回,相當于建立的新程序替換了原來的調用程序;而_spawnl建立一個新程序後,原來的調用程序依然存在,并繼續執行。

注意:system在windows和linux中都可以使用,但由于windows和linux下的指令不同,它能調用的指令也不同。如linux下能調用system("ls"),但由于windows下沒有ls指令,是以不能調用。

VC++:

1 WinExec

原型:

UINT WinExec(

LPCSTR lpCmdLine, // address of command line

UINT uCmdShow      // window style for new application

);

用于十六位作業系統及相容系統.

例如:

WinExec("notepad.exe f:\\調用程式.txt",SW_SHOW);

WinExec("notepad.exe ",SW_SHOW);

不同的參數用空格分開,故路徑中不能有空格,而大部分程式預設是安裝在"...\Program Files\...",如word,這極大的限制了WinExec的應用範圍.

以上可不帶路徑:

1,程式所在目錄.

2,目前路徑.

3,系統目錄,可以用GetSystemDirectory得到.

4,Windows 目錄. 可以用TheGetWindowsDirectory得到.

5,在環境變量中設定的目錄.

2 ShellExecute

HINSTANCE ShellExecute(

   HWND hwnd,                   //父視窗句柄

   LPCTSTR lpOperation,         //操作,"open","print","explore"

   LPCTSTR lpFile,              //檔案名,前面可加路徑

   LPCTSTR lpParameters,        //參數

   LPCTSTR lpDirectory,         //預設檔案夾

   INT nShowCmd                 //顯示方式

打開一個應用程式

ShellExecute(this->m_hWnd,"open","calc.exe","","", SW_SHOW );

ShellExecute(this->m_hWnd,"open","notepad.exe","c:\MyLog.log","",SW_SHOW );

打開一個同系統程式相關連的文檔

ShellExecute(this->m_hWnd,"open","c:\abc.txt","","",SW_SHOW );

激活相關程式,發送EMAIL

ShellExecute(this->m_hWnd,"open","","","", SW_SHOW );

用系統列印機列印文檔

ShellExecute(this->m_hWnd,"print","c:\abc.txt","","", SW_HIDE);

lpParameters的用法示例:

一,建立一個可以接受參數的程式call.exe,添加如下代碼:

BOOL CCallApp::InitInstance()

{

int n = __argc;

for(int i = 1 ; i < n ; i++)

AfxMessageBox(__targv[i]);

      //__targv[0]存儲的是程式的檔案名

...

}

二,Alt + F7的進行Project setting, Debug -> program argurments ->"1 2 3 4 5".

如果有多個參數,用空格分開.

三,運作.

四,執行ShellExecute(NULL,NULL,"f:\\call.exe","1 2 3 4 5",NULL,SW_SHOW);

3 CreateProcess

BOOL CreateProcess(

LPCTSTR lpApplicationName,

LPTSTR lpCommandLine,

LPSECURITY_ATTRIBUTES lpProcessAttributes,

LPSECURITY_ATTRIBUTES lpThreadAttributes,

BOOL bInheritHandles,

DWORD dwCreationFlags,

LPVOID lpEnvironment,

LPCTSTR lpCurrentDirectory,

LPSTARTUPINFO lpStartupInfo,

LPPROCESS_INFORMATION lpProcessInformation

STARTUPINFO   startupInfo;

memset(&startupInfo,0,sizeof(STARTUPINFO));

startupInfo.cb = sizeof(STARTUPINFO);

示例:

//程式最啟動時最大化

startupInfo.dwFlags |= STARTF_USESHOWWINDOW;

startupInfo.wShowWindow = SW_SHOWMAXIMIZED;

//運作....exe

PROCESS_INFORMATION ProcessInfo;

BOOL bCreate = ::CreateProcess

       (

       "f:\\call.exe",// 1 2 3 4",

NULL,

       NULL,

       FALSE,

       0,

       &startupInfo,

       &ProcessInfo);

//等到call.exe執行完畢

WaitForSingleObject(ProcessInfo.hProcess,1000000);

MessageBox("調用程式結束!");

繼續閱讀