BSTR与string之间的转换以及注意事项

示例代码:

// bstr_string.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <windows.h>
#include <comutil.h>
using namespace std;

#ifdef _UNICODE
#pragma comment(lib,"comsuppw.lib")
#else
#pragma comment(lib,"comsupp.lib")
#endif

//by zhaocl
int main()
{
    //BSTR to string
    BSTR bstr1 = L"my name is zhaocl.";
    string str1 = ( _bstr_t )bstr1;
    cout << str1 << endl;

    //string to BSTR
    string str2 = "Far away from home.";
    BSTR bstr2 = ( _bstr_t )str2.c_str();

    system( "pause" );
    return 0;
}


知识点:

1、头文件包含,否则无法使用 _bstr_t

2、库包含,否则提示函数无法解析

猜你喜欢

转载自blog.csdn.net/zhao3132453/article/details/80331253