海外营销平台有哪些/台州seo优化
GetLastError()返回的只是一个双字节数值(DWORD),但从双字节数无法直接知道错误出处,除非你把错误代码及其含义都记住了(呵呵,一万多个呢...),有个简单输出的方法如下:
#include <windows.h>
#include <strsafe.h>void ErrorExit(LPTSTR lpszFunction)
{ // Retrieve the system error message for the last-error codeLPVOID lpMsgBuf;LPVOID lpDisplayBuf;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 );// Display the error message and exit the processlpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, (lstrlen((LPCTSTR)lpMsgBuf)+lstrlen((LPCTSTR)lpszFunction)+40)*sizeof(TCHAR)); StringCchPrintf((LPTSTR)lpDisplayBuf, LocalSize(lpDisplayBuf),TEXT("%s failed with error %d: %s"), lpszFunction, dw, lpMsgBuf); MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK); LocalFree(lpMsgBuf);LocalFree(lpDisplayBuf);ExitProcess(dw);
}void main()
{// Generate an errorif(!GetProcessId(NULL))ErrorExit(TEXT("GetProcessId"));
}
这样就能直接打印出错误原因,而不是DWORD值了。 此段代码想放在那里就放在那里。
具体的错误代码及其含义请看 :http://www.seacha.com/article.php/knowledge/windows/mfc/2011/0423/335.html