Search code examples
asp.netactivexshellexecute

ActiveXObject("Shell.Application") - how to pass arguments with spaces?


I run exe from my asp.net with JavaScript using ActiveXObject. It runs successfully, except parameters:

function CallEXE() {
  var oShell = new ActiveXObject("Shell.Application");
  var prog = "C:\\Users\\admin\\Desktop\\myCustom.exe";                 
  oShell.ShellExecute(prog,"customer name fullname","","open","1");
}

Example, I pass that like parameters,[1] customer name,[2] fullname, but after space character, Javascript perceive different parameter.

How can I fix?


Solution

  • ShellExecute takes the 2nd parameter to be a string that represents all the arguments and processes these using normal shell processing rules: spaces and quotes, in particular.

    oShell.ShellExecute(prog,"customer name fullname",...)
    

    In this case the 3 parameters that are passed are customer, name, fullname

    oShell.ShellExecute(prog,"customer 'a name with spaces' fullname",...)

    As corrected/noted by Remy Lebeau - TeamB, double-quotes can be used to defined argument boundaries:

    oShell.ShellExecute(prog,'customer "a name with spaces" fullname',...)
    

    In this case the 3 parameters that are passed are customer, a name with spaces, fullname

    That is, think of how you would call myCustom.exe from the command-prompt. It's the same thing when using ShellExecute.

    Happy coding.