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

时间:2014-08-18 11:31:17

标签: c# internet-explorer process

如何使用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 ");
        }

提前致谢。

2 个答案:

答案 0 :(得分:3)

这适合我。

[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

的引用

答案 1 :(得分:1)

为了达到预期的效果,你可以尝试这样的方法。有一些不必要的演员正在进行,而且写得不是很好,但你应该得到它的要点。

[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流程中的特定选项卡,则代码会更长,更具黑色。