0

I would love to be able to set my executable to handle custom Url Scheme (aaa:) so that I can handle deep linking with my program.

Volker
  • 40,468
  • 7
  • 81
  • 87
Vjerci
  • 528
  • 2
  • 6
  • 16

1 Answers1

0

You need to modify Windows registry, in order to tell os which protocol to open with which executable.

There are multiple ways you can go about it, depending if you want to add your url scheme handler for all users or for current user.

Easiest way to handle it is to make Windows pass custom url as argument to default executable. That way both chrome and edge will ask user to open your installed app.

Here is a minimal Golang implementation to set your executable as default url scheme handler:

    var k registry.Key
    var err error
    prefix := "SOFTWARE\\Classes\\"
    urlScheme := "aaa"
    basePath := prefix + urlScheme
    permission := uint32(registry.QUERY_VALUE | registry.SET_VALUE)
    baseKey := registry.CURRENT_USER

    programLocation := "\"C:\\Windows\\notepad.exe\""

    // create key
    registry.CreateKey(baseKey, basePath, permission)

    // set description
    k.SetStringValue("", "Notepad app")
    k.SetStringValue("URL Protocol", "")

    // set icon
    registry.CreateKey(registry.CURRENT_USER, "lumiere\\DefaultIcon", registry.ALL_ACCESS)
    k.SetStringValue("", softwareToOpen+",1")

    // create tree
    registry.CreateKey(baseKey, basePath+"\\shell", permission)
    registry.CreateKey(baseKey, basePath+"\\shell\\open", permission)
    registry.CreateKey(baseKey, basePath+"\\shell\\open\\command", permission)

    // set open command
    k.SetStringValue("", softwareToOpen+" \"%1\"")

Getting custom URL after having your app set as default is quite straightforward

You can do this:

func main() {
    url := os.Args[1:]
    log.Printf("my custom aaa url is: %s", url)
}
Vjerci
  • 528
  • 2
  • 6
  • 16