0

I m looking for a way to improve the navigation between different tools (under windows). I would like to add in the body of an email a special "link" that when the user will click on it will launch my delphi app installed in his local computer. Is it possible and if yes how to do?

zeus
  • 12,173
  • 9
  • 63
  • 184

1 Answers1

4

Yes, it is possible. Your application must create a moniker (for example, from its setup or first launch). This will allow the mail client to open an URL pointing to your application, and Windows will launch it and pass the URL to it.

Here is some source code to register/unregister the URL protocol:

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;
    
function UnregisterURLProtocol(
  const ProtocolID : String) : Boolean;
var
  Reg           : TRegistry;
begin
  Result := FALSE;
  Reg := TRegistry.Create(KEY_WRITE);
  try
    Reg.RootKey := HKEY_CLASSES_ROOT;
    if not Reg.KeyExists(ProtocolID) then
      Exit;
    Reg.DeleteKey(ProtocolID + '\DefaultIcon');
    Reg.DeleteKey(ProtocolID + '\shell\open\command');
    Reg.DeleteKey(ProtocolID + '\shell\open');
    Reg.DeleteKey(ProtocolID + '\shell');
    Reg.DeleteKey(ProtocolID);
    Result := TRUE;
  finally
    FreeAndNil(Reg);
  end;
end;

ProtocolID is the identifier for your protocol. You can use something like 'MyApp'. If you had to register the HTTP protocol, you would use 'http'. The application will be opened by Windows when the user clicks on the link. The application will receive what is specified by the OpenCommand (A string you specify). You can include %1 in that OpenCommand and it will be replaced by the URL except the protocol.

ProtocolName is a string describing your protocol.

The URL to place in the mail would be like this:

MyApp://SomeParameters/SomeMoreParameters
fpiette
  • 11,983
  • 1
  • 24
  • 46