BHO Handle OnSubmit Event

Basically I want to develop a BHO that validates certain fields in a form and automatically places disposable emails in the appropriate fields (more for my own knowledge). Therefore, in the DOCUMENTCOMPLETE event, I have the following:

for(long i = 0; i < *len; i++)
{
    VARIANT* name = new VARIANT();
    name->vt = VT_I4;
    name->intVal = i;
    VARIANT* id = new VARIANT();
    id->vt = VT_I4;
    id->intVal = 0;
    IDispatch* disp = 0;
    IHTMLFormElement* form = 0;
    HRESULT r = forms->item(*name,*id,&disp);
    if(S_OK != r)
    {
        MessageBox(0,L"Failed to get form dispatch",L"",0);// debug only
        continue;
    }
    disp->QueryInterface(IID_IHTMLFormElement2,(void**)&form);
    if(form == 0)
    {
        MessageBox(0,L"Failed to get form element from dispatch",L"",0);// debug only
        continue;
    }

    // Code to listen for onsubmit events here...         
}

      

How can I use the IHTMLFormElement interface to listen for the onsubmit event?

+2


source to share


1 answer


Once you have a pointer to the element for which you want to enable events, you would have QueryInterface()

it for IConnectionPointContainer

, and then connect to that:

REFIID riid = DIID_HTMLFormElementEvents2;
CComPtr<IConnectionPointContainer> spcpc;
HRESULT hr = form->QueryInterface(IID_IConnectionPointContainer, (void**)&spcpc);
if (SUCCEEDED(hr))
{
    CComPtr<IConnectionPoint> spcp;
    hr = spcpc->FindConnectionPoint(riid, &spcp);
    if (SUCCEEDED(hr))
    {
        DWORD dwCookie;
        hr = pcp->Advise((IDispatch *)this, &dwCookie);
    }
}

      

Some notes:

  • You probably want to cache dwCookie

    and cpc

    , as you will need them later when you call pcp->Unadvise()

    to disable the receiver.
  • When called pcp->Advise()

    above, I pass this. You can use any object you have that implements IDispatch

    , which may or may not be that object. The design is left to you.
  • riid

    will be a disabled event. In this case, you probably want to DIID_HTMLFormElementEvents2

    .

Here's how to disable:

pcp->Unadvise(dwCookie);

      



Let me know if you have further questions.

Edit-1:

Yes, this DIID was wrong. It should be: DIID_HTMLFormElementEvents2

.

This is how I found it:

C:\Program Files (x86)\Microsoft Visual Studio 8\VC\PlatformSDK>findstr /spin /c:"Events2" *.h | findstr /i /c:"form"

      

+1


source







All Articles