Search code examples
delphiurlprotocols

Monitor a Custom Url Protocol Messages - Delphi


Have someone an example showing how I handle custom URL protocol from a webpage in my desktop-app like zoom (You got an link and this open the meeting in the desktop-app)?

I'll be happy if some one has nice example or something he can share for the purpose.


Solution

  • This is a matter of registry keys. Look at the Microsoft documentation.

    The code below can create it for you:

    function RegisterURLProtocol(
        const ProtocolID   : String;
        const ProtocolName : String;
        const DefaultIcon  : String;
        const OpenCommand  : String) : Boolean;
    var
        Reg : TRegistry;
    begin
        Result := FALSE;
        Reg    := TRegistry.Create(KEY_WRITE);
        try
            Reg.RootKey := HKEY_CLASSES_ROOT;
            if not Reg.OpenKey(ProtocolID, TRUE) then
                Exit;
    
            Reg.WriteString('', 'URL:' + ProtocolName);
            Reg.WriteString('URL Protocol', '');
    
            if Reg.OpenKey('DefaultIcon', True) then begin
                Reg.WriteString('', DefaultIcon);
            end;
            Reg.CloseKey;
    
            if not Reg.OpenKey(ProtocolID + '\shell\open\command', True) then
                Exit;
    
            Reg.WriteString('', OpenCommand);
            Result := TRUE;
        finally
            FreeAndNil(Reg);
        end;
    end;