0

I have a question about the GetOpenFileName() dialog. What is the flag that I should use to prevent the user from typing a non-existing file and open it? I would like to see a warning message stating that the file does not exist.

According to Microsoft's docs, the OFN_FILEMUSTEXIST flag should display the message, but it doesn't work in my code. What is missing in the code below?

void openfile(){
    ZeroMemory(&opn, sizeof(OPENFILENAME));
    opn.lStructSize       = sizeof(OPENFILENAME);
    opn.hwndOwner         = hWnd;
    opn.lpstrFile         = tz1;
    opn.nMaxFile          = sizeof(tz1);
    opn.lpstrFilter       = "JPG - JPEG File\0*.JPG\0TIF - TIFF File\0*.TIF\0PNG File\0*.PNG\0BMP - Bitmat File\0*.BMP\0";
    opn.nFilterIndex      = 1;
    opn.lpstrFileTitle    = NULL;
    opn.nMaxFileTitle     = 0;
    opn.lpstrInitialDir   = NULL;
    opn.hInstance         = hInstance;
    opn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST|OFN_NOVALIDATE;
    bfile=GetOpenFileName(&opn);    
}
saxbophone
  • 779
  • 1
  • 6
  • 22
nabi
  • 1
  • 1
  • 1
    Why did you use `OFN_NOVALIDATE` then? – πάντα ῥεῖ Jun 17 '22 at 10:54
  • I am not sure about this Flag , I think it is used to write the file name without the extension , since this is already selected in the dropdownlist – nabi Jun 17 '22 at 11:09
  • @nabi: Does the problem still occur if you remove the `OFN_NOVALIDATE` flag? – Andreas Wenzel Jun 17 '22 at 11:27
  • if I remove OFN_NOVALIDATE , the Warning message does work , the problem is I must press on Alt button in the keyboard to make the messagebox appear – nabi Jun 17 '22 at 11:38
  • I need to make that messagebox from type MB_DEFAULT_DESKTOP_ONLY , it will appear in this case , but this is an automatically generated messagebox , if i have access to it , I can even customise it. – nabi Jun 17 '22 at 11:58
  • The `OFN_NOVALIDATE` flag prevents the dialog from making sure the entered filename contains valid characters. It has nothing to do with the file extension, or with validating whether the file exists. Using the `OFN_(FILE|PATH)MUSTEXIST` flags is the correct solution for what you want. "*the problem is I must press on Alt button in the keyboard to make the messagebox appear*" - that is a completely separate issue from what you actually asked. "*this is an automatically generated messagebox, if i have access to it, I can even customise it*" - you don't have access to it... – Remy Lebeau Jun 17 '22 at 19:12
  • ... If you want to customize the message, don't use the `OFN_(PATH|FILE)MUSTEXIST` flags at all, you will have to do your own validation by handling the [CDN_FILEOK](https://learn.microsoft.com/en-us/windows/win32/dlgbox/cdn-fileok) notification in an [Explorer-Style Hook Procedure](https://learn.microsoft.com/en-us/windows/win32/dlgbox/open-and-save-as-dialog-boxes#explorer-style-hook-procedures), then you can display whatever kind of message you want. – Remy Lebeau Jun 17 '22 at 19:15
  • yes that what I did, I removed the OFN_FILEMUSTEXIST , instead I used the following flags: opn.Flags = OFN_ENABLEINCLUDENOTIFY | OFN_EXPLORER | OFN_ENABLEHOOK; and Then I used my own check of file existance with custom messagebox – nabi Jun 17 '22 at 22:15
  • if (GetOpenFileName(&opn)) { checkexistf.open(tz1); if (!checkexistf) { char wrnmsg[200] = "The file : "; strcat(wrnmsg, tz1); strcat(wrnmsg, " Does not exist, Please try again."); strcat(wrnmsg, "\0"); MessageBox(NULL, wrnmsg, "Info", MB_OK | MB_DEFAULT_DESKTOP_ONLY); tz1[0] = '\0'; bfile = 0; wrnmsg[0] = '\0'; } else { bfile = 1; } – nabi Jun 17 '22 at 22:20

0 Answers0