Reputation: 1753
I have a CEdit and I want to extract the data using this.
wchar_t *temp = (wchar_t*)dialog.editbox.GetBuffer(0);
dialog.editbox.ReleaseBuffer();
Now i want to save this text in a object pointer like this:
selectedShape->setText(temp);
This work perfect, but only as long as you are in the scope of the method, because when I do a file save later, the text is not in the object anymore.
Does anybody know how I can save this wchar_t*
for later?
Upvotes: 0
Views: 143
Reputation: 10357
The temp
pointer is pointing to data that goes out of scope, so you'll need to dynamically allocate memory to store the value. Something like this should work:
// Updated to use wstring, thanks praetorian
std::wstring tempStr((wchar_t*)dialog.editbox.GetBuffer(0));
Or:
int length = /*figure out the length here*/;
wchar_t *temp = new wchar_t[length];
memcpy(temp, dialog.editbox.GetBuffer(0), length*sizeof(wchar_t));
// dont forget to delete it like this: delete [] temp;
Upvotes: 1
Reputation: 33607
As @Prætorian says, your code seems to be missing a step of where you work with a CString
class whose buffer you are getting.
http://msdn.microsoft.com/en-us/library/aa314880(v=vs.60).aspx
If at all possible, avoid using dynamic memory solutions. Instead, pass around your temp
by value as a CString object that will manage its own memory. The stock Window setText
functions take string pointers (which CString can implicitly cast to) and will copy the underlying string data. If you write your own objects, hold CString objects as members by value.
(I'll add my usual "The 90s called, they want their framework back" disclaimer here. Try Qt.)
Upvotes: 2