Click here to Skip to main content
15,890,527 members
Articles / Web Development / HTML
Tip/Trick

Launching Desktop Application From Browser

Rate me:
Please Sign up or sign in to vote.
3.79/5 (8 votes)
14 Aug 2018CPOL4 min read 35.3K   11   4
How to open a desktop application from a browser

Introduction

I was requested to find a way to enable users to launch (specific) local applications from their browser. Since the server cannot directly specify a file be opened on the client, it must be handled implicitly by the client O/S, yet be triggered from a browser - which is heavily weighted against interaction with the client system for security.

Background

The company where I work has been moving almost all applications to web-based applications. There are, however, legacy application (typically C++, C#) that are used and require a desktop link to launch. The idea was to place the links within the browser to launch them. This, however, tip-toes around various security protocols built into the browser. There was, however, an indication that this was, under the proper circumstances, doable - as I can generate <XML> formatted Excel spreadsheets on the web server and the system opens them with Excel on the client.

Using the Code

In order to launch a local executable file from a browser link, one must induce the system to consider the local file to be a suitable and valid target for opening a file. Examples of this would be a .pdf file being opened by Adobe reader. The system is requested to open the file, as a link (or location.assign() from JavaScript) and one is typically asked to save or open the file.

Thus, we have our first hint into the procedure: the client O/S (and by proxy, the browser) need to be made aware of some sort of attempted file opening to require some specific local resource. We need to trigger an appropriate "open-with" query from the system and assign it to our targeted local application. Unfortunately, except for the typical built-ins, it's not quite that easy.

The second part is where the work came in - for the browser is fortunately not that cooperative and will not launch an executable file just because you assigned it to a particular file extension. The browser will just try to open the file in a browser window and display it.

Rehashing, there are two steps:

  1. Setting up your client system to associate a specific file extension to a specific application on the client. In the example, the file's name is TEXT.BOGUS.
  2. Getting the browser to react to a file (link) by opening the application and not rendering the file. This requires the link be interpreted - using a script-language extension will convince the server to do this for you and at the same time, set up the target extension which then opens the desired application.

It turns out, step one is, as expected, pretty easy. You create a file and email it to yourself as an attachment. This part is an actual tip. It seems to work properly when induced by the email-"open with" route but not with the Explorer "open with" route.

Step two took the work, and although the content is simple enough, finding it and getting it together took a fair amount of searching to determine what needs to be done and then how to do it. As it turn out, the file you target with your link is opened by the browser, examined, and then properly disposed of. It is necessary, then, to convince the browser that the incoming item is a data file to be opened by an application and not a text file to be opened in the browser. This is accomplished by controlling the "header" information with the following file content (for PHP):

PHP
<?php
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; '.'filename="TEST.BOGUS"');
// It is essential that no output occurs before these appear in the file
// or you'll receive the notorious "headers already sent" error.
?>

The first header() sets up the browser for a binary data stream, such as you'd feed to many applications. 

The second header() instructs the browser to handle the incoming data as an attachment (for external use) rather than inline (for browser use, such as displaying plain text). It also contains a file name whose extension informs the client O/S of the type of file coming and it then defaults to the assigned application to open it as the pre-selected option.

The above pre-selects opening a target file "TEST.BOGUS", which will appear in the Open / Save popup generated when you create the link. The above is the entire content of my target file and I named it header_test.php. I then used this file as the object of the various link methods. I do not link to TEST.BOGUS.

Points of Interest

The file to be opened in the browser is a script that creates header information that triggers the action. It contains a reference, internally, to a file type. This will trigger the initial offering in the open/save popup and thus target the application for your users.

The file type action is best assigned by emailing yourself the file with your custom extension as an attachment, opening that from the email and then assigning it to your executable as the default handler.

It is essential that there be no output (like ECHO) from your PHP prior to executing the header() directives.

Another way to launch these, aside from a link and location.assign() would be as the "action" target of an HTML <form>.

If your application requires an initialization file, note that at the time of execution, your current working directory is your temp directory - you can put your .ini file there but this can be a problem if the folder is purged by the O/S on a regular basis.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Bitmask Design, LLC
United States United States
In real life, a research chemist. Bitten by the bug for instrument automation, and then numeric and kinetic modeling, I turned to the dark side (or to the light), programming all sorts of lovely things. Alas, one needs to make a living, so off I went to POS, and now, the insurance industry.

Useful stuff: reducing concepts to bare abstractions and then coding as generic a solution as possible (implicitly extensible). In a sense, applications that "don't care" about much to do their job.

Doing now for money what I used to do simply for pleasure - not much different than a cheap hooker.

At best? An Optimistic Cynic. - No better; No worse - as though you cared, and as though your caring really mattered.


Pathological Genius

I have done things . . .

"Dispensing wisdom and chaos with uncanny poise and unflinching bravado."

Comments and Discussions

 
SuggestionYou could just use anchor tags Pin
20212a5-Jan-21 9:34
20212a5-Jan-21 9:34 
QuestionCan you show how one can run a C++/C# windows application. Pin
Archimedes2420-Aug-18 4:40
professionalArchimedes2420-Aug-18 4:40 
QuestionQuestion Pin
Marco Bertschi14-Aug-18 7:17
protectorMarco Bertschi14-Aug-18 7:17 
First of all, nice display Thumbs Up | :thumbsup:
May I ask why you went for the send-a-file-to-client route?
What has prevented you from registering a custom protocol?
might be nice to showcase these differences as well.
I only have a signature in order to let @DalekDave follow my posts.

AnswerRe: Question Pin
W Balboos, GHB15-Aug-18 0:40
W Balboos, GHB15-Aug-18 0:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.