Solved

Problems with GetOpenFileName

Posted on 2002-03-05
6
1,071 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Query performance - SQL Server 20 82
Mixed results 10 84
Asp.net Form view Pager Template First Record 2 76
Using MS Code on my Mac 6 68
This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

773 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