Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As LongPrivate Sub Form_Click()
Dim str1 As String * 250
GetWindowText Form1.hwnd, str1, Len(str1)
MsgBox str1
End Sub'这里用变长的为什么不行?
'这样的传str1是地址传递,是不是我在别的API应用时,我只要把字符串的地址传给函数也能得到字符串的内容呢?

解决方案 »

  1.   

    定长会分配缓冲区,变长则是动态开辟缓冲区,而实际接受的只是long型的地址,导致函数返回后,该地址指向的数据无效。
      

  2.   

    用变长也可以,但需要初始化,定长是由VB自动初始化了(全部填充为ASCII码0).如:
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As LongPrivate Sub Form_Click()
        Dim str1 As String
        str1 = String(250, Chr(0))
        GetWindowText Form1.hwnd, str1, Len(str1)
        str1 = Left(str1, InStr(1, str1, Chr(0)) - 1) '未判断返回值为vbNullString的情况
        MsgBox str1, , Len(str1)
    End Sub
      

  3.   


    Declare Function GetActiveWindow Lib "user32" () As Long
    Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
        (ByVal Hwnd As Long, ByVal lpString As String, ByVal cch As Long) As LongFunction ActiveWindowCaption() As String
       Dim strCaption As String
       Dim lngLen   As Long
       
       ' 创建由空字符填充的字符串。
       strCaption = String$(255, vbNullChar)
       ' 返回字符串长度。
       lngLen = Len(strCaption)   ' 调用 GetActiveWindow,返回活动窗口的句柄,
       ' 把句柄连同字符串和字符串长度一起传递给 GetWindowText。
       If (GetWindowText(GetActiveWindow, strCaption, lngLen) > 0) Then
          ' 返回 Windows 写到字符串中的值。
          ActiveWindowCaption = strCaption
       End If
    End Function
    'GetWindowText 函数有 3 个参数:窗口句柄;准备接受窗口标题的 NULL 结束字符串;以及该字符串的长度。所调用的系统API是不会为应用程序申请内存的,所以存放结果的缓冲区必须在调用前初始化,无论是定长还是变长的string都需要一块足够大的内存,来存放调用结果GetWindowText Function--------------------------------------------------------------------------------The GetWindowText function copies the text of the specified window's title bar (if it has one) into a buffer. If the specified window is a control, the text of the control is copied. However, GetWindowText cannot retrieve the text of a control in another application.Syntaxint GetWindowText(          HWND hWnd,
        LPTSTR lpString,
        int nMaxCount
    );
    ParametershWnd
    [in] Handle to the window or control containing the text. 
    lpString
    [out] Pointer to the buffer that will receive the text. If the string is as long or longer than the buffer, the string is truncated and terminated with a NULL character. 
    nMaxCount
    [in] Specifies the maximum number of characters to copy to the buffer, including the NULL character. If the text exceeds this limit, it is truncated. 
    Return ValueIf the function succeeds, the return value is the length, in characters, of the copied string, not including the terminating NULL character. If the window has no title bar or text, if the title bar is empty, or if the window or control handle is invalid, the return value is zero. To get extended error information, call GetLastError. This function cannot retrieve the text of an edit control in another application.
    ResIf the target window is owned by the current process, GetWindowText causes a WM_GETTEXT message to be sent to the specified window or control. If the target window is owned by another process and has a caption, GetWindowText retrieves the window caption text. If the window does not have a caption, the return value is a null string. This behavior is by design. It allows applications to call GetWindowText without hanging if the process that owns the target window is hung. However, if the target window is hung and it belongs to the calling application, GetWindowText will hang the calling application. To retrieve the text of a control in another process, send a WM_GETTEXT message directly instead of calling GetWindowText. Windows 95/98/Me: GetWindowTextW is supported by the Microsoft® Layer for Unicode (MSLU). To use this, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems.ExampleFor an example, see Sending a Message.Function InformationHeader Declared in Winuser.h, include Windows.h 
    Import library User32.lib 
    Minimum operating systems Windows 95, Windows NT 3.1 
    Unicode Implemented as Unicode and ANSI versions on Windows NT, Windows 2000, Windows XP