FW:Use the OnReadyStateChange Property in Visual Basic and Visual C/C++

原创 2011年01月10日 14:23:00


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 onreadystatechangenotification 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.

  1. Use a timer control to poll the readyState property. When the data is ready, turn the timer off.
  2. Use a DomDocument object to load the XML and handle the state using the WithEventskeyword.
    Note   If you are using the IXMLHTTPRequest and IServerXMLHTTPRequestcomponents is to first post your XML data to a Web server, this option will not work for you.
  3. 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 theonreadystatechangeevent to either the IXMLHTTPRequest or IServerXMLHTTPRequest 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

  1. Open Microsoft® Visual Basic® 6.0. In the New Project dialog box, double-clickStandard EXE.
  2. On the Project menu, click References.
  3. In the Available References list, select Microsoft XML,v4.0, and then click OK.
  4. Add four command buttons to Form1 and set the caption of each button as follows:



    Polling using Timer

    Using Class Wrapper

    Using DOMDocument

  5. Add a timer control to Form1.
  6. Copy and paste the following code into Form1.
    Option Explicit
    Public XMLHttpRequest As MSXML2.XMLHTTP40
    Public WithEvents XMLDom As MSXML2.DOMDocument40
    Private Function FunctionReadyStateChange()
        Debug.Print XMLHttpRequest.readyState
    End Function
    Private Sub Command1_Click()
    End Sub
    Private Sub Command2_Click()
    End Sub
    Private Sub Command3_Click()
    End Sub
    Private Sub Command4_Click()
    End Sub
    Private 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
        Exit Sub
        MsgBox Err.Number & ": " & Err.Description
    End Sub
    Private 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
    End Sub
    Private 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
    End Sub
    Private 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 Sub
    Private Sub Timer1_Timer()
        Debug.Print XMLHttpRequest.readyState
        If XMLHttpRequest.readyState = 4 Then
            MsgBox "Done"
            Timer1.Interval = 0
        End If
    End Sub
    Private Sub XMLDom_onreadystatechange()
        Debug.Print XMLDom.readyState
        If XMLDom.readyState = 4 Then
            MsgBox "Done"
        End If
    End Sub
  7. From the Project menu, click Add Class Module.
  8. Change the name of the new class module from "Class1" to "MyReadyStateHandler"
  9. Paste the following code into the class module:
    Option Explicit
    Sub OnReadyStateChange()
        Debug.Print Form1.XMLHttpRequest.readyState
        If Form1.XMLHttpRequest.readyState = 4 Then
            MsgBox "Done"
        End If
    End Sub
  10. In the sample code added in the previous step, highlight the procedure name "OnReadyStateChange" by selecting it in the Code window.
  11. From the Tools menu, click Procedure Attributes.

    In the Procedure Attributes dialog, the Name combo box should show "OnReadyStateChange."

  12. Click Advanced.
  13. In Procedure ID, select "(Default)" from the available options.
  14. Click OK.
  15. Save the class module (MyReadyStateHandler.cls) to file.
  16. Open Notepad and paste the following XML into it
    <?xml version="1.0"?>
    This is to test the onreadystatechange event on the XMLHTTPRequest or DOMDocumentThis is due to the event not being declared in the type library
  17. 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.
  18. In Visual Basic, from the Run menu, click Start to run the application.
  19. Try the following command options to observe the different approaches to using theonreadystatechange event within Visual Basic.
    1. To force a ready state failure, click Fail.
    2. To view the polling resolution, click Polling using Timer.
    3. To view the wrapper class solution, click Using Class Wrapper.
    4. To view the DOMDocument approach, click Using DomDocument.
  20. For each of the code paths in the previous step, you can place brake-points at various places to step through the code.

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.

Service Pack 6 for Visual Basic 6.0, Visual C++ 6.0 with Visual Source Safe 6.0d下载

下载VC6.0的SP6补丁,网址http://www.microsoft.com/en-us/download/details.aspx?id=9183 补充: 英文版  http://...
  • wanyecheng
  • wanyecheng
  • 2012年11月01日 20:47
  • 451

Visual Basic快捷教程——流程控制

  • baimafujinji
  • baimafujinji
  • 2017年04月04日 15:14
  • 3505

Visual Basic 6.0 控件和 .NET 控件的区别

Visual Basic 6.0 控件和 .NET 控件的区别 升级到 Microsoft .NET Paul D. SheriffPDSA, Inc. 2001年11月 摘要:本文简单介绍了 Mic...
  • eaglezhao
  • eaglezhao
  • 2002年01月09日 10:45
  • 1125

Visual Basic快速入门

Basic语言的全名是Beginner’s All-purpose Symbolic Instruction Code(初学者通用的符号指令代码),是几十年前流行过的一门语言。我高中时候一门数学选修课...
  • u011054333
  • u011054333
  • 2018年01月05日 18:26
  • 171

Visual Basic 6.0.with.SP6 龙卷风大企业集成安装版(含下载地址)

  • pzhan
  • pzhan
  • 2007年01月31日 09:28
  • 3150

Visual Basic for Applications 学习笔记(一)

一 创建新工作簿 如果要用 Visual Basic 创建新的工作簿,应使用 Add 方法。下述过程创建了新工作簿。Microsoft Excel 自动将该工作簿命名为“BookN”,其中 N 是下一...
  • DeRoshia
  • DeRoshia
  • 2007年01月19日 14:16
  • 884

Visual Basic的类对于面向对象的支持

VB最为人垢病的是它的面向对象特性。实际上VB是一种基于对象的开发工具。在VB中 建立的类是支持继承的。下面是范例: 首先建立一个新工程,然后添加一个新的类模块(Class Module),类名称设定...
  • IceMe
  • IceMe
  • 2004年10月30日 14:49
  • 857

Visual Basic for Application是什么

  • clj198606061111
  • clj198606061111
  • 2010年04月03日 11:45
  • 4050

将Visual Basic 6.0程序转换到Visual Basic.NET

从 Visual Basic 6.0 到 Visual Basic.NET 的转换 Microsoft Visual Basic.NET 是 Microsoft Visual Basic© 的后续版本...
  • coolstar
  • coolstar
  • 2001年04月30日 13:51
  • 2463
您举报文章:FW:Use the OnReadyStateChange Property in Visual Basic and Visual C/C++