|
GetWindowText() doesn't it supports CString or LPTSTR.
|
|
|
|
|
What might be the alternative?
|
|
|
|
|
you can use CString Class, if not may be convert it and back.
CString strIThink(_T("I think BSTR has Replace member"));
int n = strIThink.Replace(_T("BSTR"), _T("CString"));
_ASSERT(n == 1);
|
|
|
|
|
T.RATHA KRISHNAN wrote: BSTR bstrTickers = NULL;
m_EditWnd.GetWindowText(&bstrTickers);
found = bstrTickers.find('+');
bsSite = "http://www.google.com/search?hl=en&q=";
bsSite += bstrTickers;
SysFreeString(bstrTickers);
Are you sure the above approach is correct? I mean how is m_EditWnd declared?
BTW of course BSTR has no find method (has no methods at all).
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 requirement is this:
I've a string variable. I've to check whether it contains the character '+'. If it contains I've to replace it with the string "%2b". But the remaining contents of the string should not be altered.
If BSTR has no find and replace methods, how can I do this?
Declaration of m_EditWnd is:
CEditQuote m_EditWnd;
and the code is,
typedef typename Allocator::size_type size_type;
size_type found;
// if the user has entered stock quotes then append them to the url
if (m_EditWnd.GetWindowTextLength())
{
BSTR bstrTickers = NULL;
m_EditWnd.GetWindowText(&bstrTickers);
found = bstrTickers.find('+');
bsSite = "http://www.google.com/search?hl=en&q=";
bsSite += bstrTickers;
SysFreeString(bstrTickers);
}
|
|
|
|
|
Are you sure CEditQuote class provides such a misleading method, i.e. a method named GetWindowText and returning a newly allocated BSTR ?
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
|
|
|
|
|
I don't know. It's developed by someone else. It's not that we've to use find and replace methods. In normal C/C++ How to find a character in a string and replace it with a sequence of characters provided the remaining characters should not be altered.
|
|
|
|
|
IF you're using MFC then you may construct a CString object with you BSTR . CString provides such features.
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
|
|
|
|
|
because that string is BSTR.
I suggest you first you convert BSTR to basic_string and than use this find and replace function and than reconvert it into BSTR.
It is very easy to convert BSTR to basic_string. you may google it.
Yes U Can ...If U Can ,Dream it , U can do it ...ICAN
|
|
|
|
|
Can u tell me how to convert a BSTR to simple String?
|
|
|
|
|
MSDN will help you for this ->[^]
But it is wrong solution for your requirement. you convert the whole string to UTF8 .When you convert c++ to utf8 than it is automatically converted to whatever you want.
Yes U Can ...If U Can ,Dream it , U can do it ...ICAN
|
|
|
|
|
It's a toolbar(for web browser)project. If I enter "C++" in the text field it takes only C. But I want entire string("C++") in the text field.
How to do?
No need to convert + sign to %2b.
just convert the string into utf8.
actually when you search any string than that string is converted to UTF8 data.
Yes U Can ...If U Can ,Dream it , U can do it ...ICAN
|
|
|
|
|
What's the way to convert a BSTR to UTF8 data?
|
|
|
|
|
In my project i am using FormatMessage API which is returning the LPTSTR buffer pointer lpMsgBuf, if i use
CComBstr Message(lpMsgBuf); to copy the lpMsgBuf to BSTR.
But this conversion shows memory leak. I am using LocalFree(lpMsgBuf); to release the lpMsgBuf. I am using USES_CONVERSION macro also.
Is there anyway to resolve this? or what is the correct way to convert LPTSTR to BSTR?
Thanks in advance.
Regards,
Bala
|
|
|
|
|
May be posting the code snippet helps,
one possible leak in using CComBSTR is
"Passing the address of an initialized CComBSTR to a function as an [out] parameter causes a memory leak." from msdn.
|
|
|
|
|
try _bstr_t
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow Never mind - my own stupidity is the source of every "problem" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You/codeProject$$>
|
|
|
|
|
Probably posting your code will help.
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
|
|
|
|
|
Here is my code:
//Functions used to report the Error
STDMETHODIMP CVBErrAgent::ReportError(IErrMsg *pErrMsg, SAFEARRAY** args)
{
//CCOmBstr variables for unformatted and formatted message
CComBSTR formatstring, message;
pErrMsg->get_ErrMsg(&formatstring);
//Function to format the message. This function uses FormatMessage API to get formatted string.
FormatMessageFromString(formatstring, args, &message);
pErrMsg->put_ErrMsg(message);
tlErrorBehavior behav;
pErrMsg->get_Behavior(&behav);
if(behav != tlErrorIgnore)
return CErrSvcClientImpl<cvberragent>::tl_Error(pErrMsg);
return S_FALSE;
}
HRESULT CVBErrAgent::FormatMessageFromString(BSTR formatstring, SAFEARRAY **args, BSTR *outputstring)
{
std::vector<ccomvariant> vecargs;
VARIANT HUGEP * pvar;
long lbound, ubound, elcount;
//Codes to read SAFEARRAY and creates the argsarray vector, this will be passed to FormatMessage API.
if (FAILED (SafeArrayGetLBound(*args, 1, &lbound)) ||
FAILED (SafeArrayGetUBound(*args, 1, &ubound)))
return E_INVALIDARG;
elcount = ubound - lbound + 1;
if(elcount == 1)
{
SafeArrayAccessData(*args, (void HUGEP **)&pvar);
//If the SAFEARRAY consists of a single VARIANT that is of type VT_ARRAY then
//We want to call SafeArrayToVector using the embedded array.
if(pvar[0].vt & VT_ARRAY)
{
SafeArrayToVector(pvar->parray, vecargs);
}
else
SafeArrayToVector(*args, vecargs);
SafeArrayUnaccessData(*args);
}
else
SafeArrayToVector(*args, vecargs);
std::vector<variant>::iterator it;
int count = vecargs.size();
vector<pvoid> argsarray(count);
USES_CONVERSION;
for(it = vecargs.begin(), count = 0; it != vecargs.end(); it++, count++)
{
if((*it).vt & VT_BSTR)
{
if((*it).vt & VT_BYREF)
argsarray[count] = reinterpret_cast<void*>(OLE2T(*(*it).pbstrVal));
else
argsarray[count] = reinterpret_cast<void*>(OLE2T((*it).bstrVal));
}
else if((*it).vt & VT_I4 || (*it).vt & VT_I2)
{
if((*it).vt & VT_BYREF)
argsarray[count] = reinterpret_cast<void*>(*(*it).plVal);
else
argsarray[count] = reinterpret_cast<void*>((*it).lVal);
}
}
LPTSTR lpMsgBuf;
DWORD messageflags = FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_ARGUMENT_ARRAY ;
DWORD retval;
//Format the message from the format string.
retval = FormatMessage(
messageflags,
OLE2T(formatstring),
NULL,
LANG_SYSTEM_DEFAULT,
(LPTSTR) &lpMsgBuf,
0,
(va_list*)&argsarray[0]
);
if (!retval)
return MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, GetLastError());
CComBSTR message(lpMsgBuf); // Here is the leak... Why the memory allocated by this CComBSTR is not released? Atleast the when the caller CVBErrAgent::ReportError() function goes out of scope then it should be released. Am i correct? since we detached the CCOmBSTR to outputstring.
*outputstring = message.Detach();
LocalFree(lpMsgBuf);
return S_OK;
}
Where i did wrong? How CComBSTR message(lpMsgBuf); is leaking?
Regards,
Bala
|
|
|
|
|
In your FormatMessage call
balakrishnan vinchu wrote: retval = FormatMessage(
messageflags,
OLE2T(formatstring),
NULL,
LANG_SYSTEM_DEFAULT,
(LPTSTR) &lpMsgBuf,
0,
(va_list*)&argsarray[0]
);
The line
(LPTSTR) &lpMsgBuf,
is wrong. It should be simply
lpMsgBuf,
Forget it.
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
modified on Wednesday, May 7, 2008 5:40 AM
|
|
|
|
|
I changed my code as you explained in previous mail.
LPTSTR lpMsgBuf = NULL;
DWORD messageflags = FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_ARGUMENT_ARRAY ;
DWORD retval;
//Format the message from the format string.
retval = FormatMessage(
messageflags,
OLE2T(formatstring),
NULL,
LANG_SYSTEM_DEFAULT,
lpMsgBuf, // Suggested to pass simply lpMsgBug
0,
(va_list*)&argsarray[0]
);
if (!retval)
return MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, GetLastError());
CComBSTR message(lpMsgBuf);
*outputstring = message.Detach();
LocalFree(lpMsgBuf);
Now i am not getting the Formatted message, since retval returned from FormatMessgae API is zero (i.e 0), Because of this CComBSTR message didn't created. Also if i wont initialize lpMsgBuf = NULL, then functions just gives exception.
Give me some suggestion.
Thanks
Regards,
Bala
|
|
|
|
|
Once try this
initialize lpMsgBuf as:
LPVOID lpMsgBuf;
and inside FormatMessage write lpMsgBuf as:
(LPTSTR) &lpMsgBuf,<br />
Mukesh Kumar
Software Engineer
|
|
|
|
|
Is this the right way?
LPVOID lpMsgBuf;
DWORD messageflags = FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_ARGUMENT_ARRAY ;
DWORD retval;
//Format the message from the format string.
retval = FormatMessage(
messageflags,
OLE2T(formatstring),
NULL,
LANG_SYSTEM_DEFAULT,
(LPTSTR) &lpMsgBuf,
0,
(va_list*)&argsarray[0]
);
if (!retval)
return MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, GetLastError());
CComBSTR message;
message = T2OLE(LPTSTR(lpMsgBuf));
message.CopyTo(outputstring);
I modified LPTSTR lpMsgBuf; to LPVOID lpMsgBuf;
And converted LPTSTR to LPOLESTR.
Am i doing correct here?
Regards,
Bala
|
|
|
|
|
Is this working correctly now?
Mukesh Kumar
Software Engineer
|
|
|
|
|
You're right (and I was wrong...). Whenever you use FORMAT_MESSAGE_ALLOCATE_BUFFER you have to pass the address of the variable, hence your code was correct.
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
|
|
|
|
|
balakrishnan vinchu wrote: *outputstring = message.Detach();
you already taken the ownership of the BSTR, created and associated with the CComBSTR, by Detach method; hence CComBSTR is not expected to release the BSTR. If you are expecting so then you are not supposed to return that buffer as out parameter (outputstring). this outputstring is not released and i think, so the tool you used to detect memory leak points to the creator of it, CComBSTR message(lpMsgBuf);.
|
|
|
|