打开的进程是进程ID已知的

本文关键字:进程 ID | 更新日期: 2023-09-27 18:18:33

我将如何使用IE进程的进程ID从我的应用程序打开链接。

我有两个IE实例打开,我有其中一个的进程ID。

编辑:我写的代码如下:

bool isActive = false;
        if (File.Exists("ProcessID.txt"))
        {
            processID = Convert.ToInt32(File.ReadAllText("ProcessID.txt"));
            Process[] activeProcess = Process.GetProcesses();
            foreach (Process proc in activeProcess)
            {
                if (proc.Id == processID)
                {
                    isActive = true;
                    break;
                }
            }
        }
        //existingProcess = Process.GetProcessById(processID);
        if (!string.IsNullOrEmpty(textBox1.Text))
        {
            if (isActive)
            {
                //Process oldProc = Process.GetCurrentProcess();
                Process oldProc = Process.GetProcessById(processID);
                ProcessStartInfo psi = new ProcessStartInfo(textBox1.Text);

                //string processName = oldProc.ProcessName;
                //string mainWindowTitle = oldProc.MainWindowTitle;                    
                //SetFocus(new HandleRef(null, oldProc.Handle));
                //psi.UseShellExecute = false;
                oldProc.StartInfo = psi;
                oldProc.Start();
                int prhandle = Process.GetCurrentProcess().Id;
                label1.Text = prhandle.ToString();
                //File.WriteAllText("ProcessID.txt", prhandle.ToString()); 
            }
            else 
            {
                ProcessStartInfo pi = new ProcessStartInfo(@"C:'Program Files'Internet Explorer'iexplore.exe", textBox1.Text);
                newprocess.StartInfo = pi;
                newprocess.Start();
                int prhandle = newprocess.Id;
                label1.Text = prhandle.ToString();
                File.WriteAllText("ProcessID.txt", prhandle.ToString());                    
            }
        }
        else
        {
            MessageBox.Show("Enter a url ");
        }

打开的进程是进程ID已知的

这对我很有用。

[DllImport("user32.dll", SetLastError = true)]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId);
private void Form1_Load(object sender, EventArgs e)
{
    var iExplorerInstances = new ShellWindows();
    if (iExplorerInstances.Count > 0)
    {
        foreach (var instance in iExplorerInstances)
        {
            var iExplorer = (InternetExplorer)instance;
            uint processId = 0;
            GetWindowThreadProcessId((IntPtr)iExplorer.HWND, out processId);
            if (processId == 1212) // your ID
            {
                iExplorer.Navigate("http://google.de", 0x800); //0x800 means new tab
            }  
        }
    }
    else
    {
        //No iexplore running, use your processinfo method
    }
}

你必须添加一个引用到C:'Windows'System32'SHDocVw.dll

要获得期望的结果,您可以尝试这样做。这里有一些不必要的角色转换,而且写得不太好,但你应该了解它的要点。

[DllImport("user32.dll", SetLastError = true)]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId);
public void Navigate2URL(int processId, string strUrl)
{
    SHDocVw.ShellWindows SWs = new SHDocVw.ShellWindows();
    SHDocVw.InternetExplorer IE = null;
    for (int i = 0; i < SWs.Count; i++)
    {
        IE = (SHDocVw.InternetExplorer)SWs.Item(i);
        uint pid;
        GetWindowThreadProcessId((IntPtr)IE.HWND, out pid);
        if ((IntPtr)IE.HWND == (IntPtr)pid)
        {
            object o = null;
            IE.Navigate2(strUrl, ref o, ref o, ref o, ref o);
        }
    }
}

然后像这样使用

Navigate2URL(12108, "mydomain.com");

请注意,这将更改此PID中所有选项卡的URL,如果您只想在IE进程中针对特定选项卡,那么代码将更长,更黑客化。