How do I register a custom protocol with Windows so that when clicking a link in an email or on a web page my application is opened and the parameters from the URL are passed to it?
-
Possible duplicate of [how do I create my own URL protocol? (e.g. so://...)](http://stackoverflow.com/questions/389204/how-do-i-create-my-own-url-protocol-e-g-so) – phuclv Apr 30 '17 at 08:07
3 Answers
Go to
Startthen inFindtyperegedit-> it should open Registry editorClick Right Mouse on
HKEY_CLASSES_ROOTthenNew->Key
- In the Key give the lowercase name by which you want urls to be called (in my case it will be
testus://sdfsdfsdf) then Click Right Mouse ontestus-> thenNew->String Valueand addURL Protocolwithout value.
- Then add more entries like you did with protocol ( Right Mouse
New->Key) and create hierarchy liketestus->shell->open->commandand insidecommandchange(Default)to the path where.exeyou want to launch is, if you want to pass parameters to your exe then wrap path to exe in""and add"%1"to look like:"c:\testing\test.exe" "%1"
- To test if it works go to Internet Explorer (not Chrome or Firefox) and enter
testus:have_you_seen_this_manthis should fire your.exe(give you some prompts that you want to do this - say Yes) and pass into argstestus://have_you_seen_this_man.
Here's sample console app to test:
using System;
namespace Testing
{
class Program
{
static void Main(string[] args)
{
if (args!= null && args.Length > 0)
Console.WriteLine(args[0]);
Console.ReadKey();
}
}
}
Hope this saves you some time.
- 7,638
- 19
- 69
- 90
- 54,146
- 29
- 227
- 241
-
3
-
-
@MatasVaitkevicius can I specify "working directory for the application to run from" in registry(within custom URL protocol entry). Ex. for triggering a Batch file if I create a custom Url registry entry, the batch file runs from system32 when launched from browser irrespective of the location of the batch file, whereas if I run the batch file by double clicking it, then the working directory remains its current directory. – user1066231 Jul 30 '21 at 05:45
-
@user1066231 https://superuser.com/questions/396394/how-do-i-set-an-executables-working-directory-via-the-command-line-prior-to-ex ? – Matas Vaitkevicius Aug 04 '21 at 10:17
-
1@MatasVaitkevicius Thanks. yeah found the answer. posting here-may be usefull for someone else. https://stackoverflow.com/questions/68577785/how-to-make-url-protocol-to-launch-application-from-its-own-directory-instead?noredirect=1#comment121226027_68577785 – user1066231 Aug 04 '21 at 12:34
-
I've followed these steps, and using custom protocol "testus:foo" gets recognized from Run dialog in windows 7, but not from any of the browsers I tried, nor from the cmd shell. Any idea on what I could have done wrong or missed? – Joe Sep 20 '21 at 00:18
-
-
@WiiLF that would be a nice answer, you are welcome to use the stuff above to write it. I promise a +1 if you do. – Matas Vaitkevicius Dec 05 '21 at 08:40
The MSDN link is nice, but the security information there isn't complete. The handler registration should contain "%1", not %1. This is a security measure, because some URL sources incorrectly decode %20 before invoking your custom protocol handler.
PS. You'll get the entire URL, not just the URL parameters. But the URL might be subject to some mistreatment, besides the already mentioned %20->space conversion. It helps to be conservative in your URL syntax design. Don't throw in random // or you'll get into the mess that file:// is.
- 167,472
- 9
- 150
- 334
-
-
6There's no formal mapping of file: URLs to local paths. There's not even a consensus on the use of two or three leading slashes, or the use of forward versus backward slashes when the path refers to a Windows directory. – MSalters May 01 '09 at 12:51
-
Late comment, I know. But is it also possible to somehow access the URL parameters *only*, without the protocol handler? – Danilo Bargen May 12 '10 at 11:10
-
2That sounds like a separate question. Please do get your terms straight, though. The protocol handler is the program that receives the URL. "Without the protocol handler" there's nobody to parse the URL and access the URL parameters. – MSalters May 14 '10 at 08:01
-
Wrapping the path or the parameter in quotes makes no difference to the registry validity, some installers do it either way – WiiLF Dec 19 '21 at 16:56
-
1@WiiLF: Correct, the registry treats it as just a `REG_SZ`. And internally, Windows doesn't parse command lines so spaces are preserved as well. But any language runtime that parses a command line (such as C's `argv[]`parsing) will break on an unquoted space. – MSalters Dec 20 '21 at 09:10
There is an npm module for this purpose.
link :https://www.npmjs.com/package/protocol-registry
So to do this in nodejs you just need to run the code below:
First Install it
npm i protocol-registry
Then use the code below to register you entry file.
const path = require('path');
const ProtocolRegistry = require('protocol-registry');
console.log('Registering...');
// Registers the Protocol
ProtocolRegistry.register({
protocol: 'testproto', // sets protocol for your command , testproto://**
command: `node ${path.join(__dirname, './index.js')} $_URL_`, // $_URL_ will the replaces by the url used to initiate it
override: true, // Use this with caution as it will destroy all previous Registrations on this protocol
terminal: true, // Use this to run your command inside a terminal
script: false
}).then(async () => {
console.log('Successfully registered');
});
Then suppose someone opens testproto://test then a new terminal will be launched executing :
node yourapp/index.js testproto://test
It also supports all other operating system.
- 396
- 3
- 12