【问题标题】:Open the process is the process ID is known打开进程是进程ID是已知的
【发布时间】:2014-08-18 11:31:16
【问题描述】:

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

提前致谢。

【问题讨论】:

  • @AgentFire 有什么东西还是你还在搜索?

标签: c# internet-explorer process


【解决方案1】:

这对我有用。

[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 的引用

【讨论】:

  • 谢谢,我试试看。
  • iExplorer.Navigate("google.de", 0x800);抛出参数数量不正确的错误。
【解决方案2】:

要达到预期的效果,您可以尝试这样的方法。有一些不必要的选角,写得不是很好,但你应该明白它的要点。

[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 进程中的特定选项卡,那么代码会更长且更hackish。

【讨论】:

  • 当然我想打开一个新标签。并且不想影响其他选项卡。是否有可能以某种方式将焦点设置到该 IE 进程,然后将 URL 传递给该实例?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多