char * 、BSTR、long、wchar_t *、LPCWSTR、string、QString类型转换

char* 转 BSTR

char* s1 = "zhangsan";
CString s2 = CString(s1);
BSTR s3 = s2.AllocSysString();

char* 转 LPCWSTR

char* a = "a.jpg";
WCHAR b[256];
memset(b, 0, sizeof(b));
MultiByteToWideChar(CP_ACP, 0, a, strlen(a) + 1, b, sizeof(b) / sizeof(b[0]));

long 转 char*

// param[0]: long,要转换的数字
// param[1]: char *,转换后指向字符串的指针
// param[2]: 进制
ltoa(age, "age is ", 10);

wchar_t * 转 char *

wchar_t buffer[MAX_PATH];
BOOL result = SHGetSpecialFolderPath(0, buffer, CSIDL_LOCAL_APPDATA, false);
wcscat(buffer, L"\GPR.log");

int iSize;
char* pszMultiByte;

//返回接受字符串所需缓冲区的大小,已经包含字符结尾符''
iSize = WideCharToMultiByte(CP_ACP, 0, buffer, -1, NULL, 0, NULL, NULL); 
pszMultiByte = (char*)malloc(iSize * sizeof(char)); 
WideCharToMultiByte(CP_ACP, 0, buffer, -1, pszMultiByte, iSize, NULL, NULL);

string 转 BSTR

#include <string>
#include <comutil.h>
#pragma comment(lib, "comsuppw.lib")


std::string a = "hello world";
_bstr_t bstr_t(a.c_str());
BSTR res_bstr = bstr_t.GetBSTR();

BSTR 转 string

#include <string>
#include <comutil.h>
#pragma comment(lib, "comsuppw.lib")

BSTR s = L"hello world";
_bstr_t bstr_t(s);
std::string str(bstr_t);

QString 转 BSTR

QString qstr;
BSTR bstr = SysAllocString((OLECHAR*)qstr.unicode());
原文地址:https://www.cnblogs.com/shiyixirui/p/15228203.html