Solved

Problems with GetOpenFileName

Posted on 2002-03-05
6
1,053 Views
Last Modified: 2013-12-03
I know this is an easy problem.

Here's the deal:

The main window of this program is a dialog box.  (the hwnd it passes is valid, I have tested it by activating/deactivating buttons on the dialog - so I know the HWND is correct).  I push a button and it calls this function:

BOOL     OpenUpdates(HWND hwnd, HDBINST *hDBInst)
{
     
     static char szFilterUpdate[] = "DB Updates (*.efs)\0*.efs\0";
     static char szStartPath[] = "C:\\";
     char szFileName[MAX_PATH];
     char cMisc[MAX_PATH];
     int x;

     ofn.lStructSize = sizeof(OPENFILENAME);
     ofn.hwndOwner = hWndMain;
     ofn.hInstance = NULL;
     ofn.lpstrFilter = szFilterUpdate;
     ofn.lpstrCustomFilter = NULL;
     ofn.nMaxCustFilter = 0;
     ofn.nFilterIndex = 0;
     ofn.nMaxFile = MAX_PATH;
     ofn.nMaxFileTitle = MAX_PATH;
     ofn.lpstrTitle = NULL;
     ofn.Flags = 0;
     ofn.nFileOffset = 0;
     ofn.nFileExtension = 0;
     ofn.lCustData = 0l;
     ofn.lpfnHook = NULL;
     ofn.lpTemplateName = NULL;

     ofn.lpstrFile = cMisc;
     ofn.lpstrFileTitle = szFileName;
     
     ofn.lpstrDefExt = "efs";

               
     ofn.lpstrInitialDir = NULL;

     

     x = GetOpenFileName(&ofn);
     x=CommDlgExtendedError();

     if (x==CDERR_INITIALIZATION)
     {
          x=0;
     }

          LPVOID lpMsgBuf;

     lpMsgBuf = NULL;
FormatMessage(
    FORMAT_MESSAGE_ALLOCATE_BUFFER |
    FORMAT_MESSAGE_FROM_SYSTEM |
    FORMAT_MESSAGE_IGNORE_INSERTS,
    NULL,
    GetLastError(),
    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
    (LPTSTR) &lpMsgBuf,
    0,
    NULL
);
// Process any inserts in lpMsgBuf.
// ...
// Display the string.
MessageBox( NULL, (LPTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
// Free the buffer.
LocalFree( lpMsgBuf );




//     if (GetOpenFileName(&ofn))
//          return TRUE;

     return FALSE;
}

I am getting a CDERR_INITIALIZATION, and FormatMessage of GetLastError is reporting an invalid parameter.  I just don't see which one is invalid.  Does anyone see where I have screwed up?
0
Comment
Question by:navigator010897
  • 3
  • 3
6 Comments
 
LVL 1

Expert Comment

by:JefDuncan
ID: 6842884
First, you have:
    ofn.hwndOwner = hWndMain;
hWndMain is not declared anywhere in the snippet.

Next, you have:
    x = GetOpenFileName(&ofn);
    x=CommDlgExtendedError();
What is the value of x after the call to GetOpenFileName (before the call to CommDlgExtendedError)? If it's non-zero, the function succeeded and you shouldn't even be calling CommDlgExtendedError.

Finally, instead of
    ofn.lpstrInitialDir = NULL;
try
    ofn.lpstrInitialDir = szStartPath;

HTH,
JefDuncan
0
 
LVL 1

Author Comment

by:navigator010897
ID: 6844309
hWndMain is a global variable.  Initially I was passing hwnd from my wndproc to this function - I was concerned that something wasn't going right in my passed variables (don't ask) so in my WinMain, I set hWndMain = hwnd of the master window (in this case a dialog box).

In terms of the x being set from one function then again with the call to the other...

Originally I wasn't even calling CommDlgExtendedError() -when I click on the button to open the file, it would just drop right thru the function.  When I stepped thru doing debugging I found that x from the GetOpenFileName is returning a 0  At this point, I no longer needed to know about x, so I reassigned it to CommDlgExtendedError().

CommDlgExtendedError is returning a 2.  Since I was stepping thru this, I really didn't have a problem testing each return with an if/then.  I learned that x == CDERR_INITIALIZATION

Also on
ofn.lpstrInitialDir

I have tried both NULL and szStartPath - both forms gave me the same error set.  0 on GetOpenFileName and 2 on CommDlgExtendedError.
0
 
LVL 1

Accepted Solution

by:
JefDuncan earned 100 total points
ID: 6845310
navigator,
You need to initialize szFileName and cMisc before the call to GetOpenFileName. I did it this way:

    szFileName[0] = '\0';
    cMisc[0] = '\0';

but there are other methods. Windows doesn't like it if these buffers contain garbage (which they do if they don't get initialized somehow).

HTH,
JefDuncan
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 1

Author Comment

by:navigator010897
ID: 6845392
Well hell.  I knew I had forgotten to do something somewhere, and such a simple small thing to miss.  Geeze do I feel like an idiot today.

Thanks JefDuncan!
0
 
LVL 1

Expert Comment

by:JefDuncan
ID: 6845506
navigator,
You need to initialize szFileName and cMisc before the call to GetOpenFileName. I did it this way:

    szFileName[0] = '\0';
    cMisc[0] = '\0';

but there are other methods. Windows doesn't like it if these buffers contain garbage (which they do if they don't get initialized somehow).

HTH,
JefDuncan
0
 
LVL 1

Author Comment

by:navigator010897
ID: 6845726
I don't know why you re-posted... I already accepted your answer =)
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now