C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old May 14th, 2005, 12:30 AM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
[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); }
 

Reply With Quote
  #2  
Old May 14th, 2005, 09:34 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
Got it fixed now. The problem was that I was setting filetext to the address of a string literal and then attempting to write to it.

Reply With Quote
  #3  
Old May 14th, 2005, 11:11 PM
B-Con's Avatar
B-Con B-Con is offline
:bcon: moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: int main()
Posts: 351 B-Con User rank is Private First Class (20 - 50 Reputation Level)B-Con User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 23 h 8 m 6 sec
Reputation Power: 4
lol
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips.



Reply With Quote
  #4  
Old May 14th, 2005, 11:55 PM
ShadowCoder ShadowCoder is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Location: Ottawa
Posts: 20 ShadowCoder User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 54 m 12 sec
Reputation Power: 0
nvm, didn't see you had already fixed it.

Last edited by ShadowCoder : May 14th, 2005 at 11:56 PM. Reason: doh

Reply With Quote
  #5  
Old May 14th, 2005, 11:57 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
shadow deleted his post as i was replying to it...

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > [winapi] edit control is lying to me...?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT