ShellExecute()功能: 运行一个外部程序,并对外部程序有一定的控制.
原型:
HINSTANCE ShellExecute( HWND hwnd,
LPCTSTR lpOperation,
LPCTSTR lpFile,
LPCTSTR lpParameters,
LPCTSTR lpDirectory,
INT nShowCmd
);
hwnd: 父窗口句柄;
lpOperation: 指定进行的操作,默认操作为open;
open:执行由lpFile指定的程序,或打开由lpFile指定的文件或文件夹
print: 打印由pFile指定的文件
explore: 浏览由pFile指定的文件夹
find: 从指定的目录开始搜索
lpFile: 指定需要执行指定操作的文件或文件夹
lpParameters: 若lpFile参数是一个可执行程序,则此参数指定命令行参数,否则此参数应为NULL
lpDirectory: 指定默认目录
nShowCmd:若lpFile参数是一个可执行程序,则此参数指定程序窗口的初始显示方式,否则此参数应设置为NULL
返回值: 执行成功则返回大于32的实例句柄,失败则返回小于32的值
实例:
1.打开一个应用程序:
ShellExecute(NULL,"open","D://myprocess.exe","","", SW_SHOW );
2.用系统程序打开一个相关的文档
ShellExecute(NULL,"open","notepad.exe","c://MyLog.log","",SW_SHOW );
3.打开一个网页
ShellExecute(NULL,"open","http://www.google.com","","", SW_SHOW );
4. 发送email
ShellExecute(NULL,"open","[email protected]","","", SW_SHOW );
5.用系统打印机打印文档
ShellExecute(NULL,"print","c://test.txt","","", SW_HIDE);
6.用系统查找功能来查找指定文件
ShellExecute(NULL,"find","d://master",NULL,NULL,SW_SHOW);
7.启动一个程序直到它运行完毕
SHELLEXECUTEINFO ShExecInfo = {0};
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = "c://MyTest.exe";
ShExecInfo.lpParameters = "";
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_SHOW;
ShExecInfo.hInstApp = NULL;
ShellExecuteEx(&ShExecInfo);
WaitForSingleObject(ShExecInfo.hProcess,INFINITE); //等待句柄有信号
8.显示文件或文件夹的属性
SHELLEXECUTEINFO ShExecInfo ={0};
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_INVOKEIDLIST ;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = "properties";
ShExecInfo.lpFile = "c://Programer Files";
ShExecInfo.lpParameters = "";
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_SHOW;
ShExecInfo.hInstApp = NULL;
ShellExecuteEx(&ShExecInfo);
9.运行一个批处理,把当前运行目录传送进去
ShellExecute(NULL,"open","d://master.cmd",NULL,"D://",SW_SHOW);