Solved

WebBrowserClass returning system.__comobject instead of string

Posted on 2004-08-01
7
1,880 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Handling arrays 14 33
Exception in Log4Net 1 17
Create XML 5 29
XML & .net 5 0
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now