Solved

Listbox contents with only handle

Posted on 1998-08-28
8
768 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

746 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

9 Experts available now in Live!

Get 1:1 Help Now