[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2141
  • Last Modified:

WebBrowserClass returning system.__comobject instead of string

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
protemus
Asked:
protemus
  • 2
1 Solution
 
gregoryyoungCommented:
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
 
protemusAuthor Commented:
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
 
protemusAuthor Commented:
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
 
PocketMattCommented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now