When we use FormatMessage to get the system error message by the DWORD(from GetLastError), we will get a LPTSTR that end with NewLine (CRLF). How to convert to be NULL end string? It’s a very easy way to do it:
Code Snippet
- CString CGetInfo::GetError(void){
- LPVOID lpMsgBuf;
- DWORD dw = GetLastError();
- FormatMessage(
- FORMAT_MESSAGE_ALLOCATE_BUFFER |
- FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL,
- dw,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
- (LPTSTR) &lpMsgBuf,
- 0, NULL );
- CString s = (LPTSTR)lpMsgBuf;
- s.Remove('\r');
- s.Remove('\n');
- return s;
- }
當我們在用win32 API裡面的FormatMessage去處理GetLastError傳出來的DWORD 錯誤碼的時候, FormatMessage會回傳一個LPTSTR, 以\r\n結尾的字串, 那怎麼把那個\r\n去掉呢? 其實很簡單, 用CString.Rmove() 兩行輕鬆解決.