C/C++调用外部程序整理
2012-05-28 11:58
309 查看
今天在工作中,需要在C++程序中调用其它的可执行程序,在网上搜罗了一些,感觉很零碎,现整理如下,希望能对一些人有所帮助:
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.
Each function in this family loads and executes a new process:
The letter at the end of the function name determines the variation.
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","mailto:xxxx@yahoo.com","","", 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,
NULL,
FALSE,
0,
NULL,
NULL,
&startupInfo,
&ProcessInfo);
//等到call.exe执行完毕
WaitForSingleObject(ProcessInfo.hProcess,1000000);
MessageBox("调用程序结束!");
C语言:
1._execl函数家庭。
Each function in this family loads and executes a new process:
_execl, _wexecl | _execv, _wexecv |
_execle, _wexecle | _execve, _wexecve |
_execlp, _wexeclp | _execvp, _wexecvp |
_execlpe, _wexeclpe | _execvpe, _wexecvpe |
Each function in this family loads and executes a new process:
_execl, _wexecl | _execv, _wexecv |
_execle, _wexecle | _execve, _wexecve |
_execlp, _wexeclp | _execvp, _wexecvp |
_execlpe, _wexeclpe | _execvpe, _wexecvpe |
_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. |
Each of the _spawn functions creates and executes a new process:
_spawnl, _wspawnl | _spawnv, _wspawnv |
_spawnle, _wspawnle | _spawnve, _wspawnve |
_spawnlp, _wspawnlp | _spawnvp, _wspawnvp |
_spawnlpe, _wspawnlpe | _spawnvpe, _wspawnvpe |
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","mailto:xxxx@yahoo.com","","", 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,
NULL,
FALSE,
0,
NULL,
NULL,
&startupInfo,
&ProcessInfo);
//等到call.exe执行完毕
WaitForSingleObject(ProcessInfo.hProcess,1000000);
MessageBox("调用程序结束!");
相关文章推荐
- C/C++调用外部程序整理
- C++执行外部程序(调用外部exe程序)
- C++打开网页,发起QQ对话,调用外部exe程序
- C++调用外部程序
- 【VS开发】C++调用外部程序
- c++ 调用外部程序
- C/C++执行外部程序(调用外部exe程序)
- C++中调用外部可执行程序的方法--创建子进程
- C/C++执行外部程序(调用外部exe程序)
- C++调用外部应用程序的方法的整理总结
- C++调用外部应用程序的方法的整理总结(常用)
- C++调用外部应用程序的方法的整理总结
- C++中外部程序的调用方法
- c++调用外部程序
- C/C++使用WinAPI CreateProcess函数调用外部程序
- C/C++中调用外部程序
- C/C++执行外部程序(调用外部exe程序)
- c++ 调用外部程序exe-ShellExecuteEx
- C/C++项目调用外部exe程序方法
- C/C++中调用外部程序