0

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.

rsk82
  • 28,217
  • 50
  • 150
  • 240
  • According to the sample at http://msdn.microsoft.com/en-us/library/windows/desktop/ms646829(v=vs.85).aspx#finding_text, your application has to process FINDMSGSTRING messages. You set that up by calling `RegisterWindowMessage`. See the sample. – Jim Mischel Mar 14 '13 at 15:14
  • There are a litany of requirements for using the **modeless** common dialog managed with [`FindText()`](http://msdn.microsoft.com/en-us/library/windows/desktop/ms646918(v=vs.85).aspx), many of which you're not meeting. I suggest you review the specifics of that API. – WhozCraig Mar 14 '13 at 15:15
  • Updated the code, still doesn't work. What I try to achieve is to although make the window show up. – rsk82 Mar 14 '13 at 15:24
  • There is nowhere any information relating to this error and function. All I get trough google is my own question. – rsk82 Mar 14 '13 at 15:44
  • Read that article a little more. Your windows proc has to actually handle that registered message and do something with it. – Jim Mischel Mar 14 '13 at 17:07
  • Yes, but why isn't there a single concise and compilable example. Learning that seems like approaching knowledge of some hideous cult when I must read whole books to do one simple thing. It looks like the people who write ms documentation did not really want to people learn this, as this should seem as something cryptic and impossible to comprehend so they could keep their jobs. (Sorry I'm so frustrated I had to write this). – rsk82 Mar 14 '13 at 17:31
  • @JimMischel: but why the error, I copied the example from MS site, it compiles, but it gives the same error. Dialog doesn't show up. Even if I need some more code to this to work (to actually show the dialog window) I understand but at least there should be valid window handle returned by the FindTextW function it is not. – rsk82 Mar 14 '13 at 17:44

1 Answers1

1

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.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
  • I've checked that, the CommDlgExtendedError is 0xFFFF that is CDERR_DIALOGFAILURE, hmmm...*For example, this error occurs if the common dialog box call specifies an invalid window handle.* - and Bingo here with this dialog valid hwndOwner is a must, even GetConsoleWindow() works. Now the dialog shows, and I can go on. (This error message is cryptic it should say clearly of a parent window handle. These quirks make me truly hate hate winapi documentation.) – rsk82 Mar 14 '13 at 18:27