• COM ACTIVEX 中的BSTR 类型与其它类型转换


    BSTR与其它字符串类型转换

    常用字符串件的类型转换。
     
    From
    To
    Sample
    字符串常量
    BSTR
    Right:
    BSTR bs = ::SysAllocString(_T("Test string"));
    ::SysFreeString();
    Wrong:
    BSTR bs = _T("Test string"); //ERROR
    LPWSTR /
    LPCWSTR /
    WCHAR* /
    wchar_t
    BSTR
    Right:
    LPCTSTR sz1 = _T("Test String");
    BSTR bs = ::SysAllocString(sz1);
    ::SysFreeString();
     
    Wrong:
    LPTSTR sz1 = _T("Test String");
    BSTR bs = sz1; //ERROR
    BSTR
    LPCWSTR /
    const WCHAR * /
    const wchar_t *
    Right:
    BSTR bs = ...; //
    ...
    LPCTSTR sz = static_cast<LPCTSTR>bs;
    ...
    ::SysFreeString(bs);
    //Never use sz after this line
     
    Wrong:
    BSTR bs = ...; //
    ...
     
    LPCTSTR sz = bs;
    ...
    ::SysFreeString(bs);
    //Never use sz after this line
    _tcslen(sz); //ERROR
     
    BSTR
    LPWSTR /
    WCHAR* /
    wchar_t*
    Right:
    BSTR bs = ...; //
    //...
    UINT len = ::SysStringLen(bs);
     
    // Do not modify the BSTR content by
    // C/C++ string functions
    LPTSTR sz = new TCHAR[len+1];
    _tcsncpy(sz, bs, len);
    ::SysFreeString(bs);
     
    delete []sz;
    Wrong:
    BSTR bs = ...; //
    //...
     
    // Do not modify the BSTR content by
    // C/C++ string functions
    LPTSTR sz = bs; //Error
     
    CString
    BSTR
    Right:
     
    CString str1 = ...;
     
    BSTR bs = str1.AllocSysString();
    SomeMethod(bs);
    // void SomeMethod([in]BSTR)
    ::SysFreeString(bs);
     
    CComBSTR bs1(static_cast<LPCTSTR>(str1));
    SomeMethod(static_cast<BSTR> (bs1) );
     
    // void SomeMethod([in] BSTR )
    _bstr_t bs2( static_cast<LPCTSTR>(str1));
    SomeMethod(static_cast<BSTR> (bs2) );
     
    Wrong:
    CString str1 = ...;
     
    SomeMethod(str1.AllocSysString());
     
    // No one will releasee the return BSTR of
    // str1.AllocSysString()
     
    BSTR
    CString
    Right:
     
    BSTR bs = SysAllocString(_T(“Test”));
    CString str1(bs);
    CString str2;
    Str2 = bs;
    SysFreeString(bs); // Never forget this line
    char* / LPSTR / LPCSTR
    BSTR
    Right:
    Solution 1
    char str[MAX_STR_LEN] = "ANSI string";
    WCHAR wstr[MAX_WSTR_LEN];
    // Convert ANSI to Unicode
     
    MultiByteToWideChar( CP_ACP, 0, str,
            strlen(str)+1, wstr,  
         sizeof(wstr)/sizeof(wstr[0]) );
     
    BSTR bs1 = ::SysAllocString(wstr);
     
    CString cs = str;
    BSTR bs2 = cs.AllocSysString()
     
    Solution 2
    char str[MAX_STR_LEN] = "ANSI string";
    _bstr_t bs1(str);
    CComBSTR bs2(str);
     
    Wrong:
    char *str = "ANSI string";
    BSTR bstr1 = SysAllocString(
                (const OLECHAR*) str);
    BSTR
    char* / LPSTR / LPCSTR
    Right:
    Solution 1
    char str[MAX_STR_LEN];
    BSTR bs = ::SysAllocString(L"Test");
    // Convert ANSI to Unicode
    WideCharToMultiByte( CP_ACP, 0,
       (LPCWSTR)bs, -1,
       str, MAX_STR_LEN, NULL, NULL );
    ::SysFreeString(bs);
     
    Solution 2
    BSTR bs = ::SysAllocString(L"Test");
    _bstr_t bs1(bs, false);
    const char* str = static_cast <const char*> bs1;
     
    Wrong:
    BSTR bstr1 = SysAllocString(L”ANSI string");
    char *str = (char*) bstr1;    
     

    IMPORTANT: 上面所有的例子都是按照UNICODE应用程序设计的。并且不考虑BSTR中包含多个字串的情况,

    摘自:http://lanhy2000.blog.163.com/blog/static/43678608200881022713326/

  • 相关阅读:
    复杂系统架构设计<1>
    DevExpress Applications<3>
    DevExpress Skins<2>
    DevExpress MVVM<1>
    Unity容器<1>
    spring cloud微服务快速教程之(九) Spring Cloud Alibaba--sentinel-限流、熔断降级
    spring cloud微服务快速教程之(八) Spring Cloud Alibaba--nacos(二)、配置中心
    spring cloud微服务快速教程之(七) Spring Cloud Alibaba--nacos(一)、服务注册发现
    小白的springboot之路(十五)、mybatis的PageHelper分页插件使用
    spring cloud微服务快速教程之(六) 应用监控 spring boot admin
  • 原文地址:https://www.cnblogs.com/wonderfuly/p/2849611.html
Copyright © 2020-2023  润新知