代码之家  ›  专栏  ›  技术社区  ›  pushkin

我能要一个电子窗的主窗把手吗?

  •  1
  • pushkin  · 技术社区  · 6 年前

    我有一个电子应用程序生成一个C应用程序。C app想要获取电子浏览器窗口的 主窗口句柄 ,但它总是返回 intptr.zero ,而我不知道为什么。

    docs say:。

    必须使用 refresh method刷新 process object以获取当前主窗口句柄(如果更改了)。

    如果关联的进程没有主窗口,则 MainWindowHandle value为零。对于已隐藏的进程,即在任务栏中不可见的进程,该值也为零。

    我的C应用程序运行 refresh just in case,and my electron window is expectly visible,and I see the icon in the taskbar:。

    我的电子代码启动我的C应用程序并将其发送给渲染器进程的PID(您可以下载电子快速启动并进行以下更改以进行复制):

    const mainwindow=new browserwindow(width:800,height:600,show:false);
    mainwindow.once(“准备好显示”,()=>。{
    主窗口.show();
    (});
    
    mainwindow.once(“显示”,()=>。{
    //现在,我们的窗口应该已经启动了,我们应该有一个PID
    const windowpid=mainwindow.webContents.getosProcessID();
    
    const proc=cp.spawn(“my/exefile.exe”);
    
    //将PID发送到C进程
    const buff=缓冲器。allocunsafe(4);
    buff.writeintle(windowpid,0,4);
    proc.stdin.write(buff);
    (});
    
    
    

    C进程启动并将线程与一个无限循环连接起来,该循环读取PID并尝试获取其主窗口句柄:

    byte[]buffer=new byte[4];
    流内读取(缓冲区,0,4);
    int pid=bitconverter.toint32(buffer,0);//我已经验证了我发送的pid是我得到的pid
    
    process proc=process.getProcessByID(PID);
    proc.refresh();//以防万一
    
    intptr windowhandler=proc.mainwindowhandle;/0x00000000
    intptr handle=proc.handle;//0x000004B8
    
    
    
    1. 我是否发送了正确的电子PID?我不知道还能用哪种PID。主进程pid似乎不正确,所以我只剩下渲染器pid,这是我正在使用的。

    2. 当窗口是电子/铬窗口时,我应该期望设置MainWindowHandle

    3. 我不知道为什么。

      这个docs说:

      你必须使用Refresh方法刷新Process对象获取当前主窗口句柄(如果已更改)。

      如果关联的进程没有主窗口,则主窗口句柄值为零。对于已隐藏的进程(即在任务栏中不可见的进程),该值也为零。

      我的C应用程序运行刷新以防万一,我的电子窗口是绝对可见的,我看到了任务栏中的图标:

      enter image description here

      我的电子代码启动了我的C应用程序,并将其发送给渲染器进程的PID(您可以下载electron-quick-start应用并进行以下更改以复制):

      const mainWindow = new BrowserWindow({width: 800, height: 600, show: false});
      mainWindow.once("ready-to-show", () => {
          mainWindow.show();
      });
      
      mainWindow.once("show", () => {
          // by now, our window should have launched, and we should have a pid for it
          const windowPid = mainWindow.webContents.getOSProcessId();
      
          const proc = cp.spawn("my/exeFile.exe");
      
          // send the pid to the C# process
          const buff = Buffer.allocUnsafe(4);
          buff.writeIntLE(windowPid, 0, 4);
          proc.stdin.write(buff);
      });
      

      C进程启动并将线程与一个无限循环连接起来,该循环读取PID并尝试获取其主窗口句柄:

      byte[] buffer = new byte[4];
      inStream.Read(buffer, 0, 4);
      int pid = BitConverter.ToInt32(buffer, 0); // I've verified that the pid I'm sending is the pid I'm getting
      
      Process proc = Process.GetProcessById(pid);
      proc.Refresh(); // just in case
      
      IntPtr windowHandler = proc.MainWindowHandle; // 0x00000000
      IntPtr handle = proc.Handle; // 0x000004b8
      
      1. 我是否发送了正确的电子PID?我不知道还能用哪种PID。主进程PID看起来不正确,所以我只剩下渲染器PID,这就是我使用的。

      2. 我应该期待吗主窗口句柄当窗口是电子/铬窗口时设置,还是仅适用于C窗口?

    1 回复  |  直到 5 年前
        1
  •  3
  •   pergy Javier    6 年前

    有一个 BrowserWindow 此应用程序接口:

    win.getNativeWindowHandle()

    返回可以在任何本机Windows代码中使用的Hwnd

    在您的情况下,我想您可以这样使用它:

    byte[] bytes = new byte[8];
    for (int i = 0; i < data.Length; i++) {
      object item = data[i];
      bytes[i] = (byte)(int)item;
    }
    return BitConverter.ToUInt64(bytes, 0);