谷歌浏览器打破了ShellExecute()?

时间:2020-03-06 14:30:57  来源:igfitidea点击:

多年以来,我一直在使用ShellExecute()API从应用程序中启动默认的Web浏览器。像这样:

ShellExecute( hwnd, _T("open"), 
    _T("http://www.winability.com/home/"), 
    NULL, NULL, SW_NORMAL );

直到几周前,当Google发布其Chrome浏览器时,它一直运行良好。现在,如果计算机上安装了Chrome,则ShellExecute API将不再打开网页。

有没有人想出如何解决这个问题? (检测Chrome并显示一条消息告诉用户是Chrome的故障吗?)

编辑:谢尔盖(Sergey)提供的代码似乎可以正常工作,因此我已将其作为"答案"。除了我不喜欢WinExec的调用外:MSDN读取到提供WinExec只是为了与16位应用程序兼容。 IOW,它可能会停止与任何Service Pack一起使用。我没有尝试过,但是如果它已经停止与Windows x64一起使用,我不会感到惊讶,因为它根本不支持16位应用程序。因此,我将使用ShellExecute,而不是WinExec,将路径从注册表中提取出来,就像Sergey的代码一样,并以URL作为参数。谢谢!

解决方案

这是适用于所有浏览器的代码。诀窍是如果ShellExecute失败,则调用WinExec。

HINSTANCE GotoURL(LPCTSTR url, int showcmd)
{
    TCHAR key[MAX_PATH + MAX_PATH];

    // First try ShellExecute()
    HINSTANCE result = 0;

    CString strURL = url;

    if ( strURL.Find(".htm") <0 && strURL.Find("http") <0 )
        result = ShellExecute(NULL, _T("open"), url, NULL, NULL, showcmd);

    // If it failed, get the .htm regkey and lookup the program
    if ((UINT)result <= HINSTANCE_ERROR) {

        if (GetRegKey(HKEY_CLASSES_ROOT, _T(".htm"), key) == ERROR_SUCCESS) {
            lstrcat(key, _T("\shell\open\command"));

            if (GetRegKey(HKEY_CLASSES_ROOT,key,key) == ERROR_SUCCESS) {
                TCHAR *pos;
                pos = _tcsstr(key, _T("\"%1\""));
                if (pos == NULL) {                     // No quotes found
                    pos = strstr(key, _T("%1"));       // Check for %1, without quotes
                    if (pos == NULL)                   // No parameter at all...
                        pos = key+lstrlen(key)-1;
                    else
                        *pos = '##代码##';                   // Remove the parameter
                }
                else
                    *pos = '##代码##';                       // Remove the parameter

                lstrcat(pos, _T(" \""));
                lstrcat(pos, url);
                lstrcat(pos, _T("\""));
                result = (HINSTANCE) WinExec(key,showcmd);
            }
        }
    }

    return result;
}