
May 14th, 2005, 12:30 AM
|
|
Contributing User
|
|
Join Date: Jan 2005
Posts: 600

Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
|
|
|
[winapi] edit control is lying to me...?
code says a thousand words:
Code:
bool saveFile(HWND hwnd, char *filename)
{
char *realfilename = filename;
if (realfilename == NULL)
{
char selectedfilename[MAX_PATH+1] = "";
OPENFILENAME sfn = { 0 };
sfn.lStructSize = sizeof(OPENFILENAME);
sfn.hwndOwner = hwnd;
sfn.lpstrFilter = "Text Files\0*.txt\0GeekEdit Files\0*.gke\0All Files\0*.*\0";
sfn.lpstrFile = selectedfilename;
sfn.nMaxFile = MAX_PATH;
sfn.lpstrTitle = "Geekedit: Save File";
sfn.Flags = OFN_OVERWRITEPROMPT;
sfn.lpstrDefExt = "txt";
if (!GetSaveFileName(&sfn))
{
errmsg(hwnd, "Save dialog box failed.");
return false;
}
realfilename = selectedfilename;
}
HWND hEdit = GetDlgItem(hwnd, IDC_EDIT);
if (hEdit == NULL)
{
errmsg(hwnd, "Could not find edit control.");
return false;
}
int filetextlen = GetWindowTextLength(hEdit);
if (filetextlen == 0)
{
errmsg(hwnd, "No text in edit control or could not ascertain length of text in edit control.");
return false;
}
char *filetext = new char[filetextlen+1];
if (filetext == NULL)
{
errmsg(hwnd, "Out of memory.");
return false;
}
filetext = "";
GetWindowText(hEdit, filetext, filetextlen);
dbgmsg(hwnd, filetext);
if (filetext == "")
{
errmsg(hwnd, "No text in edit control or failed to retrieve text from edit control because:");
DWORD err = GetLastError();
char *errstr = NULL;
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, err, 0, errstr, 0, NULL);
dbgmsg(hwnd, errstr);
LocalFree(errstr);
return false;
}
ofstream file(realfilename);
if (!file.is_open())
{
errmsg(hwnd, "Could not open file for output.");
return false;
}
file << filetext << flush;
file.close();
return true;
}
this is a function in my notepad-like windows application. It is passed the HWND of the program's main window and a filename (although if the filename passed is NULL it prompts the user for one). My problem is that the call to GetWindowText doesn't work. Even if I type something into the edit control, I get an empty string (""). This seems rather odd to me. What am I doing wrong?
Furthermore, GetLastError tells me that nothing went wrong (zero error code and empty string returned by FormatMessage).
Btw, dbgmsg and errmsg are defined as:
Code:
inline int dbgmsg(HWND hwnd, char *msg) { return MessageBox(hwnd, msg, "Geekedit: Debugging Message", MB_OK | MB_ICONINFORMATION); }
inline int errmsg(HWND hwnd, char *msg) { return MessageBox(hwnd, msg, "Geekedit: Error", MB_OK | MB_ICONERROR); }
|