MFC库,CRT库,Win32 Api三种库里面拥有的函数都不一样,下面分享的这些方法不一定能在这三种库中完美运行。以后我整理出来了不同的库,不同的方法会继续更新这个系列的
//String 转换为 Const Char*/LPCSTR
String Str = “Test”;
Const char = Str.C_str();
//const char*转换为char *
const char *msg = “hhhh”;
char *key = new char[strlen(msg)+1];
strcpy(key, msg);
// char* 转换为 wchar_t*
wchar_t * char2wchar(const char* cchar)
{
wchar_t *m_wchar;
int len = MultiByteToWideChar(CP_ACP, 0, cchar, strlen(cchar), NULL, 0);
m_wchar = new wchar_t[len + 1];
MultiByteToWideChar(CP_ACP, 0, cchar, strlen(cchar), m_wchar, len);
m_wchar[len] = ‘\0’;
return m_wchar;
}
// wchar_t* 转换为 char*
char * wchar2char(const wchar_t* wchar)
{
char * m_char;
int len = WideCharToMultiByte(CP_ACP, 0, wchar, wcslen(wchar), NULL, 0, NULL, NULL);
m_char = new char[len + 1];
WideCharToMultiByte(CP_ACP, 0, wchar, wcslen(wchar), m_char, len, NULL, NULL);
m_char[len] = ‘\0’;
return m_char;
}
// LPWSTR 转换char*
LPWSTR lpCmdLine
char buffer[500];
wcstombs(buffer, lpCmdLine, 500);
//String转换为 GUID (LPO)
CLSIDFromString
//GUID转换为 String
StringFromCLSID
//String转换为 GUID
BOOL LPCTSR2GUID(LPCTSTR szGUID, GUID &Guid) {
memset(&Guid, 0, sizeof(Guid));
int nRet =
_stscanf(szGUID,
_T(“%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x”),
&Guid.Data1,
&Guid.Data2,
&Guid.Data3,
&Guid.Data4[0], &Guid.Data4[1],
&Guid.Data4[2], &Guid.Data4[3], &Guid.Data4[4], &Guid.Data4[5], &Guid.Data4[6], &Guid.Data4[7]
);
return (nRet == 11) ? TRUE : FALSE;
}
//GUID转换为 String
LPCTSTR GUID2LPCTS(GUID Guid) {
static TCHAR szBuf[65] = { 0 };
_sntprintf(szBuf, 64, _T(“%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X”),
Guid.Data1,
Guid.Data2,
Guid.Data3,
Guid.Data4[0], Guid.Data4[1],
Guid.Data4[2], Guid.Data4[3], Guid.Data4[4], Guid.Data4[5], Guid.Data4[6], Guid.Data4[7]
);
return szBuf;
}
Stscanf 报错处理方式 // 头文件#include <tchar.h>
- 找到菜单栏的项目→项目的属性→C/C++→预处理器
- 右侧有个预处理器定义,在其参数中加上 _CRT_SECURE_NO_WARNINGS,如下图
//String转换为 LPCWSTR
LPCWSTR stringToLPCWSTR(std::string orig)
{
size_t origsize = orig.length() + 1;
const size_t newsize = 100;
size_t convertedChars = 0;
wchar_t *wcstring = (wchar_t )malloc(sizeof(wchar_t)(orig.length() - 1));
mbstowcs_s(&convertedChars, wcstring, origsize, orig.c_str(), _TRUNCATE);
return wcstring;
}
// LPCWSTR转换为 String
string WCharToMByte(LPCWSTR lpcwszStr)
{
string str;
DWORD dwMinSize = 0;
LPSTR lpszStr = NULL;
dwMinSize = WideCharToMultiByte(CP_OEMCP, NULL, lpcwszStr, -1, NULL, 0, NULL, FALSE);
if (0 == dwMinSize)
{
return FALSE;
}
lpszStr = new char[dwMinSize];
WideCharToMultiByte(CP_OEMCP, NULL, lpcwszStr, -1, lpszStr, dwMinSize, NULL, FALSE);
str = lpszStr;
delete[] lpszStr;
return str;
}
//Const W_chat * to LPWSTR
const wchar_t* source = L"Hello, world!"; // 源常量宽字符字符串
LPWSTR destination = const_cast(source); // 进行强制类型转换
// 此时,destination 指向与 source 相同的宽字符字符串,但是要注意不要对其进行修改
DebugView使用方法:
包含目录:KaTeX parse error: Undefined control sequence: \include at position 15: (TosCommonLib)\̲i̲n̲c̲l̲u̲d̲e̲;(IncludePath) –>TosCommonLib文件夹所在地
库目录:KaTeX parse error: Undefined control sequence: \lib at position 15: (TosCommonLib)\̲l̲i̲b̲\x64;(LibraryPath) –> TosCommonLib文件夹所在地
链接器附加依赖项目:DbgLog.lib(-)如果使用debug使用DbgLogd.lib)
引用:TosError.h (在pch.h里面引用)
Note:编译Tos的版本需要引用Tos的版本一致
捕获服务log,需要以管理员方式运行
$(TosCommonLib) 是需要注册的一个宏定义
方法二:
直接把那TosCommonLib的五个文件包含到工程里面去,然后应该TosError.h即可
Note:编译Tos的版本需要引用Tos的版本一致
Vs C++ 字符串类型解析:
LPCTSTR
L:long
P:指针
C:Const
T:宏定义
STR:字符串
Note:一般情况下:string=char *,Wstring = wchat_t *;
例:
Char msg[] = “hello”;
String str = msg;
CString:
%c:单个字符
%d:十进制int
%ld:十进制Long
%f:十进制float
%lf:十进制double
%s:字符串
例:
CString CStr;
int Number=15;
CStr.Format(_T(“%d”),Number);
//Cstring 转 LPTSTR(TCHAR *)
例:Cstring Cstr;
LPTSTR LPT;
LPT = Cstr.GetBuffer(MAX_PATH);
使用完Cstr后需要调用
Cstr. ReleaseBuffer();
//整形,长整型转CString
CString Cstr;
Int I;
Itoa(i,Cstr,10);
//CString to 整形,长整型
CString CStr = “112”;
Int I = atoi(CStr);
//Char * to CString /多字节字符集:
CString Str;
Char Sz[] = “daddad”;
Str.format(“%s”,Sz);
Str = (CString)Sz;
/Unicode
Char * p= “sdadad”;
USES_CONVERSION;
CString S = A2T§; / A2W§;
//CString to Char * /多字节字符集
CString str = “dada”
Char *p = (LPSTR)(LPCTSTR)str;
P = str.GetBuffer();
/Unicode
CString str = _T(“fafa”);
USES_CONVERSION;
Char * p =T2A(str);/ W2A(str);