• 0

[VB] Adding to listbox via API?


Question

13 answers to this question

Recommended Posts

  • 0

Todo this you will probably have to go into that processes address space using hooking. I'll check this now. :)

Dan

EDIT: Checked up on it, and its actually quite simple.. just find the window you want with FindWindow/FindWindowEx and then use SendMessage to send the LB_ADDSTRING message. Heres an example of what todo in C#:

//Decare the API's we need..
 ?[DllImport("user32.dll", CharSet=CharSet.Auto)]
 ?private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);

 ?[DllImport("user32.dll", CharSet=CharSet.Auto)]
 ?private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

 ?[DllImport("user32.dll", CharSet=CharSet.Auto)]
 ?private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

  private void button4_Click(object sender, System.EventArgs e)
  {
 	 //Find main window
 	 IntPtr ptr = FindWindow("SomeClass", "SomeWindow");
 	 //find listbox
 	 IntPtr ptr2 = FindWindowEx(ptr, IntPtr.Zero, "SomeClass", "SomeWindow");

 	 if (ptr2 != IntPtr.Zero)
 	 {
    //add the item
    int result = SendMessage(ptr2, 0x180 /*LB_ADDSTRING*/, IntPtr.Zero, "TextToAdd");
    //check for failiure
    if (result == -1 /*LB_ERR*/ | result == -2 /*LB_ERRSPACE*/)
    {
   	 MessageBox.Show("Failed!");
    }
 	 }
  }

http://msdn.microsoft.com/library/default.....asp?frame=true

Edited by dannyres
Link to comment
Share on other sites

  • 0

I've never used C#

The code to find the listbox for VB:

Dim aolframe As Long, mdiclient As Long, aolchild As Long
Dim aollistbox As Long
aolframe = FindWindow("aol frame25", vbNullString)
mdiclient = FindWindowEx(aolframe, 0&, "mdiclient", vbNullString)
aolchild = FindWindowEx(mdiclient, 0&, "aol child", vbNullString)
aollistbox = FindWindowEx(aolchild, 0&, "_aol_listbox", vbNullString)

So what would I put for the C# code?

Thanks

Link to comment
Share on other sites

  • 0
I've never used C#

The code to find the listbox for VB:

Dim aolframe As Long, mdiclient As Long, aolchild As Long
Dim aollistbox As Long
aolframe = FindWindow("aol frame25", vbNullString)
mdiclient = FindWindowEx(aolframe, 0&, "mdiclient", vbNullString)
aolchild = FindWindowEx(mdiclient, 0&, "aol child", vbNullString)
aollistbox = FindWindowEx(aolchild, 0&, "_aol_listbox", vbNullString)

So what would I put for the C# code?

Thanks

IntPtr aolframe;
IntPtr mdiclient;
IntPtr aolchild;
IntPtr aollistbox;

aolframe = FindWindow("aol frame25", String.Empty);

mdiclient = FindWindowEx(aolframe, IntPtr.Zero, "mdiclient", String.Empty);

aolchild = FindWindowEx(mdiclient, IntPtr.Zero, "aol child", String.Empty);

aollistbox = FindWindowEx(aolchild, IntPtr.Zero, "_aol_listbox", String.Empty);

Link to comment
Share on other sites

  • 0

Okay, thank you. So this would be the final code?:

//Decare the API's we need..
 [DllImport("user32.dll", CharSet=CharSet.Auto)]
 private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);

 [DllImport("user32.dll", CharSet=CharSet.Auto)]
 private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

 [DllImport("user32.dll", CharSet=CharSet.Auto)]
 private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

 private void button4_Click(object sender, System.EventArgs e)
 {
  IntPtr aolframe;
  IntPtr mdiclient;
  IntPtr aolchild;
  IntPtr aollistbox;

  aolframe = FindWindow("aol frame25", String.Empty);
  mdiclient = FindWindowEx(aolframe, IntPtr.Zero, "mdiclient", String.Empty);
  aolchild = FindWindowEx(mdiclient, IntPtr.Zero, "aol child", String.Empty);
  aollistbox = FindWindowEx(aolchild, IntPtr.Zero, "_aol_listbox", String.Empty);

  if (aollistbox != IntPtr.Zero)
  {
   //add the item
   int result = SendMessage(aollistbox, 0x180 /*LB_ADDSTRING*/, IntPtr.Zero, "TextToAdd");
   //check for failiure
   if (result == -1 /*LB_ERR*/ | result == -2 /*LB_ERRSPACE*/)
   {
    MessageBox.Show("Failed!");
   }
  }
 }

Link to comment
Share on other sites

  • 0

Well apparently there's something wrong with all of that. The whole top part -- the "[DllImport..." stuff is getting errors. I'm using Microsoft Visual C# .Net with the 1.1 framework. Do I need to update my framework?

Thanks for any help.

Link to comment
Share on other sites

  • 0

Okay so I put "using System.Runtime.InteropServices;" at the top and it runs fine, but it's not doing anything. I'm going to mess around with the window classes and names, because that's the only thing I know how to mess with.

If you guys have any more suggestions, please post!

Thanks.

Link to comment
Share on other sites

  • 0

I used Pat or JK's API Spy to find the window classes for the AOL listbox, because AOL is sort of tricky. I'm thinking maybe this just won't work with AOL, so I'm going to try it on a more standard listbox, like the AIM window right now.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.