How do I register a custom URL protocol in Windows?

WindowsUrlProtocolsCustom Url-Protocol

Windows Problem Overview


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?

Windows Solutions


Solution 1 - Windows

  1. Go to Start then in Find type regedit -> it should open Registry editor

  2. Click Right Mouse on HKEY_CLASSES_ROOT then New -> Key

enter image description here

  1. 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 on testus -> then New -> String Value and add URL Protocol without value.

enter image description here

  1. Then add more entries like you did with protocol ( Right Mouse New -> Key ) and create hierarchy like testus -> shell -> open -> command and inside command change (Default) to the path where .exe you 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"

enter image description here

  1. To test if it works go to Internet Explorer (not Chrome or Firefox) and enter testus:have_you_seen_this_man this should fire your .exe (give you some prompts that you want to do this - say Yes) and pass into args testus://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.

Solution 2 - Windows

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.

Solution 3 - Windows

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlukeckView Question on Stackoverflow
Solution 1 - WindowsMatas VaitkeviciusView Answer on Stackoverflow
Solution 2 - WindowsMSaltersView Answer on Stackoverflow
Solution 3 - WindowsShubham KumarView Answer on Stackoverflow