使用 ShellExecute c++ 打开长网址

Open long url using ShellExecute c++

本文关键字:ShellExecute c++ 使用      更新时间:2023-10-16

我正在尝试使用 ShellExecute() 打开一个网址。该 url 由我的程序为长 http get 请求生成,ShellExecute()不起作用并且没有显示响应。

    ShellExecute(NULL, _T("open"), url, NULL, NULL, SW_SHOWNORMAL); // Does nothing when url is too long

比我为同一个命令编写了一个批处理文件,当 url 长度大于 259 个字符时,它会显示此错误:

start "" "{mywebsite}/&&&&..." // Repeating &

Windows cannot find 
'{my-url}/{long-get-request} ....
Make sure you typed the name correctly, and then try again.

扩展ShellExecute()字符限制的任何想法?或者也许是一个很酷的解决方案,除了ShellExecute()system()System::Diagnostics::Process::Start()之外,打开一个长网址,它们都无法正常工作。

链接复制到 *.html 文件的建议将起作用,但是ShellExecute将运行与*.html关联的程序,而不是http:。从理论上讲,这些关联可能是不同的。如果您不在乎,那么只需找到与*.html的关联并使用CreateProcess,如下所示:

std::wstring url = L"http://localhost/fake.php?123";
wchar_t buf[MAX_PATH] = { 0 };
DWORD size = _countof(buf);
AssocQueryString(0, ASSOCSTR_EXECUTABLE, L".html", 0, &buf[0], &size);
std::wstring cmd(buf);
cmd += L" ";
cmd += url;
PROCESS_INFORMATION pi;
STARTUPINFO si{ sizeof(si) };
CreateProcess(0, &cmd[0], 0, 0, FALSE, NORMAL_PRIORITY_CLASS, 0, 0, &si, &pi);
...

在 Windows 10 中,ShellExecute接受大型参数行。

我想我有一个解决方案,但这可能不是最好的解决方案。基本上,我创建了一个重定向到我的长网址的 html 文件。

private: Void showPage(const string& httpGetString) {
            string url = "{my-website}?" + httpGetString;
            remove("jumper.html");
            string htmlJumper = "<html><meta http-equiv="refresh" content="0; url=" + url + ""</html>";
            fstream jumperFile;
            jumperFile.open("jumper.html", ios::out);
            jumperFile << htmlJumper;
            jumperFile.close();
            ShellExecute(NULL, _T("open"), L"jumper.html", NULL, NULL, SW_SHOWNORMAL);
        }