Results 1 to 21 of 21

Thread: Lifting text from another app

  1. #1

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359

    Lifting text from another app

    Just wondering, is there a way of lifting text from labels in another application ?

    Thanks,
    Jamie.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  2. #2
    Fanatic Member crispin's Avatar
    Join Date
    Aug 2000
    Location
    2 clicks west of a Quirkafleeg...Cornwall, England
    Posts
    754
    Yes, but im not telling you how to do it!!!



    (just kidding, ill post a sample app in a minute)
    Crispin
    VB6 ENT SP5
    VB.NET
    W2K ADV SVR SP3
    WWW.BLOCKSOFT.CO.UK

    [Microsoft Basic: 1976-2001, RIP]

  3. #3

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Oh thank-you kindly
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  4. #4
    Fanatic Member crispin's Avatar
    Join Date
    Aug 2000
    Location
    2 clicks west of a Quirkafleeg...Cornwall, England
    Posts
    754
    Code:
    Option Explicit
    
    Private Declare Function FindWindow& Lib "user32" Alias "FindWindowA" (ByVal lpClassName$, _
    ByVal lpWindowName$)
    
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, _
    ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    
    Private Declare Function SendMessage& Lib "user32" Alias "SendMessageA" (ByVal hWnd&, ByVal _
    wMsg&, ByVal wParam&, lParam As Any)
    Private Const WM_SETTEXT = &HC
    Private Const WM_GETTEXT = &HD
    Private Declare Function SendMessageByStr& Lib "user32" Alias "SendMessageA" _
     (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal _
      lParam As String)
    
    Private Sub cmdLiftIt_Click()
    Dim t$
    Dim c&
    Dim hApp&
    Dim hLabel&
      t = String(256, " ")
      'insert the right windowname obviously (MSDN just an example)
      hApp = FindWindowEx(0&, 0&, vbNullString, "MSDN Library - April 2001")
      hLabel = FindWindowEx(hApp, 0&, "Static", vbNullString)
      c = SendMessageByStr(hLabel, WM_GETTEXT, 256, t)
      MsgBox Trim(t)
    End Sub
    now the only other problem is to search for the right label, you could modify this to recurse all labels in a parent window (lemme know if you want that code too, and i will supply)
    Crispin
    VB6 ENT SP5
    VB.NET
    W2K ADV SVR SP3
    WWW.BLOCKSOFT.CO.UK

    [Microsoft Basic: 1976-2001, RIP]

  5. #5

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Yes please do supply
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  6. #6
    Fanatic Member crispin's Avatar
    Join Date
    Aug 2000
    Location
    2 clicks west of a Quirkafleeg...Cornwall, England
    Posts
    754

    that was a bloody nightmare!

    Sorry abou the delay Jamie, I got mixed up with processes,threads, kangaroos - sh*! thats a different story, anyhow, heres the code:

    Code:
    'in a form
    Option Explicit
    
    Private Sub command1_Click()
      Dim hApp&
      Dim retval&
      Dim lParam&
      Dim v As Variant
      hApp = FindWindowEx(0&, 0&, vbNullString, "MSDN Library - April 2001")
      retval = EnumChildWindows(hApp, AddressOf EnumChildProc, lParam)
      For Each v In clabels
        Debug.Print v
      Next v
    End Sub
    
    
    'in a module
    Option Explicit
    Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent _
    As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, _
    ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    
    Declare Function GetClassName Lib "user32" Alias "GetClassNameA" _
       (ByVal hwnd As Long, ByVal lpClassName As String, _
       ByVal nMaxCount As Long) As Long
       
    Private Const WM_GETTEXT = &HD
    
    Private Declare Function SendMessageByStr& Lib "user32" Alias "SendMessageA" _
    (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal _
    lParam As String)
    Public clabels As New Collection
       
    Function EnumChildProc(ByVal lhWnd As Long, ByVal lParam As Long) As Long
      Dim retval As Long
      Dim t$
      Dim c&
      Dim WinClassBuf As String * 255, WinTitleBuf As String * 255
      Dim WinClass As String, WinTitle As String
      Dim WinWidth As Long, WinHeight As Long
      t = String(256, " ")
      retval = GetClassName(lhWnd, WinClassBuf, 255)
      WinClass = StripNulls(WinClassBuf)
      If WinClass = "Static" Then
        c = SendMessageByStr(lhWnd, WM_GETTEXT, 256, t)
        clabels.Add Trim(t)
      End If
      Debug.Print WinClass
      EnumChildProc = True
    End Function
    
    Public Function StripNulls(OriginalStr As String) As String
       If (InStr(OriginalStr, Chr(0)) > 0) Then
          OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)
       End If
       StripNulls = OriginalStr
    End Function
    Crispin
    VB6 ENT SP5
    VB.NET
    W2K ADV SVR SP3
    WWW.BLOCKSOFT.CO.UK

    [Microsoft Basic: 1976-2001, RIP]

  7. #7

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    The code looks great, but it only seems to be able to grab the class names, not the captions themselves .... should I be doing something specific ?
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  8. #8
    Fanatic Member crispin's Avatar
    Join Date
    Aug 2000
    Location
    2 clicks west of a Quirkafleeg...Cornwall, England
    Posts
    754
    when it iterates the collection at the end and debug.prints the captions does it not give you the captions - I worked hard on that bit...

    Crispin
    VB6 ENT SP5
    VB.NET
    W2K ADV SVR SP3
    WWW.BLOCKSOFT.CO.UK

    [Microsoft Basic: 1976-2001, RIP]

  9. #9

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    ... no it just gives the class names, no captions ...
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  10. #10
    Addicted Member
    Join Date
    Apr 2000
    Location
    England
    Posts
    246
    works fine for me

  11. #11
    Fanatic Member crispin's Avatar
    Join Date
    Aug 2000
    Location
    2 clicks west of a Quirkafleeg...Cornwall, England
    Posts
    754
    is it a VB app?
    Crispin
    VB6 ENT SP5
    VB.NET
    W2K ADV SVR SP3
    WWW.BLOCKSOFT.CO.UK

    [Microsoft Basic: 1976-2001, RIP]

  12. #12

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Not a notion
    But I tested it on a vb app and it didnt work either.

    Looking at the code, I see no reason why it would print the captions of the controls ....
    Last edited by plenderj; May 3rd, 2001 at 10:31 AM.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  13. #13
    Fanatic Member crispin's Avatar
    Join Date
    Aug 2000
    Location
    2 clicks west of a Quirkafleeg...Cornwall, England
    Posts
    754
    I had problems trying it with the API viewer, I think it's because vb labels don't have a hwnd or something like that, i'll keep working on it, maybe i'll ask Megatron about this one im getting a bit stuck - sorry
    Crispin
    VB6 ENT SP5
    VB.NET
    W2K ADV SVR SP3
    WWW.BLOCKSOFT.CO.UK

    [Microsoft Basic: 1976-2001, RIP]

  14. #14

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Well im trying it with a database app.
    I would be very grateful if you could get this working
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  15. #15
    Addicted Member
    Join Date
    Apr 2000
    Location
    England
    Posts
    246
    Use Spy++ to see if you can highlight the Labels, if you can it will work, if you can't then it will not

    some programs such as Office have protections against doing this

  16. #16
    Fanatic Member crispin's Avatar
    Join Date
    Aug 2000
    Location
    2 clicks west of a Quirkafleeg...Cornwall, England
    Posts
    754
    Yeah thats what I have been doing, I reckon there must be some slimy hack out there somewhere to do this!
    Crispin
    VB6 ENT SP5
    VB.NET
    W2K ADV SVR SP3
    WWW.BLOCKSOFT.CO.UK

    [Microsoft Basic: 1976-2001, RIP]

  17. #17
    Tygur
    Guest
    If it means anything, VB Labels do not have a hWnd. So you can't use an API function to get the text out of a VB label.

  18. #18

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    I can use Spy++ to get the captions out of my own apps, but I cant use your code to do it ...
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  19. #19

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Any progress ?
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  20. #20
    Tygur
    Guest
    Try replacing EnumChildProc in the code from crispin with this: (It's in the module)
    Code:
    Function EnumChildProc(ByVal lhWnd As Long, ByVal lParam As Long) As Long
      Dim retval As Long
      Dim t$
      Dim c&
      Dim WinClassBuf As String * 255, WinTitleBuf As String * 255
      Dim WinClass As String, WinTitle As String
      Dim WinWidth As Long, WinHeight As Long
      t = String(256, " ")
      retval = GetClassName(lhWnd, WinClassBuf, 255)
      WinClass = StripNulls(WinClassBuf)
      c = SendMessageByStr(lhWnd, WM_GETTEXT, 256, t)
      If c > 0 Then
        t = Left(t, c)
        clabels.Add "ClassName: " & WinClass & vbTab & "Text: " & t
      End If
      'Debug.Print WinClass
      EnumChildProc = True
    End Function
    This was changed to list everything on the window it can get text for. And it lists the ClassName and Text together. See if it shows the contents of any of the labels you want.

    If it doesn't, I don't think it is possible to get the contents of these labels.

  21. #21

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Its getting some labels ...
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width