c/c++中运行外部程序或打开文件(转)

c/c++中运行外部程序或打开文件(转)

关于三个SDK函数: WinExec, ShellExecute,CreateProcess 的其他注意事项:

【1】定义头文件
必须定义以下两个头文件:

#include <shlobj.h> // 可替换为 windows.h
#include <shellapi.h>

如果定义了头文件 #include <windows.h>的话就不必定义 #include <shlobj.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);

1、调用API: int system( const char *command ); 
  你可以传入一命令,启动某个程序。如"ping www.vccode.com", "YourExe"等等 
  不过这里有几点要值得注意: 
  (1)、他不会立即返回,直到你启动的程序执行完成。 
  (2)、如果你启动是windows程序,它仍然会启动一个控制台,这就给人感觉太差劲了,但如果本身是控制台的,而且又需要等待它的完成,那这将是比较好的选择。 
  (3)、它的返回值代表是否执行成功以及程序的退出码。
  (4)、不能运行*.txt文件或"www.baidu.com"

2、调用API: 
  UINT WinExec( 
   LPCSTR lpCmdLine, // command line 
   UINT uCmdShow // window style 
  ); 
  这个API与API:system同样的使用简单,同用是使用命令行型式。 
  不过它与API:system相比,有几个优点: 
  (1)、它将启动了一个新进程,并且立即返回,因此你的程序无需等待。 
  (2)、它的多了一个参数:uCmdShow,通过它你可以一定程度上控件窗体的显示,比如让它后台运行而不显示出来。 
  (3)、它无论启动控制台程序还是windows程序都只做你想要做的事。 

  它的不足之处: 
  (1)、它完全与本进程脱离,无法对做些必要的控制 
  (2)、无法得知启动的程序是否退出。 
  (3)、得不到启动的程序的退出码。 
  (4)、不能运行*.txt文件或"www.baidu.com"
3、调用: 
  HINSTANCE ShellExecute( 
   HWND hwnd, 
   LPCTSTR lpVerb, 
   LPCTSTR lpFile, 
   LPCTSTR lpParameters, 
   LPCTSTR lpDirectory, 
   INT nShowCmd 
  ); 
  它也有WinExec同样的缺点。 
  它虽然传回一个HINSTANCE,但他并不是真正的句柄,我们仅能拿它来做一些错误值检查。 

  但它的功能比前两者更强大,它执行系统的Shell命令。 
  1、2中如果传入“XX.txt”,它们将不能成功执行,ShellExecute却能很好地执行,它将启动一个默认的文字处理程序来打开它。 
  1、2中如果传入“www.vccode.com”,将不能成功执行,而ShellExecute却能很好地执行,它将启动一个默认浏览器来打开这个网站。 

  参数讲解: 
  参数1 hwnd:一窗体句柄,将作为启动的程序的父窗体。 
  参数2 lpVerb:你想执行的操作(edit 、explore、find、open、print、properties),你也可以传入NULL值,它将执行默认操作(win2000以前与以后处理略有差别,请见MSDN)。 
  参数3 lpFile:一文件名或操作的对象。 
  参数4 lpParameters:如果lpFile是一可执行文件,这个将作为它的参数。它的格式由执行的操作决定。而且当lpFile为一document文件时,此参数需为NULL。 
  参数5 lpDirectory:指定它的工作目录。 
  参数6 nShowCmd:窗体显示的控制。 

  以下是几个例子: 
  //启动一个Dos命令, 启动windows程序相同 
  ::ShellExecute(this->GetSafeHwnd(), NULL, "ping", "www.vccode.com", NULL, SW_SHOWNORMAL); 

  //打开一个文件 
  ::ShellExecute(this->GetSafeHwnd(), "open", "ReadMe.txt", NULL, NULL, SW_SHOWNORMAL); 
  //上面的动词可传可不传,但如果"ReadMe.txt"为"ReadMe.bat"那就得指定,否则将会当命令执行,而不是打开它。 

  //可打开目录 
  ::ShellExecute(this->GetSafeHwnd(), "open", "c:", NULL, NULL, SW_SHOWNORMAL); 

  //可打开网页 
  ::ShellExecute(this->GetSafeHwnd(), "open", "www.vccode.com", NULL, NULL, SW_SHOWNORMAL); 
  
  //浏览一个目录 
  ShellExecute(handle, "explore", "c:"NULL, NULL, SW_SHOWNORMAL); 

  //查看一个文件或目录的属性 
  //使用ShellExecuteEx,实现请见所附源码对应部分 

  如果没有特殊的控制要求,它己能为我们做很了,但当我们一定要能控件这个启动的进程时,那我们就得使用第4点了。 


4:调用API: 
  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>
#include<shellapi.h>
#include<stdio.h>
void main()
{
HWND handle;
printf("Function <WinExec>: It can run a cmd command,but can`t open *.txt and "www.*.*" ");
printf("Please press Enter go on ");
getchar();
WinExec("mspaint.exe",SW_SHOWNOACTIVATE);
/*winexec不能打开网站或txt文件*/
printf("Function <ShellExecute>: It can run a cmd command to open file or web ");
getchar();
printf("Open a txt file ");
ShellExecute(NULL,"open","C:\test.txt",NULL,NULL,SW_MINIMIZE);
getchar();
printf("Open a web ");
ShellExecute(NULL,NULL,"www.baidu.com",NULL,NULL,SW_SHOWNA);
getchar();
printf("Run a cmd command:ping www.sina.com ");
ShellExecute(NULL, NULL, "ping", "sina.com", NULL, SW_SHOWNORMAL);
getchar();
printf("打开目录 ");
ShellExecute(NULL, "open", "c:", NULL, NULL, SW_SHOWNORMAL);
getchar();
printf("浏览目录 ");
ShellExecute(NULL, "explore", "c:", NULL, NULL, SW_SHOWNORMAL);
getchar();
printf("文件属性 ");
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).
*/

原文地址:https://www.cnblogs.com/actionke/p/4192629.html