Solved

Problems with GetOpenFileName

Posted on 2002-03-05
6
1,062 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

861 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

23 Experts available now in Live!

Get 1:1 Help Now