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

发布于 2024-11-07 21:06:10 字数 1595 浏览 6 评论 0原文

我在使用 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 中,等效项可以完美工作,无需以任何方式进行转换。

难道是我做错了什么?如果这是我创建的对象,以下是(大致)我用来测试该功能的代码:

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.Body.InnerHtml等)。整个项目是一堆包含在 DLL 中的函数,可供其他项目引用。

I'm having a problem with the InternetExplorer in SHDocVw.dll. I also have mshtml.tlb referenced (while googling, I did read 1 comment that said I should have mshtml.dll referenced, but that this couldn't be done in Microsoft Visual Studio Express, I don't know how true this is though). Here's one small function in its most basic form that won't work for me:

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

The problem is that when I create the IEObj instance, it is considered type System.__ComObject instead of SHDocVw.InternetExplorer, and all subparts are also of type System.__ComObject. When I try any of the following statements...

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

...I keep getting the same error message:

Cannot implicitly convert type 'System.__ComObject' to 'System.Windows.Forms.HtmlElement'
(obviously the convert-to type is different for the IEObj.Document).

I am new to c# (coming from VBA, so I am familiar with programming), but in VBA, the equivelant works perfectly without needing to convert it in any way.

Is it something I'm doing wrong? In case it's my creation of the object, the following is (roughly) the code I used to test the function:

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.

}

All I really want to do is latch onto various elements so that I can enter text into textboxes, click buttons etc. I would also need to be able to use the Document variables as well (like Document.Body.InnerHtml, etc). The whole project is to be a bunch of functions to be contained in a DLL to be referenced by other projects.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇妻 2024-11-14 21:06:10

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

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

You're trying to use the WinForms HtmlElement class, which isn't a COM object.
You can't mix the native InternetExplorer COM object with the managed classes in WinForms.

You should use the WinForms classes (the WebBrowser control) instead.
In most cases, you don't need COM at all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文