简体   繁体   中英

Event in MSXML2.XMLHTTP

Does anyone know how MSXML2.XMLHTTP implement its event like onreadystatechange?
Since I need construct a COM component with C# which will raise a event, but the client which use vbscript to call this COM object does not support WScript.CreateObject, so I can't handle the COM event.
But I find that MSXML2.XMLHTTP's event can be handled in my client as follows:

Function Hello()
If(objHttp.readyState=4) Then
MsgBox objHttp.responseText
End If
End Function
Set objHttp = CreateObject("MSXML2.XMLHTTP")
Set xmlDoc = CreateObject("MSXML.DOMDocument")
strWebserviceURL="http://localhost:8083/WebService.asmx/HelloWorld"
objHttp.onreadystatechange=getRef("Hello")
objHttp.Open "POST", strWebserviceURL
objHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objHttp.send()

I've learnt from here that the event of MSXML2.XMLHTTP is not implemented as a COM automation event.
So I wonder How does MSXML2.XMLHTTP implement its event, can anyone give a hint, any help will be appreciated.

"onreadystatechange" is not a COM Automation event because COM Automation events require the caller (mainly written in scripting languages) to implement specific COM Interfaces, which scripting languages often cannot do (look up COM Connection Points to get the full details).

If you start with the link you provided, and add information about MSXML that you can gather with the "OLE View" utility (also called the "OLE-COM Object Viewer"), you have everything you need to figure it out.

Use Ole View and navigate to the MSXML type library ("\\Type Libraries\\Microsoft XML, v6.0 (ver 6.0)"). Open that, which will open a separate window with the Type Library shown in detail.

From the Type Library viewer, open "CoClasses" and find XMLHTTP60 (that's the exact class name used in the MSDN post). You will verify that it implements the IXMLHTTPRequest interface.

Now, open the Interfaces node and find IXMLHTTPRequest . That will yield the following code (which is generated IDL based on the metadata stored in the type library):

...
[
  odl,
  uuid(ED8C108D-4349-11D2-91A4-00C04F7969E8),
  helpstring("IXMLHTTPRequest Interface"),
  dual,
  oleautomation
]
interface IXMLHTTPRequest : IDispatch {
    ...
    [id(0x0000000e), propput, helpstring("Register a complete event handler")]
    HRESULT onreadystatechange([in] IDispatch* rhs);
};

That tells you how it all works. You will need to add a method to your code that takes an IDispatch reference as an argument. Save the reference. The contract for your object will say that the object provided must have a method named "OnWhateverMyEventNameIs", and possibly specify a list of arguments the method must take.

When your event needs to be signaled, your code will take that reference that was previously provided, look for a method with that name, and execute it if found.

Since you're working on C#, your setter method or property needs to be [COMVisible] (obviously) and it should take an Object reference. The simplest thing for your event triggering implementation might be to use the dynamic support in the language to just call the method (inside a try/catch, in case the method doesn't exist or is otherwise invalid).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM