_bstr_t к возможному UTF-8?

Ask_controller:
Пожалуйста, внимательно прочитайте все комментарии.

9
задан JoeG 21 June 2012 в 12:50
поделиться

2 ответа

Используйте WideCharToMultiByte () – передают CP_UTF8 как первый параметр.

Остерегайтесь этого, BSTR может быть нулевым указателем, и это соответствует пустой строке – рассматривают это как особый случай.

16
ответ дан 4 December 2019 в 13:05
поделиться

Очень удобная ссылка MSDN для этого вида вещи: http://msdn.microsoft.com/en-us/library/ms235631 (По сравнению с 80) .aspx

Я думаю, что необходимо перейти к wchar_t*, так как символ* потеряет материал Unicode, хотя я не уверен.

// convert_from_bstr_t.cpp
// compile with: /clr /link comsuppw.lib

#include <iostream>
#include <stdlib.h>
#include <string>

#include "atlbase.h"
#include "atlstr.h"
#include "comutil.h"

using namespace std;
using namespace System;

int main()
{
    _bstr_t orig("Hello, World!");
    wcout << orig << " (_bstr_t)" << endl;

    // Convert to a char*
    const size_t newsize = 100;
    char nstring[newsize];
    strcpy_s(nstring, (char *)orig);
    strcat_s(nstring, " (char *)");
    cout << nstring << endl;

    // Convert to a wchar_t*
    wchar_t wcstring[newsize];
    wcscpy_s(wcstring, (wchar_t *)orig);
    wcscat_s(wcstring, L" (wchar_t *)");
    wcout << wcstring << endl;

    // Convert to a CComBSTR
    CComBSTR ccombstr((char *)orig);
    if (ccombstr.Append(L" (CComBSTR)") == S_OK)
    {
        CW2A printstr(ccombstr);
        cout << printstr << endl;
    }

    // Convert to a CString
    CString cstring((char *)orig);
    cstring += " (CString)";
    cout << cstring << endl;

    // Convert to a basic_string
    string basicstring((char *)orig);
    basicstring += " (basic_string)";
    cout << basicstring << endl;

    // Convert to a System::String
    String ^systemstring = gcnew String((char *)orig);
    systemstring += " (System::String)";
    Console::WriteLine("{0}", systemstring);
    delete systemstring;
}
-1
ответ дан 4 December 2019 в 13:05
поделиться
Другие вопросы по тегам:

Похожие вопросы: