ShellExecute 會自動解析系統檔案關聯,無需指定完整路徑即可啟動對應應用程式。支援檔案、URL、郵件、列印、搜尋等多種動作。
// 開啟應用程式
ShellExecute(m_hWnd, L"open", L"calc.exe", nullptr, nullptr, SW_SHOW);
ShellExecute(m_hWnd, L"open", L"notepad.exe", L"c:\\MyLog.log", nullptr, SW_SHOW);
// 開啟關聯文件
ShellExecute(m_hWnd, L"open", L"c:\\abc.txt", nullptr, nullptr, SW_SHOW);
// 開啟網頁
ShellExecute(m_hWnd, L"open", L"https://www.google.com", nullptr, nullptr, SW_SHOW);
// 發送郵件
ShellExecute(m_hWnd, L"open", L"mailto:user@example.com", nullptr, nullptr, SW_SHOW);
// 列印文件
ShellExecute(m_hWnd, L"print", L"c:\\abc.txt", nullptr, nullptr, SW_HIDE);
// 搜尋資料夾
ShellExecute(m_hWnd, L"find", L"d:\\folder", nullptr, nullptr, SW_SHOW);
SHELLEXECUTEINFO sei = { sizeof(sei) };
sei.fMask = SEE_MASK_NOCLOSEPROCESS;
sei.lpVerb = L"open";
sei.lpFile = L"c:\\MyProgram.exe";
sei.nShow = SW_SHOW;
ShellExecuteEx(&sei);
WaitForSingleObject(sei.hProcess, INFINITE);
CloseHandle(sei.hProcess);
PROCESS_INFORMATION pi;
STARTUPINFO si = { sizeof(si) };
if (CreateProcess(L"c:\\winnt\\notepad.exe", nullptr, nullptr, nullptr,
FALSE, 0, nullptr, nullptr, &si, &pi)) {
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}
SHELLEXECUTEINFO sei = { sizeof(sei) };
sei.fMask = SEE_MASK_INVOKEIDLIST;
sei.lpVerb = L"properties";
sei.lpFile = L"c:\\";
ShellExecuteEx(&sei);
lpVerb:open、print、edit、find、propertiesnShow:SW_SHOW、SW_HIDE、SW_MAXIMIZE 等