HELP: SendMessage and CB_GETLBTEXT in C# 
Author Message
 HELP: SendMessage and CB_GETLBTEXT in C#

Ladies 'n Gentlemen,

I made a C# app and trying now to get the listbox entries of a combobox
control in a windows application, especially the internet explorer. I've
got the right handle to the comboBox (I guess) and I'm able to get
information about the amount of listBox entries. Furthermore I get the
right text length of any entry. But I can't retrieve any character or
any string not to mention.
Sometimes the SendMessages return value is greater than 0, sometimes it
is -1.

[DllImport("user32.exe")]
SendMessage(int hwnd, int Msg, int wParam, object lParam);
..
const int CB_GETLBTEXT = 0x0148;
const int CB_GETCOUNT = 0x0146;
const int CB_GETLBTEXTLEN = 0x0149;
..
// cbHandle = Handle to the combobox control
int totalEntries = SendMessage(cbHandle, CB_GETCOUNT, 0, 0);
char[] ch;
for(int i = 0; i < totalEntries; i++)
{
   int txtLength = SendMessage(cbHandle, CB_GETLBTEXTLEN,  
                               i, 0);
   ch = new char[txtLength];
   SendMessage(cbHandle, CB_GETLBTEXT, i, ch)
   string itemText = new String(ch);

Quote:
}

// to fill a comboBox with retrieved items
listBox3.Items.Add(itemText);

(Hope I did not forgot essential codelines).

Thanks

T. La Benche

*** Sent via Developersdex http://www.*-*-*.com/ ***
Don't just participate in USENET...get rewarded for it!



Sun, 27 Mar 2005 16:42:39 GMT  
 HELP: SendMessage and CB_GETLBTEXT in C#


Wed, 18 Jun 1902 08:00:00 GMT  
 HELP: SendMessage and CB_GETLBTEXT in C#
Thomas,

    If you are trying to get the value of a combo box in internet explorer
(one that is on the page) then you should be using the DOM model to get the
options of the control on the page.

    Also, your declaration for the SendMessage method is wrong.  It should
be:

[DllImport("user32.exe", CharSet=CharSet.Auto)]
public static extern int SendMessage(
    IntPtr hwnd,
    [MarshalAs(UnmanagedType.U4)] int Msg,
    IntPtr wParam,
    IntPtr lParam); // This is the culprit.  You had object, and that lets
the runtime pick what it
                    // wants to send.

    So, now that the last parameter is an IntPtr, how do you get the text?
Well, first you call AllocCoTaskMem or AllocHGlobal to allocate the space in
memory for the string.  Once you have that, pass the IntPtr to the memory to
SendMessage to get the result.  The IntPtr will now point to memory that is
populated with the contents of the textbox.  At that point, you have to call
the static PrtToStringAuto method on the Marshal class.  This will give you
the string contents.  Finally, remember to free the unmanaged memory you
allocated.

    Hope this helps.

--
               - Nicholas Paldino [.NET/C# MVP]



Quote:

> Ladies 'n Gentlemen,

> I made a C# app and trying now to get the listbox entries of a combobox
> control in a windows application, especially the internet explorer. I've
> got the right handle to the comboBox (I guess) and I'm able to get
> information about the amount of listBox entries. Furthermore I get the
> right text length of any entry. But I can't retrieve any character or
> any string not to mention.
> Sometimes the SendMessages return value is greater than 0, sometimes it
> is -1.

> [DllImport("user32.exe")]
> SendMessage(int hwnd, int Msg, int wParam, object lParam);
> ..
> const int CB_GETLBTEXT = 0x0148;
> const int CB_GETCOUNT = 0x0146;
> const int CB_GETLBTEXTLEN = 0x0149;
> ..
> // cbHandle = Handle to the combobox control
> int totalEntries = SendMessage(cbHandle, CB_GETCOUNT, 0, 0);
> char[] ch;
> for(int i = 0; i < totalEntries; i++)
> {
>    int txtLength = SendMessage(cbHandle, CB_GETLBTEXTLEN,
>                                i, 0);
>    ch = new char[txtLength];
>    SendMessage(cbHandle, CB_GETLBTEXT, i, ch)
>    string itemText = new String(ch);
> }
> // to fill a comboBox with retrieved items
> listBox3.Items.Add(itemText);

> (Hope I did not forgot essential codelines).

> Thanks

> T. La Benche

> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!



Sun, 27 Mar 2005 20:45:58 GMT  
 HELP: SendMessage and CB_GETLBTEXT in C#
Nicolas,

great tip. Functioning well.

Thanks a lot. Now I can get busy to my main stuff.

T. LaBenche

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



Sun, 27 Mar 2005 23:17:10 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. C++ SendMessage equivalent in C# ???

2. SendMessage with C#

3. SendMessage and C# controls

4. Newbie: pls help with SendMessage

5. SendMessage for PopUP Help

6. Sendmessage HELP !!

7. Help:PostMessage Acts Like SendMessage in Release Mode

8. Help! SendMessage not working

9. SendMessage Help

10. SendMessage Help - Repost with correct user info

11. SendMessage Help Repost with correct user info

12. Help on SendMessage between 16 bits And 32 Bits Applications

 

 
Powered by phpBB® Forum Software