Skip to content
⌘ NSIS Forum Archive

Detecting default browser on Windows 8

9 posts

stefque#

Detecting default browser on Windows 8

Hi guys,

I have trouble detecting the default browser on Win 8.1 Pro, but it goes the same for Win 8 also.
I have a script that worked just fine on Win 7:

FileOpen $0 "$PLUGINSDIR\dummy.htm" "w"
FileClose $0
System::Call "Shell32::FindExecutable(t '$PLUGINSDIR\dummy.htm', i 0, t .r1)"
${StrLoc} $0 $1 "chrome.exe" ">"
${If} $0 == ""
${StrLoc} $0 $1 "firefox.exe" ">"
${If} $0 == ""
${StrLoc} $0 $1 "iexplore.exe" ">"
${If} $0 == ""
StrCpy $BR "00"
${Else}
StrCpy $BR "IE"
${EndIf}
${Else}
StrCpy $BR "FF"
${EndIf}
${Else}
StrCpy $BR "CH"
${EndIf}
This doesn't work on Windows above Win 7.



After this command:
System::Call "Shell32::FindExecutable(t '$PLUGINSDIR\dummy.htm', i 0, t .r1)"
on Win 7 I can print $1 and get: "C:\Program Files\Internet Explorer\iexplore.exe"

but for some reason, on Win 8.1, I get: "C:\Windows\system32\OpenWith.exe" and I always get BR = "00", meaning that some other browser (other than GC, FF, IE) is the default browser.

Do you have any instructions, or code samples, that could help me overcome this Win 8 issue?

Best,
Stefan
Anders#
There is no such thing as a default browser in Windows, a user can set FF to open .htm files, CH to open .html files, IE to open the HTTP protocol and Safari to open the HTTPS protocol if they wanted to. OpenWith.exe probably means the user has not chosen a default handler for .htm files yet. There are of course other browsers like Opera and Maxthon so you need to be able to handle BR = "00" anyway.

If you just want to open a .htm file or a URL you should just use ExecShell and not actually try to detect the browser.

If you look at http://blogs.msdn.com/b/oldnewthing/.../10641381.aspx you see that what they call the default browser is the application that handles the http protocol. You can use the same detection if you want:

!define ASSOCSTR_COMMAND 1
!define ASSOCSTR_EXECUTABLE 2
!define ASSOCF_NOTRUNCATE 0x00000020
!define ASSOCF_REMAPRUNDLL 0x00000080
!define ASSOCF_NOFIXUPS 0x00000100
!define ASSOCF_ISPROTOCOL 0x00001000



System::Call 'SHLWAPI::AssocQueryString(i ${ASSOCF_NOTRUNCATE}|${ASSOCF_REMAPRUNDLL}|${ASSOCF_NOFIXUPS}|${ASSOCF_ISPROTOCOL}, i ${ASSOCSTR_COMMAND}, t "http", t "open", t.r1, *i ${NSIS_MAX_STRLEN})i.r0' ; WinXP is buggy is you don't force the open verb...
DetailPrint Command:hr=$0,string=$1

System::Call 'SHLWAPI::AssocQueryString(i ${ASSOCF_NOTRUNCATE}|${ASSOCF_REMAPRUNDLL}|${ASSOCF_NOFIXUPS}|${ASSOCF_ISPROTOCOL}, i ${ASSOCSTR_EXECUTABLE}, t "http", t "open", t.r1, *i ${NSIS_MAX_STRLEN})i.r0'
DetailPrint Executable:hr=$0,string=$1
T.Slappy#
Try this simple code which is working for me (on all OSs):

    ; Detect browser
    FileOpen $1 "$PLUGINSDIR\dummy.htm" "w"
    FileClose $1
    !ifdef IS_UNICODE
        System::Call "Shell32::FindExecutableW(t '$PLUGINSDIR\dummy.htm', i 0, t .r1)"
    !else
        System::Call "Shell32::FindExecutableA(t '$PLUGINSDIR\dummy.htm', i 0, t .r1)"
    !endif
    ${DebugMsg} "Your Default Browser is: $1" 

