I get some mysterious crashes w/out any messages when working with Common File Dialogs. I have a Dialog Box with three edit controls and three buttons (IDC_BUTTON1, IDC_BUTTON2, IDC_BUTTON3) to browse for files. I want to fill the Edit controls with the file names. Usually when I am pushing the third button the application crashes without a warning. Below I give the code for one button in the Dialog procedure. The other two are identical except that ofn has to be replaced by ofn1 and ofn2, respectively. And szFileName is replaced by szFileName1 and szFileName2, respectively.
Code:
case IDC_BUTTON1:
{
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = TEXT("Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0");
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrDefExt = TEXT("txt");
if(GetOpenFileName(&ofn))
{
ILength = lstrlen(szFileName);
buffer = (LPWSTR)GlobalAlloc(GPTR, ILength*sizeof(TCHAR));
if(buffer)
{
wsprintf(buffer, szFileName);
SetDlgItemText(hwnd, IDC_EDIT1, (LPWSTR) buffer);
GlobalFree((HANDLE)buffer);
}
}
}
break;
And for completeness, the variables are as such:
Code:
OPENFILENAME ofn, ofn1, ofn2;
wchar_t szFileName[MAX_PATH] = TEXT("");
wchar_t szFileName1[MAX_PATH] = TEXT("");
wchar_t szFileName2[MAX_PATH] = TEXT("");
int ILength;
LPWSTR buffer;
Has anyone encountered this problem before? Thanx.