Tech Support > Microsoft Windows > Development Resources > GetSaveFileName function error
GetSaveFileName function error
Posted by matthew goodwin on July 30th, 2003


i am trying to set the lpstrFile on the OPENFILENAME structure. i'd
like to display a filename to the user that they may change if they'd
like or keep it. the problem is that if i set the lpstrFile to some
char* then i get a fnerr_invalidfilename error. if the lpstrFile is
NULL everything works fine. so my question is what is the correct way
to set the initial filename to display?

here is the code:

TCHAR szFile[MY_MAX_PATH];
szFile[0]='\0';
std::string filename="Samplefilename.txt";

OpenFileName.lStructSize = sizeof(OPENFILENAME);
OpenFileName.hwndOwner = ::GetDesktopWindow();
OpenFileName.hInstance = NULL;
OpenFileName.lpstrFilter = NULL;
OpenFileName.lpstrCustomFilter = NULL;
OpenFileName.nMaxCustFilter = 0;
OpenFileName.nFilterIndex = 0;

strcpy(szFile,fileName.c_str()); // if i comment this out it works

OpenFileName.lpstrFile = szFile;
OpenFileName.nMaxFile = sizeof(szFile);
OpenFileName.lpstrFileTitle = NULL;
OpenFileName.nMaxFileTitle = 0;
OpenFileName.lpstrInitialDir = NULL;
OpenFileName.nFileOffset = 0;
OpenFileName.nFileExtension = 0;
OpenFileName.lpstrDefExt = NULL;
OpenFileName.lCustData = 0;
OpenFileName.lpfnHook = NULL;
OpenFileName.lpTemplateName = NULL;
OpenFileName.Flags = OFN_EXPLORER|OFN_OVERWRITEPROMPT;
OpenFileName.lpstrTitle = title.c_str();


if(GetSaveFileName(&OpenFileName)) { // never displays dialog box
// gets error
fileName = (char*)OpenFileName.lpstrFile;
return SUCCESS;
}

thanks,
matt


Similar Posts