FW:Use the OnReadyStateChange Property in Visual Basic and Visual C/C++
来源:互联网 发布:淘宝企业店铺收费 编辑:程序博客网 时间:2024/06/03 19:09
Link:http://www.nedcomp.nl/support/origdocs/xml4/extracted/dom_howdoi_0rj7.aspx
The onreadystatechange
property and event notification was intended primarily for use by scripting clients (VBScript, Jscript). It can, however, also be used in Win32 applications.
This topic discusses the implementation details necessary to use onreadystatechange
notification in applications written using Microsoft Visual Basic and Microsoft Visual C/C++.
Background Information for OnReadyStateChange events
The onreadystatechange
callback function was not implemented as a COM automation event in the IXMLHTTPRequest
and IServerXMLHTTPRequest
components. This is because these components are heavily used in scripting environments, many of which do not support COM events. The onreadystatechange
callback function was intended to be easy to use when working with scripting clients such as VBScript and JScript.
Because the onreadystatechange
property was not implemented through COM-based automation events, using this callback functionality in Visual Basic and Visual C/C++ applications can be awkward for developers using those tools. Several options for implementing onreadystatechange
, however, allow you to choose a way to work around this awkwardness.
Using OnReadyStateChange in Visual Basic applications
In Visual Basic, you can use any of the following approaches to design applications that support onreadystatechange
events.
- Use a timer control to poll the
readyState
property. When the data is ready, turn the timer off. - Use a
DomDocument
object to load the XML and handle the state using theWithEvents
keyword.Note If you are using the
IXMLHTTPRequest
andIServerXMLHTTPRequest
components is to first post your XML data to a Web server, this option will not work for you. - Create a wrapper class and create a procedure to handle the event within the class module. Set the procedure to be the default, and bind the class to the
onreadystatechange
event to either theIXMLHTTPRequest
orIServerXMLHTTPRequest
component, depending on which component you are using with your application.
The following sample application demonstrates each of these three approaches.
To demonstrate OnReadyStateChange in a Visual Basic application
- Open Microsoft® Visual Basic® 6.0. In the New Project dialog box, double-clickStandard EXE.
- On the Project menu, click References.
- In the Available References list, select Microsoft XML,v4.0, and then click OK.
- Add four command buttons to Form1 and set the caption of each button as follows:
Control
CaptionCommand1
FailCommand2
Polling using TimerCommand3
Using Class WrapperCommand4
Using DOMDocument - Add a timer control to Form1.
- Copy and paste the following code into Form1.
Option ExplicitPublic XMLHttpRequest As MSXML2.XMLHTTP40Public WithEvents XMLDom As MSXML2.DOMDocument40Private Function FunctionReadyStateChange() Debug.Print XMLHttpRequest.readyStateEnd FunctionPrivate Sub Command1_Click() FailedOnReadyStateEnd SubPrivate Sub Command2_Click() TimerResolutionEnd SubPrivate Sub Command3_Click() ClassResolutionEnd SubPrivate Sub Command4_Click() DOMResolutionEnd SubPrivate Sub FailedOnReadyState()On Error GoTo FailedState If Not XMLHttpRequest Is Nothing Then Set XMLHttpRequest = Nothing Set XMLHttpRequest = New MSXML2.XMLHTTP40 ' Assign the wrapper class object to onreadystatechange. XMLHttpRequest.OnReadyStateChange = FunctionReadyStateChange ' Get some stuff asynchronously. XMLHttpRequest.open "GET", "http://localhost/test.xml", True XMLHttpRequest.send Exit SubFailedState: MsgBox Err.Number & ": " & Err.DescriptionEnd SubPrivate Sub TimerResolution() If Not XMLHttpRequest Is Nothing Then Set XMLHttpRequest = Nothing Timer1.Interval = 1 Set XMLHttpRequest = New MSXML2.XMLHTTP40 ' Get some stuff asynchronously. XMLHttpRequest.open "GET", "http://localhost/test.xml", True XMLHttpRequest.sendEnd SubPrivate Sub ClassResolution() If Not XMLHttpRequest Is Nothing Then Set XMLHttpRequest = Nothing Dim MyOnReadyStateWrapper As MyReadyStateHandler Set XMLHttpRequest = New MSXML2.XMLHTTP40 ' Create an instance of the wrapper class. Set MyOnReadyStateWrapper = New MyReadyStateHandler ' Assign the wrapper class object to onreadystatechange. XMLHttpRequest.OnReadyStateChange = MyOnReadyStateWrapper ' Get some stuff asynchronously. XMLHttpRequest.open "GET", "http://localhost/test.xml", True XMLHttpRequest.sendEnd SubPrivate Sub DOMResolution() If Not XMLHttpRequest Is Nothing Then Set XMLHttpRequest = Nothing If Not XMLDom Is Nothing Then Set XMLDom = Nothing Set XMLDom = New MSXML2.DOMDocument40 XMLDom.async = True XMLDom.Load "http://localhost/test.xml"End SubPrivate Sub Timer1_Timer() Debug.Print XMLHttpRequest.readyState If XMLHttpRequest.readyState = 4 Then MsgBox "Done" Timer1.Interval = 0 End IfEnd SubPrivate Sub XMLDom_onreadystatechange() Debug.Print XMLDom.readyState If XMLDom.readyState = 4 Then MsgBox "Done" End IfEnd Sub
- From the Project menu, click Add Class Module.
- Change the name of the new class module from "Class1" to "MyReadyStateHandler"
- Paste the following code into the class module:
Option ExplicitSub OnReadyStateChange() Debug.Print Form1.XMLHttpRequest.readyState If Form1.XMLHttpRequest.readyState = 4 Then MsgBox "Done" End IfEnd Sub
- In the sample code added in the previous step, highlight the procedure name "OnReadyStateChange" by selecting it in the Code window.
- From the Tools menu, click Procedure Attributes.
In the Procedure Attributes dialog, the Name combo box should show "OnReadyStateChange."
- Click Advanced.
- In Procedure ID, select "(Default)" from the available options.
- Click OK.
- Save the class module (MyReadyStateHandler.cls) to file.
- Open Notepad and paste the following XML into it
This is to test the onreadystatechange event on the XMLHTTPRequest or DOMDocument This is due to the event not being declared in the type library - Save the file as test.xml to your IIS localhost directory. For example, this folder might be C:Inetpubwwwroot for a typical default installation of IIS with Windows 2000.
- In Visual Basic, from the Run menu, click Start to run the application.
- Try the following command options to observe the different approaches to using the
onreadystatechange
event within Visual Basic.- To force a ready state failure, click Fail.
- To view the polling resolution, click Polling using Timer.
- To view the wrapper class solution, click Using Class Wrapper.
- To view the DOMDocument approach, click Using DomDocument.
- For each of the code paths in the previous step, you can place brake-points at various places to step through the code.
Remarks
For step 19a, the following error message should appear indicating the failure:
424: Object required
For steps 19b, 19c and 19d, return values for onreadystatechange
should appear in the Immediate window while the document loads. When the document completes loading, a message box saying "Done" should then appear.
Using OnReadyStateChange in Visual C/C++ applciations
In C++, use connection points to trap all XMLDOMDocument
events. The DISPID of the connection point container is DIID_XMLDOMDocumentEvents
. The DISPID for the connection point to trap the readystatechange
event is DISPID_XMLDOMEVENT_ONREADYSTATECHANGE
.
For a C/C++ example, see onreadystatechange Event.
- FW:Use the OnReadyStateChange Property in Visual Basic and Visual C/C++
- How To Pass Arrays Between Visual Basic and C
- What's the difference between Interface and an abstract class in visual c#?
- Using UTF-8 as the internal representation for strings in C and C++ with Visual Studio
- How To Use the Spreadsheet Web Component with Visual Basic
- 7.3Basic and visual basic
- How To Use the ODBC .NET Managed Provider in Visual C# .NET and Connection Strings
- Pro Visual C++/CLI and the .NET 2.0 Platform
- Visual Basic .NET and the .NET Platform: An Advanced Guide
- How to use composition and inheritance in visual c# ?
- Use HttpUtility and NameValueCollection in c# of Visual Studio 2010
- Delphi、C C++、Visual Basic数据类型的对照
- Pascal、C / C + +、Visual Basic数据类型的对照
- Delphi、C/C++、Visual Basic数据类型的对照
- Delphi、C/C++、Visual Basic数据类型的对照
- Delphi、C/C++、Visual Basic数据类型的对照
- Professional Refactoring in Visual Basic
- How to use the System Restore API to save and to restore system data in Visual C++
- 包裝ffmpeg中的codecs成為DirectShow中的transform filter
- NVIDIA联合ARM推PC及服务器芯片
- Todd Veldhuizen:Template Metaprograms
- 对sqlite数据库的增,删,改,查的一般性操作代码
- 动画的使用
- FW:Use the OnReadyStateChange Property in Visual Basic and Visual C/C++
- 图像处理小结
- How to Call Back In Com ?
- Linux设备驱动程序第三版学习(5)- 高级字符驱动程序操作 - ioctl
- release 时去掉nslog的方法
- 英特尔首席执行官:不介意Windows8支持ARM架构
- How Does Excel’s RTD Function Work ?
- 常用小问题
- FW:走近COM Interop--编程以实现COM–>Assembly