tags:

views:

106

answers:

1

Hi , I am trying to run a .exe file from Javascript .This is what I have-

var oShell = new ActiveXObject("Shell.Application");
var commandtoRun = "C:\Documents and Settings\User\Desktop\ABCD.exe"; oShell.ShellExecute(commandtoRun,"","","open","1");

If i have only the first 2 lines code it seems to work fine (it asked me do i want activeX when I opened it first time in IE) but if I add the last line (ShellExecute) there seems to be an error.I want to pass arguments to the exe .Does anyone know how to do it ?
Thanks,

+2  A: 

You need to escape the backslashes, e.g.,

var commandtoRun = "C:\\Documents and Settings\\User\Desktop\\ABCD.exe";

Update:

This works fine on my machine:

var oShell = new ActiveXObject("Shell.Application");
var commandtoRun = "C:\\Windows\\notepad.exe"; 
oShell.ShellExecute(commandtoRun,"","","open","1");

Update 2

You can save this as a file with the extension .hta and it should work in your browser:

<script>
var oShell = new ActiveXObject("Shell.Application");
var commandtoRun = "C:\\Windows\\notepad.exe"; 
oShell.ShellExecute(commandtoRun,"","","open","1");
</script>
RedFilter
Also: @Jason's link suggests using %20 for escaping the spaces and using the format: file:///C:/Program%20Files/...etc - perhaps it's some variation on that?
Ciaran Archer
@Red Filter :-I already tried that before posting ...doesnt help though
Manish
See my uppdate.
RedFilter
Still doesnt work for me :( ....Although it shouldnt matter- I call this .exe from a GWT project using native Javascript module ...is there a way to know the error? (If I remove the last line web page opens up fine ...if I keep the line though it runs fine until that line and then shows a blank page in the browser)
Manish
Let's be clear, I am running this from the command-line, executing a file name test.js using cscript.exe. Do not expect to be able to launch applications from a web page with this Javascript embedded. That is against the security restrictions. And I gues technically I am using JScript, not Javascript.
RedFilter
Is there a work around for this ? Everything is done locally (as a prototype on my machine ...I am not publically hosting it)....I just want the .exe to run when a particular event happens in the UI ....
Manish
I mean is there a way of changing the security settings to allow this execution since I vouch for the .exe that I have created ....
Manish
Is this how you want the .hta to be run from Javascript? var oShell = new ActiveXObject("Shell.Application"); var commandtoRun = "C:\\Documents and Settings\\User\\Desktop\\ABCD.hta"; oShell.ShellExecute(commandtoRun,"","","open","1"); ABCD.hta contains exact same code [I copied it in a txtfile and saved it as .hta] that you wrote above .I tried running it like this in IE and it doesnt work.
Manish