1. Get the previous error code of the current thread
A: Code getlasterror ()
And 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: Compiler view $ err, HR
2. When processing strings, try to use string processing functions with _ S.
3. When new allocates string memory:
new TCHAR( nCharacters * sizeof(TCHAR) )
Or customizable macros:
#define chNew(nCharacters) new TCHAR( nCharacters * sizeof(TCHAR) )
4. ANSI to Unicode
General method:
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; }
Remember to delete punicode after using ansitounicode;
MFC conversion macro:
#include <atlconv.h>USES_CONVERSION;DoSomething(A2W("SomeString"));
Pay attention to both a2w and w2a, because the macro-allocated memory is allocated in the function stack. The default stack memory space of the VC compiler is 2 MB. Therefore, do not call these two macros cyclically in a function, and the conversion size cannot exceed 2 MB.
The following conversion functions are available for reference:
1. ANSI to Unicodewstring 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 ANSIstring 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 Unicodewstring 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;}