1. 获取当前线程的上一错误代码
a:) 代码 GetLastError() 和 FormatMessage()
LPVOID lpMsgBuffer;
DWORD dwRet=FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
::GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPTSTR>(&lpMsgBuffer),
0,
NULL);
OutputDebugString(reinterpret_cast<LPTSTR>(lpMsgBuffer));
LocalFree(lpMsgBuffer);
b:) 编译器查看 $err,hr
2. 在进行字符串处理时, 尽量使用带有_s的字符串处理函数.
3. new 分配字符串内存时:
new TCHAR( nCharacters * sizeof(TCHAR) )
或可定义宏:
#define chNew(nCharacters) new TCHAR( nCharacters * sizeof(TCHAR) )
4. Ansi 转 Unicode
通用方法:
LPWSTR ANSIToUnicode( LPCTSTR lpszSrc)
{
if (lpszSrc==NULL)
return NULL;
int unicodeLen = ::MultiByteToWideChar( CP_ACP,
0,
lpszSrc,
-1,
NULL,
0 );
LPWSTR pUnicode = new wchar_t[unicodeLen+1];
if (pUnicode==NULL)
return NULL;
memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
::MultiByteToWideChar( CP_ACP,
0,
lpszSrc,
-1,
(LPWSTR)pUnicode,
unicodeLen );
return pUnicode;
}
ANSIToUnicode 使用后一定要记得delete pUnicode;
MFC转换宏:
#include <atlconv.h>
USES_CONVERSION;
DoSomething(A2W("SomeString"));
使用A2W 或者 W2A 都要注意一点, 由于宏分配的内存是在函数的栈中分配的。而VC编译器默认的栈内存空间是2M,所以最后不要在一个函数里面循环调用这2个宏,而且转换的大小不能超过2M.
以下转换函数可供参考
1. ANSI to Unicode
wstring ANSIToUnicode( const string& str )
{
int len = 0;
len = str.length();
int unicodeLen = ::MultiByteToWideChar( CP_ACP,
0,
str.c_str(),
-1,
NULL,
0 );
wchar_t * pUnicode;
pUnicode = new wchar_t[unicodeLen+1];
memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
::MultiByteToWideChar( CP_ACP,
0,
str.c_str(),
-1,
(LPWSTR)pUnicode,
unicodeLen );
wstring rt;
rt = ( wchar_t* )pUnicode;
delete pUnicode;
return rt;
}
2. Unicode to ANSI
string UnicodeToANSI( const wstring& str )
{
char* pElementText;
int iTextLen;
// wide char to multi char
iTextLen = WideCharToMultiByte( CP_ACP,
0,
str.c_str(),
-1,
NULL,
0,
NULL,
NULL );
pElementText = new char[iTextLen + 1];
memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) );
::WideCharToMultiByte( CP_ACP,
0,
str.c_str(),
-1,
pElementText,
iTextLen,
NULL,
NULL );
string strText;
strText = pElementText;
delete[] pElementText;
return strText;
}
3. UTF-8 to Unicode
wstring UTF8ToUnicode( const string& str )
{
int len = 0;
len = str.length();
int unicodeLen = ::MultiByteToWideChar( CP_UTF8,
0,
str.c_str(),
-1,
NULL,
0 );
wchar_t * pUnicode;
pUnicode = new wchar_t[unicodeLen+1];
memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
::MultiByteToWideChar( CP_UTF8,
0,
str.c_str(),
-1,
(LPWSTR)pUnicode,
unicodeLen );
wstring rt;
rt = ( wchar_t* )pUnicode;
delete pUnicode;
return rt;
}
4. Unicode to UTF-8
string UnicodeToUTF8( const wstring& str )
{
char* pElementText;
int iTextLen;
// wide char to multi char
iTextLen = WideCharToMultiByte( CP_UTF8,
0,
str.c_str(),
-1,
NULL,
0,
NULL,
NULL );
pElementText = new char[iTextLen + 1];
memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) );
::WideCharToMultiByte( CP_UTF8,
0,
str.c_str(),
-1,
pElementText,
iTextLen,
NULL,
NULL );
string strText;
strText = pElementText;
delete[] pElementText;
return strText;
}