Why is this crashing on my face?
Code:
void Save(HWND hwnd)
{
//open the Save As prompt, which is built in in windows (set it up here)
OPENFILENAME ofn;
char szFileName[MAX_PATH] = "";
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER |OFN_OVERWRITEPROMPT;
ofn.lpstrDefExt = "txt";
if(GetSaveFileName(&ofn)) //Open The Save As Prompt
{
HWND DialogBoxH;
//Get the info on the Edit Box
int len = GetWindowTextLength(DialogBoxH=GetDlgItem(hwnd, IDC_EDIT2));//Use GetDlgItem(hwnd, IDC_TEXT)
//to get a handle to the
//Edit Box
if(len > 0)//if it's more than zero, meaning it has characters
{
char* buf;
buf = (char*)GlobalAlloc(GPTR, len + 1);//we add one here for the null
GetDlgItemText(hwnd, IDC_EDIT2, buf, len + 1);//get the text from the edit box
memcpy(Cliente.ClientName,buf,strlen (buf)); //copy the text from buf to the struct
MessageBox (DialogBoxH, Cliente.ClientName,"Something", MB_OK);
//... do stuff with text ...
GlobalFree((HANDLE)buf);
}
LPCTSTR FileName;
HANDLE FileSave=NULL;
BOOL Success;
FileSave=CreateFile(FileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
DWORD dwWritten;
if (FileSave!= INVALID_HANDLE_VALUE)
{
WriteFile(FileSave, &Cliente, sizeof Cliente,&dwWritten, NULL);
}
// Do something usefull with the filename stored in szFileName
}
}