M
ME
I need to be able to get the list from a combobox using PInvoke. This code
works fine for a windows form application however I need to read the
combobox that is on an HTML page (inside iexplorer, the class name for the
box is "Internet Explorer_TridentCmboBx" ). When I read the combobox from
IE it will accurately get the count, but the text of each item returns non
printable ascii characters.
What am I doing wrong?
Thanks,
Matt
//C#
//------------------------------ CODE ---------------------
public const UInt32 CB_GETCOUNT = 0x0146;
public const UInt32 CB_GETCURSEL = 0x0147;
public const UInt32 CB_GETLBTEXT = 0x0148;
public const UInt32 CB_GETLBTEXTLEN = 0x0149;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam,
IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam,
StringBuilder lParam);
public static List<ListItemVals> GetListItems(IntPtr controlPointer, object
sender)
{
int count = 0;
List<ListItemVals> retVal = new List<ListItemVals>();
ListItemVals item = new ListItemVals();
IntPtr zero = new IntPtr(0);
//Get the count of items in the list
IntPtr ptr = SendMessage(GetHandleRef(controlPointer, sender), CB_GETCOUNT,
zero, zero);
count = ptr.ToInt32();
for (int i = 0; i < count; i++)
{
item = GetValueFromCombo(controlPointer, sender, i);
retVal.Add(item);
}
return retVal;
}
public static ListItemVals GetValueFromCombo(IntPtr pointer, object sender,
int index)
{
//List<ListItemVals> retVal = new List<ListItemVals>();
int len = 0;
StringBuilder sb = new StringBuilder();
ListItemVals item = new ListItemVals();
IntPtr wParam = new IntPtr(index);
IntPtr zero = new IntPtr(0);
//Get the count of items in the list
IntPtr ptr = SendMessage(GetHandleRef(pointer, sender), CB_GETLBTEXTLEN ,
wParam, zero);
len = ptr.ToInt32();
sb.Capacity = len;
IntPtr txtPtr = SendMessage(GetHandleRef(pointer, sender), CB_GETLBTEXT,
wParam, sb);
//Get the list item
item.textValue = sb.ToString();
item.itemValue = wParam.ToInt32();
//return the value
return item;
}
works fine for a windows form application however I need to read the
combobox that is on an HTML page (inside iexplorer, the class name for the
box is "Internet Explorer_TridentCmboBx" ). When I read the combobox from
IE it will accurately get the count, but the text of each item returns non
printable ascii characters.
What am I doing wrong?
Thanks,
Matt
//C#
//------------------------------ CODE ---------------------
public const UInt32 CB_GETCOUNT = 0x0146;
public const UInt32 CB_GETCURSEL = 0x0147;
public const UInt32 CB_GETLBTEXT = 0x0148;
public const UInt32 CB_GETLBTEXTLEN = 0x0149;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam,
IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam,
StringBuilder lParam);
public static List<ListItemVals> GetListItems(IntPtr controlPointer, object
sender)
{
int count = 0;
List<ListItemVals> retVal = new List<ListItemVals>();
ListItemVals item = new ListItemVals();
IntPtr zero = new IntPtr(0);
//Get the count of items in the list
IntPtr ptr = SendMessage(GetHandleRef(controlPointer, sender), CB_GETCOUNT,
zero, zero);
count = ptr.ToInt32();
for (int i = 0; i < count; i++)
{
item = GetValueFromCombo(controlPointer, sender, i);
retVal.Add(item);
}
return retVal;
}
public static ListItemVals GetValueFromCombo(IntPtr pointer, object sender,
int index)
{
//List<ListItemVals> retVal = new List<ListItemVals>();
int len = 0;
StringBuilder sb = new StringBuilder();
ListItemVals item = new ListItemVals();
IntPtr wParam = new IntPtr(index);
IntPtr zero = new IntPtr(0);
//Get the count of items in the list
IntPtr ptr = SendMessage(GetHandleRef(pointer, sender), CB_GETLBTEXTLEN ,
wParam, zero);
len = ptr.ToInt32();
sb.Capacity = len;
IntPtr txtPtr = SendMessage(GetHandleRef(pointer, sender), CB_GETLBTEXT,
wParam, sb);
//Get the list item
item.textValue = sb.ToString();
item.itemValue = wParam.ToInt32();
//return the value
return item;
}