NO

Author Topic: Problems with OPENFILENAME and GetOpenFileName  (Read 663 times)

Offline dstritt

  • Member
  • *
  • Posts: 2
Problems with OPENFILENAME and GetOpenFileName
« on: June 27, 2016, 10:34:16 AM »
I doubt this is a bug, it must be something I am overlooking, but I keep getting undeclared identifier on all usage of OPENFILENAME and GetOpenFileName. I can right click on them and go to the definitions of them, so I know I have the right headers included. If you create a stock windows program with the wizard, a simple 64bit "Hello World" program then build it, it works fine. At the beginning of WinMain, declare "OPENFILENAME ofn;", then try to build. You get errors. I've done this before, but it's been a while. Anyone have any ideas? Using version 8.00

Thanks,
Daniel

Offline JohnF

  • Member
  • *
  • Posts: 1114
    • http://www.johnfindlay.plus.com/
Re: Problems with OPENFILENAME and GetOpenFileName
« Reply #1 on: June 27, 2016, 11:08:20 AM »
Take out

Code: [Select]
#define WIN32_LEAN_AND_MEAN
replace with

Code: [Select]
#define NOCRYPT
#define NOSERVICE
#define NOMCX
#define NOIME

John

Scripter

  • Guest
Re: Problems with OPENFILENAME and GetOpenFileName
« Reply #2 on: June 27, 2016, 03:32:54 PM »
I doubt this is a bug, it must be something I am overlooking, but I keep getting undeclared identifier on all usage of OPENFILENAME and GetOpenFileName. I can right click on them and go to the definitions of them, so I know I have the right headers included. If you create a stock windows program with the wizard, a simple 64bit "Hello World" program then build it, it works fine. At the beginning of WinMain, declare "OPENFILENAME ofn;", then try to build. You get errors. I've done this before, but it's been a while. Anyone have any ideas? Using version 8.00

First, you need to follow the documentation...
https://msdn.microsoft.com/en-us/library/windows/desktop/ms646927(v=vs.85).aspx
https://msdn.microsoft.com/en-us/library/windows/desktop/ms646839(v=vs.85).aspx

The OPENFILENAME struct has to be preset to some state when it is created. C does not do this for you, the declaration merely creates stack space for the struct, it's content at that point will be whatever garbage is floating around in memory.  The correct declaration is like this:
Code: [Select]
OPENFILENAME ofn = {0};

This will ensure that all elements are preset to 0, which is what the functions expect. 
(In fact you should make a habit of doing this with all variables in C.)

GetOpenFileName() and GetSaveFileName() require quite a bit of initialization of the OPENFILENAME struct before calling the function... You need to set quite a few parameters and flags beforehand, or it simply won't open. The example below is typical...
Code: [Select]
// browse for a filename
BOOL GetFileName(HWND Parent, pFILEINFO File)
  {
    OPENFILENAME ofn = {0};
    WCHAR lf[MAX_PATH] = {0};

    wcscpy(lf, File->Name);
    PathRemoveFileSpec(lf);
    PathAddBackslash(lf);

    ofn.lStructSize     = sizeof(ofn);
    ofn.hInstance       = gInst;
    ofn.hwndOwner       = Parent;
    ofn.lpstrFilter     = DEF_FILTER;
    ofn.nFilterIndex    = 1;
    ofn.lpstrTitle      = L"Open...";
    ofn.lpstrFile       = File->Name;
    ofn.lCustData       = (LPARAM) File;
    ofn.lpstrInitialDir = lf;
    ofn.nMaxFile        = MAX_PATH;
    ofn.lpfnHook        = OFHookProc;
    ofn.lpTemplateName  = L"OPENAS";

    ofn.Flags           = OFN_ENABLEHOOK |
                          OFN_ENABLETEMPLATE |
                          OFN_EXPLORER |
                          OFN_ENABLESIZING |
                          OFN_HIDEREADONLY |
                          OFN_FILEMUSTEXIST |
                          OFN_PATHMUSTEXIST;
    ofn.FlagsEx         = OFN_EX_NOPLACESBAR;

    // launch dialog
    if(GetOpenFileName(&ofn))
      return 1;

    return 0;
  }


Also these dialogs will not open unless you have a message loop running. This is a matter of some coding before hand...
https://msdn.microsoft.com/en-us/library/windows/desktop/ms644928(v=vs.85).aspx

And finally, you need to add the appropriate headers and link with the appropriate libraries. Pelles C has a nice little cheat for this...  Add WIN32_DEFAULT_LIBS to your project's defines and when you add a header, it will automatically link with the correct libraries.


« Last Edit: June 27, 2016, 03:49:32 PM by Scripter »

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 1561
Re: Problems with OPENFILENAME and GetOpenFileName
« Reply #3 on: June 27, 2016, 06:22:28 PM »
For fast compilling.
Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <commdlg.h>
...
May the source be with you