2017-10-10 183 views
2

我試圖用ShellExecute()打開一個URL。 該網址是由我的程序生成的一個很長的HTTP請求,並且ShellExecute()不起作用並且沒有響應。使用ShellExecute C++打開長網址

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()以外,它們都無法工作。

+0

你可以得到默認的網頁瀏覽器,並通過'CreateProcessW'直接執行它。命令行最多可以有32766個字符。在Windows 8+中,獲取默認Web瀏覽器的命令行模板的一種方法是調用['AssocQueryString'](https://msdn.microsoft.com/en-us/library/bb773471),其標誌值爲' ASSOCF_ISPROTOCOL'來獲取'ASSOCSTR_COMMAND'來「打開」「http」協議。您必須替換模板中的參數 - 例如該URL字符串被提取爲「%1」。 – eryksun

回答

0

我想我有一個解決方案,但它可能不是最好的解決方案。基本上,我創建一個重定向到我的長網址的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); 
     } 
0

用於複製到* .html文件的鏈接將工作的建議,但是ShellExecute將運行與*.htmlhttp:相關聯的程序。理論上這些聯繫可以不同。如果你不關心,那麼只要找到伴隨*.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接受大爭論線。