您的位置:首页 > 编程语言 > C语言/C++

c/c++中运行外部程序

2009-10-22 14:17 99 查看
关于三个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).
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: