可以使用 ShellExecute 打开文件或执行程序。
HINSTANCE ShellExecute(
_In_opt_ HWND hwnd,//父窗口句柄或出错时显示错误父窗口的句柄,可以为 NULL
_In_opt_ LPCTSTR lpOperation,//操作
_In_ LPCTSTR lpFile,//要打开的文件名、执行的程序名、浏览的文件夹等。
_In_opt_ LPCTSTR lpParameters,//可执行程序的参数,否则为 NULL
_In_opt_ LPCTSTR lpDirectory,//默认目录
_In_ INT nShowCmd//显示类型
);
lpOperation 是一个字符串,通常为:
nShowCmd 执行操作之后程序显示类型,指定该参数后运行起来的程序不一定能按照指定参数显示,只是通知程序的显示状态。类型如下:
返回值:
返回值为被执行程序的实例句柄。若返回值小于32,则表示出现错误。错误如下:
假设将FileName參数设置为http:
协议格式,那么该函数将打开默认浏览器并链接到指定的URL地址。
若用户机器中安装了多个浏览器,则该函数将依据Windows 9x/NT注冊表中http协议处理程序(Protocols Handler)的设置确定启动哪个浏览器。
如:ShellExecute(handle,L"open",L" http://www.neu.edu.cn", NULL, NULL, SW_SHOWNORMAL);
ShellExecute()函数在HKEY_CLASSES_ROOT\http\shell\open\command
下进行搜索。默认浏览器在注冊表HKEY_CLASSES_ROOT\.htm
键下的Default设置。
假设将FileName參数设置为mailto:
协议格式,那么该函数将启动默认邮件客户程序,如Microsoft Outlook(也包含Microsoft Outlook Express)或Netscape Messanger。若用户机器中安装了多个邮件客户程序,则该函数将依据Windows 9x/NT注冊表中mailto协议处理程序的设置确定启动哪个邮件客户程序。mailto:用户账号@邮件server地址?subject=邮件主题&body=邮件正文;
如:ShellExecute(handle,L"open",L"mailto:[email protected]?subject=Hello&Body=This is a test", NULL,NULL, SW_SHOWNORMAL);
打开新邮件窗体,并自己主动填入收件人地址、邮件主题和邮件正文。若邮件正文包含多行文本,则必须在每行文本之间增加换行转义字符%0a。ShellExecute()函数在HKEY_CLASSES_ROOT\mailto\shell\open\command
下搜索。
ShellExecute(NULL,L"open",L"notepad.exe", L"c:\\MyLog.log", NULL, SW_SHOW );
ShellExecute(NULL,L"print", L"c:\\abc.txt",NULL, NULL, SW_HIDE);
ShellExecute(NULL,L"find",L"d:\\nish", NULL, NULL, SW_SHOW);
SHELLEXECUTEINFO ShExecInfo = {0};
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile =L"c:\\MyProgram.exe";
ShExecInfo.lpParameters = NULL;
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_SHOW;
ShExecInfo.hInstApp = NULL;
ShellExecuteEx(&ShExecInfo);
WaitForSingleObject(ShExecInfo.hProcess,INFINITE);
或:
PROCESS_INFORMATION ProcessInfo;
STARTUPINFO StartupInfo; //This is an [in] parameter
ZeroMemory(&StartupInfo, sizeof(StartupInfo));
StartupInfo.cb = sizeof StartupInfo ; //Only compulsory field
if(CreateProcess(L"c:\\winnt\\notepad.exe", NULL, NULL,NULL,FALSE,0,NULL, NULL,&StartupInfo,&ProcessInfo))
{
WaitForSingleObject(ProcessInfo.hProcess,INFINITE);
CloseHandle(ProcessInfo.hThread);
CloseHandle(ProcessInfo.hProcess);
}
else
MessageBox(NULL,L"The process could not be started",NULL,NULL);
SHELLEXECUTEINFO ShExecInfo ={0};
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_INVOKEIDLIST ;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = L"properties";
ShExecInfo.lpFile = L"c:\\"; //can be a file as well
ShExecInfo.lpParameters = NULL;
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_SHOW;
ShExecInfo.hInstApp = NULL;
ShellExecuteEx(&ShExecInfo);
int ret = (int)ShellExecute(NULL, _T("open"), _T("Dbgview.exe"), NULL, NULL, SW_NORMAL);//打开exe
if (ret < 32)//检测是否指定成功
MessageBox(_T("ERROR"));
ret = (int)ShellExecute(NULL, _T("open"), _T("help.pdf"), NULL, NULL, SW_NORMAL);//打开指定文件,将调用默认处理的程序打开
if (ret < 32)
MessageBox(_T("ERROR"));
ret = (int)ShellExecute(NULL, _T("open"), _T("https://www.baidu.com"), NULL, NULL, SW_NORMAL);//打开网址
if (ret < 32)
MessageBox(_T("ERROR"));
ret = (int)ShellExecute(NULL, _T("open"), _T("c:\\windows"), NULL, NULL, SW_NORMAL);//打开文件夹
if (ret < 32)
MessageBox(_T("ERROR"));
ret = (int)ShellExecute(NULL, _T("runas"), _T("cmd.exe"), NULL, NULL, SW_NORMAL);//请求管理员权限打开cmd
if (ret < 32)
MessageBox(_T("ERROR"));
实例:
#include
#include
#include
#include
int main(void)
{
test01();
}
int test01()
{
HINSTANCE hNewExe = ShellExecuteA(NULL, "open", "calc.exe", NULL, NULL, SW_SHOW);
if ((DWORD)hNewExe <= 32)
{
printf("return value:%d\n", (DWORD)hNewExe);
}
else
{
printf("successed!\n");
}
printf("GetLastError: %d\n", GetLastError());
system("pause");
return 1;
}