CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Nov 2009
    Posts
    128

    convert CComBstr to char

    how do i convert CComBstr to char;

    char c;
    CComBstr code;

    c = *code;

    is the above conversion correct or is there any other method ?

    please help.its urgent....

  2. #2
    Join Date
    Nov 2003
    Posts
    1,902

    Re: convert CComBstr to char

    If you know that the first character is a simple ASCII value (in the range of 0x00-0x1F), then you could simply use:
    Code:
    char c = static_cast<char>(*code.m_str);
    gg

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: convert CComBstr to char

    See the CComBSTR::operator = in msdn.

    Code:
    CComBstr code(_T("Hello World!"));
    
    // Get the string as ANSI or UNICODE
    LPCSTR sz = code;
    
    // Retrieve the ANSI or UNICODE character
    TCHAR c = sz[0]; // 'H'

  4. #4
    Join Date
    Nov 2003
    Posts
    1,902

    Re: convert CComBstr to char

    Doesn't compile.
    "CComBSTR" - and it doesn't have an "operator LPCSTR".

    Code:
    #include <atlbase.h>
    #include <iostream>
    using namespace std;
    
    int main()
    {
        CComBSTR code(_T("Hello World!"));
        char c = static_cast<char>(*code.m_str);
        cout << c << endl;
        return 0;
    }//main
    gg

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: convert CComBstr to char

    No cast, as BSTR is WCHAR[] inside. Therefore, conversion is needed from WCHAR[] to CHAR[]:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <atlbase.h>
    
    int main()
    {
    	CComBSTR bstr(L"Hello World!");
    	
    	CHAR mbs[20] = {0};
    	wcstombs(mbs, bstr, sizeof(mbs));
    	printf("&#37;s\n", mbs);
    
    	return 0;
    }
    Best regards,
    Igor

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: convert CComBstr to char

    Quote Originally Posted by Codeplug View Post
    Doesn't compile.
    "CComBSTR" - and it doesn't have an "operator LPCSTR".

    Code:
    #include <atlbase.h>
    #include <iostream>
    using namespace std;
    
    int main()
    {
        CComBSTR code(_T("Hello World!"));
        char c = static_cast<char>(*code.m_str);
        cout << c << endl;
        return 0;
    }//main
    gg
    Looks like the msdn link I gave was incorrect.

    No biggie - just use the string conversion macro.

    Code:
    CComBSTR code( _T("Hello World!") );
    
    USES_CONVERSION;
    
    LPTSTR sz = OLE2T( code );
    
    TCHAR c = sz[0];
    The above works in ANSI and UNICODE.

    When working in Windows code, any code that uses 'char' makes me cringe ('cuz I now it's going to be a pain if I ever need to port the code to UNICODE).

  7. #7
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: convert CComBstr to char

    Quote Originally Posted by Codeplug View Post
    If you know that the first character is a simple ASCII value (in the range of 0x00-0x1F), then you could simply use:
    Code:
    char c = static_cast<char>(*code.m_str);
    gg
    I don't have my crystal ball here with me (I am at work), but I am pretty sure OP doesn't just want one character from that BSTR.
    The issue is that BSTR is a pointer to WCHAR, so a conversion (like W2A) is needed.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  8. #8
    Join Date
    Nov 2003
    Posts
    1,902

    Re: convert CComBstr to char

    >> ** how do i convert CComBstr to char **
    I don't have a crystal ball either - so I only answered the quested asked by the OP (under the condition that it's an ASCII value less than 0x1F).

    They didn't ask how to convert to a TCHAR, TCHAR[], or CHAR[] - but now this thread contains solutions to all of those

    >> Looks like the msdn link I gave was incorrect.
    No, its correct.

    gg

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: convert CComBstr to char

    Quote Originally Posted by Codeplug View Post
    >> Looks like the msdn link I gave was incorrect.
    No, its correct.
    While the link is correct, the content within the link is incorrect.

    The link shows the following operator = overload...

    Code:
    CComBSTR& operator =(
       LPCSTR pSrc 
    );
    It doesn't exist and there is no such thing as LPCSTR.

  10. #10
    Join Date
    Nov 2003
    Posts
    1,902

    Re: convert CComBstr to char

    It's there - and it works.
    Code:
    #include <atlbase.h>
    #include <iostream>
    using namespace std;
    
    int main()
    {
        CComBSTR code;
        code = "Hello World";
        char c = static_cast<char>(*code.m_str);
        cout << c << endl;
        return 0;
    }//main
    gg

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: convert CComBstr to char

    Quote Originally Posted by Codeplug View Post
    It's there - and it works.
    If you are referring to my response, it isn't there.

    Notice the difference between your code
    Code:
    *code.m_str
    and mine
    Code:
    OLE2T( code );
    My code uses the LPOLESTR = operator, yours accesses the underlying BSTR pointer.

    You're getting lucky with your code.
    Last edited by Arjay; August 24th, 2011 at 01:51 PM.

  12. #12
    Join Date
    Nov 2003
    Posts
    1,902

    Re: convert CComBstr to char

    Quote Originally Posted by Arjay View Post
    While the link is correct, the content within the link is incorrect.

    The link shows the following operator = overload...

    Code:
    CComBSTR& operator =(
       LPCSTR pSrc 
    );
    It doesn't exist and there is no such thing as LPCSTR.
    I didn't really think you needed a C++ lesson...

    First, LPCSTR is such a thing - it's a pointer to a const CHAR string.

    Second, "operator=(LPCSTR)" is invoked when there is an LPCSTR on the right-hand-side of the object. Which is why my example included {code = "Hello World"}. It compiles without error. "It" obviously exists - as the documentation indicates.

    >> LPCSTR sz = code;
    Your code here does not compile because there is no "operator LPCSTR()" - which I mentioned in post #4. This is also refered to as a "cast operator". operator LPCSTR() would be invoked here, if it existed.

    gg

  13. #13
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: convert CComBstr to char

    Quote Originally Posted by Codeplug View Post
    I didn't really think you needed a C++ lesson...

    First, LPCSTR is such a thing - it's a pointer to a const CHAR string.

    Second, "operator=(LPCSTR)" is invoked when there is an LPCSTR on the right-hand-side of the object. Which is why my example included {code = "Hello World"}. It compiles without error. "It" obviously exists - as the documentation indicates.

    >> LPCSTR sz = code;
    Your code here does not compile because there is no "operator LPCSTR()" - which I mentioned in post #4. This is also refered to as a "cast operator". operator LPCSTR() would be invoked here, if it existed.

    gg
    Thanks. Everyone once in a while things fall through the cracks. Still don't like the char though.

  14. #14
    Join Date
    Nov 2009
    Posts
    128

    Re: convert CComBstr to char

    Thank's a lot for the replies.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured