Alright, so I need some more help. This time, the problem is not inherently in the code (Though I've had trouble with that thus far too).

I am currently playing around with open and close file dialogs trying to get what seems to be an out-dated "GetOpenFileName()" function to work. and this lead me down the path of what is this IFileOpenDialog this and that and other things... EVENTUALLY, I found the suggestion to use the Open Watcom compiler instead of the GCC compiler. I followed the code::blocks instructons to plug the Watcom compiler in and now I am getting the following error:

--------------------------------------------------------------------------
Error! E068: invalid option
Note! N746: processing command line, switch: -O2
Error: Compiler returned a bad status compiling "main.cpp"
Process terminated with status 1 (0 minutes, 0 seconds)
0 errors, 0 warnings

--------------------------------------------------------------------------

I have closed and reopened code::blocks to make sure it is consistant.
I have put deliberate syntax errors in the code and under those cases it tells me to fix them.
I have fixed them and gotten the same error message.
I just compiled and ran a working code I made a while back and got the same error

Please let me know what you (any you whom you may be) think about this and... hopefully I can get this sucker to work.
Thanks much.

Recommended Answers

All 12 Replies

Post the code you tried, otherwise can't help you. In the OPENFILENAME structure, make sure lpstrFile points to a character array that you created and the first byte of that array is '\0'.

Okeydokey. The code is as follows:

#include <windows.h>
#include <Commdlg.h>
#include "header.h"


const char g_szClassName[] = "myWindowClass";

// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_CREATE:
            {
                HFONT hfDefault;
                HWND hEdit;

                hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL, 0, 0, 100, 100, hwnd, (HMENU)IDC_MAIN_EDIT, GetModuleHandle(NULL), NULL);

                if(hEdit == NULL)
                {
                    MessageBox(hwnd, "Could not create edit box.", "Error", MB_OK | MB_ICONERROR);
                }
                SendMessage(hEdit, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0));
            }
            break;
        case WM_SIZE:
            {
                HWND hEdit;
                RECT rcClient;

                GetClientRect(hwnd, &rcClient);

                hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT);
                SetWindowPos(hEdit, NULL, 0, 0, rcClient.right, rcClient.bottom, SWP_NOZORDER);
            }
            break;
        case WM_CLOSE:
            DestroyWindow(hwnd);
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                case ID_FILE_OPEN:
                    {
                        OPENFILENAME ofn;
                        char szFileName[MAX_PATH] = "";

                        ZeroMemory(&ofn, sizeof(ofn));

                        ofn.lStructSize = sizeof(ofn); // SEE NOTE BELOW
                        ofn.hwndOwner = hwnd;
                        ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
                        ofn.lpstrFile = szFileName;
                        ofn.nMaxFile = MAX_PATH;
                        ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
                        ofn.lpstrDefExt = "txt";

                        if(GetOpenFileName(&ofn))
                        {
                            // Do something usefull with the filename stored in szFileName
                        }
                    }
                    break;
            }
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WwinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    //Step 1: Registering the Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = MAKEINTRESOURCE(ID_EDITOR_MENU);
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Step 2: Creating the Window
    hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, g_szClassName, "The title of my window", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 240, 120, NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}

I should state I am still learning and am just beginning to understand most of these things... The lpstrFile points to the array szFileName [MAX_PATH] if I'm not mistaken... When it was giving me trouble, it was over the line:

if(GetOpenFileName(&ofn))

As hard as I looked, all I could find is that either my GCC compiler couldn't compile that function or it was obsolete and I needed to learn how to use IFileOpenDialog and... such.

Anyways not to be too long-winded, but I did find that code::blocks had build options that the error may be referring to... and the "-O2" from the afore mentioned:
"Note! N746: processing command line, switch: -O2"
was among them. Thanks again for the time.

Also, to be totally honest, I am completely unsure of the location of my OPENFILENAME struct... but I can't play around with it to find out if it is located incorrectly... =/

... >.< AND, as though that weren't enough already... the #include <Commdlg.h> was something I was trying to use to help myself figure this out (cnan't remember where I found the idea to use it... but I am not sure if it's ... you know what... I'll just let whoever figures out what's up tell me what's up. Or if you (whoever you happens to be) have a working open and save file code I would love to see it and maybe learn that way.

