Click to See Complete Forum and Search --> : A2wbstr
George2
April 3rd, 2008, 10:40 AM
Hello everyone,
1.
BSTR is always wide character buffer. I do not know why the name of the macro A2WBSTR is so special and add a needless 'W'?
For others dealing with BSTR, since it is already wide character buffer, 'W' is no need. Like W2BSTR, and A2WSTR.
2.
What is differences between A2WBSTR and A2BSTR? Can not find answer from search.
thanks in advance,
George
Igor Vartanov
April 3rd, 2008, 04:45 PM
George, please. Don't tell me you searched Google while the answer was waiting for you in your Visual Studio folder!..
...\Microsoft Visual Studio .NET 2003\Vc7\atlmfc\include>grep -n A2WBSTR *.h
atlcomcli.h:982: m_str = A2WBSTR(pSrc);
atlcomcli.h:1000: m_str = A2WBSTR(sz, nSize);
atlcomcli.h:1020: m_str = A2WBSTR(pSrc);
atlconv.h:774:inline BSTR A2WBSTR(LPCSTR lp, int nLen = -1)
atlconv.h:806: inline BSTR A2BSTR_EX(LPCSTR lp) {return A2WBSTR(lp);}
atlconv.h:812: inline BSTR A2BSTR(LPCSTR lp) {USES_CONVERSION; return A2WBSTR(lp);}
atlconv.h:818: inline BSTR T2BSTR_EX(LPCTSTR lp) {return A2WBSTR(lp);}
atlconv.h:819: inline BSTR A2BSTR_EX(LPCSTR lp) {return A2WBSTR(lp);}
atlconv.h:824: inline BSTR T2BSTR(LPCTSTR lp) {USES_CONVERSION; return A2WBSTR(lp);}
atlconv.h:825: inline BSTR A2BSTR(LPCSTR lp) {USES_CONVERSION; return A2WBSTR(lp);}Here we go:
inline BSTR A2WBSTR(LPCSTR lp, int nLen = -1)
{
if (lp == NULL || nLen == 0)
return NULL;
USES_CONVERSION_EX;
BSTR str = NULL;
int nConvertedLen = MultiByteToWideChar(_acp_ex, 0, lp,
nLen, NULL, NULL);
int nAllocLen = nConvertedLen;
if (nLen == -1)
nAllocLen -= 1; // Don't allocate terminating '\0'
str = ::SysAllocStringLen(NULL, nAllocLen);
if (str != NULL)
{
int nResult;
nResult = MultiByteToWideChar(_acp_ex, 0, lp, nLen, str, nConvertedLen);
ATLASSERT(nResult == nConvertedLen);
if(nResult != nConvertedLen)
{
SysFreeString(str);
return NULL;
}
}
return str;
}... and somewhat below:
inline BSTR A2BSTR(LPCSTR lp) {USES_CONVERSION; return A2WBSTR(lp);}
George2
April 4th, 2008, 03:12 AM
Thanks Igor,
After checking, my conclusion,
A2BSTR is for null terminated string input, and A2WBSTR can be non-null terminated and assign an additional length field.
Correct?
George, please. Don't tell me you searched Google while the answer was waiting for you in your Visual Studio folder!..
...\Microsoft Visual Studio .NET 2003\Vc7\atlmfc\include>grep -n A2WBSTR *.h
atlcomcli.h:982: m_str = A2WBSTR(pSrc);
atlcomcli.h:1000: m_str = A2WBSTR(sz, nSize);
atlcomcli.h:1020: m_str = A2WBSTR(pSrc);
atlconv.h:774:inline BSTR A2WBSTR(LPCSTR lp, int nLen = -1)
atlconv.h:806: inline BSTR A2BSTR_EX(LPCSTR lp) {return A2WBSTR(lp);}
atlconv.h:812: inline BSTR A2BSTR(LPCSTR lp) {USES_CONVERSION; return A2WBSTR(lp);}
atlconv.h:818: inline BSTR T2BSTR_EX(LPCTSTR lp) {return A2WBSTR(lp);}
atlconv.h:819: inline BSTR A2BSTR_EX(LPCSTR lp) {return A2WBSTR(lp);}
atlconv.h:824: inline BSTR T2BSTR(LPCTSTR lp) {USES_CONVERSION; return A2WBSTR(lp);}
atlconv.h:825: inline BSTR A2BSTR(LPCSTR lp) {USES_CONVERSION; return A2WBSTR(lp);}Here we go:
inline BSTR A2WBSTR(LPCSTR lp, int nLen = -1)
{
if (lp == NULL || nLen == 0)
return NULL;
USES_CONVERSION_EX;
BSTR str = NULL;
int nConvertedLen = MultiByteToWideChar(_acp_ex, 0, lp,
nLen, NULL, NULL);
int nAllocLen = nConvertedLen;
if (nLen == -1)
nAllocLen -= 1; // Don't allocate terminating '\0'
str = ::SysAllocStringLen(NULL, nAllocLen);
if (str != NULL)
{
int nResult;
nResult = MultiByteToWideChar(_acp_ex, 0, lp, nLen, str, nConvertedLen);
ATLASSERT(nResult == nConvertedLen);
if(nResult != nConvertedLen)
{
SysFreeString(str);
return NULL;
}
}
return str;
}... and somewhat below:
inline BSTR A2BSTR(LPCSTR lp) {USES_CONVERSION; return A2WBSTR(lp);}
regards,
George
Igor Vartanov
April 4th, 2008, 04:57 AM
Correct?I don't know, can you believe that? :D
And in fact, don't care. I never use those conversion macros, as I always stick with CString/CComBSTR/_bstr_t.
George2
April 4th, 2008, 05:04 AM
Thanks Igor,
I think I am correct and just let you review and confirm. You are guru here. :-)
I maintain some legacy code and have to read and understand the conversion macros.
I don't know, can you believe that? :D
And in fact, don't care. I never use those conversion macros, as I always stick with CString/CComBSTR/_bstr_t.
regards,
George
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.