Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Strange EnumChildWindows issue

Strange EnumChildWindows issue

Scheduled Pinned Locked Moved C / C++ / MFC
help
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Maxwell Chen
    wrote on last edited by
    #1

    When I declare a LVFINDINFO object in the function body, EnumChildWindows fails to locate the control of SysListView32 type (a Windows class type). When I remove the LVFINDINFO object from the body, it works well. The code snippet is as the below.

    void CMainDlg::OnBnClickedGetDolby()
    {
    // Kill any running window if any.
    KillRunningApplet(_T("Sound"));

    // Launch it.
    if(!LaunchCplApplet(\_T("mmsys.cpl"), 0, 0)) {
    	OutputDebugString(\_T("LaunchCplApplet fails. \\n"));
    	return;
    }
    
    // Make sure the applet pops up.
    CWnd\* pWnd = NULL;
    int i = 0;
    while(!pWnd) {
    	pWnd = FindWindow(NULL, \_T("Sound"));
    	if(pWnd) {
    		OutputDebugString(\_T("Found Sound window. \\n"));
    		break;
    	}
    	Sleep(10);
    	i++;
    	if(i > 1000) {
    		OutputDebugString(\_T("Sound window is not found. \\n"));
    		return;
    	}
    }
    
    // ---
    m\_pListView = NULL;
    EnumChildWindows(pWnd->m\_hWnd, DoSomethingHelper, (LPARAM)this);
    
    if(m\_pListView) {
    	OutputDebugString(\_T("Got SysListView32"));
    }
    else {
    	OutputDebugString(\_T("SysListView32 is not found."));
    	return;
    }
    m\_pListView->SendMessage(WM\_KEYDOWN, VK\_DOWN, 0);
    
    CListView\* pView = (CListView\*)m\_pListView;
    CListCtrl& Ctrl = pView->GetListCtrl();
    
    LVFINDINFO info;  // <--- This line fails SysListView32.
    int iIndex = 0;
    
    info.flags = LVFI\_PARTIAL | LVFI\_STRING;
    info.psz = \_T("Speaker");
    

    /*

    while ((iIndex = Ctrl.FindItem(&info)) != -1) {
    	OutputDebugString(\_T("Speaker-like entry is found."));
    	CString s = Ctrl.GetItemText(iIndex, 0);
    	OutputDebugString(s);
    }
    

    */

    }

    BOOL CALLBACK DoSomethingHelper(HWND hwnd, LPARAM lParam)
    {
    TCHAR sClassName[MAX_PATH] = {0};
    GetClassName(hwnd, sClassName, MAX_PATH);
    if(wcscmp(sClassName, _T("SysListView32")) == 0) {
    CMainDlg* pMain = (CMainDlg*)lParam;
    pMain->m_pListView = CWnd::FromHandle(hwnd);
    }
    OutputDebugString(sClassName);
    return TRUE;
    }

    Maxwell Chen

    M C 2 Replies Last reply
    0
    • M Maxwell Chen

      When I declare a LVFINDINFO object in the function body, EnumChildWindows fails to locate the control of SysListView32 type (a Windows class type). When I remove the LVFINDINFO object from the body, it works well. The code snippet is as the below.

      void CMainDlg::OnBnClickedGetDolby()
      {
      // Kill any running window if any.
      KillRunningApplet(_T("Sound"));

      // Launch it.
      if(!LaunchCplApplet(\_T("mmsys.cpl"), 0, 0)) {
      	OutputDebugString(\_T("LaunchCplApplet fails. \\n"));
      	return;
      }
      
      // Make sure the applet pops up.
      CWnd\* pWnd = NULL;
      int i = 0;
      while(!pWnd) {
      	pWnd = FindWindow(NULL, \_T("Sound"));
      	if(pWnd) {
      		OutputDebugString(\_T("Found Sound window. \\n"));
      		break;
      	}
      	Sleep(10);
      	i++;
      	if(i > 1000) {
      		OutputDebugString(\_T("Sound window is not found. \\n"));
      		return;
      	}
      }
      
      // ---
      m\_pListView = NULL;
      EnumChildWindows(pWnd->m\_hWnd, DoSomethingHelper, (LPARAM)this);
      
      if(m\_pListView) {
      	OutputDebugString(\_T("Got SysListView32"));
      }
      else {
      	OutputDebugString(\_T("SysListView32 is not found."));
      	return;
      }
      m\_pListView->SendMessage(WM\_KEYDOWN, VK\_DOWN, 0);
      
      CListView\* pView = (CListView\*)m\_pListView;
      CListCtrl& Ctrl = pView->GetListCtrl();
      
      LVFINDINFO info;  // <--- This line fails SysListView32.
      int iIndex = 0;
      
      info.flags = LVFI\_PARTIAL | LVFI\_STRING;
      info.psz = \_T("Speaker");
      

      /*

      while ((iIndex = Ctrl.FindItem(&info)) != -1) {
      	OutputDebugString(\_T("Speaker-like entry is found."));
      	CString s = Ctrl.GetItemText(iIndex, 0);
      	OutputDebugString(s);
      }
      

      */

      }

      BOOL CALLBACK DoSomethingHelper(HWND hwnd, LPARAM lParam)
      {
      TCHAR sClassName[MAX_PATH] = {0};
      GetClassName(hwnd, sClassName, MAX_PATH);
      if(wcscmp(sClassName, _T("SysListView32")) == 0) {
      CMainDlg* pMain = (CMainDlg*)lParam;
      pMain->m_pListView = CWnd::FromHandle(hwnd);
      }
      OutputDebugString(sClassName);
      return TRUE;
      }

      Maxwell Chen

      M Offline
      M Offline
      Maxwell Chen
      wrote on last edited by
      #2

      Okay, never mind. Constructing the LVFINDINFO object takes some time so EnumChildWindows takes longer time to complete. So I added a Sleep(10) after EnumChildWindows call to make it happy.

      Maxwell Chen

      1 Reply Last reply
      0
      • M Maxwell Chen

        When I declare a LVFINDINFO object in the function body, EnumChildWindows fails to locate the control of SysListView32 type (a Windows class type). When I remove the LVFINDINFO object from the body, it works well. The code snippet is as the below.

        void CMainDlg::OnBnClickedGetDolby()
        {
        // Kill any running window if any.
        KillRunningApplet(_T("Sound"));

        // Launch it.
        if(!LaunchCplApplet(\_T("mmsys.cpl"), 0, 0)) {
        	OutputDebugString(\_T("LaunchCplApplet fails. \\n"));
        	return;
        }
        
        // Make sure the applet pops up.
        CWnd\* pWnd = NULL;
        int i = 0;
        while(!pWnd) {
        	pWnd = FindWindow(NULL, \_T("Sound"));
        	if(pWnd) {
        		OutputDebugString(\_T("Found Sound window. \\n"));
        		break;
        	}
        	Sleep(10);
        	i++;
        	if(i > 1000) {
        		OutputDebugString(\_T("Sound window is not found. \\n"));
        		return;
        	}
        }
        
        // ---
        m\_pListView = NULL;
        EnumChildWindows(pWnd->m\_hWnd, DoSomethingHelper, (LPARAM)this);
        
        if(m\_pListView) {
        	OutputDebugString(\_T("Got SysListView32"));
        }
        else {
        	OutputDebugString(\_T("SysListView32 is not found."));
        	return;
        }
        m\_pListView->SendMessage(WM\_KEYDOWN, VK\_DOWN, 0);
        
        CListView\* pView = (CListView\*)m\_pListView;
        CListCtrl& Ctrl = pView->GetListCtrl();
        
        LVFINDINFO info;  // <--- This line fails SysListView32.
        int iIndex = 0;
        
        info.flags = LVFI\_PARTIAL | LVFI\_STRING;
        info.psz = \_T("Speaker");
        

        /*

        while ((iIndex = Ctrl.FindItem(&info)) != -1) {
        	OutputDebugString(\_T("Speaker-like entry is found."));
        	CString s = Ctrl.GetItemText(iIndex, 0);
        	OutputDebugString(s);
        }
        

        */

        }

        BOOL CALLBACK DoSomethingHelper(HWND hwnd, LPARAM lParam)
        {
        TCHAR sClassName[MAX_PATH] = {0};
        GetClassName(hwnd, sClassName, MAX_PATH);
        if(wcscmp(sClassName, _T("SysListView32")) == 0) {
        CMainDlg* pMain = (CMainDlg*)lParam;
        pMain->m_pListView = CWnd::FromHandle(hwnd);
        }
        OutputDebugString(sClassName);
        return TRUE;
        }

        Maxwell Chen

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #3

        Maxwell Chen wrote:

        pMain->m_pListView = CWnd::FromHandle(hwnd);

        Since CWnd::FromHandle documentation [^] states:

        The pointer may be temporary and should not be stored for later use.

        I would use instead something like

        pMain->m_hListView = hwnd;

        and then obtain the CWnd pointer inside the CMainDlg::OnBnClickedGetDolby body. Anyway I really don't know if it would help. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        M 1 Reply Last reply
        0
        • C CPallini

          Maxwell Chen wrote:

          pMain->m_pListView = CWnd::FromHandle(hwnd);

          Since CWnd::FromHandle documentation [^] states:

          The pointer may be temporary and should not be stored for later use.

          I would use instead something like

          pMain->m_hListView = hwnd;

          and then obtain the CWnd pointer inside the CMainDlg::OnBnClickedGetDolby body. Anyway I really don't know if it would help. :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          M Offline
          M Offline
          Maxwell Chen
          wrote on last edited by
          #4

          CPallini wrote:

          pMain->m_hListView = hwnd;

          Thanks! It resolved the timing problem. :-D

          Maxwell Chen

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups