从另一个应用程序的文本框中读取文本

时间:2014-06-12 06:42:21

标签: vb6

我尝试从其他应用程序(例如记事本或写字板)中读取文本。 我必须在VB 6中做那个以前没有编程过的女巫,但是我不能用其他语言。

我找到了Vb.Net的代码,我为VB 6修改了它,但我总是得到错误:"运行时错误' 424&#39 ;:对象需要"

代码是:

     Const WM_GETTEXTLENGTH = &HE
     Const WM_GETTEXT = &HD
Private Declare Function FindWindow Lib "USER32" Alias "FindWindowA" ( _
    ByVal lpClassName As String, _
    ByVal lpWindowName As String _
) As Long

Private Declare Function FindWindowEx Lib "USER32" Alias "FindWindowExA" ( _
    ByVal hWndParent As Long, _
    ByVal hWndChildAfter As Long, _
    ByVal lpszClassName As String, _
    ByVal lpszWindowName As String _
) As Long
Private Declare Function GetWindowText Lib "USER32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "USER32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
Private Declare Function SendMessage Lib "USER32" Alias "SendMessageA" ( _
    ByVal hWnd As Long, ByVal Msg As Long, _
    wParam As Any, lParam As Any) As Long





Private Sub Command1_Click()
         Dim notepadHandle As Long
         notepadHandle = FindWindow("Notepad", vbNullString)
         Dim childhandle As Long
         childhandle = FindWindowEx(notepadHandle, 0, "Edit", vbNullString)
         If notepadHandle = 0 Then
             MessageBox.Show ("can't find")
         Else
             'MessageBox.Show (CStr(childhandle))
          '   MessageBox.Show (WindowText(childhandle))
              MessageBox.Show (WindowText(childhandle))
End If

     End Sub

     Public Function WindowText(ByVal window_hwnd As Long) As String
         Dim txtlen As Integer
         WindowText = ""
         If window_hwnd = 0 Then Exit Function
         txtlen = SendMessage(window_hwnd, WM_GETTEXTLENGTH, 0, _
             0)
         If txtlen = 0 Then Exit Function
         txtlen = txtlen + 1
         Dim txt As String
         txt = String(txtlen + 1, Chr$(0))
         txt = Convert.ToString(SendMessage(window_hwnd, WM_GETTEXT, txtlen, txt))

         WindowText = Convert.ToString(txt)

     End Function

我犯错误的地方?

感谢名单

2 个答案:

答案 0 :(得分:2)

您的代码中没有对象。但是,msgbox只是msgbox而不是msgbox.show。这让VB认为它是一个对象而不是一个函数(用作子函数)。

另外,因为它没有括号。只有返回值的东西才需要括号。在所有基础知识中,只有vb.net具有子支架。

答案 1 :(得分:2)

不要指望VB.NET示例在VB6中工作。这两种语言在许多层面上都是不兼容的,这不仅仅是因为VB6不使用.NET框架。语法也略有不同。

首先,在风格上,VB中的声明通常位于过程的顶部。这是因为VB范围规则是按程序完成的,并忽略了代码中的所有块。

VB6本身并没有真正的静态对象,尽管你可以使用“全局”对象伪造它,它会在你第一次尝试绑定时自动实例化一个公共(即来自外部组件)的多用途类它

VB6消息框命令只是 MsgBox() Convert.ToString 只是 CStr(),虽然VB在需要时会自动转换为字符串而无需您做任何工作。

在任何情况下,行::

txt = Convert.ToString(SendMessage(window_hwnd, WM_GETTEXT, txtlen, txt))

......错误不仅仅是转换。 SendMessage()API调用返回Long值,而不是字符串。在这种情况下,txt是字符串缓冲区。

在我的版本中,我通过使用Unicode版本的SendMessage()大大简化了这一点。这意味着我可以简单地使用空字符预加载返回值WindowText,并直接将其与API调用一起使用。 StrPtr()返回指向与VB字符串相关的Unicode缓冲区的指针。 API理解这一点,并使用此缓冲区没有问题。我只需要在调用SendMessage()时添加一个长度,以便考虑到VB6字符串自动以空值终止。

Private Const WM_GETTEXT                    As Long = &HD
Private Const WM_GETTEXTLENGTH              As Long = &HE

Private Declare Function FindWindow Lib "USER32" Alias "FindWindowA" ( _
    ByVal lpClassName As String, _
    ByVal lpWindowName As String _
) As Long

Private Declare Function FindWindowEx Lib "USER32" Alias "FindWindowExA" ( _
    ByVal hWndParent As Long, _
    ByVal hWndChildAfter As Long, _
    ByVal lpszClassName As String, _
    ByVal lpszWindowName As String _
) As Long

Private Declare Function SendMessage Lib "USER32" Alias "SendMessageW" ( _
    ByVal hWnd As Long, ByVal Msg As Long, ByRef wParam As Any, ByRef lParam As Any _
) As Long

Private Sub Command1_Click()

    Dim notepadHandle           As Long
    Dim childhandle             As Long

    notepadHandle = FindWindow("Notepad", vbNullString)
    childhandle = FindWindowEx(notepadHandle, 0, "Edit", vbNullString)

    If notepadHandle = 0 Then
        MessageBox.Show "can't find"
    Else
        MsgBox WindowText(childhandle)
    End If

End Sub

Public Function WindowText(ByVal window_hwnd As Long) As String

    Dim txtlen              As Long

    If window_hwnd = 0 Then
        Exit Function
    End If

    txtlen = SendMessage(window_hwnd, WM_GETTEXTLENGTH, ByVal 0, ByVal 0)
    If txtlen = 0 Then
        Exit Function
    End If

    WindowText = String$(txtlen, vbNullChar)
    SendMessage window_hwnd, WM_GETTEXT, ByVal (txtlen + 1), ByVal StrPtr(WindowText)

End Function