Solved

Listbox contents with only handle

Posted on 1998-08-28
8
769 Views
Last Modified: 2008-02-01
How can I get the contents of a listbox if all i have is the list box's window handle?
0
Comment
Question by:Bdsabian
  • 4
  • 2
  • 2
8 Comments
 
LVL 1

Expert Comment

by:cip
ID: 1431744
You can use the SendMessage() API function to retrieve the content of a window listbox control.

The following code will retrieve the content of a listbox item only using the window handle of a visual basic listbox control. You should get the same result for any standard windows listbox.

- Draw a listbox control and a command button on your form.
- Paste this code:

Private Const LB_ERR = (-1)
Private Const LB_GETTEXT = &H189
Private Const LB_GETTEXTLEN = &H18A

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Sub Command1_Click()
    Dim i As Integer
    Dim sItem As String, iLen As Integer
    Cont ITEMINDEX = 20
   
    ' Fill the listbox
    For i = 1 To 40
        List1.AddItem "Item " & i
    Next
   
    ' Get length of item 21 before
    ' retrieving it
    iLen = SendMessage(List1.hwnd, LB_GETTEXTLEN, ITEMINDEX, 0&)
    If iLen = LB_ERR Then
        MsgBox "Error retrieving item length"
        Exit Sub
    End If
   
    ' Allocate enough room in the
    ' string variable, then get item
    sItem = Space$(iLen)
    iLen = SendMessage(List1.hwnd, LB_GETTEXT, ITEMINDEX, ByVal sItem)
    If iLen = LB_ERR Then
        MsgBox "Error retrieving item"
        Exit Sub
    End If
   
    MsgBox "Item retrieved: " & sItem
   
End Sub

0
 

Author Comment

by:Bdsabian
ID: 1431745
that works for listboxes within my program, however, the listbox whose contents i need is in another program.  ive gotten that listbox's handle using a few API calls.
0
 
LVL 1

Expert Comment

by:cip
ID: 1431746
I thought you already had the listbox window handle, since this is what you state in your question, so I focused the answer about how to get the listbox contents.

However you can substitute list1.hwnd with the window handle you get with the API calls.
0
 

Author Comment

by:Bdsabian
ID: 1431747
I tried to substitute list1.hwnd with the handle i got using API calls, but it gave me garbage.  the first thing in the list box was Bdsabian, but what it returned to me was pŒ’
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:mkmccreary
ID: 1431748
Another thing to consider is to try to get the listbox handle from the owner program and compare it with the window handle you are getting from the 'client' application.  It may be that one window handle is a 16 bit number, and the other is a 32 bit handle.  This could especially be the case if one application is Win16 and the other is Win32,  or if a 'Win32' application actually uses a 'Win16' handle.  I've had a similar problem before.  I had a 32 bit application that worked fine under Win95, but didn't work under NT because a component in the application was returning a 16 bit handle.  NT was unforgiving, Win95 didn't care.

Just A Thought,
Martin
0
 
LVL 1

Accepted Solution

by:
cip earned 100 total points
ID: 1431749
Maybe you should provide more information. What API did you use? What application owns the listbox you're trying to access?

I didn't know a 32bit API call could return a 16bit handle as Martin said, I thought they would be converted to 32bit anyway, but this is worth checking.
0
 
LVL 1

Expert Comment

by:cip
ID: 1431750
Woops, I posted my last comment as an answer, please feel free to reject it.
0
 
LVL 2

Expert Comment

by:mkmccreary
ID: 1431751
Actually it was an ActiveX component that was returning a device context to a printer.  The publisher admitted it was a bug and published an updated control.  It was weird that the buggy control worked under Win95 and not NT.  I just thought it may be a similar situation.

Later,
Martin

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now