Implementing XMLHTTPRequest onReadyStateChange in C++

Environment: Developed in (but not restricted to) VC++ .NET

I recently needed to support the asynchronous version of the send() operation of the XMLHTTPRequest object that's included in Microsoft's XML 4.0 library. To achieve this, I chose to leverage the "onreadystatechange" property to signal when the send() operation was completed and data was ready to be received.

IXMLHTTPRequest provides the "onreadystatechange" property to allow the caller to monitor and react to the state changes of an asynchronous send() invocation. The documentation for this property states that "onreadystatechange" is "not readily accessible" in C++. However, it turns out to be rather simple to implement using ATL templates.

Architecture

To receive onreadystatechange events, I created a class called CXMLHTTPEvent that implements the IDispEventSimpleImpl template. IDispEventSimpleImpl provides the minimum framework to capture events returned by XMLHTTPRequest.

class CXMLHTTPEvent : public IDispEventSimpleImpl</*nID =*/ 1,
      CXMLHTTPEvent, &__uuidof(MSXML::XMLDOMDocumentEvents)>

I then added a SINK_ENTRY_INFO to the SINK_MAP to capture the XMLHTTPRequest events.

BEGIN_SINK_MAP(CXMLHTTPEvent)
  SINK_ENTRY_INFO(/*nID =*/ 1, __uuidof(MSXML::
                                        XMLDOMDocumentEvents),
                          /*dispid =*/ 0, OnReadyStateChange,
                          &OnEventInfo)
END_SINK_MAP()

When an event is captured, I then notify the main process of the state change via a Windows message or function callback.

// State change call back handler
void __stdcall CXMLHTTPEvent::OnReadyStateChange ( )
{
  ATLTRACE(L"CXMLHTTPEvent: ReadyStateChange = %i \n",
           m_spRequest->readyState);

  if (m_pCallBack)
    m_pCallBack->OnReadyStateChange(m_spRequest->GetreadyState());

  if (m_hwndPostWindow)
    ::PostMessage(m_hwndPostWindow, WM_XMLHTTP_READYSTATE_CHANGE,
                  0, MAKELPARAM(m_spRequest->GetreadyState(), 0));
}

Using CXMLHTTPEvent

After creating the XMLHTTPRequest object, create an instance of the CXMLHTTPEvent prior to invoking XMLHTTPRequest operations. This will connect XMLHTTPRequest with how you'd like your application to be notified about the state changes.

void CXMLHTTPCallBackDlg::OnBnClickedButton1()
{
  // Create the XMLHTTPRequest
  m_spXMLHTTPRequest.CreateInstance(L"Msxml2.XMLHTTP.4.0");

  ...

  // Use WM_MESSAGE pump
  m_pXMLHTTPEvent = new CXMLHTTPEvent(m_spXMLHTTPRequest,
                                      GetSafeHwnd());

  ...

  // Use Function Pointer CallBack
  m_pXMLHTTPEvent = new CXMLHTTPEvent(m_spXMLHTTPRequest,
                                      NULL, this);

  ..

  // Open the async connection
    m_spXMLHTTPRequest->open("GET", m_sURL.AllocSysString(),
                              VARIANT_TRUE);

  // Send the async request
  m_spXMLHTTPRequest->send();
}

If you are using the message pump to get notifications, add a message handler and map entry for the WM_XMLHTTP_READYSTATE_CHANGE message. This message will be sent by CXMLHTTPEvent when XMLHTTPRequest signals a ready state change.

BEGIN_MESSAGE_MAP(CXMLHTTPCallBackDlg, CDialog)
  ...
  ON_MESSAGE(WM_XMLHTTP_READYSTATE_CHANGE,
             OnReadyStateChange2)
  ...
END_MESSAGE_MAP()

When WM_XMLHTTP_READYSTATE_CHANGE is sent, the lParam will contain the ReadyState value.

LRESULT CXMLHTTPCallBackDlg::OnReadyStateChange2(WPARAM wParam,
                                                 LPARAM lParam)
{
  UINT nState = (UINT)lParam;
  ...
  return 0;
}

If you're using the callback method, make sure the target class for events inherits from the CXMLHTTPEventCallBack class and implements the OnReadyStateChange(long lReadyState) method.

  class CXMLHTTPCallBackDlg : public CDialog, CXMLHTTPEventCallBack

  ...

  void CXMLHTTPCallBackDlg::OnReadyStateChange(long lReadyState)
  {
    ...
  }

Downloads

Download demo project - 23 Kb
Download source - 27 Kb


Comments

  • A good knowledge sharing, thank you!

    Posted by Legacy on 08/10/2003 12:00am

    Originally posted by: TW

    TQ!

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Live Event Date: October 29, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this eSeminar, Gene Kim will discuss these survey findings and will share woeful tales of artifact management gone wrong! Gene will also share examples of how …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds