|
-
June 8th, 1999, 05:56 PM
#1
Help! GetBuffer and IsEmpty confusing me!
Can anyone explain the behavior of CString::GetBuffer in this instance? It's yet another of those cases where you're trying to use a CString var as a param to a function that takes an LPTSTR. I thought using GetBuffer would be a good way to cast the CString to a LPTSTR and CString would automatically get set. The CString gets set, but I'm puzzled as to why calling IsEmpty on the string still returns TRUE after the call, and GetLength returns 0, even though there is now a string in strErrMsg. Any explanation would be much appreciated. Thanks!
// Code snippet that produces unexpected behavior
CString strErrMsg; // new var
strErrMsg.Empty(); // set it to empty
...
catch (COleDispatchException *e) {
BOOL empty = strErrMsg.IsEmpty(); // empty = 1
int len = strErrMsg.GetLength(); // len = 0
e->GetErrorMessage(strErrMsg.GetBuffer(1024), 1024, NULL); // I thought I was setting buffer here!
e->Delete();
// strErrMsg now contains string "Invalid Point Object.", BUT...
empty = strErrMsg.IsEmpty(); // empty still = 1!
len = strErrMsg.GetLength(); // len still = 0!
}
// However, this produces different results
CString strErrMsg; // new var
strErrMsg.Empty(); // set it to empty
...
catch (COleDispatchException *e) {
BOOL empty = strErrMsg.IsEmpty(); // empty = 1
int len = strErrMsg.GetLength(); // len = 0
char buf[1024];
e->GetErrorMessage(buf, 1024, NULL); // put error msg into char buffer
e->Delete();
strErrMsg = buf; // set CString to buffer
// strErrMsg now contains string "Invalid Point object."
empty = strErrMsg.IsEmpty(); // empty now = 0!
len = strErrMsg.GetLength(); // len now = 21!
}
-
June 8th, 1999, 06:19 PM
#2
Call ReleaseBuffer right after GetBuffer
Everything's working as expected. If you read the documentation for GetBuffer, you see that it says that you must call ReleaseBuffer before using any other CString member functions.
Basically you need to call ReleaseBuffer so that CString can update it's internal member variables with respect to the altered string.
Cheers!
Alvaro
-
June 8th, 1999, 06:25 PM
#3
Re: Help! GetBuffer and IsEmpty confusing me!
From the CString::GetBuffer documentation:
"If you use the pointer returned by GetBuffer to change the string contents, you must call ReleaseBuffer before using any other CString member functions. "
-
June 8th, 1999, 06:46 PM
#4
Re: Call ReleaseBuffer right after GetBuffer
Many thanks to both. I had read about ReleaseBuffer, but it didn't click until now!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|