Click to See Complete Forum and Search --> : Error after Save and Open file common dialog


Highlord
August 18th, 2004, 05:01 AM
The following error (appears in dialog) occurred after I close my program:

Debug Error!

Program:...\Application.exe

DAMAGE: after Normal block (#818) at 0x00ACF920.

(Press Retry to debug the application)


I have two common dialogs GetOpenFileName(&ofn), and GetSaveFileName(&ofn).

If I don't call the function, the program exit fine, but if I call them during the program execution, the above error occurred. What might be the cause of it?

The following are the functions that initiate the ofn structure, and call the function:

/** This is the general dialog generation for all load and save **/
void PopFileInitialize(HWND hwnd)
{
static TCHAR szFilter[] = TEXT ("Text Files (*.TXT)\0*.txt\0") \
TEXT ("All Files (*.*)\0*.*\0\0");

ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hwnd;
ofn.hInstance = NULL;
ofn.lpstrFilter = szFilter;
ofn.lpstrCustomFilter = NULL;
ofn.nMaxCustFilter = 0;
ofn.nFilterIndex = 0;
ofn.lpstrFile = NULL;
ofn.nMaxFile = 100;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 100;
ofn.lpstrInitialDir = NULL;
ofn.lpstrTitle = NULL;
ofn.Flags = 0;
ofn.nFileOffset = 0;
ofn.nFileExtension = 0;
ofn.lpstrDefExt = TEXT ("txt");
ofn.lCustData = 0L;
ofn.lpfnHook = NULL;
ofn.lpTemplateName = NULL;
}

int PopFileOpenDlg(HWND hwnd, PTSTR FileName, PTSTR TitleName)
{
TitleName = NULL;
ofn.hwndOwner = hwnd;
ofn.lpstrFile = FileName;
ofn.lpstrFileTitle = TitleName;
ofn.Flags = OFN_HIDEREADONLY | OFN_CREATEPROMPT ;
return GetOpenFileName(&ofn);
}

int PopFileSaveDlg(HWND hwnd, PTSTR FileName, PTSTR TitleName)
{
TitleName = NULL;
ofn.hwndOwner = hwnd;
ofn.lpstrFile = FileName;
ofn.lpstrFileTitle = TitleName;
ofn.Flags = OFN_OVERWRITEPROMPT;
return GetSaveFileName(&ofn);
}



I pass a string that is typecast into a (char*), I don't know if that makes a difference or not.

kirants
August 18th, 2004, 11:20 AM
Please provide the code you call to initialize and call the dialog. Also, provide post code on how the variables are declared.

Highlord
August 19th, 2004, 02:31 AM
This is how I call load function (with filename_multi as a string, and IDD_MULTI_LOAD as a button):

case IDD_MULTI_LOAD:
filename_multi = " ";
if(PopFileOpenDlg(hDlg,(char*)filename_multi.c_str(),"Multi"))
{
}

/** Start loading data into appropriate setting **/
packet_multi_in = load_setting(filename_multi,"multi_packet=",30);
SetDlgItemText(hDlg,IDD_MULTI_PACKET,packet_multi_in.c_str());

This is how I call save function:

case IDD_MULTI_SAVE:
PopFileSaveDlg(hDlg,(char*)filename_multi.c_str(),"session");

And I call PopFileInitialize in WndProc(...) as

PopFileInitialize(hwnd);

with hwnd has the handle in hwnd.

One thing it might help is that I have a main window with 2 dialog display simultaneously (using visible option), the structure "ofn" is declared as a static global variable.
This means that the function is called by the dialog, not the main window. I do not know whether or not it will have an impact on the common dialog.

Thanks for the help.

kirants
August 19th, 2004, 11:10 AM
One thing I don't understand. Why are you doing a TitleName = NULL in those 2 functions ?

Highlord
August 19th, 2004, 08:21 PM
Reason I have title = NULL is that whatever value I put in there, the program crashes. The only thing work is to put a NULL there. To be honest, I do not know what to put in title.

JamesSchumacher
August 23rd, 2004, 07:43 PM
you are passing a pointer to a null terminated string from std::string that SHOULD NOT BE EDITED.

Plus, to make matters worse - if that wasn't bad enough, you have no size for the buffers you are giving to the structure.

to fix this, use either dynamically allocated c-style string buffer or static storage c-style string buffers.



char * pFileData = new char[256];
char * pFileTitle = new char[40];

// or

char szFileData[256];
char szFileTitleData[40];


And remember to set the size of the buffers in the nMaxFile and nMaxFileTitle fields of the structure.

See this page. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/commondialogboxlibrary/commondialogboxreference/commondialogboxstructures/openfilename.asp

Highlord
August 23rd, 2004, 09:08 PM
Ah...I see. Thank you. I always suspect that there is some issues with string, but not too sure about it. Thanks again for clarifying that up.