Append BStr
-
Thanks working great now . i don't know the +2 funda , actually i am c# developer but my firm wants me to develop project in c++. ::SysFreeString(dest); can i free this in StringAppend function itself or after my function returns.
“You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
Technically you can do it inside. However you probably should revise your design, for instance using a prototype like the following for your function:
HRESULT StringAppend( BSTR * pdst, const BSTR src);
or, better, using, as suggested, the handy
_bstr_t
wrapper. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Technically you can do it inside. However you probably should revise your design, for instance using a prototype like the following for your function:
HRESULT StringAppend( BSTR * pdst, const BSTR src);
or, better, using, as suggested, the handy
_bstr_t
wrapper. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]1. BSTR * pdst why pointer here. BSTR is itself a pointer. 2. What code you have write for StringAppend function ? I want to take some idea from you.It would be very helpful for me.It's a humble request from a programmer to a professional.
“You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
-
1. BSTR * pdst why pointer here. BSTR is itself a pointer. 2. What code you have write for StringAppend function ? I want to take some idea from you.It would be very helpful for me.It's a humble request from a programmer to a professional.
“You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
Mogaambo wrote:
1. BSTR * pdst why pointer here. BSTR is itself a pointer.
Because the function will internally change the value of the original pointer, i.e.
pdst
is aINOUT
parameter (this way you state clearly that the function may alter the originaldst
string).Mogaambo wrote:
2. What code you have write for StringAppend function ?
Something like
HRESULT StringAppend(BSTR * pdest, const BSTR src)
{
if (pdest == NULL) return E_FAIL;
long destLen=::SysStringLen(*pdest);
long srcLen=::SysStringLen(src);BSTR tmp = ::SysAllocStringLen(NULL,destLen+srcLen);
if( tmp == NULL) return E_FAIL;
wcscpy(tmp, *pdest);
wcscat(tmp, src);
::SysFreeString(*pdest);pdest = &tmp;
*pdest = tmp;
return S_OK;
}Beware: I didn't check this code (moreover I've used just the very basic
E_FAIL
failure return value, a good function should be more informative on failure.If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]modified on Wednesday, May 13, 2009 8:02 AM
-
Mogaambo wrote:
1. BSTR * pdst why pointer here. BSTR is itself a pointer.
Because the function will internally change the value of the original pointer, i.e.
pdst
is aINOUT
parameter (this way you state clearly that the function may alter the originaldst
string).Mogaambo wrote:
2. What code you have write for StringAppend function ?
Something like
HRESULT StringAppend(BSTR * pdest, const BSTR src)
{
if (pdest == NULL) return E_FAIL;
long destLen=::SysStringLen(*pdest);
long srcLen=::SysStringLen(src);BSTR tmp = ::SysAllocStringLen(NULL,destLen+srcLen);
if( tmp == NULL) return E_FAIL;
wcscpy(tmp, *pdest);
wcscat(tmp, src);
::SysFreeString(*pdest);pdest = &tmp;
*pdest = tmp;
return S_OK;
}Beware: I didn't check this code (moreover I've used just the very basic
E_FAIL
failure return value, a good function should be more informative on failure.If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]modified on Wednesday, May 13, 2009 8:02 AM
BSTR tmp = ::SysAllocStringLen(NULL,destLen+srcLen); dude you haven't added +2 bytes, it is crashing as that of mine, but when i added +2 it is running fine, so whenever i want allocate BSTR do i have 2 add + bytes. BSTR tmp = ::SysAllocStringLen(NULL,destLen+srcLen+2);
“You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
-
this is very lame code. You better make at first the output string with CString (or otherwise) and at last ::SysAllocString() :rolleyes:
Press F1 for help or google it. Greetings from Germany
What code you have written if you have to append the BSTR. It's a humble request from programmer to professional.
“You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
-
BSTR tmp = ::SysAllocStringLen(NULL,destLen+srcLen); dude you haven't added +2 bytes, it is crashing as that of mine, but when i added +2 it is running fine, so whenever i want allocate BSTR do i have 2 add + bytes. BSTR tmp = ::SysAllocStringLen(NULL,destLen+srcLen+2);
“You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
Mogaambo wrote:
dude you haven't added +2 bytes
I know, I don't need to (I'm using
SysAllocStringLen
, notSysAllocStringByteLen
).Mogaambo wrote:
it is crashing as that of mine, but when i added +2 it is running fine
This is very strange.
Mogaambo wrote:
so whenever i want allocate BSTR do i have 2 add + bytes
Nope. Even if your point about the crash is true (and is true, I know) this is a wrong conclusion. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
What code you have written if you have to append the BSTR. It's a humble request from programmer to professional.
“You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
-
BSTR tmp = ::SysAllocStringLen(NULL,destLen+srcLen); dude you haven't added +2 bytes, it is crashing as that of mine, but when i added +2 it is running fine, so whenever i want allocate BSTR do i have 2 add + bytes. BSTR tmp = ::SysAllocStringLen(NULL,destLen+srcLen+2);
“You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
I made a silly error in my code, now fixed (please see again the posted snippet). It should be OK now, the following test runs fine:
BSTR dest = SysAllocString(L"Hi ");
BSTR s1,s2,s3,s4;
s1 = SysAllocString(L"People, ");
s2 = SysAllocString(L"How do ");
s3 = SysAllocString(L"you ");
s4 = SysAllocString(L"do?");
HRESULT hr;
hr = StringAppend(&dest, s1);
hr = StringAppend(&dest, s2);
hr = StringAppend(&dest, s3);
hr = StringAppend(&dest, s4);MessageBoxW(dest, L"Info");
SysFreeString(s1);
SysFreeString(s2);
SysFreeString(s3);
SysFreeString(s4);
SysFreeString(dest);:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
I made a silly error in my code, now fixed (please see again the posted snippet). It should be OK now, the following test runs fine:
BSTR dest = SysAllocString(L"Hi ");
BSTR s1,s2,s3,s4;
s1 = SysAllocString(L"People, ");
s2 = SysAllocString(L"How do ");
s3 = SysAllocString(L"you ");
s4 = SysAllocString(L"do?");
HRESULT hr;
hr = StringAppend(&dest, s1);
hr = StringAppend(&dest, s2);
hr = StringAppend(&dest, s3);
hr = StringAppend(&dest, s4);MessageBoxW(dest, L"Info");
SysFreeString(s1);
SysFreeString(s2);
SysFreeString(s3);
SysFreeString(s4);
SysFreeString(dest);:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]If you run your code in debug mode , it crashes at line where you freeing the variable, but runs in release mode.
“You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
-
If you run your code in debug mode , it crashes at line where you freeing the variable, but runs in release mode.
“You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford
My test works fine in DEBUG mode on my machine. What inputs are you providing? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]