Delphi Programming Observations

Tuesday, March 23, 2010

Custom Protocol Handler in Delphi

I have wanted to look into creating a custom protocol handler to plug into a TWebBrowser with Delphi for a while, called an Asynchronous Pluggable Protocol. I have seen a few implementations, but they were all quite old, and none did exactly what I wanted, which is to be able to create a temporary APP, which is local to the application and self contained.

From this StackOverflow question, I found a basic implementation of a registered APP, at http://www.doogal.co.uk/plugprot.php. Starting from this, I figured out how to create a temporary asynchronous pluggable protocol.

The way I implemented it allows you to specify a callback procedure of object which gets called to handle any URLs for your custom protocol. My implementation allows you to decide the content you return and the MIME type, and also handles POSTs, which I haven’t seen in other Delphi implementations.

See the AsyncPlugProtoDemo project in twebbrowser.utilities/AsynchronousPluggableProtocol/. Note that the code should work with Delphi 2009 and later. I was able to get it working in Delphi 2007, by changing some PChar to PWideChar, and String to WideString where Delphi showed errors, and use UTF8Decode() instead of UTF8ToString().

posted by Jason at 7:11 pm  

3 Responses to “Custom Protocol Handler in Delphi”

  1. de77 says:

    It doesn’t work. It compiles perfectly under 2005 after I did the changes you described for 2007.
    It shows no errors, but I can’t navigate to the protocol page. It goes to

    • Jason says:

      Well I never said it worked under 2005. Delphi 2005 was a terrible, buggy version, I would suggest upgrading to 2007. That said, I don’t know what changes are required to get it to work with < Delphi 2007.

  2. Tom Conlon says:

    Thanks for this post Jason.