473,396 Members | 2,037 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

C#, IE and C++ : getting C# Explorer Bar and C++ BHO to talk

Hello,
I'm a bit new at in-depth IE programming, and am having a bit of trouble with a couple of IE browser extensions (getting them to talk to each other).

The first one is a C++ Browser Helper Object (BHO).

I have a class (MyClass) that has a method called "ReferenceMe" that calls the browser's "PutProperty" method to save a reference to the BHO instance in IE.

Theoretically other COM object should be able to call IE's "GetProperty" method to retrieve this reference and use it to invoke methods in the BHO.

I also have another method in the BHO called "toggleActiveState" that just sets a boolean flag. That's the method I want to invoke from outside of the BHO.

Then I have a C# Explorer Toolbar object. I'm trying to call the BHO's "toggleActiveState" method from the C# code.

The problem I'm having is that I can't figure out how to invoke that method.

Below are snippets of my code...

Here's the C++ code...

Expand|Select|Wrap|Line Numbers
  1. // This seems to work fine... it saves a reference to the BHO in an IE Property for later retrieval.
  2. HRESULT MyClass::ReferenceMe()
  3. {
  4.     BSTR bstrThisKey = SysAllocString(L"MyClass_IDisp");
  5.     VARIANT vThis;
  6.     HRESULT hr = S_OK;
  7.  
  8.     if (!m_spWebBrowser)
  9.         hr = S_FALSE;
  10.     else
  11.     {
  12.         // Save this to a variant that will be referenced in IE Session
  13.         VariantInit(&vThis);
  14.         vThis.vt = VT_DISPATCH;
  15.         vThis.pdispVal = static_cast<IDispatch*>(this);
  16.  
  17.         // Add our this pointer to IE session by adding a named property
  18.         if (FAILED( m_spWebBrowser->PutProperty(bstrThisKey, vThis) ))
  19.         {
  20.             hr = S_FALSE;
  21.         }
  22.     }
  23.  
  24.     VariantClear(&vThis);
  25.     SysFreeString(bstrThisKey);
  26.     return hr;
  27.  
  28. }
  29.  
  30. // This is the method that I want to call from the C# code
  31. // It's declared in the Public portion of the class in the .h file.
  32. void STDMETHODCALLTYPE MyClass::toggleActiveState()
  33. {
  34.     m_bActive = !m_bActive;
  35. }
  36.  
  37.  
And here's my C# code...

Expand|Select|Wrap|Line Numbers
  1. // 
  2. private void toggleBHO()
  3. {
  4.     Object pvarPBHO = null;
  5.     // Get the property from IE that was put there by the C++ code's call to "PutProperty"
  6.     // This does seem to return a COM object, which I'm assuming is my BHO reference.
  7.     pvarPBHO = Explorer.GetProperty("MyClass_IDisp");
  8.     if (pvarPBHO != null)
  9.     {            
  10.         MessageBox.Show("About to call GetType.");
  11.         // Get the object's type.  This seems to work fine.
  12.         Type typExternal = pvarPBHO.GetType();
  13.         if (typExternal != null)
  14.         {                    
  15.             // Now try to retrieve a reference ot the "toggleActiveState" method within the C++ code
  16.             MethodInfo mthInv = null;
  17.             MessageBox.Show("About to call GetMethod.");
  18.  
  19.             // AT THIS POINT THE GetMethod FUNCTION RETURNS NULL...
  20.  
  21.             // I'm not sure if I'm not calling this right, or if my prototype isn't quite right
  22.             // to allow public access to the method, or what.
  23.             mthInv = typExternal.GetMethod("toggleActiveState");
  24.             if (mthInv != null)
  25.                 mthInv.Invoke(pvarPBHO, null);
  26.             else
  27.                 MessageBox.Show("GetMethod returned NULL.");
  28.         }
  29.         else
  30.             MessageBox.Show("GetType returned NULL.");
  31.     }
  32.     else
  33.         MessageBox.Show("GetProperty returned NULL.");
  34. }
  35.  
Perhaps my whole approach to this is off. Is there a better way to get these two COM objects to talk to each other?

Thanks very much!
Jun 26 '07 #1
0 1466

Sign in to post your reply or Sign up for a free account.

Similar topics

303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
0
by: TOM | last post by:
Hello, I am having problems with the server explorer making a connection to the SQL server. I can see the instances that i want to connect to but when i try this error comes up PROVIDER CANNOT BE...
3
by: Jason | last post by:
I have a web page that contains textareas. These are dynamically added via asp when the page loads based on database records. The user also has the ability to add more text areas via innerhtml. ...
9
by: Brian Henry | last post by:
How do you go about getting the "type" of a file from explorer? Say if I want to know what the type for the extension .DOC was, which is "Micrsoft Word Document" in explorer, how would I do this? I...
2
by: Alex | last post by:
Hi all, I'm writing a small web application which searches a database based on a date field, and populates a datagrid control with the results. The datagrid control has selection buttons added...
3
by: news.microsoft.com | last post by:
Hello, I'm writing an explorer kind of application. How can I extract icons for a specific type of OS? for example the icon for 'My Computer' looks different on Windows 2000 then on Windows...
2
by: Jim S | last post by:
Is there a way in .Net or via interop (com etc.) for an external application (a .net winform for example) to read the html contents (or the DOM) of a running instance or internet explorer? I...
2
by: slaterino | last post by:
Hello, I'm hoping someone can help me with a couple of queries. I am in the process of designing a site which I have uploaded to: http://www.cca-ltd.co.uk/New/index.html, but I currently have 2...
0
by: Shafiq | last post by:
Hi All, Based on the sample kbbar.net applicaation i have created a tool bar for windows explorer. Now i have to enable and disable few buttons based the user slects file in the explorer. For to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.