sp0rk Posted August 13, 2004 Share Posted August 13, 2004 Is it possible to add stuff to another program's listbox using API? If so, can you provide me with example code? Thanks. Link to comment Share on other sites More sharing options...
0 Andareed Posted August 13, 2004 Share Posted August 13, 2004 You have to use win32 api. Check SendMessage + LB_ADDSTRING. Link to comment Share on other sites More sharing options...
0 lexecutil Posted August 13, 2004 Share Posted August 13, 2004 (edited) 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 August 13, 2004 by dannyres Link to comment Share on other sites More sharing options...
0 sp0rk Posted August 15, 2004 Author Share Posted August 15, 2004 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 More sharing options...
0 azcodemonkey Posted August 15, 2004 Share Posted August 15, 2004 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 More sharing options...
0 sp0rk Posted August 15, 2004 Author Share Posted August 15, 2004 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 More sharing options...
0 azcodemonkey Posted August 15, 2004 Share Posted August 15, 2004 Try it. It looks like it would work... :whistle: <crosses fingers> Serioulsy, though, just step through it with the debugger. Link to comment Share on other sites More sharing options...
0 sp0rk Posted August 15, 2004 Author Share Posted August 15, 2004 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 More sharing options...
0 azcodemonkey Posted August 15, 2004 Share Posted August 15, 2004 Is anything being underlined or highlighted in the Tasks window? Link to comment Share on other sites More sharing options...
0 sp0rk Posted August 15, 2004 Author Share Posted August 15, 2004 No, the debug box just says: The type or namespace name 'DllImport' could not be found (are you missing a using directive or an assembly reference?) Link to comment Share on other sites More sharing options...
0 azcodemonkey Posted August 15, 2004 Share Posted August 15, 2004 Ah, you need to have a using that imports System.Runtime.InteropServices Link to comment Share on other sites More sharing options...
0 sp0rk Posted August 15, 2004 Author Share Posted August 15, 2004 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 More sharing options...
0 azcodemonkey Posted August 15, 2004 Share Posted August 15, 2004 Do you have Spy++ installed on your system? You can use it to find window classes. Link to comment Share on other sites More sharing options...
0 sp0rk Posted August 15, 2004 Author Share Posted August 15, 2004 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 More sharing options...
Question
sp0rk
Is it possible to add stuff to another program's listbox using API? If so, can you provide me with example code?
Thanks.
Link to comment
Share on other sites
13 answers to this question
Recommended Posts