The ID_FILE_OPEN part of your program works ok for me, I just copied and pasted it into an existing win32 api project.

line 75: WwinMain should be WinMain, there is only one 'W', not two.

As long as you have the windows header files and libraries it shouldn't matter what compiler you use. I use either vc++ (Microsoft) or Code::Blocks with MinGW (Windows port of gcc). But other 32-bit compilers should work too.

Yes, you have to include commdlg.h because that's where the struct is declared. Look at the end of this page and you will find where it's referenced.

Don't get discouraged if you get confused and find win32 api difficult to understand, everyone has those problerms. That's one reason not too many people code like that any more.

Phhhhhhhh this is troublesome. I downloaded Visual Studio Express. It kind of helps... kind of.
I have two problems now. One is under code::blocks and the other under Visual Studio.

Code::Blocks gives me this error when I try to compile (using the GNU GCC Compiler - NOT the Open Watcom - otherwise it's still the same problem).

"undefined reference to GetOpenFileNameA@4"
Simply cutting the function out lets it compile normally

Visual Studio runs the program, but it doesn't attatch the menu to the window so... I can't tell if the open file dialog is even working or not.

Just in case, this is my menu code.

#include "header.h"

ID_EDITOR_MENU MENU
{
    POPUP "File"
    {
        MENUITEM "New", ID_FILE_NEW
        MENUITEM "Open", ID_FILE_OPEN
        MENUITEM "Save", ID_FILE_SAVE
        MENUITEM "Save As", ID_FILE_SAVEAS
        MENUITEM "Exit", ID_FILE_EXIT
    }
    POPUP "Edit"
    {
        MENUITEM "Undo", ID_EDIT_UNDO
        MENUITEM "Redo", ID_EDIT_REDO
        MENUITEM "Cut", ID_EDIT_CUT
        MENUITEM "Copy", ID_EDIT_COPY
        MENUITEM "Paste", ID_EDIT_PASTE
        MENUITEM "Find", ID_EDIT_FIND
        MENUITEM "Select All", ID_EDIT_SELALL
    }
    POPUP "Format"
    {
        MENUITEM "Word Wrap", ID_FORMAT_WWRAP
        MENUITEM "Status Bar", ID_FORMAT_STATBAR
        MENUITEM "Hot Keys", ID_FORMAT_HOTKEY
    }
    POPUP "View"
    {
        MENUITEM "Zoom", ID_VIEW_ZOOM
    }
    POPUP "Help"
    {
        MENUITEM "Controls", ID_HELP_CONTROLS
        MENUITEM "FAQ", ID_HELP_FAQ
    }
}

And this is the header file content.

#define IDC_MAIN_EDIT 100

#define ID_EDITOR_MENU 1000

#define ID_FILE_NEW 1010
#define ID_FILE_OPEN 1011
#define ID_FILE_SAVE 1012
#define ID_FILE_SAVEAS 1013
#define ID_FILE_EXIT 1014

#define ID_EDIT_UNDO 1020
#define ID_EDIT_REDO 1021
#define ID_EDIT_CUT 1022
#define ID_EDIT_COPY 1023
#define ID_EDIT_PASTE 1024
#define ID_EDIT_FIND 1025
#define ID_EDIT_SELALL 1026

#define ID_FORMAT_WWRAP 1030
#define ID_FORMAT_STATBAR 1031
#define ID_FORMAT_HOTKEY 1032

#define ID_VIEW_ZOOM 1040

#define ID_HELP_CONTROLS 1050
#define ID_HELP_FAQ 1051

HAHA!!!! Got it! ... the menu anyways

YESSS!!! Thank you so much for the assistance. You're my hero for today. It seems to me that I just needed to pull my old resource file from the code blocks one directly. I got the menu on the window and behold: When I click open it shows an open dialog. Thanks so much for the visual c++ studio dealio suggestion.

I would like to know if I won't be able to use this option 30 days from now or if the "demo" or whatever it is stays avaliable to me beyond that point.

I forgot to mention that many compilers do not compile resourc files -- those with *.rc file names. Instead, you have to download a resource compiler and use it whenever you modify the *.rc file or resource.h header file. VC++ includes the resource compiler, and I think Code::Blocks does too.

Okeydokey that's really good to know for future endeavors. Thanks again for all the help. No doubt, I'll be posting more questions in the future.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.