Hello, World! (_bstr_t) Hello, World! (char *) Hello, World! (wchar_t *) Hello, World! (CComBSTR) Hello, World! (CString) Hello, World! (basic_string) Hello, World! (System::String)
此示例演示如何从 CComBSTR 转换为上面列出的其他字符串类型。
// convert_from_ccombstr.cpp
// compile with /clr /link comsuppw.lib
#include <iostream>
#include <stdlib.h>
#include <string>
#include "atlbase.h"
#include "atlstr.h"
#include "comutil.h"
#include "vcclr.h"
using namespace std;
using namespace System;
using namespace System::Runtime::InteropServices;
int main()
{
CComBSTR orig("Hello, World!");
CW2A printstr(orig);
cout << printstr << " (CComBSTR)" << endl;
// Convert to a char*
const size_t newsize = 100;
char nstring[newsize];
CW2A tmpstr1(orig);
strcpy_s(nstring, tmpstr1);
strcat_s(nstring, " (char *)");
cout << nstring << endl;
// Convert to a wchar_t*
wchar_t wcstring[newsize];
wcscpy_s(wcstring, orig);
wcscat_s(wcstring, L" (wchar_t *)");
wcout << wcstring << endl;
// Convert to a _bstr_t
_bstr_t bstrt(orig);
bstrt = " (_bstr_t)";
cout << bstrt << endl;
// Convert to a CString
CString cstring(orig);
cstring = " (CString)";
cout << cstring << endl;
// Convert to a basic_string
wstring basicstring(orig);
basicstring = L" (basic_string)";
wcout << basicstring << endl;
// Convert to a System::String
String ^systemstring = gcnew String(orig);
systemstring = " (System::String)";
Console::WriteLine("{0}", systemstring);
delete systemstring;
}Hello, World! (CComBSTR) Hello, World! (char *) Hello, World! (wchar_t *) Hello, World! (_bstr_t) Hello, World! (CString) Hello, World! (basic_string) Hello, World! (System::String)
此示例演示如何从
评论加载中…
![]() |