: +91-265-2775555
 : +1-856-831-0505
 : +44-788-443-25-28
 : +45-4052-3137
Prakash software blogs - software development,custom sharepoint,MOSS 2007,Telerik development,C#.net,sharepoint webparts > Posts > Open native windows application from web page

Open native windows application from web page

Recently I needed to open windows calculator from web page. Different browser has 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 Calculater” calculator will be opened.

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

·         Go to Tools > Internet Options > Security Tab > Local Intranet.

·         Click Custom Level in Security level for this zone.

·         SetInitialize 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:\\windows\\system32\\calc.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.

Comments

There are no comments yet for this post.