积极答复者
Process.MainWindowHandle抛出的奇怪异常

问题
-
InternetExplorer ie = null;
Process pro = Process.Start("iexplore.exe", url);System.Threading.Thread.Sleep(10000);
if (pro == null)
{
MessageBox.Show("Start IE failed, please check the IE setting on your machine!");
return null;
}
MessageBox.Show("Debug");
MessageBox.Show(pro.Handle.ToString());
// MessageBox.Show(pro.MainWindowHandle.ToString());SHDocVw.ShellWindows allBrowsers = new ShellWindows();
MessageBox.Show(allBrowsers.Count.ToString());if (allBrowsers.Count == 0)
{
throw new Exception("Could not found the IE");
}for (int index = 0; index < allBrowsers.Count && ie == null; index++)
{
MessageBox.Show("Debug 2");InternetExplorer temp = (InternetExplorer)allBrowsers.Item(index);
if(temp.HWND.ToString() == pro.MainWindowHandle.ToString())
{
ie = temp;
}
}我在网上找了些答案,但是都没有解决掉,有另外一帖子说,可以直接用
”你引用 activex dll(com) system32/SHDocVw.dll
SHDocVw.InternetExplorerClass ie = new SHDocVw.InternetExplorerClass();
ie.Visible = true;
但是我的程序上显示它没有构造函数。
请帮帮我。谢谢。
答案
-
您好
假如您要使用 SHDocVw,請添加參考 Microsoft HTML Object Library 與 Microsoft Internet Controls,參考以下文章
[C#]透過 SHDocVw 將 Internet Explorer 轉址
http://www.dotblogs.com.tw/chou/archive/2010/05/31/15515.aspx
[C#]透過 SHDocVw 與 GetForegroundWindow 取得正在使用的 Internet Explorer 網址
http://www.dotblogs.com.tw/chou/archive/2010/01/11/12953.aspx
歡迎參觀我的Blog.NET菜鳥自救會- 已标记为答案 Leo Liu - MSFTModerator 2011年5月6日 3:13
-
Thanks for you guys, I have found how to fix the isses seems that this issues is only repro on Win7. Now I can do it like this:
SHDocVw.ShellWindows allBrowsers = new ShellWindows();
foreach (InternetExplorer browser in allBrowsers)
{
string processName = Path.GetFileNameWithoutExtension(browser.FullName).ToLower();if (processName.Equals("iexplore"))
{
if (browser.HWND == (int)pro.MainWindowHandle)
{
ie = browser;
}
}
}In this case, it works well for alll brower here should have a hwnd for we are ensure that's an IE window.
Thanks for you guys help. I would like to close this thread at this moment.
Edison
- 已标记为答案 Leo Liu - MSFTModerator 2011年5月6日 3:13
全部回复
-
IE进程不一定有主窗口的……根据目标url的保护模式设置和目前IE窗口的IL而定。
The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
Visual C++ MVP -
您好
假如您要使用 SHDocVw,請添加參考 Microsoft HTML Object Library 與 Microsoft Internet Controls,參考以下文章
[C#]透過 SHDocVw 將 Internet Explorer 轉址
http://www.dotblogs.com.tw/chou/archive/2010/05/31/15515.aspx
[C#]透過 SHDocVw 與 GetForegroundWindow 取得正在使用的 Internet Explorer 網址
http://www.dotblogs.com.tw/chou/archive/2010/01/11/12953.aspx
歡迎參觀我的Blog.NET菜鳥自救會- 已标记为答案 Leo Liu - MSFTModerator 2011年5月6日 3:13
-
Thanks for you guys, I have found how to fix the isses seems that this issues is only repro on Win7. Now I can do it like this:
SHDocVw.ShellWindows allBrowsers = new ShellWindows();
foreach (InternetExplorer browser in allBrowsers)
{
string processName = Path.GetFileNameWithoutExtension(browser.FullName).ToLower();if (processName.Equals("iexplore"))
{
if (browser.HWND == (int)pro.MainWindowHandle)
{
ie = browser;
}
}
}In this case, it works well for alll brower here should have a hwnd for we are ensure that's an IE window.
Thanks for you guys help. I would like to close this thread at this moment.
Edison
- 已标记为答案 Leo Liu - MSFTModerator 2011年5月6日 3:13