DebugMsg is simple MessageBox call.
What you need is to set symbol IS_UNICODE correctly: If you are using Unicode NSIS.
Anders#
Originally Posted by T.Slappy View Post
Try this simple code which is working for me (on all OSs).
This code is basically the same as the original, just a little broken! The OP is getting back a valid string so their system syntax is already correct!

Why invent a new Unicode define when we already have one?

Why hardcode A and W suffix when it is not required?

You should not use the t string type when forcing A/W.

Did you actually test on everything from Win95 to Win10? Win95RTM did not ship with a browser...
stass#
Anders
From your example, it turns out that something strange ... Windows 7 x32
Executable:hr=-2147023741,string=
Completed
Anders#
Originally Posted by stass View Post
Anders
From your example, it turns out that something strange ... Windows 7 x32

Executable:hr=-2147023741,string=
Completed
0x80070483 = No application is associated with the specified file for this operation.

There can probably be multiple reasons why this is returned. It is unlikely that your machine does not have a open verb for http but I guess it could happen. A more likely reason is that a shell extension is the handler and not a simple .exe file, this is completely legal, there is no guarantee that you can find the name of the .exe that will run when you ShellExecute something...
stass#edited
Got it ... ASSOCF_IS_PROTOCOL - in Win7 does not work ...

In Win7 x32 as possible :

System::Call 'SHLWAPI::AssocQueryString(i ${ASSOCF_NOTRUNCATE}|${ASSOCF_REMAPRUNDLL}|${ASSOCF_NOFIXUPS}, i ${ASSOCSTR_EXECUTABLE}, t ".htm", t "open", t.r1, *i ${NSIS_MAX_STRLEN})i.r0'

However, Win 10 x64 - it does not work ...
and :

System::Call 'SHLWAPI::AssocQueryString(i ${ASSOCF_NOTRUNCATE}|${ASSOCF_REMAPRUNDLL}|${ASSOCF_NOFIXUPS}|${ASSOCF_ISPROTOCOL}, i ${ASSOCSTR_EXECUTABLE}, t ".htm", t "open", t.r1, *i ${NSIS_MAX_STRLEN})i.r0'

and :

System::Call 'SHLWAPI::AssocQueryString(i ${ASSOCF_NOTRUNCATE}|${ASSOCF_REMAPRUNDLL}|${ASSOCF_NOFIXUPS}|${ASSOCF_ISPROTOCOL}, i ${ASSOCSTR_EXECUTABLE}, t "http", t "open", t.r1, *i ${NSIS_MAX_STRLEN})i.r0'

Result:
Executable:hr=0,string=C:\Windows\system32\LaunchWinApp.exe
The only thing that happened:

!define ASSOCSTR_FRIENDLYAPPNAME 4
System::Call 'SHLWAPI::AssocQueryString(i ${ASSOCF_NOTRUNCATE}|${ASSOCF_REMAPRUNDLL}|${ASSOCF_NOFIXUPS}|${ASSOCF_ISPROTOCOL}, i ${ASSOCSTR_FRIENDLYAPPNAME}, t ".htm", t "open", t.r1, *i ${NSIS_MAX_STRLEN})i.r0'

Result:
Command:hr=0,string=Microsoft Edge
How to get the full path to Microsoft Edge of function ?
Anders#
ASSOCF_IS_PROTOCOL is documented as Win8+ but XP did not care when I tested but you should of course only set it for URL protocols like HTTP, not on file extensions like .htm.

If the function returns 0 then it was successful but like I already said, it does not mean you can always get the name of the .exe. If Microsoft uses a generic launcher then that is the design they chose and there might not be a lot you can do about it.
Anders#
Another thing you can try is to read from the registry under \SOFTWARE\Clients\StartMenuInternet ( https://msdn.microsoft.com/en-us/lib...=vs.85%29.aspx "This registration is deprecated as of Windows 7"). You must see if there is a default in HKCU and fall back to HKLM (On my machine I have not used the default applications applet in the control panel so I don't have a Clients key under HKCU!)