Solved

Problems with GetOpenFileName

Posted on 2002-03-05
6
1,094 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi Mdi application Child forms get behind control 7 365
windbg taking it's time 32 108
Licensing Sharepoint 2016 for developers 11 100
C# LINQ ForEach() question 6 119
If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

710 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