Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 206
user182944 Avatar asked Oct 17 '14 14:10

user182944


2 Answers

Not that I want to reopen such an old question but I was looking for a better answer to this all morning with no luck. The OP was lucky in that he had a batch file that was modifiable. However, I wanted to add a URI to an existing program (putty) without having to install additional files.

There were two problems as I see them, the first was the full URI including the protocol being passed as one parameter. The second is not being able to pass multiple parameters.

For the first solution of the protocol prefix, it can be solved by doing a little string manipulation. Such that the command value in the registry looks like:

cmd /V:ON /C "SET r=%1 & start D:\demo.bat !r:Demo:=!"
  • /V:ON allows for the !r! replace to be delayed in execution.
  • /C opens a command window, executes the specified command and closes the command window.
  • SET r=%1 saves the URI, which by the time the SET is run has already been replaced by the actually URI string
  • start starts your program or batch file in this case

The second solution now that we can manipulate strings would be to do another replace, which although I am sure there is a better way you could just chain the replaces together like so:

cmd /V:ON /C "SET r=%1 & SET s=!r:Demo:=! & start D:\demo.bat !s:_= !"

This last addition replaces underscores(_) with spaces ( ), and by making them spaces makes them become separate arguments used to call demo.bat. Therefore, to run:

D:\demo.bat -ping -ip 172.18.102.65

Your URI would be:

Demo:-ping_-ip_172.18.102.65

Note: I would recommend using a lowercase protocol but going with the OP's example for consistency

like image 198
Cutch Avatar answered Oct 18 '22 20:10

Cutch


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 tbe 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

like image 33
Remy Lebeau Avatar answered Oct 18 '22 20:10

Remy Lebeau