Convert C String to BSTR


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 15 of 15

Thread: Convert C String to BSTR

  1. #1
    Bill Caton Guest

    Convert C String to BSTR


    In an ATL ActiveX control, I am retrieving information in the form of strings
    from API functions such as GetUserName(). What is the proper and most straightforward
    way to convert these C strings to BSTR without using the MFC for the COM
    interface?

    Thanks,
    Bill

  2. #2
    Dave Bradshaw Guest

    Re: Convert C String to BSTR


    You can use the string conversion macros to handle this for you or, if you
    prefer, you could use the _bstr_t class. I think the conversion macros are
    more efficient since it uses stack based temporaries.

    USES_CONVERSION;
    *pbstrUserName = SysAllocString( T2OLE( userName ) );

    - or -

    *pbstrUserName = _bstr_t( userName ).copy();

    "Bill Caton" <wbc2@cdc.gov> wrote:
    >
    >In an ATL ActiveX control, I am retrieving information in the form of strings
    >from API functions such as GetUserName(). What is the proper and most straightforward
    >way to convert these C strings to BSTR without using the MFC for the COM
    >interface?
    >
    >Thanks,
    >Bill



  3. #3
    Neil Schulman Guest

    Re: Convert C String to BSTR


    This is very easy. All you need to do is:

    #include <comdef.h>

    void SomeFunc()
    {
    _bstr_t bstrString;
    char val[]="Neil";

    // convert to bstr
    bstrString = val;

    // retrieve from bstr
    printf("%s", (char *)bstrString);
    }

    _bstr_t also works with wchar variables and other BSTRs. You can also use
    ATL's helper class CComBSTR to do this. But there is some conversion functionality
    that CComBSTR doesn't provide. Also, _bstr_t can be used in any project that
    includes <comdef.h> so if you write COM outside of ATL you can use it.

    Neil Schulman
    R&D
    PhoneOnline.com

    "Bill Caton" <wbc2@cdc.gov> wrote:
    >
    >In an ATL ActiveX control, I am retrieving information in the form of strings
    >from API functions such as GetUserName(). What is the proper and most straightforward
    >way to convert these C strings to BSTR without using the MFC for the COM
    >interface?
    >
    >Thanks,
    >Bill



  4. #4
    Subham Mitra Guest

    Re: Convert C String to BSTR


    BSTR Data Type is having some interesting features : It is a WCHAR pointer
    to a wide string, it contains the string
    length in the first two bytes before pointing to the first character of the
    string. This is allowing the string to have
    pre-determined size so that marshaling layer should know how much data to
    send.

    BSTR are created with sysAllocString() and released with sysFreeString()


    The resultant string from GetUserName to be converted into BSTR.

    BOOL GetUserName( LPTSTR lpBuffer, LPDWORD nSize); is the prototype of
    GetUsrerName.

    void func()
    {
    BSTR *pstr
    ......
    ......

    if(GetUserName(LPTSTR lpB, LPDWORD nSz))
    {
    *pstr=SysAllocString(OLECHAR(lpB));
    }

    .......
    .....
    }




    "Bill Caton" <wbc2@cdc.gov> wrote:
    >
    >In an ATL ActiveX control, I am retrieving information in the form of strings
    >from API functions such as GetUserName(). What is the proper and most straightforward
    >way to convert these C strings to BSTR without using the MFC for the COM
    >interface?
    >
    >Thanks,
    >Bill



  5. #5
    Subham Mitra Guest

    Re: Convert C String to BSTR


    BSTR Data Type is having some interesting features : It is a WCHAR pointer
    to a wide string, it contains the string
    length in the first two bytes before pointing to the first character of the
    string. This is allowing the string to have
    pre-determined size so that marshaling layer should know how much data to
    send.

    BSTR are created with sysAllocString() and released with sysFreeString()


    The resultant string from GetUserName to be converted into BSTR.

    BOOL GetUserName( LPTSTR lpBuffer, LPDWORD nSize); is the prototype of
    GetUsrerName.

    void func()
    {
    BSTR *pstr
    ......
    ......

    if(GetUserName(LPTSTR lpB, LPDWORD nSz))
    {
    *pstr=SysAllocString(OLECHAR(lpB));
    }

    .......
    .....
    }



    "Bill Caton" <wbc2@cdc.gov> wrote:
    >
    >In an ATL ActiveX control, I am retrieving information in the form of strings
    >from API functions such as GetUserName(). What is the proper and most straightforward
    >way to convert these C strings to BSTR without using the MFC for the COM
    >interface?
    >
    >Thanks,
    >Bill



  6. #6
    Wayne Mack Guest

    Re: Convert C String to BSTR


    Let me add another simplification (inline below).
    Use "%ls" with printf() and you do not need the
    overhead of generating a char * from a BSTR.

    "Neil Schulman" <nschulman@phoneonline.com> wrote:
    >
    >This is very easy. All you need to do is:
    >
    >#include <comdef.h>
    >
    >void SomeFunc()
    >{
    > _bstr_t bstrString;
    > char val[]="Neil";
    >
    > // convert to bstr
    > bstrString = val;
    >
    > // retrieve from bstr
    > printf("%s", (char *)bstrString);


    // instead of previous line use:
    printf("%ls", (const wchar_t *)bstrString);

    >}
    >
    >_bstr_t also works with wchar variables and other BSTRs. You can also use
    >ATL's helper class CComBSTR to do this. But there is some conversion functionality
    >that CComBSTR doesn't provide. Also, _bstr_t can be used in any project

    that
    >includes <comdef.h> so if you write COM outside of ATL you can use it.
    >
    >Neil Schulman
    >R&D
    >PhoneOnline.com
    >
    >"Bill Caton" <wbc2@cdc.gov> wrote:
    >>
    >>In an ATL ActiveX control, I am retrieving information in the form of strings
    >>from API functions such as GetUserName(). What is the proper and most straightforward
    >>way to convert these C strings to BSTR without using the MFC for the COM
    >>interface?
    >>
    >>Thanks,
    >>Bill

    >



  7. #7
    Myron Guest

    Re: Convert C String to BSTR


    From one of MS's examples, we have:



    BSTR AsciiToBSTR(const char* pszFName);

    ...

    BSTR AsciiToBSTR(const char* pszFName)
    {
    WCHAR wszURL[MAX_PATH];
    ::MultiByteToWideChar(CP_ACP, 0, pszFName, -1, wszURL, MAX_PATH);
    return SysAllocString(wszURL);
    }

    Remember that you also need to do SysFreeString(bstr);.

    Myron

    "Bill Caton" <wbc2@cdc.gov> wrote:
    >
    >In an ATL ActiveX control, I am retrieving information in the form of strings
    >from API functions such as GetUserName(). What is the proper and most straightforward
    >way to convert these C strings to BSTR without using the MFC for the COM
    >interface?
    >
    >Thanks,
    >Bill



  8. #8
    Yan Maniez Guest

    Re: Convert C String to BSTR


    You can also use the ATL macro T2BSTR like that :

    bstrString=T2BSTR(tszString);

    In fact, these ATL macros will do any conversion between LPSTR, LPTSTR, LPWSTR
    or BSTR (the last two are not exactly the same, because LPWSTR IS zero terminated,
    and BSTR uses a prefixed size)

    The syntax is X2Y, where A is the source type, and Y the destination type.
    A is for ANSI, W for Unicode (Wide), T for the "neutral" type, and BSTR for
    Unicode + allocation using COM (other macros are using alloca to allocate
    memory on the stack)

    Most of these macros requires the USES_CONVERSION macro at the top of your
    function code.

    For example :
    USES_CONVERSION;
    LPTSTR tszString=TEXT("Hello world");

    // those variables doesn't require freeing memory
    LPWSTR wszString=T2W(tszString);
    LPSTR szString=W2A(wszString);

    // that variable require SysFreeString
    BSTR bstrString=A2BSTR(szString);

    You can use any combination you want, except for the BSTR that can only be
    the destination (the source for BSTR is W)

    Note that another good way to do all these stuff is to use _bstr_t class,
    that is defined in comdef.h, and used by the #import directive.

    Hope this helps

    "Myron" <mandell@maxwell.com> wrote:
    >
    >From one of MS's examples, we have:
    >
    >
    >
    >BSTR AsciiToBSTR(const char* pszFName);
    >
    >...
    >
    >BSTR AsciiToBSTR(const char* pszFName)
    >{
    > WCHAR wszURL[MAX_PATH];
    > ::MultiByteToWideChar(CP_ACP, 0, pszFName, -1, wszURL, MAX_PATH);
    > return SysAllocString(wszURL);
    >}
    >
    >Remember that you also need to do SysFreeString(bstr);.
    >
    >Myron
    >
    >"Bill Caton" <wbc2@cdc.gov> wrote:
    >>
    >>In an ATL ActiveX control, I am retrieving information in the form of strings
    >>from API functions such as GetUserName(). What is the proper and most straightforward
    >>way to convert these C strings to BSTR without using the MFC for the COM
    >>interface?
    >>
    >>Thanks,
    >>Bill

    >



  9. #9
    Pete Guest

    Re: Convert C String to BSTR


    I know BSTR is supposed to have the length in the first two bytes, but I am
    not able to see this. No matter how I create a BSTR--using SysAllocString,
    using _bstr_t, or using the macro T2BSTR--it never seems to store the length,
    and it always seems to be NULL terminated. Why is that?

    -Pete


  10. #10
    Pat Guest

    Re: Convert C String to BSTR


    The 2 bytes indicating length are the two bytes PRECEDING the pointer value
    (i.e., the 2 bytes before where the string starts). BSTR's are null terminated.
    Also note that a BSTR can contain more than one null terminated string.

    "Pete" <pspiro@cognet.com> wrote:
    >
    >I know BSTR is supposed to have the length in the first two bytes, but I

    am
    >not able to see this. No matter how I create a BSTR--using SysAllocString,
    >using _bstr_t, or using the macro T2BSTR--it never seems to store the length,
    >and it always seems to be NULL terminated. Why is that?
    >
    >-Pete
    >



  11. #11
    Yan Maniez Guest

    Re: Convert C String to BSTR


    The 2 bytes indicating length are actually 4 bytes ;-)


    "Pat" <xxx@yyy.com> wrote:
    >
    >The 2 bytes indicating length are the two bytes PRECEDING the pointer value
    >(i.e., the 2 bytes before where the string starts). BSTR's are null terminated.
    > Also note that a BSTR can contain more than one null terminated string.
    >
    >"Pete" <pspiro@cognet.com> wrote:
    >>
    >>I know BSTR is supposed to have the length in the first two bytes, but

    I
    >am
    >>not able to see this. No matter how I create a BSTR--using SysAllocString,
    >>using _bstr_t, or using the macro T2BSTR--it never seems to store the length,
    >>and it always seems to be NULL terminated. Why is that?
    >>
    >>-Pete
    >>

    >



  12. #12
    Bill Caton Guest

    Re: Convert C String to BSTR


    Sorry it took so long to respond, been on vacation. :-)
    Thanks for the info.
    ill

    "Myron" <mandell@maxwell.com> wrote:
    >
    >From one of MS's examples, we have:
    >
    >
    >
    >BSTR AsciiToBSTR(const char* pszFName);
    >
    >...
    >
    >BSTR AsciiToBSTR(const char* pszFName)
    >{
    > WCHAR wszURL[MAX_PATH];
    > ::MultiByteToWideChar(CP_ACP, 0, pszFName, -1, wszURL, MAX_PATH);
    > return SysAllocString(wszURL);
    >}
    >
    >Remember that you also need to do SysFreeString(bstr);.
    >
    >Myron
    >
    >"Bill Caton" <wbc2@cdc.gov> wrote:
    >>
    >>In an ATL ActiveX control, I am retrieving information in the form of strings
    >>from API functions such as GetUserName(). What is the proper and most straightforward
    >>way to convert these C strings to BSTR without using the MFC for the COM
    >>interface?
    >>
    >>Thanks,
    >>Bill

    >



  13. #13
    Bill Caton Guest

    Re: Convert C String to BSTR


    Sorry it took so long to respond, been on vacation. :-)
    Thanks for the info.
    Bill

    "Subham Mitra" <subhamm@blr.aptech.ac.in> wrote:
    >
    >BSTR Data Type is having some interesting features : It is a WCHAR pointer
    >to a wide string, it contains the string
    >length in the first two bytes before pointing to the first character of

    the
    >string. This is allowing the string to have
    >pre-determined size so that marshaling layer should know how much data to
    >send.
    >
    >BSTR are created with sysAllocString() and released with sysFreeString()
    >
    >
    >The resultant string from GetUserName to be converted into BSTR.
    >
    >BOOL GetUserName( LPTSTR lpBuffer, LPDWORD nSize); is the prototype of
    >GetUsrerName.
    >
    >void func()
    >{
    >BSTR *pstr
    >......
    >......
    >
    >if(GetUserName(LPTSTR lpB, LPDWORD nSz))
    >{
    >*pstr=SysAllocString(OLECHAR(lpB));
    >}
    >
    >.......
    >.....
    >}
    >
    >
    >
    >"Bill Caton" <wbc2@cdc.gov> wrote:
    >>
    >>In an ATL ActiveX control, I am retrieving information in the form of strings
    >>from API functions such as GetUserName(). What is the proper and most straightforward
    >>way to convert these C strings to BSTR without using the MFC for the COM
    >>interface?
    >>
    >>Thanks,
    >>Bill

    >



  14. #14
    Bill Caton Guest

    Re: Convert C String to BSTR


    Sorry it took so long to respond, been on vacation. :-)
    Thanks for info.

    Bill

    "Neil Schulman" <nschulman@phoneonline.com> wrote:
    >
    >This is very easy. All you need to do is:
    >
    >#include <comdef.h>
    >
    >void SomeFunc()
    >{
    > _bstr_t bstrString;
    > char val[]="Neil";
    >
    > // convert to bstr
    > bstrString = val;
    >
    > // retrieve from bstr
    > printf("%s", (char *)bstrString);
    >}
    >
    >_bstr_t also works with wchar variables and other BSTRs. You can also use
    >ATL's helper class CComBSTR to do this. But there is some conversion functionality
    >that CComBSTR doesn't provide. Also, _bstr_t can be used in any project

    that
    >includes <comdef.h> so if you write COM outside of ATL you can use it.
    >
    >Neil Schulman
    >R&D
    >PhoneOnline.com
    >
    >"Bill Caton" <wbc2@cdc.gov> wrote:
    >>
    >>In an ATL ActiveX control, I am retrieving information in the form of strings
    >>from API functions such as GetUserName(). What is the proper and most straightforward
    >>way to convert these C strings to BSTR without using the MFC for the COM
    >>interface?
    >>
    >>Thanks,
    >>Bill

    >



  15. #15
    Bill Caton Guest

    Re: Convert C String to BSTR


    Sorry it took so long to respond, been on vacation. :-)
    Thanks for the info.

    Bill

    "Dave Bradshaw" <lank3403@my-deja.com> wrote:
    >
    >You can use the string conversion macros to handle this for you or, if you
    >prefer, you could use the _bstr_t class. I think the conversion macros

    are
    >more efficient since it uses stack based temporaries.
    >
    > USES_CONVERSION;
    > *pbstrUserName = SysAllocString( T2OLE( userName ) );
    >
    >- or -
    >
    > *pbstrUserName = _bstr_t( userName ).copy();
    >
    >"Bill Caton" <wbc2@cdc.gov> wrote:
    >>
    >>In an ATL ActiveX control, I am retrieving information in the form of strings
    >>from API functions such as GetUserName(). What is the proper and most straightforward
    >>way to convert these C strings to BSTR without using the MFC for the COM
    >>interface?
    >>
    >>Thanks,
    >>Bill

    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center