简体   繁体   中英

C++, ShellExecute a URL with the hash sign (#)

I use ShellExecute to open links from my program. It works fine, but not for all links. When the link has the hash sign (#), the link still opens, but not full (it's cut before the #). The code I'm using is:

ShellExecuteW(NULL, L"open", L"http://blablabla.com/something#something", NULL, NULL, SW_SHOWNORMAL);

I also tried:

ShellExecute(NULL, "open", "rundll32.exe", "url.dll,FileProtocolHandler http://blablabla.com/something#something",NULL,SW_SHOWNORMAL);

with the same result.

Does anybody know why this happens?

I solved the problem, after I read a similar question: ShellExecute fails for local html or file URLs

Then using: http://www.codeproject.com/Tips/607833/Where-is-the-Default-Browser-Command-Line-in-Regis I identified the default Internet Browser location, and I made a function:

std::size_t hhhel2 = linknewone.find("#");
    if (hhhel2 != std::string::npos) {
        LPTSTR pszBrowserPath;
        if (GetDefaultBrowserLaunchPath(&pszBrowserPath))
        {
            string browserpathWithLink = pszBrowserPath;
            if (browserpathWithLink.length() > 1) {
                string browserpathWithLinkTemp = browserpathWithLink.substr(0, 1);
                    std::size_t dfdX = browserpathWithLinkTemp.find("\"");
                    if (dfdX != std::string::npos) {
                        browserpathWithLink = browserpathWithLink.substr(1);
                    }
                std::size_t dfd = browserpathWithLink.find("\"");
                if (dfd != std::string::npos) {
                    browserpathWithLink = browserpathWithLink.substr(0, dfd);

                }
            }
            delete[] pszBrowserPath;
            ShellExecute(NULL, _T("open"), browserpathWithLink.c_str(), linknewone.c_str(), NULL, SW_SHOWNORMAL);
        }
    }

I think I solved the problem with a simpler method in a QT Applicaiton. When I didn't add the "file:///" to string, it truncates after the sharp character in the url.

QString currentpath = QDir::currentPath();
QString url = "/help//html/index.html#current";
QString full_url = "file:///" + currentpath + url;
QByteArray full_url_arr= full_url.toLocal8Bit();
LPCSTR lp = LPCSTR(full_url_arr.constData());
ShellExecute(NULL, "open", lp, NULL, NULL, SW_SHOWNORMAL);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM