Solved

WebBrowserClass returning system.__comobject instead of string

Posted on 2004-08-01
7
1,936 Views
Last Modified: 2008-01-09
I have a WebBrowserClass object called Explorer contained in my C# object.

I use the following code to access the element with an id of "footerClientArea" of the currently loaded page:

      HTMLDocument htmlDocument = (HTMLDocument)Explorer.Document;
      IHTMLElement e;
      e = htmlDocument.getElementById("footerClientArea");
      if (e!=null)      
      {
            string s = "";
            s = e.getAttribute("style", 0).ToString().ToLower();
            System.Windows.Forms.MessageBox.Show(null,s,"title");
      }
      e = null;
      htmlDocument = null;

What I am expecting is a message box containing the value of the "style" attribute, however, all I get is "system.__comobject"

it is strange, because when I display a message box with e.innerHTML, it correctly displays the HTML inside the element.  I am assuming that I need to cast the return value of getAttribute to some object, but what object?  Intellisense states that the return value of  getAttribute is of type "object", which doesn't help.  I know from the documentation that it should be returning a string, but the compiler won't accept a the value into a string variable without a cast, and casting the return value to a string doesn't work either...

Any help would be greatly appreciated!

0
Comment
Question by:protemus
  • 2
7 Comments
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11689963
it would depend on what type of object it is ....

what do you get when you add a watch on the object ? it should be able to tell you what fields you need in debug.
0
 

Author Comment

by:protemus
ID: 11700452
um... I don't really know what adding a watch to it is going to do.....

Maybe I didn't clearly state my problem, here is a more in depth explanation...

My class contains a WebBrowserClass member like so:

protected WebBrowserClass Explorer;

It is attached in a straightforward way by creating RCW from Com interface like so:

sp.QueryService( ref guid,ref riid,out w );
Explorer = (WebBrowserClass) Marshal.CreateWrapperOfType(
                        w as IWebBrowser,
                        typeof(WebBrowserClass) );

Then when I am sure a web page is loaded into the explorer object, I try to look at the HTML by accessing the document member of the WebBrowserClass object like so:

HTMLDocument htmlDocument = (HTMLDocument)Explorer.Document;

All that works perfectly, then I search for a specifically named DIV within the HTML like so:

IHTMLElement e;
e = htmlDocument.getElementById("footerClientArea");

Still, all is well because if I display a message box with e.innerHTML, it displays the correct HTML that is inside the DIV.  However, when I try to get just the value of a specific attribute of that DIV like the style attribute as follows:

s = e.getAttribute("style", 0)

Instead of a string like the docs say it is supposed to return, I get a class that identifies itself as "system.__comobject" (it doesn't even compile without a cast)

I think maybe it has something to do with the wrapper not marshalling the type correctly, but I am at a loss.  Another theory I have is that the function is actually returning a variant type instead of a straight string.  If that is the case, does anyone know how to get passed this point?


0
 

Author Comment

by:protemus
ID: 11700736
With a little more research, I have confirmed that the type being returned by the function ia actually a variant.  There must be a way to extract the BSTR value from the variant that is returned as a System.__ComObject.  However, from scouring the net, I could only fin references for marshalling managed to unmanaged, not vice-versa.

Anyone know?  

I would REALLY REALLY appreciate any insight here.
0
 
LVL 3

Accepted Solution

by:
PocketMatt earned 500 total points
ID: 11713855
Hi protemus

to invoke functions on a __ComObject you can do the following:

object s;
s = e.getAttribute("style", 0).ToString().ToLower();
s = s.GetType().InvokeMember("",System.Reflection.BindingFlags.Default,null,s,null);
MessageBox.Show(s.ToString());

the Type.InvokeMember - Method is used to invoke a Member on an object of a class you do not really know. all you need to know is the name of the Member you need to invoke to get the string back. by passing "" you invoke the default member..
i'm not sure if this really works.. what i know is, that you need the InvokeMember - Method of the Type. maybe with another Membername...

hope this helps
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question