Search code examples
windowsbatch-filecommand-lineregistryuri

Passing parameters to a custom URI


I have a custom uri like this:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Demo]
@="URL:Demo Protocol"
"URL Protocol"=""

[HKEY_CLASSES_ROOT\Demo\DefaultIcon]
@="\"D:\\demo.bat\""

[HKEY_CLASSES_ROOT\Demo\shell]

[HKEY_CLASSES_ROOT\Demo\shell\open]

[HKEY_CLASSES_ROOT\Demo\shell\open\command]
@="\"D:\\demo.bat\" \"%1\" \"%2\" \"%3\" \"%4\""

For running this from the command prompt, I use this command:

D:\demo.bat -ping -ip 172.18.102.65

But for running the same from browser, if I use the custom URI as Demo: then it is asking me to pass the parameters. I am not aware of how to pass the parameters to the custom URI if I run it form the browser.

How to pass the parameters while running this custom uri from browser? Please advise.


Solution

  • When using a custom URL, the entire URL is passed to the registered app/script as a single parameter. Your app/script needs to parse the URL to extract what it needs. For example:

    [HKEY_CLASSES_ROOT\Demo\shell\open\command]
    @="\"D:\\demo.bat\" \"%1\""
    

    You can format the URL any way you want, as long as it is a valid URL and begins with demo:, eg:

    D:\demo.bat "demo:ping?ip=172.18.102.65"
    
    D:\demo.bat "demo:ping=172.18.102.65"
    
    D:\demo.bat "demo:ping%20172.18.102.65"
    

    Update: Note, however, that this only works with Internet Explorer (and Windows Explorer, and the Windows Shell). You need another solution for other browsers. For instance, Firefox has its own Protocol Handler mechanisms:

    Web-based protocol handlers

    Adding a New Protocol to Mozilla

    Writing a Firefox Protocol Handler