CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2014
    Location
    Las Vegas, NV
    Posts
    85

    [RESOLVED] EnumChildWindows & FindWindow doesn't work MFC but works Console

    Hello
    I'm trying to get the message from a static text in another application. I have this code which works perfectly in console application:
    Code:
    BOOL CALLBACK EnumChildProc2(HWND hwnd, LPARAM lParam) {
    	counter2++;
    	if (counter2 == 6) // counter 6 is the static text child
    	{
    
    		cout << "hwnd_Child 6 = " << hwnd << endl;
    		HWND hand = (HWND)hwnd;
    
    		char szBuf[2048];
    		LONG lResult;
    		lResult = SendMessage(hwnd, WM_GETTEXT, sizeof(szBuf) / sizeof(szBuf[0]), (LPARAM)szBuf);
    		printf("%s", szBuf); // it prints the message correctly
    	}
    	return TRUE; 
    }
    
    int main() {
    EnumChildWindows(FindWindow("#32770", "Unable to Write"), EnumChildProc2, 0);
    }
    It works very well in console application but it seems that bool callback function doesn't work as i need in MFC application . This is the code i used in MFC:

    Code:
    BOOL CALLBACK EnumChildProc2(HWND hwnd, LPARAM lParam) {
    	counter_Child6++;
    	if (counter_Child6 == 6)
    	{
    		HWND hand = (HWND)hwnd;
    		
    		char szBuf[2048];
    		LONG lResult;
    		lResult = SendMessage(hwnd, WM_GETTEXT, sizeof(szBuf) / sizeof(szBuf[0]), (LPARAM)szBuf);
    		CString ssString;
    		ssString.Format(_T("%s"), szBuf);
    		AfxMessageBox(ssString);
    	}
    	return TRUE; 
    }
    
    CWnd *pWnd = FindWindow(_T("#32770"), _T("Unable to Write"));
    	if (NULL != pWnd)
    	{
    		listControl1.InsertItem(0, _T("Window Exists"));
    		EnumChildWindows((HWND)pWnd, EnumChildProc2, 0); //this function doesn't afxmessagebox anything
    	}
    	else
    	{
    		listControl1.InsertItem(0, _T("Does not Exists"));
    	}
    Any help would be greatly appreciated! Thanks

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: EnumChildWindows & FindWindow doesn't work MFC but works Console

    Code:
    CWnd *pWnd = FindWindow(_T("#32770"), _T("Unable to Write"));
    FindWindow() returns type HWND, not CWnd*

    If you want to use the mfc FindWindow() which does return a CWnd*, then you need

    Code:
    CWnd *pWnd = CWnd::FindWindow(_T("#32770"), _T("Unable to Write"));
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: EnumChildWindows & FindWindow doesn't work MFC but works Console

    Quote Originally Posted by 2kaud View Post
    FindWindow() returns type HWND, not CWnd*

    If you want to use the mfc FindWindow() which does return a CWnd*, then you need

    Code:
    CWnd *pWnd = CWnd::FindWindow(_T("#32770"), _T("Unable to Write"));
    ...unless you call it from the class that is derived from CWnd. In that case, you would need to specify global scope to get the HWND version:
    Code:
    HWND hWnd = ::FindWindow(_T("#32770"), _T("Unable to Write"));
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  4. #4
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: EnumChildWindows & FindWindow doesn't work MFC but works Console

    Quote Originally Posted by eclessiastes View Post
    Code:
    BOOL CALLBACK EnumChildProc2(HWND hwnd, LPARAM lParam) {
    	counter_Child6++;
    Any help would be greatly appreciated! Thanks
    What is counter_Child6++? Where is it defined? How is it initialized?
    Did you debug your EnumChildProc2() function? Is it called? How many times? Looks like counter_Child6 is never equal to 6...
    Also, you shouldn't rely on the sequential index of the static control; what if the order changes or another control is inserted?
    Besides, you don't need to enumerate windows if you are not checking any of their properties.
    Check out GetWindow function.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  5. #5
    Join Date
    Sep 2014
    Location
    Las Vegas, NV
    Posts
    85

    Re: EnumChildWindows & FindWindow doesn't work MFC but works Console

    Quote Originally Posted by VladimirF View Post
    ...unless you call it from the class that is derived from CWnd. In that case, you would need to specify global scope to get the HWND version:
    Code:
    HWND hWnd = ::FindWindow(_T("#32770"), _T("Unable to Write"));
    I changed
    Code:
    CWnd *pWnd = CWnd::FindWindow(_T("#32770"), _T("Unable to Write"));
    with
    Code:
    HWND hWnd = ::FindWindow(_T("#32770"), _T("Unable to Write"));
    now it works perfectly! I've been up 2 days for this
    Thank you Vladimir

  6. #6
    Join Date
    Sep 2014
    Location
    Las Vegas, NV
    Posts
    85

    Re: [RESOLVED] EnumChildWindows & FindWindow doesn't work MFC but works Console

    One last question: I'm trying to change static text from bool callback function but i get error: "Encountered an improper argument"
    CMFCApplication1Dlg* dlsg;
    dlsg->status.SetWindowTextW(L"Status: Working..");
    Any idea how to access main dialog controls from bool callback ? Thank you

  7. #7
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: [RESOLVED] EnumChildWindows & FindWindow doesn't work MFC but works Console

    only a handfull of messages support sending data between programs like this.
    SetWIndowText internally calls SendWindow with as parameter the pointer to your string data.
    that pointer value is entirely meaningless in the other application.

    THe work around is somewhat involving...
    allocate memory in the other process (VirtualAllocEx)
    copy the string contents into that memory in the other process (WriteProcessMemory)
    SendMessage with the v-pointer of that other process
    clean up the allocated memory. (VirtualFreeEx)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured