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 ?
-
1A little searching found this - dotnetspider.com/resources/19547-Run-exe-file-Java-Script.aspxJasCav– JasCav2010年06月30日 19:04:10 +00:00Commented Jun 30, 2010 at 19:04
-
4Are you making a virus or what ? :)Christophe Roussy– Christophe Roussy2016年07月18日 09:32:33 +00:00Commented Jul 18, 2016 at 9:32
1 Answer 1
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:
<HTA:APPLICATION ID="oMyApp"
APPLICATIONNAME="Application Executer"
BORDER="no"
CAPTION="no"
SHOWINTASKBAR="yes"
SINGLEINSTANCE="yes"
SYSMENU="yes"
SCROLL="no"
WINDOWSTATE="normal">
<script type="text/javascript" language="javascript">
var oShell = new ActiveXObject("Shell.Application");
var commandtoRun = "C:\\Windows\\notepad.exe";
oShell.ShellExecute(commandtoRun,"","","open","1");
</script>
answered Jun 30, 2010 at 19:05
D'Arcy Rittich
172k41 gold badges298 silver badges287 bronze badges
Sign up to request clarification or add additional context in comments.
9 Comments
Ciaran Archer
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?
Manish
@Red Filter :-I already tried that before posting ...doesnt help though
Manish
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)
D'Arcy Rittich
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.
Manish
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 ....
|
lang-js