Open native windows application from web page

Recently I needed to open windows calculator from web page. Different browsers have different approach to allow this. Below I have mentioned how to implement this for Internet Explorer 6.0/7.0/8.0 and Mozilla Firefox 3.0/3.5.

Internet Explorer:
In IE you need to create the ActiveX control to open the application. You need to write the following HTML code to do this.

<html>
<head>
</head>
<body>
<input type="button" id ="buttonOpenCalculator"  value="Open Calculator" />
<script type="text/javascript">
function OpenCalculator()
{
var commandShell = new ActiveXObject("WScript.Shell");
commandShell.Run("calc");
commandShell.Quit;
}
document.getElementById("buttonOpenCalculator").attachEvent('onclick', OpenCalculator);
</script>
</body>
</html>

Here when you click the button “Open Calculator”, calculator will be opened.

Note: In order to open calculator you need to change the browser settings as below.

  • Go to Tools > Internet Options > Security Tab > Local Intranet.
  • Click Custom Level in Security level for this zone.
  • Set “Initialize and script ActiveX controls not marked as safe” to prompt in ActiveX Controls and Plug-Ins zone.

Mozilla Firefox:
To open the application in web page from Mozilla you need to create an extension (.xpi).
In extension you need to write the following code in JS file.

//This is the function that will create a process for an application
eventListener = function(event) {
// get system32 path for user;
var userSystemFilePath = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("SysD", Components.interfaces.nsIFile);
var stringApplicationPath = userSystemFilePath.path + "calc.exe";// "c:windowssystem32calc.exe";
event.target.setAttribute("IsAddOnInstalled", "true");
var stringParameter = "";
try
{
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
var objectFile = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
objectFile.initWithPath(stringApplicationPath);
if (objectFile.exists())
{
// create an nsIProcess
var objectProcess = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
objectProcess.init(objectFile);
// Run the process.
// If first param is true, calling process will be blocked until
// called process terminates.
// Second and third params are used to pass command-line arguments
// to the process.
var arguments = null;
var argumentLenth = 0;
if (stringParameter)
{
arguments = [stringParameter];
argumentLenth = 1;
}
objectProcess.run(false, arguments, argumentLenth);
}
}
catch (error)
{
alert(error );
}
}
document.addEventListener("sendEvent", eventListener, false, true);

In the web page you need to write the following code

<html>
<head>
</head>
<body>
<input type="button" id ="buttonOpenCalculator"  value="Open Calculator" />
<script type="text/javascript">
var element = document.createElement("ExtensionDataElement");
element.setAttribute("IsAddOnInstalled", 'false');
document.documentElement.appendChild(element);

function OpenCalculator()
{
var event = document.createEvent("Events");
event.initEvent("sendEvent", true, false);
element.dispatchEvent(event);
if (element.getAttribute("IsAddOnInstalled") == 'false')
{
window.open("download url of xpi","calculator","","");
}
}
document.getElementById("buttonOpenCalculator").addEventListener('click', OpenCalculator, false);
</script>
</body>
</html>

If you run the code and click the button for first time you will be prompted to install xpi file. Firefox needs to be restarted after installing the extension. Clicking the button now will open the calculator.

Prakash Software Solutions PVT LTD. Offer wide range of services to their clients all over the globe. We successfully delivered projects i.e. software development, cloud computing solutions, SharePoint development solutions, mobile app development and Microsoft technology solution to our Sweden, Denmark, Netherlands, France, Norway, Germany, Spain, Italy, Switzerland, Belgium, Luxembourg, Ireland, United Kingdom, United States, Canada, Brazil, Peru, UAE, Dubai, Saudi Arabia, Kuwait, Qatar, Bahrain, Singapore, New Zealand and Australia Clients.

 

Leave a reply