0

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.

fpiette
  • 11,983
  • 1
  • 24
  • 46
  • 1
    Does this answer your question? [How do I create my own URL protocol? (e.g. so://...)](https://stackoverflow.com/questions/389204/how-do-i-create-my-own-url-protocol-e-g-so); also this is unbound to Delphi. – AmigoJack Jul 12 '21 at 12:28

1 Answers1

1

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;
fpiette
  • 11,983
  • 1
  • 24
  • 46
  • 2
    Shouldn't `Result` be initialized to false instead of true? – Wouter van Nifterick Jul 12 '21 at 14:35
  • Also two more `Reg.CloseKey()` would be fine to clean up each `Reg.OpenKey()`. – AmigoJack Jul 12 '21 at 14:49
  • @WoutervanNifterick A matter of taste. In the context of the application I extracted this code, the intent is to continue even if the function fails (Lack of permission for example). – fpiette Jul 12 '21 at 15:09
  • @AmigoJack No need to cleanup since the Reg object is freed and CloseKey is called by destructor. – fpiette Jul 12 '21 at 15:14
  • 2
    As a stand-alone general-purpose library function, it is not a matter of taste. As it is now, the function always returns `True`, so that's what you'd write in its documentation. Not very useful, though. If the caller wants to continue on failure, just don't stop on the function's returning `False`! – Andreas Rejbrand Jul 12 '21 at 16:21
  • 1
    Edited to return FALSE in case of error. – fpiette Jul 12 '21 at 16:30