Windows core Programming Study Notes 1

Source: Internet
Author: User

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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.