InternetExplorer被设置为__ComObject而不是SHDocVw.InternetExplorer

时间:2011-05-16 14:57:43

标签: c# getelementbyid shdocvw comobject

我在SHDocVw.dll中遇到了InternetExplorer的问题。我也引用了mshtml.tlb(当谷歌搜索时,我确实读过1条评论说我应该引用mshtml.dll,但是这不能在 Microsoft Visual Studio Express 中完成,我不知道知道这是多么真实)。这是一个最基本形式的小函数,对我来说不起作用:

public static HtmlElement GetDocumentControlByID(
    ref SHDocVw.InternetExplorer IEObj, 
    string ControlID)
{
    HtmlElement ReturnElement = IEObj.Document.GetElementById(ControlID);
    return ReturnElement;
}

问题在于,当我创建IEObj实例时,它被视为类型System.__ComObject而不是SHDocVw.InternetExplorer,并且所有子部分也是System.__ComObject类型。当我尝试以下任何陈述时......

Document WebDoc = IEObj.Document;
HtmlElement ReturnElement = IEObj.Document.GetElementById(ControlID);

...我一直收到同样的错误消息:

  

无法将类型'System .__ ComObject'隐式转换为'System.Windows.Forms.HtmlElement'   (显然,IEObj.Document)的转换类型不同。

我是c#的新手(来自VBA,所以我熟悉编程),但在VBA中,equivelant完美无需以任何方式转换它。

我做错了吗?如果它是我创建的对象,以下是(大致)我用来测试函数的代码:

public static void Main(String [] args)
{
    SHDocVw.InternetExplorer IEObj = new SHDocVw.InternetExplorer();
    IEObj.Navigate("http://sports.ladbrokes.com/");
    while (IEObj.ReadyState != 4)
    {

    }

    // There is a textbox that definitely exists

    HtmlElement NetControl = GetDocumentControlByID(ref IEObj, "username");

    // I was goint to manipulate it after this, but it crashes in the above function.

}

我真正想做的就是锁定各种元素,这样我就可以在文本框中输入文本,点击按钮等。我还需要能够使用Document变量(比如 Document.Body。 InnerHtml 等)。整个项目是要包含在DLL中的一堆函数,以供其他项目引用。

1 个答案:

答案 0 :(得分:0)

您正在尝试使用WinForms HtmlElement类,不是 COM对象。
您不能将本机InternetExplorer COM对象与WinForms中的托管类混合。

您应该使用WinForms类(WebBrowser控件) 在大多数情况下,您根本不需要COM。

相关问题