Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,790 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,318 people online right now. Registration is fast and FREE... Join Now!




GetDlgItemText and string conversion problem

 
Reply to this topicStart new topic

GetDlgItemText and string conversion problem, Help finding reason for strange string when using GetDlgItemText

rtokars
23 Sep, 2008 - 02:24 PM
Post #1

New D.I.C Head
*

Joined: 18 Jul, 2008
Posts: 18

hello im causing my program to output strange results when i use SetDlgItemText() GetDlgItemText() and equating char strings together... here is my code:

CODE

HWND hwnd
LPCTSTR StrBmpFileName = "SavedImage";
char temp[128];

SetDlgItemText(hwnd, IDC_SAVEFILE, StrBmpFileName);
GetDlgItemText(hwnd, IDC_SAVEFILE, temp, 128);

StrBmpFileName = temp;              //    <--------------arrow1
MessageBox(hwnd, StrBmpFileName, "Output", MB_OK);


When executing my code, the first messagebox properly displays text: SavedImage. But if it runs through again, the messagebox outputs something strange like: Ô,e . What i think happens is the StrBmpFileName is being converted into some other datatype at the arrow1 pointed out in code. Then when the SetDlgItemText is run for a second time, its unable to set the IDC_SAVEFILE correctly. How would I fix this problem? thanks in advance
User is offlineProfile CardPM
+Quote Post

rtokars
RE: GetDlgItemText And String Conversion Problem
23 Sep, 2008 - 02:43 PM
Post #2

New D.I.C Head
*

Joined: 18 Jul, 2008
Posts: 18

perhaps something like: sprintf_s(StrBmpFileName, temp); would be better instead of equating the two... I'm still working on it.
User is offlineProfile CardPM
+Quote Post

Ambercroft
RE: GetDlgItemText And String Conversion Problem
23 Sep, 2008 - 07:21 PM
Post #3

D.I.C Head
Group Icon

Joined: 5 Jan, 2007
Posts: 107



Thanked: 3 times
Dream Kudos: 25
My Contributions
Are you unable to use 'temp' in MessageBox?

Try using strncpy. You will get a memory leak when StrBmpFileName abandons "SavedImage" to point to 'temp'. Which I think you are now passing the first character instead of the address. Thus pointing to a random place in memory.

cool.gif
User is offlineProfile CardPM
+Quote Post

rtokars
RE: GetDlgItemText And String Conversion Problem
3 Oct, 2008 - 12:03 PM
Post #4

New D.I.C Head
*

Joined: 18 Jul, 2008
Posts: 18

Thanks for the feedback Ambercroft. I ended up doing away with LPCTSTR StrBmpFileName and instead just made it a char StrBmpFileName[128]. I did use the strcpy_s() to add on the .bmp ending too. Thx for getting me to think
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 01:35PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month