Problem converting a CComBSTR to LPCTSTR with CW2CT
-
I have a problem converting a CComBSTR to LPCTSTR with CW2CT. Eveything works fine until my CComBSTR gets around 64 caracters... then the result of the convertion is a long "corrupted" string: "îþîþîþîþîþîþîþîþîþîþîþîþîþîþîþ...." Please help me ! :(( ----- See comments between /* */ for more info -----
LRESULT CListBoxDlg::OnBnClickedBtngetselitems(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
LPCTSTR sTitle; // Message box title
int arrSelList[12]; // Message buffer
TCHAR pszBuf[128] = "";// Message buffer
int iMaxNum = 12; // Max number of selected items
LRESULT iSelCount = 0; // Total selected items in the list
LPCTSTR sMsg = 0; // Message to put in the message box
CComBSTR bsMessage; // Message accumulatoriSelCount = this->SendDlgItemMessage(IDC\_AVSCODES, LB\_GETSELITEMS, WPARAM(iMaxNum), LPARAM(arrSelList)); if (iSelCount != LB\_ERR) { bsMessage.Append("You have selected the fields: "); \_stprintf(pszBuf, "%li", arrSelList\[0\]); bsMessage.Append(pszBuf); for (int i = 1; i < iSelCount; i++) { bsMessage.Append(", "); \_stprintf(pszBuf, "%li", arrSelList\[i\]); bsMessage.Append(pszBuf); } bsMessage.Append("."); /\* The following line will fails if bsMessage.m\_str contains: "You have selected the fields: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11." But will not fail if it bsMessage.m\_str contains: "You have selected the fields: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10." (4 caracter less) but long size is not suppose the cause problems with ATL 7.0 compared to ATL 3.0 \*/ sMsg = CW2CT(bsMessage.m\_str); /\* // The following two lines makes it work fine but USES\_CONVERSION is not supposed // to be needed with ATL 7.0 USES\_CONVERSION; sMsg = W2CT(bsMessage.m\_str);\*/ sTitle = \_T("Selected Items List"); ::MessageBox(this->m\_hWnd, sMsg, sTitle, MB\_OK); } return 0;
}
-
I have a problem converting a CComBSTR to LPCTSTR with CW2CT. Eveything works fine until my CComBSTR gets around 64 caracters... then the result of the convertion is a long "corrupted" string: "îþîþîþîþîþîþîþîþîþîþîþîþîþîþîþ...." Please help me ! :(( ----- See comments between /* */ for more info -----
LRESULT CListBoxDlg::OnBnClickedBtngetselitems(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
LPCTSTR sTitle; // Message box title
int arrSelList[12]; // Message buffer
TCHAR pszBuf[128] = "";// Message buffer
int iMaxNum = 12; // Max number of selected items
LRESULT iSelCount = 0; // Total selected items in the list
LPCTSTR sMsg = 0; // Message to put in the message box
CComBSTR bsMessage; // Message accumulatoriSelCount = this->SendDlgItemMessage(IDC\_AVSCODES, LB\_GETSELITEMS, WPARAM(iMaxNum), LPARAM(arrSelList)); if (iSelCount != LB\_ERR) { bsMessage.Append("You have selected the fields: "); \_stprintf(pszBuf, "%li", arrSelList\[0\]); bsMessage.Append(pszBuf); for (int i = 1; i < iSelCount; i++) { bsMessage.Append(", "); \_stprintf(pszBuf, "%li", arrSelList\[i\]); bsMessage.Append(pszBuf); } bsMessage.Append("."); /\* The following line will fails if bsMessage.m\_str contains: "You have selected the fields: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11." But will not fail if it bsMessage.m\_str contains: "You have selected the fields: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10." (4 caracter less) but long size is not suppose the cause problems with ATL 7.0 compared to ATL 3.0 \*/ sMsg = CW2CT(bsMessage.m\_str); /\* // The following two lines makes it work fine but USES\_CONVERSION is not supposed // to be needed with ATL 7.0 USES\_CONVERSION; sMsg = W2CT(bsMessage.m\_str);\*/ sTitle = \_T("Selected Items List"); ::MessageBox(this->m\_hWnd, sMsg, sTitle, MB\_OK); } return 0;
}
-
This is the problem:
TCHAR pszBuf[128] = "";// Message buffer
buffer size is for 64 wide char's soptest
:~ even with 1024 it doesn't work
TCHAR pszBuf[1024] = "";// Message buffer
maybe there is something else I need to do. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= J.-C. Gauthier - http://www.grandmenhir.com/
-
:~ even with 1024 it doesn't work
TCHAR pszBuf[1024] = "";// Message buffer
maybe there is something else I need to do. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= J.-C. Gauthier - http://www.grandmenhir.com/
Use thi9s code:
//------------------------//
// Convert BSTR to char * //
//------------------------//
inline char* ConvertBSTRToString(BSTR pSrc)
{
if(!pSrc) return NULL;DWORD cb,cwch = ::SysStringLen(pSrc);//convert even embeded NULL char \*szOut = NULL; if(cb = ::WideCharToMultiByte(CP\_ACP, 0, pSrc, cwch + 1, NULL, 0, 0, 0)) { szOut = new char\[cb\]; if(szOut) { szOut\[cb - 1\] = '\\0'; if(!::WideCharToMultiByte(CP\_ACP, 0, pSrc, cwch + 1, szOut, cb, 0, 0)) { delete \[\]szOut;//clean up if failed; szOut = NULL; } } } return szOut;
};
soptest