Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Problem converting a CComBSTR to LPCTSTR with CW2CT

Problem converting a CComBSTR to LPCTSTR with CW2CT

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++css
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JC Gauthier
    wrote on last edited by
    #1

    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 accumulator

    iSelCount = 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;
    

    }

    S 1 Reply Last reply
    0
    • J JC Gauthier

      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 accumulator

      iSelCount = 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;
      

      }

      S Offline
      S Offline
      soptest
      wrote on last edited by
      #2

      This is the problem:

      TCHAR pszBuf[128] = "";// Message buffer

      buffer size is for 64 wide char's soptest

      J 1 Reply Last reply
      0
      • S soptest

        This is the problem:

        TCHAR pszBuf[128] = "";// Message buffer

        buffer size is for 64 wide char's soptest

        J Offline
        J Offline
        JC Gauthier
        wrote on last edited by
        #3

        :~ 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/

        S 1 Reply Last reply
        0
        • J JC Gauthier

          :~ 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/

          S Offline
          S Offline
          soptest
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups