?
Solved

WebBrowserClass returning system.__comobject instead of string

Posted on 2004-08-01
7
Medium Priority
?
2,052 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

770 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