banderson777

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 "toggleActiveState" method in the C++ BHO from the C# code.

When I try to call the GetMethod function call in C# to retrieve the method's reference, it returns NULL.

Below are snippets of my code...

Here's the C++ code...


// This seems to work fine... it saves a reference to the BHO in an IE Property for later retrieval.
HRESULT MyClass::ReferenceMe()
{
BSTR bstrThisKey = SysAllocString(L"MyClass_IDisp");
VARIANT vThis;
HRESULT hr = S_OK;

if (!m_spWebBrowser)
hr = S_FALSE;
else
{
// Save this to a variant that will be referenced in IE Session
VariantInit(&vThis);
vThis.vt = VT_DISPATCH;
vThis.pdispVal = static_cast<IDispatch*>(this);

// Add our this pointer to IE session by adding a named property
if (FAILED( m_spWebBrowser->PutProperty(bstrThisKey, vThis) ))
{
hr = S_FALSE;
}
}

VariantClear(&vThis);
SysFreeString(bstrThisKey);
return hr;

}

// This is the method that I want to call from the C# code
// It's declared in the Public portion of the class in the .h file.
void STDMETHODCALLTYPE MyClass::toggleActiveState()
{
m_bActive = !m_bActive;
}



And here's my C# code...


private void toggleBHO()
{
Object pvarPBHO = null;
// Get the property from IE that was put there by the C++ code's call to "PutProperty"
// This does seem to return a COM object, which I'm assuming is my BHO reference.
pvarPBHO = Explorer.GetProperty("MyClass_IDisp");
if (pvarPBHO != null)
{
MessageBox.Show("About to call GetType.");
// Get the object's type. This seems to work fine.
Type typExternal = pvarPBHO.GetType();
if (typExternal != null)
{
// Now try to retrieve a reference ot the "toggleActiveState" method within the C++ code
MethodInfo mthInv = null;
MessageBox.Show("About to call GetMethod.");

// AT THIS POINT THE GetMethod FUNCTION RETURNS NULL...

// I'm not sure if I'm not calling this right, or if my prototype isn't quite right
// to allow public access to the method, or what.
mthInv = typExternal.GetMethod("toggleActiveState");
if (mthInv != null)
mthInv.Invoke(pvarPBHO, null);
else
MessageBox.Show("GetMethod returned NULL.");
}
else
MessageBox.Show("GetType returned NULL.");
}
else
MessageBox.Show("GetProperty returned NULL.");
}


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!


Re: Internet Explorer Extension Development C# Toolbar not talking to C++ BHO

IECUSTOMIZER

Hi B,

You have to invoke your toggleBHO method from within the browsers PropertyChange event.

Here's some pseudo code (I'm a vb'er)

Toolbar uses PutProperty method of the browser to indicate the status of the BHO.. eg.. PutProperty("BHOState","On")

This will cause the PropertyChange event of the browser to fire.... Your BHO will have a hook into the browsers' events. Here you should check for changes to the "BHOState" property..

eg.

Event PropertyChange(szProperty as String)

if szProperty = "BHOState" then

szState = Me.GetProperty(szProperty)

if szState = "On" then

toggleBHOOn

else

toggleBHOOff

end if

end if

Hope this helps..

Regards.






Re: Internet Explorer Extension Development C# Toolbar not talking to C++ BHO

banderson777

Thanks!! I'll give this a try!

B.