Solved

Problems with GetOpenFileName

Posted on 2002-03-05
6
1,106 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 Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

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…
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…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

623 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