0

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.

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
user755729
  • 3
  • 1
  • 2

1 Answers1

0

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.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Thanks for your answer SLaks. When you say WebBrowser, do you mean SHDocVw.WebBrowser or System.Windows.Forms.WebBrowser? The SHDocVw one gives me an error if I try to .Navigate, and I thought the System.Windows.Forms one requires the control to be added to a form. Sorry if my understanding is completely off. If it does require a userform, then it wouldn't be that useful as the codes I'm creating must require no UserInput or userforms at all. Thanks again, Mark – user755729 May 16 '11 at 15:57
  • No; I mean System.Windows.Forms.WebBrowser. It does not need to be added to a form. – SLaks May 16 '11 at 16:03
  • And this would still have all the functionality that SHDocVw provides? Last question, I heard that it is resource-intensive. I will probably have 20 / 25 apps running at the same time on 1 computer all relying on this library. Will that WebBrowser be able to handle that ok? The PC will not be manned, so I don't have to worry about a user adding extra processes to the mix and taking up resources. If the WebBrowser isn't suitable, is there an alternative that will allow manipulating Textboxes etc. online and getting web source codes? Thanks, Mark – user755729 May 16 '11 at 16:17
  • @user: `WebBrowser` is a thin wrapper around the native control. They're both resource-intensive. Consider using the HTML Agility Pack instead, which will be orders of magnitude faster and much easier to work with. – SLaks May 16 '11 at 16:21
  • That's perfect. Thanks for all your help SLaks. – user755729 May 16 '11 at 16:25