简体   繁体   English

通过ActiveX控件在JavaScript中启动应用程序时转义路径

[英]Escaping the path when launching an application in JavaScript via an ActiveX control

Objective 目的

I want to send a file path to a .NET COM component via JavaScript without having to escape it in JavaScript. 我想通过JavaScript发送文件路径到.NET COM组件,而不必在JavaScript中转义它。


Environment 环境

  • Windows 7 x64 Windows 7 x64
  • .NET 4.0 NET 4.0

What do we know? 我们知道什么?

  • When you pass a path like C:\\temp you get the following... 当您通过类似C:\\temp的路径时,将得到以下信息...

在此处输入图片说明

  • When you pass a UNC path like \\\\Server\\Directory you get the following... 当传递诸如\\\\Server\\Directory类的UNC路径时,您将得到以下信息...

在此处输入图片说明

  • When you escape the path (eg C:\\\\temp or \\\\\\\\Server\\\\Directory ) the value that is received is correct. 当您转义路径(例如C:\\\\temp\\\\\\\\Server\\\\Directory )时,接收到的值是正确的。

What have I tried? 我尝试了什么?

  • I've tried using the @ in front of the variable name when storing to a private field. 存储到私有字段时,我尝试在变量名称前使用@
  • I've tried to find somebody doing the same thing with my Google Fu, but haven't. 我试图找到有人用我的Google Fu做同样的事情,但是没有。

Code

COM Component COM组件

[Guid("30307EE0-82D9-4917-B07C-D3AB185FEF13")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[ComVisible(true)]
public interface ILauncher
{
    [DispId(1)]
    void setDirectory(string location);

    [DispId(2)]
    void launch(string args, bool debug = false);
}

[Guid("F91A7E9F-2397-4DEC-BDAD-EBFC65CFCCB2")]
[ProgId("MyActiveXControl.MyControl")]
[ClassInterface(ClassInterfaceType.None)]
[ComDefaultInterface(typeof(ILauncher))]
[ComVisible(true)]
public class Launcher : ILauncher
{
    private string _location;

    public void setDirectory(string location)
    {
        _location = location;
    }

    public void launch(string args, bool debug = false)
    {
        var programFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86);
        if (string.IsNullOrEmpty(programFiles))
        {
            programFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
        }

        var exe = string.Format(@"{0}\MyApp\MyApp.exe", programFiles);
        if (debug)
        {
            MessageBox.Show(exe, "Target Path", MessageBoxButtons.OK, MessageBoxIcon.Information);
            MessageBox.Show(_location, "Drop Location", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        var startInfo = new ProcessStartInfo(exe, string.Format("\"{0}\" \"{1}\"", args, _location));
        startInfo.WorkingDirectory = Path.GetDirectoryName(exe);
        startInfo.CreateNoWindow = false;

        try
        {
            var p = Process.Start(startInfo);
            if (p == null)
            {
                MessageBox.Show("The app could not be started, please try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}

JavaScript 的JavaScript

<html>
    <body>
        <input type="button" value="Launch Control" onclick="launchControl();" />

        <script>
            function launchControl() {
                o = new ActiveXObject("MyActiveXControl.MyControl");
                o.setDirectory("C:\\temp");
                o.launch("test_args", true);
            }
        </script>
    </body>
</html>

In the end we found that we just needed to change the JavaScript because it can't be done later. 最后,我们发现我们只需要更改JavaScript,因为以后将无法完成。 By the time it gets to us we'd have to end up adding backslashes, but we wouldn't really be able to because we wouldn't know where one directory began and another ended. 等到到达我们的目录时,我们将不得不添加反斜杠,但实际上并不能,因为我们不知道一个目录从哪里开始而另一个目录从哪里结束。

Just modify the JavaScript. 只需修改JavaScript。

So we worked with the powers that be and all is well. 因此,我们与存在的力量合作,一切都很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM