CFindInFiles Class (similar to Visual Studio 6.0)

Environment: Visual C++ 6.0

I recently had to add Find in Files functionality similar to that found in Visual C++ 6.0 to an application of mine. I have posted the work here in the hope that some of you might find it useful. I have packaged the classes in a DLL to ease integration with your own project.

The find in files operation runs in its own thread to make it possible to cancel a lengthy search operation. Follow these steps to add Find in Files to your MFC application:

Step 1: Create a window were CFindInFiles can write the results of the search

This window must accept LB_ADDSTRING messages. The LB_ADDSTRING is sent by CFindInFiles to your window to report the results of the find in files operation. The reason I use the LB_ADDSTRING is that used to use a list box in the output window. I have later replaced the list box with an edit control but I am still using LB_ADDSTRING to be compatible with earlier versions. Have a look at the included demo project for an example on how I did this.

Step 2: Add command handlers to start/stop the find in files operation

void CMainFrame::OnEditFindinfiles() 
{
  if (m_hStop == INVALID_HANDLE_VALUE)
      m_hStop = CreateEvent(NULL, TRUE, FALSE, NULL);
  if (m_bFindInFiles)
      CancelFindInFiles();
  else
      StartFindInFiles();
}

void CMainFrame::CancelFindInFiles()
{
  DWORD dwExit;

  SetEvent(m_hStop);
  // Wait for the find in files thread to exit and 
  // process windows messages while waiting:
  while (m_pFindThread &&
         GetExitCodeThread(m_pFindThread->m_hThread,
                           &dwExit))
  {
    MSG msg;
    if (dwExit != STILL_ACTIVE)
        break;
    theApp.OnIdle(0);
    while (::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
    { 
        if (!theApp.PumpMessage())
        { 
            ::PostQuitMessage(0); 
            return; 
        }
    }
  }
  ResetEvent(m_hStop);
  m_bFindInFiles = false;
}

void CMainFrame::StartFindInFiles()
{
    m_pFindThread =
         StartFindThread(m_wndOutputBar.GetFindHwnd(),
                         m_hStop);
    m_bFindInFiles = true;
}

void CMainFrame::OnUpdateEditFindinfiles(CCmdUI* pCmdUI) 
{
  if (m_pFindThread != NULL)
  {
    DWORD dwExit;
    GetExitCodeThread(m_pFindThread->m_hThread, &dwExit);
    if (dwExit != STILL_ACTIVE)
    {
       delete m_pFindThread;
       m_pFindThread = NULL;
       m_bFindInFiles = false;
    }
  }
  pCmdUI->SetCheck(m_pFindThread != NULL);
}

Again, have a look at the accompanying demo program for a complete example.

Downloads

Download source and demo project - 70Kb


Comments

  • Re Assertion Bug Solution

    Posted by Legacy on 12/30/2003 12:00am

    Originally posted by: Kisumu Kinshasa

    Re Assertion Bug:
    
    

    While this is an excellent little utility, can you please
    supply the .lib code. Perhaps I can help to clear up the
    problem.

    Thank You

    Reply
  • Small bug

    Posted by Legacy on 09/30/2003 12:00am

    Originally posted by: Avri

    Hi,
    
    

    Nice class :)
    I have found a small bug in the stop event:

    UINT CFindInFiles::FindThread(LPVOID pParam)
    {
    struct FindStruct* pFindStruct = static_cast<struct FindStruct*>(pParam);
    CFindInFiles fif(pFindStruct->strFolder, pFindStruct->strTypes, pFindStruct->nFlags);
    HANDLE hStop = pFindStruct->hStop; /* added */
    CString strFindWhat = pFindStruct->strFindWhat;
    HWND hWnd = pFindStruct->hWnd;
    SetEvent(pFindStruct->hStart);
    fif.Find(strFindWhat, hWnd, hStop /* changed*/);
    return 0;
    }

    you can't use the structure after the start event has been set.

    Thank's

    Avri.

    Reply
  • good programming - but...

    Posted by Legacy on 04/21/2002 12:00am

    Originally posted by: Mike

    good coding!
    but the match-case is always on, even if the checkbox isn't checked.
    and You don't close the event-handle 'CMainFrame::m_hStop' (ResourceLeak).

    Reply
  • Debug Assertion

    Posted by Legacy on 04/15/2002 12:00am

    Originally posted by: Tester

    I build demo project, run and click Find In Files --> Debug Assertion window.
    Help me to solve it.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Live Event Date: November 13, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT APIs can be a great source of competitive advantage. The practice of exposing backend services as APIs has become pervasive, however their use varies widely across companies and industries. Some companies leverage APIs to create internal, operational and development efficiencies, while others use them to drive ancillary revenue channels. Many companies successfully support both public and private programs from the same API by varying levels …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds