Openfilename

This is a discussion on Openfilename within the Windows Programming forums, part of the Platform Specific Boards category; Hello, Currently I am working on a very simle programme which should work like Windows notepad, suddenly I ve got ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    162

    Openfilename

    Hello,

    Currently I am working on a very simle programme which should work like Windows notepad, suddenly I ve got stuck with saving and opening files.

    Especially use of OPENFILENAME is very ununderstandable for me, I ve seen a couple of examples on that but I dont still understand.

    What libraries should I include and how should I declare the OPENFILENAME structure so it does work?
    Can you please create a little peace of code where is the structure clearly obvious.

    Thanks very much for your time

  2. #2
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Main references:
    GetOpenFileName
    OPENFILENAME
    The first link has a code example link in the examples section:
    Code:
    ...
    OPENFILENAME ofn;       // common dialog box structure
    char szFile[260];       // buffer for file name
    
    // Initialize OPENFILENAME
    ZeroMemory(&ofn, sizeof(ofn));
    ofn.lStructSize = sizeof(ofn);
    ofn.hwndOwner = hwnd;  // handle to owner window
    ofn.lpstrFile = szFile;
    //
    // Set lpstrFile[0] to '\0' so that GetOpenFileName does not 
    // use the contents of szFile to initialize itself.
    //
    ofn.lpstrTitle = TEXT("Your Title Here");
    ofn.lpstrFile[0] = '\0';
    ofn.nMaxFile = sizeof(szFile);
    // filter name followed by null followed by filter extension,
    // repeat for each filter, and end list with double-null
    ofn.lpstrFilter = "All\0*.*\0Text\0*.TXT\0";
    ofn.nFilterIndex = 1; // initial filter to select
    ofn.lpstrFileTitle = NULL; // initial file to select
    ofn.nMaxFileTitle = 0;
    ofn.lpstrInitialDir = NULL;
    ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
    
    // Display the Open dialog box. 
    
    if(GetOpenFileName(&ofn) == TRUE) // check the return value
    //now you can do something with lpstrFile

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    162
    Well that is pretty strange, the code which I tried before was quite similar to yours. Either one didnt work.

    The programme appears not to even recognise the OPENFILENAME structure.
    If i type :
    Code:
    OPENFILENAME ofn;
    The compiller return error that "ofn" even OPENFILENAME is not declared.

    When I type a (dot) ofter "ofn" no popup combo box filled with possible varialbes appears as it should.

    I dont see where is the problem, maybe I have forgotten to include some important library? (<windows.h> is already included...)

  4. #4
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,534
    Why would you put a period after the init of a struct?

    I'm assuming you have your code in a function by itself (...or maybe not), either way, post your code.
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    162
    Lets stay at the previous examle its mostly the same as I have...

    If I type the first line of the code the compiller should not return error that OPENFILENAME is not declared as it returns in my project... Somewhere there is problem with the OPENFILENAME struct but I cant find out where.

    If I use whole code as it is posted it returns about 18 errors, madness isnt it.

    Have you got any idea why might my compiller does not recognize the OPENFILENAME struct?

  6. #6
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    307
    See msdn's of OPENFILENAME and go below to see the required headers..But I'm sure as, a programmer, you visit this site first
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    162
    Quote Originally Posted by Joelito View Post
    See msdn's of OPENFILENAME and go below to see the required headers..But I'm sure as, a programmer, you visit this site first
    Unfortunately I did previously, Ive got <windows.h> included I tried to add the commdlg.dll to additional dependencies but either worked.

    Am I forgetting something else? At this point I have no idea why this problem occurs.

  8. #8
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Minimum DLL Version: comdlg32.dll
    Header Declared in: Commdlg.h, include Windows.h
    Import library: Comdlg32.lib
    Minimum operating systems: Windows 95, Windows NT 3.1
    Unicode Implemented as: ANSI and Unicode versions.
    From this, the first things I would try would be to include commdlg.h, and if that didn't work, link with comdlg32.lib.
    As for any weird behaviour:
    Quote Originally Posted by GetOpenFileName
    Windows NT 4.0: The OPENFILENAME structure includes additional members on more recent versions of Windows. However, this causes problems for applications on previous versions of Windows. To use the current header files for applications that must run on Windows NT 4.0, either use the #define "/D_WIN32_WINNT=0x0400" or use OPENFILENAME_SIZE_VERSION_400 for the lStructSize member of OPENFILENAME.
    You may need to use certain #defines for compatability issues.

  9. #9
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,534
    >> You may need to use certain #defines for compatability issues.
    I had too. But not just to get the compiler to recognize a struct!

    You could put this line before the include of window.h and see if it helps.
    Code:
    #define _WIN32_WINNT 0x0500
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  10. #10
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    307
    What compiler and IDE are you using?
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  11. #11
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,534
    He has both DEV and VISUAL. Not sure which one he's using for this though.

    The webmaster really needs to replace the Location info with Compiler/IDE info.
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  12. #12
    Registered User
    Join Date
    Apr 2007
    Posts
    162
    Quote Originally Posted by Joelito View Post
    What compiler and IDE are you using?
    I am using VISUAL STUDIO 2005

    What is quite strange that in some precreated project which containes OPENFILENAME there is no problem with it. But if I start using this struct in blank project it fails to work.

  13. #13
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    because it probably links comdlg32.lib for you, did you try that yet?

  14. #14
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,534
    >> because it probably links comdlg32.lib for you, did you try that yet?

    That can't be the problem. OPENFILENAME is declared in the header. So linked to comdlg32.lib or not the compiler should see it.

    Code:
    #define _WIN32_WINNT 0x0500
    #include <windows.h>
    #include <commctrl.h>
    I'm assuming you have this declared somewhere right Gordon?
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  15. #15
    Registered User
    Join Date
    Apr 2007
    Posts
    162
    Quote Originally Posted by Yarin View Post
    >> because it probably links comdlg32.lib for you, did you try that yet?

    That can't be the problem. OPENFILENAME is declared in the header. So linked to comdlg32.lib or not the compiler should see it.

    Code:
    #define _WIN32_WINNT 0x0500
    #include <windows.h>
    #include <commctrl.h>
    I'm assuming you have this declared somewhere right Gordon?
    Yes I have tried including the <windows.h> even linking to comdlg32.lib but either failed

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Save vs Save As.... OPENFILENAME flags
    By csonx_p in forum Windows Programming
    Replies: 16
    Last Post: 06-01-2008, 02:42 PM
  2. Using OPENFILENAME
    By IdioticCreation in forum Windows Programming
    Replies: 9
    Last Post: 01-16-2007, 07:26 AM
  3. OPENFILENAME structure
    By SuperNewbie in forum Windows Programming
    Replies: 1
    Last Post: 01-22-2004, 06:51 AM
  4. OPENFILENAME ofn;
    By ROCKaMIC in forum Windows Programming
    Replies: 22
    Last Post: 10-17-2003, 03:48 AM
  5. OPENFILENAME and multiple files.
    By -leech- in forum Windows Programming
    Replies: 4
    Last Post: 02-25-2003, 12:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21