banderson777 0 Newbie Poster

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...

// 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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.