Thread: read a listbox in another app

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    170

    Question read a listbox in another app

    Thanks to the help I have received so far, I now am using FindWindow and FindwindowEx to get my listbox. The problem now is when I try and send a message it fails an IsWindow assert.

    I decided to bypass the Cwnd::SendMessage which does the assert and went with ::SendMessage which takes the hwnd asa parameter. This works, (doesn't give me an error) but does not return any usefull info.



    Code:
    HWND ChildWnd,Wnd;
    
    Cwnd *pParentWnd = FindWindow("Afx:40000:b...", NULL);
    ChildWnd=FindWindowEx( pParentWnd->m_hWnd,Null,"#32770",NULL);
    Wnd=FindWindowEx( ChildWnd, NULL, "ListBox", NULL);
    
    pDC-> TextOut(1,1,(CString)::SendMessage(Wnd,LB_GETTEXT,
    0,0));
    This just prints a block character.

    Any further help would be greatly appreciated.
    Best Regards,

    Bonkey

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Please dont duplicate posts...its against board rules and will earn you no friends........

    The problem you have is the the return from LB_GETTEXT gives the number of chars....not the chars themselves

    Anyway.....

    Personally, I like MFC, but when I am doing this kind of stuff I prefer to use straight API calls....the MFC functions go through loads of ASSERTS before calling APIS, and as MFC was not really made to be used to break into other windows controlled by other processes its not the tool for the job!

    I wrote this to look at the first entry of a list in a dialog based app I wrote....

    Code:
    #include <windows.h>
    #include <iostream>
    using namespace std;
    
    
    int main(void)
    {
    
    	HWND hMainWnd = FindWindow(NULL,"WinXRay");
    	if(!hMainWnd){
    		MessageBox(NULL,"Error with Main Wnd!",NULL,MB_OK);
    		return 1;
    	}
    
    	HWND hListWnd = FindWindowEx(hMainWnd,NULL,"ListBox",NULL);
    	if(!hListWnd){
    		MessageBox(NULL,"Error with Child Wnd!",NULL,MB_OK);
    		return 1;
    	}
    
    	DWORD dwCount = SendMessage(hListWnd,LB_GETTEXTLEN,0,0);
    	if(dwCount){
    		char* lpBuff = new char[dwCount+1];
    		if(!lpBuff){
    			MessageBox(NULL,"Error with memory!",NULL,MB_OK);
    			return 1;
    		}
    		if(LB_ERR != SendMessage(hListWnd,LB_GETTEXT,0,(LPARAM)lpBuff)){
    			cout << "First item in ListBox " << lpBuff << endl;
    		}
    		delete [] lpBuff;
    	}
    
    
      return 0;
    }
    That's faily simple, but the complexity comes in where you have layeres of child windows and more than 1 child on a layer shares a windows class....then you need to compose some sort of logic to loop through the childeren until you find the one you need......in such a case - check the first child with FindWindowEx() with the second param to NULL..save the HWND...if that child isnt the one you need, go back, but this time use the previous call's HWND as the second param....that will then allow you to cycle through the childeren that share a class.......

    I cant give you the anser as I dont have your details...but it shouldnt be difficult

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    170

    Talking

    Thanks for your help.

    I knew the thing about not reposting, but someone suggested I should be in this folder. I didn't mean to offend. I am extremely appreciative of all the help I've received.

    This seems to work. Still in MFC. I took everything from dwCount on. It is only giving me the first entry. I will try and get the rest figured out on my own.

    The only other issue is that the I will have to find a better way ot find the main window. It is a temporary Afx number that changes each time the app runs. I am hoping the FindWindowEx will help me get past that.

    Again thanks for all the help. You will probably see me around here from now on.
    Best Regards,

    Bonkey

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    >>The only other issue is that the I will have to find a better way ot find the main window. It is a temporary Afx number that changes each time the app runs. I am hoping the FindWindowEx will help me get past that.


    Yeah...not perfect a solution

    There are other ways to find window.....you can FindWindows() by title as I did....or a brute force method by using EnumWindows()...the you get a HWND to each window.......you then apply some testing until you get the windows you need

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    Getting the other lines of text was easy enough. I just iterate the third paramater.
    Best Regards,

    Bonkey

  6. #6
    Registered User
    Join Date
    Aug 2002
    Posts
    170

    Cool

    Got it all working. Thanks again.

    I wanted to post a note of advice for anyone using EnumWindows for the lParam pass (this) to have access to your variables. Otherwise its a pain.
    Best Regards,

    Bonkey

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "sorting news" assignment
    By prljavibluzer in forum C Programming
    Replies: 7
    Last Post: 02-06-2008, 06:45 AM
  2. How to cast a ListBox item to an int for a switch statment?
    By Swaine777 in forum C++ Programming
    Replies: 8
    Last Post: 09-26-2004, 08:52 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Getting FULL filename from listbox
    By Garfield in forum Windows Programming
    Replies: 8
    Last Post: 01-27-2002, 08:28 AM
  5. Help! Can't read decimal number
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-07-2001, 02:09 AM