In C ++, XMLHttpRequest asynchronously uses onreadystatuschange

Source: Internet
Author: User

First, you need to write a class to implement the idispatch interface, and then upload the class. The Code is as follows:
# Ifndef xmlhttpeventsink_h _
# Define xmlhttpeventsink_h _

# Include "MSXML. H"
# Include <winnt. h>
# Include <wtypes. h>
# Include <windows. h>

Class xmlhttpeventsink: Public idispatch
{
Public:
Xmlhttpeventsink (ixmlhttprequest * request, handle completedevent): _ refcount (1), _ Request (request), _ completedevent (completedevent)
{
// Don't increase the reference count to the request object; doing so wocould create a circular reference
// And thus a memory leak.
}
Virtual ~ Xmlhttpeventsink (){}

// Iunknown
Stdmethodimp QueryInterface (refiid riid, void ** ppvobject );
Stdmethodimp _ (ulong) addref ();
Stdmethodimp _ (ulong) release ();

// Idispatch
Stdmethodimp gettypeinfocount (uint * pctinfo );
Stdmethodimp gettypeinfo (uint itinfo, lcid, itypeinfo ** pptinfo );
Stdmethodimp getidsofnames (refiid riid, lpolestr * rgsznames, uint cnames, lcid, dispid * rgdispid );
Stdmethodimp invoke (dispid dispidmember, refiid riid, lcid, word wflags, dispparams * pdispparams, variant * pvarresult, interval info * p1_info, uint * puargerr );
PRIVATE:
Long _ refcount;
Ixmlhttprequest * _ request;
Handle _ completedevent;
};

# Endif
-============================================== ====================
# Include "xmlhttpeventsink. H"

Stdmethodimp xmlhttpeventsink: QueryInterface (const IID & riid, void ** ppvobject)
{
If (ppvobject = NULL)
Return e_invalidarg;

* Ppvobject = NULL;

If (riid = iid_iunknown)
* Ppvobject = static_cast <iunknown *> (this );
Else if (riid = iid_idispatch)
* Ppvobject = static_cast <idispatch *> (this );

If (* ppvobject = NULL)
Return e_nointerface;

Addref ();

Return s_ OK;
}

Stdmethodimp _ (ulong) xmlhttpeventsink: addref ()
{
Return interlockedincrement (& _ refcount );
}

Stdmethodimp _ (ulong) xmlhttpeventsink: release ()
{
Long refcount = interlockeddecrement (& _ refcount );
If (refcount = 0)
{
Delete this;
Return 0;
}
Else
Return refcount;
}

Stdmethodimp xmlhttpeventsink: gettypeinfocount (uint * pctinfo)
{
Return e_notimpl;
}

Stdmethodimp xmlhttpeventsink: gettypeinfo (uint itinfo, lcid, itypeinfo ** pptinfo)
{
Return e_notimpl;
}

Stdmethodimp xmlhttpeventsink: getidsofnames (const IID & riid, lpolestr * rgsznames, uint cnames, lcid, dispid * rgdispid)
{
Return e_notimpl;
}

Stdmethodimp xmlhttpeventsink: invoke (dispid dispidmember, const IID & riid, lcid, word wflags, dispparams * handle, variant * pvarresult, interval info * p1_info, uint * puargerr)
{
// Since this class isn't used for anything else, invoke will get called only for onreadystatechange, and
// Dispidmember will always be 0.

Long state;
// Retrieve the state
_ Request-> get_readystate (& State );
If (State = 4)
{
// The request has completed.
// Get the Request status.
Long status;
_ Request-> get_status (& status );

If (status = 200)
{
// Get the response body if we were successful.
BSTR body;
_ Request-> get_responsetext (& Body );
}

// Signal the main thread we're done.
Setevent (_ completedevent );
}
Return s_ OK;
}
-============================================== ================
Call code:
Coinitialize (null );
Ixmlhttprequest * prequest = getxmlhttp ();

Prequest-> setRequestHeader (L "Content-Type", l "text/XML ");
Prequest-> setRequestHeader ("soapaction", "http://schemas.microsoft.com/exchange/services/2006/messages/FindItem ");
Widestring url = l "http://mail.pudong.sh/ews/exchange.asmx ";

Tagvariant account;
Account. Vt = vt_bstr;
Account. bstrval = l "MailAdmin@pudong.sh ";

Tagvariant password;
Password. Vt = vt_bstr;

Password. bstrval = l "app1234 ";
Tagvariant basync;
Basync. Vt = vt_bool;
Basync. boolval = false;

Widestring datastr = l "";
Tagvariant data;
Data. Vt = vt_bstr;
Data. bstrval = datastr. c_bstr ();

Handle completedevent = createevent (null, false, false, null );
Prequest-> open (L "Post", URL. c_bstr (), basync, account, password );

Idispatch * sink = new xmlhttpeventsink (prequest, completedevent );
Prequest-> put_onreadystatechange (sink );
Prequest-> send (data );

Sink-> release ();
Prequest-> release ();

Waitforsingleobject (completedevent, infinite );
Closehandle (completedevent );

Couninitialize ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.