我无法从javascript执行ShellExecute来运行带参数的Windows窗体

本文关键字:行带 运行 参数 窗体 Windows ShellExecute javascript 执行 | 更新日期: 2023-09-27 17:58:10

是否真的可以使用activex控件运行安装在用户机器上的带有参数的应用程序。我可以成功运行以下形式的应用程序:

$('#ejecutarActivo').click(function () {
                var Comando = $('#Comando').val();
                if (Comando.trim() != "") {
                    try {
                        var objShell = new window.ActiveXObject("shell.application");
                        objShell.ShellExecute('C:''Users''Jerry''Documents''visual studio 2012''Projects''PruebaComandos''PruebaComandos''bin''Debug''PruebaComandos.exe', '', '', 'open',1);
                    }
                    catch (e) {
                        alert('Active opciones de seguridad y utilice el navegador Internet Explorer');
                    }
                }
            });

然而,我似乎无法用参数运行它。我真的需要用这种方式运行它,我可以用命令行中的参数运行它,但我几乎尝试了每一种单引号和双引号的组合,也只是简单地在调用中放入变量名(例如"Comando"),但每次我在参数部分中放入一些东西时,(根据Microsoft文档MSDN,它是ShellExecute的第二个参数)应用程序在我的计算机中启动,但屏幕上什么都不显示(我的意思是,我看到应用程序在任务管理器中运行,但出现了问题,什么也不显示)。我还需要检查应用程序,看看windows窗体是否出了问题。但我想知道我调用activex的方式出了什么问题。

有人能帮忙吗?

我无法从javascript执行ShellExecute来运行带参数的Windows窗体

我找到了答案,ShellExecute javascript方法没有问题。事实上,哎呀!,这是我的Windows窗体的一个问题,我想在询问之前,我会更好地了解串行端口。我留下了这样的javascript:

$('#ejecutarActivo').click(function () {
                var Comando = $('#Comando').val();
                if (Comando.trim() != "") {
                    try {
                        var objShell = new window.ActiveXObject("shell.application");
                        if (Comando.indexOf('"') != -1) {
                            Comando = Comando.replace(/"+/g, '"""');
                        }
                        objShell.ShellExecute('C:''Users''Jerry''Documents''visual studio 2012''Projects''PruebaComandos''PruebaComandos''bin''Debug''PruebaComandos.exe', Comando , '', 'open', 1);
                    }
                    catch (e) {
                        alert('Active opciones de seguridad y utilice el navegador Internet Explorer');
                    }
                }
            });

Windows窗体的代码是这样的:(这是一个与端口在另一次调用之前没有关闭有关的问题,导致应用程序崩溃。它非常简单)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
namespace PruebaComandos
{
    public partial class Form1 : Form
    {
        SerialPort Puerto = new SerialPort();
        string buffer = String.Empty;
        Thread DemoThread;
        byte[] Existen;
        public Form1(string argumento)
        {
            InitializeComponent();
            Puerto.PortName = "Com4";
            Puerto.BaudRate = 9600;
            this.textoComandoLinea.Text = argumento;
            Puerto.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
            CloseSerial();
            OpenSerial();
            Puerto.Write(argumento + "'r'n");
           }
        public Form1()
        {
            InitializeComponent();
            Puerto.PortName = "Com4";
            Puerto.BaudRate = 9600;
            Puerto.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
            CloseSerial();
            OpenSerial();
            Puerto.Write("Inicio");
        }
        private void botonSend_Click(object sender, EventArgs e)
        {
            string Input = textoComandoLinea.Text;
            OpenSerial();
            Puerto.Write(Input);
        }
        void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            switch (e.EventType)
            {
                case SerialData.Chars:
                    buffer += sp.ReadExisting();
                    this.DemoThread = new Thread(new ThreadStart(ThreadProcSafe));
                    this.DemoThread.Start();
                    break;
                case SerialData.Eof:
                    CloseSerial();
                    break;
            }

        }

        void OpenSerial()
        {
            if (!Puerto.IsOpen)
            {
                Puerto.Open();
            }
        }
        void CloseSerial()
        {
            if (Puerto.IsOpen)
            {
                Puerto.Close();
            }
        }
        private void ThreadProcSafe()
        {
            this.SetText(buffer);
            //CloseSerial();
        }
        delegate void SetTextCallback(string text);
        private void SetText(string text)
        {
            if (this.textoRecibido.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.BeginInvoke(d, new object[] { text });
            }
            else
            {
                if (text.EndsWith("'r'n"))
                {
                    this.textoRecibido.Text = text;
                    if (MessageBox.Show(this, text, "", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, (MessageBoxOptions)0x40000) == DialogResult.Yes)
                    {
                        Application.Exit();
                    }
                }
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }
    }
}

不管怎样,这个问题很愚蠢,一天内就结束了,我只发布我的代码,以防它对某人有帮助。再见