Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6564633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:59:31+00:00 2026-05-25T13:59:31+00:00

I seem to be having a very strange problem with GetOpenFileName. It errors for

  • 0

I seem to be having a very strange problem with GetOpenFileName.

It errors for no apparent reason, however, if I call CommDlgExtendedError() in the error check, the error never happens in the first place.

Here is my code:

#include <windows.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    OPENFILENAME fm;
    char flnm[MAX_PATH];
    ZeroMemory(&fm, sizeof(fm));

    fm.lStructSize = sizeof(OPENFILENAME);
    fm.hwndOwner = NULL;
    fm.lpstrFilter = "Text Files (*.txt)\0*.txt\0";
    fm.lpstrFile = flnm;
    fm.nMaxFile = MAX_PATH;
    fm.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
    fm.lpstrDefExt = "";

    if(!GetOpenFileNameA(&fm))
    {
        MessageBoxA(NULL, "failed! :(", NULL, NULL);
    }

    return 0;
}

What’s shown? “failed! :(”

If I remove this check, I do see a file dialog. However, it doesn’t work, and the filename box is pre-filled with random junk.

If I change to:

if(!GetOpenFileNameA(&fm))
{
    DWORD dwErr = CommDlgExtendedError();
    MessageBoxA(NULL, "failed! :(", NULL, NULL);
}

“failed! :(” is NOT shown. The file dialog shows and performs without issue.

What is going on!?!?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:59:31+00:00Added an answer on May 25, 2026 at 1:59 pm
    OPENFILENAME fm;
    char flnm[MAX_PATH]; // nobody initialized me ...
    ZeroMemory(&fm, sizeof(fm));
    
    fm.lStructSize = sizeof(OPENFILENAME);
    fm.hwndOwner = NULL;
    fm.lpstrFilter = "Text Files (*.txt)\0*.txt\0";
    fm.lpstrFile = flnm; // ... who knows what I am?
    fm.nMaxFile = MAX_PATH;
    fm.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
    fm.lpstrDefExt = "";
    
    if(!GetOpenFileNameA(&fm))
    {
            MessageBoxA(NULL, "failed! :(", NULL, NULL);
    }
    

    The documentation for lpstrFile states:

    The file name used to initialize the File Name edit control. The first character of this buffer must be NULL if initialization is not necessary. When the GetOpenFileName or GetSaveFileName function returns successfully, this buffer contains the drive designator, path, file name, and extension of the selected file.

    You are not initializing flnm and therein lies the problem. You can solve the problem by writing flnm[0] = '\0' before you call GetOpenFileName.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a very strange problem. When I log stacktraces, they seem to be
I seem to be having a very odd problem with my iPhone project in
I am having a very basic problem in IE7 that I cannot seem to
I'm having a very odd problem that I can't seem to track down. Any
I seem to be having a very simple problem but I can't wrap my
I seem to be having a very frustrating time with an inherited class calling
I seem to be having a problem with checking for list equality. In my
Okay, I seem to be having a problem. I'm trying to create a twicker
I'm having trouble with MySQL or Python and can't seem to isolate the problem.
I am having a strange issue that I can't seem to resolve. Here is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.