1

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?

Mona
  • 337
  • 3
  • 15
  • 1
    Works fine for me, maybe it's an issue with your browser? Also note that your sample code does not compile. – MrEricSir Sep 13 '15 at 20:22
  • Sorry, I forgot to add the "L" before http address. I was using wstring.c_str() in the orginal code. I use Google Chrome as the browser on Windows 10. – Mona Sep 13 '15 at 20:27
  • 1
    You should move your edit into an answer (you can and should answer your own question if you found the answer) and accept it. – Claudiu Sep 13 '15 at 23:12
  • Thank you for the tip, I added the answer now, but I can "accept it" in 2 days (a message with this information displays). – Mona Sep 14 '15 at 01:49

2 Answers2

0

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);
        }
    }
Mona
  • 337
  • 3
  • 15
0

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);
  • Welcome on the SO! Please explain, how your answer works and how does it solve the problem. Snippet-only answers are not very high quality. – peterh Aug 17 '20 at 15:47