繁体   English   中英

C# 使用 Process Not Working in Firefox Native Messaging 但在 Chrome 中工作启动 Excel

[英]C# Start Excel using Process Not Working in Firefox Native Messaging but works in Chrome

这是我在 c# 中的主机应用程序。

编辑:我已将示例主机应用程序代码更改为简单代码。

class Program
{
    public static void Main(string[] args)
    {
        System.Diagnostics.Process.Start(@"c:\sample.xls");
    }
}

当我的程序使用 Firefox 扩展程序运行时,excel 不会打开。 如果我使用 Chrome 扩展程序,excel 可以毫无问题地打开。

这是我的Firefox扩展设置: manifest.json

{
  "name": "ExcelPrint",
  "description" : "ExcelPrint",
  "version": "1.0",
  "permissions" :[
    "activeTab", "nativeMessaging", "tabs"
  ],
  "content_scripts": [
    {
      "matches": [
        "https://*/*",
        "http://*/*"
      ],
      "js": ["content.js"]
    }
  ],
  "background": {
      "scripts": [
          "background.js"
      ]
  },
  "manifest_version": 2
}

Firefox: sample.excelprint.json

{
  "name": "sample.excelprint",
  "description": "ExcelPrint API Host",
  "path": "ExcelPrint.bat",
  "type": "stdio",
  "allowed_extensions": [
    "3042a0d1d72c86e7952c552c6984bbea99bc5f0e@temporary-addon"
  ]
}

这是我的Chrome扩展设置: manifest.json 与 firefox 相同

铬: sample.excelprint.json

{
  "name": "sample.excelprint",
  "description": "ExcelPrint API Host",
  "path": "ExcelPrint.bat",
  "type": "stdio",
  "allowed_origins": [
    "chrome-extension://ncdpaccgfgdcifdjlncninlegabfgkgl/"
  ]
}

需要帮助如何使用 firefox 扩展打开 excel。

根据MDN 文档

当本地消息连接关闭时,Firefox 如果子进程没有断开,则将其杀死。 在 Windows 上,浏览器将本机应用程序的进程放入作业 object 并终止该作业。 假设本机应用程序启动其他进程并希望它们在本机应用程序被杀死后保持打开状态。 在这种情况下,本机应用程序必须使用 CreateProcess 而不是 ShellExecute 来启动带有 CREATE_BREAKAWAY_FROM_JOB 标志的附加进程。

基本上,在 Firefox(与 Chrome 和 Edge 不同)中,第二个进程在本机进程完成后被杀死。 因此,我们需要使用带有CREATE_BREAKAWAY_FROM_JOB标志的CreateProcess function。 CreateProcess 不能执行文件,只能执行应用程序,但我们可以利用 cmd.exe 应用程序打开 excel 文件。 感谢createprocess 中的 Process.arguments? ,我想出了以下解决方案:

using System;
using System.Runtime.InteropServices;

namespace FirefoxNativeMessagingLauncher
{
    public struct PROCESS_INFORMATION
    {
        public IntPtr hProcess;
        public IntPtr hThread;
        public uint dwProcessId;
        public uint dwThreadId;
    }

    public struct STARTUPINFO
    {
        public uint cb;
        public string lpReserved;
        public string lpDesktop;
        public string lpTitle;
        public uint dwX;
        public uint dwY;
        public uint dwXSize;
        public uint dwYSize;
        public uint dwXCountChars;
        public uint dwYCountChars;
        public uint dwFillAttribute;
        public uint dwFlags;
        public short wShowWindow;
        public short cbReserved2;
        public IntPtr lpReserved2;
        public IntPtr hStdInput;
        public IntPtr hStdOutput;
        public IntPtr hStdError;
    }

    class Program
    {
        [DllImport("Kernel32.dll")]
        private static extern bool CreateProcess(string lpApplicationName, string lpCommandLine, IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles, uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory, ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);

        static void Main(string[] args)
        {
            const uint CREATE_BREAKAWAY_FROM_JOB = 0x01000000;
            PROCESS_INFORMATION pi = new PROCESS_INFORMATION();
            STARTUPINFO si = new STARTUPINFO();
            CreateProcess(@"C:\Windows\System32\cmd.exe", @"/c c:\sample.xls", IntPtr.Zero, IntPtr.Zero, false, CREATE_BREAKAWAY_FROM_JOB, IntPtr.Zero, null, ref si, out pi);
        }
    }
}

暂无
暂无

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

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