winapireplacecommon-dialog

can't make windows common find dialog working


I really do not understand these examples from the web. They're all fragmentary. There is nowhere a simple concise example how to make a classic find text dialog.

I put what I know into this , but is is not showing any window and returns: 2147500037 0x80004005

#include <windows.h>
#include <iostream>
#include <iomanip>

int main() {
  using namespace std;
  UINT uFindReplaceMsg;  // message identifier for FINDMSGSTRING 
  uFindReplaceMsg = RegisterWindowMessage(FINDMSGSTRING);
  wstring search_str = L"text to search";
  HWND findDialog = NULL;
  wchar_t szFindWhat[MAX_PATH];
  FINDREPLACEW fr;
  ZeroMemory( & fr, sizeof( FINDREPLACEW ) );
  fr.lStructSize = sizeof( FINDREPLACEW );
  fr.hwndOwner = NULL;
  fr.lpstrFindWhat = szFindWhat;
  fr.wFindWhatLen = MAX_PATH;
  findDialog = FindTextW(&fr);
  cout << GetLastError() << endl;
  cout << hex << GetLastError() << endl;
}

Could you provide me with code that works so I can build from that.


Solution

  • You're not checking the return result from FindTextW. That is, you have:

    findDialog = FindTextW(&fr);
    cout << GetLastError() << endl;
    

    If the function succeeds, the result is a handle. If the function fails, the return value is NULL.

    According to the documentation:

    If the function fails, the return value is NULL. To get extended error information, call the CommDlgExtendedError function. CommDlgExtendedError may return one of the following error codes:

    In other words, GetLastError isn't going to tell you anything meaningful. Call CommDlgExtendedError after you check the handle to see if it's NULL.