Buscar este blog

sábado, 26 de diciembre de 2015

Java - Web based protocol handler with Inno Setup

This is the second post about how to build and launch a desktop app from a web app, i.e, from a browser.

In the previous post (http://trabajosdesisifo.blogspot.com.es/2015/12/java-bundle-jre-inside-executable-file.html) I built and packed up a desktop java app. Now I want to be able to invoke this app, wich is running in the user PC, from the browser. In order to do that you need to create a custom protocol handler and register it in the user PC.

With a protocol invocation a browser can launch a user app. A very common example of this type of invocation is "mailto:". Just like when you click a link which points to "http:xxxx" the browser calls himself, when you click a link  which points to "mailto:mymail@gmail.com" the browser call the user app associated with the mailto protocol.

The magic with this type of invocations happens in the windows registry. There must be an entry in the registry that associates the protocol name with some local app. In this way, when the browser requested for this protocol, windows check its registry, reads the path to the local app, and calls it with the appropriate params.


I have installed Outlook as my default mail app, so the value of the key containing the app path is "C:\PROGRA~2\MICROS~1\Office12\OUTLOOK.EXE" -c IPM.Note /m "%1". 

If I type the following command in a DOS window I will call my Outlook to send a message to someDir@gmail.com:
"C:\PROGRA~2\MICROS~1\Office12\OUTLOOK.EXE" -c IPM.Note /m "someDir@gmail.com"

You can check in the Microsoft doc, https://msdn.microsoft.com/en-us/library/aa767914(v=vs.85).aspx, what this registry entries means.


So, back to my desktop app, what I´m going to do is to register custom protocol called "desktop01:". This will be done during the installation of the own application, so I need to modify the installer script to tweak the registry.

Step 4, modify Inno Setup script

Inno Setup is like a compiler, you start with a code file (.java or, in this case .iss) and in the end you get the compilation (.class or in this case a .exe). In the Step 3 of this serie of post, I created the script file by using the graphical wizard, but the result was a .iss file like this one.

Desktop.iss
; Script generated by the Inno Setup Script Wizard.
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!

#define MyAppName "DesktopApp"
#define MyAppVersion "1.0.0"
#define MyAppPublisher "Sisifo INC"
#define MyAppURL "http://trabajosdesisifo.blogspot.com.es/"
#define MyAppExeName "desktopApp.exe"

[Setup]
; NOTE: The value of AppId uniquely identifies this application.
; Do not use the same AppId value in installers for other applications.
; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
AppId={{732AE723-F81E-4F76-A792-F94CC358F9C6}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName={pf}\{#MyAppName}
DefaultGroupName={#MyAppName}
OutputBaseFilename=setup
Compression=lzma
SolidCompression=yes

[Languages]
Name: "english"; MessagesFile: "compiler:Default.isl"
Name: "spanish"; MessagesFile: "compiler:Languages\Spanish.isl"

[Tasks]
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked

[Files]
Source: "E:\desktopApp\desktopApp.exe"; DestDir: "{app}"; Flags: ignoreversion
Source: "E:\desktopApp\resources\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs
; NOTE: Don't use "Flags: ignoreversion" on any shared system files

[Icons]
Name: "{group}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"
Name: "{commondesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon

[Run]
Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent


As you can see, the script is divided in sections marked with the "[xxxx]". This is what really tells Inno Setup how to build the installer.

I want to register a custom protocol called "desktop01:", so according with MS doc, you need to create the following registry entries:

HKEY_CLASSES_ROOT
   desktop01
      (Default) = "URL:My custom protocol handler 01"
      URL Protocol = ""
      DefaultIcon
         (Default) = "desktopApp.exe,1"
      shell
         open
            command
               (Default) = "C:\Archivos de programa\DesktopApp\desktopApp.exe" "%1"

Some of the values are variables you can obtain from Inno Setup in runtime, so the final path will depend on the user election.

To translate this registry entries into the Inno Setup script, you have to added the following lines in this file.
[Registry]
Root: HKCR; Subkey: "desktop01"; Flags: uninsdeletekey
Root: HKCR; Subkey: "desktop01"; ValueType: string; ValueName: ""; ValueData: "URL:My custom protocol handler 01"; Flags: uninsdeletekey
Root: HKCR; Subkey: "desktop01"; ValueType: string; ValueName: "URL Protocol"; ValueData: ""; Flags: uninsdeletekey
Root: HKCR; Subkey: "desktop01\DefaultIcon"; Flags: uninsdeletekey
Root: HKCR; Subkey: "desktop01\DefaultIcon"; ValueType: string; ValueName: ""; ValueData: "{#MyAppExeName},1"; Flags: uninsdeletekey
Root: HKCR; Subkey: "desktop01\Shell\open\command"; Flags: uninsdeletekey
Root: HKCR; Subkey: "desktop01\Shell\open\command"; ValueType: string; ValueName: ""; ValueData: """{app}\{#MyAppExeName}"" ""%1"""; Flags: uninsdeletekey

The complete doc about how to manage registry with Inno Setup can be found in its offical doc, http://www.jrsoftware.org/ishelp/index.php?topic=registrysection.

Then you need to rebuild the installer.

When you reinstall the app, and check the registry, you will found the new entries.


To test the app, I create the following simple HTML:
<html>

<body> 
 <a href="desktop01:myCustomArgument">Launch App</a> 
</script>

</body>
</html>

When you click in the link, the browser will show a warning to confirm the launch of the desktop app.



This solution supports only one argument, but you can pass multiple parameters in a query string - like fashion, just like in a URL.

1 comentario:

  1. How to Connect to MS SQL Server using Inno Setup? Contact to Microsoft SQL Server Support
    If you find any type of complexity to connect to MS SQL Server using Inno setup then frequently you can solve this particular problem with Remote Infrastructure Management Support for Microsoft SQL Server or Online MS SQL Server Support. Our technical experts provide unmatched and tailored support which effectively ensures your MS SQL Server environment. We always deliver cutting-edge technology and high performance which meets your business needs.
    For More Info: https://cognegicsystems.com/
    Contact Number: 1-800-450-8670
    Email Address- info@cognegicsystems.com
    Company’s Address- 507 Copper Square Drive Bethel Connecticut (USA) 06801

    ResponderEliminar