view raw
Marcus Chr Marcus Chr - 11 months ago 43 Question

How do i make my program act on a specific protocol? (Example: 'GN://example')

I tried looking up a solution myself, but I still don't have any idea how to make my VB.NET program start up and run a sub, when a link with its protocol is used.
I'm looking for a code or help to write a code, that'll automaticly add a custom protocol.

Example: 'GN://example'


There is no part of the framework that will do it for you. You need to split the question.

First, where does Windows stores that information?

Here's the Microsoft documentation:

Second, how do I write those registry keys?

Here's the link to the Registry class in the .NET framework:

Here's an example on how to write those values to the registry:

' Creates the custom protocol and sets the description
Registry.ClassesRoot.CreateSubKey("GN", RegistryKeyPermissionCheck.ReadWriteSubTree).SetValue(String.Empty, "Custom Protocol Description")

Dim protocolSubKey = Registry.ClassesRoot.OpenSubKey("GN", True)

' Icon
' TODO : Update Executable Name
protocolSubKey.CreateSubKey("DefaultIcon").SetValue(String.Empty, "ExecutableName.exe,1")

' URL Protocol string value indicates that this key declares a custom pluggable protocol handler (stays empty)
protocolSubKey.SetValue("URL Protocol", "")

' Program to execute
Dim commandSubKey = protocolSubKey.CreateSubKey("shell").CreateSubKey("open").CreateSubKey("command")

' TODO : Update path to Executable File
commandSubKey.SetValue(string.Empty, """C:\Program Files\Alert\ExecutableName.exe\"" ""%1""")

Example on how to handle the request:

Private Shared Function ProcessInput(s As String) As String
    ' TODO Verify and validate the input 
    ' string as appropriate for your application.
    Return s
End Function

Private Shared Sub Main(args As String())

    Console.WriteLine("ExecutableName.exe invoked with the following parameters." & vbCr & vbLf)
    Console.WriteLine("Raw command-line: " & vbLf & vbTab + Environment.CommandLine)

    Console.WriteLine(vbLf & vbLf & "Arguments:" & vbLf)
    For Each s As String In args
        Console.WriteLine(Convert.ToString(vbTab) & ProcessInput(s))
    Console.WriteLine(vbLf & "Press any key to continue...")

End Sub

Will output:

ExecutableName.exe invoked with the following parameters.

Raw command-line:
    "C:\Program Files\Alert\ExecutableName.exe" "GN:"Hello World""



Press any key to continue...

Making it work seamlessly with browsers

I won't repeat what's already been said in that great answer: