Retrieving the Last-Error Code
来源:互联网 发布:中华网络一条龙 编辑:程序博客网 时间:2024/05/18 03:19
When many system functions fail, they set the last-error code. If your application needs more details about an error, it can retrieve the last-error code using theGetLastError function and display a description of the error using theFormatMessage function.
The following example includes an error-handling function that prints the error message and terminates the process. ThelpszFunction parameter is the name of the function that set the last-error code.
#include <windows.h>#include <strsafe.h>void ErrorExit(LPTSTR lpszFunction) { // Retrieve the system error message for the last-error code LPVOID 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 process lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR)); StringCchPrintf((LPTSTR)lpDisplayBuf, LocalSize(lpDisplayBuf) / sizeof(TCHAR), 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 error if(!GetProcessId(NULL)) ErrorExit(TEXT("GetProcessId"));}
- Retrieving the Last-Error Code
- Retrieving Last Inserted ID
- InnoDB: ERROR: the age of the last checkpoint is
- error: Error retrieving parent for item: No resource found that matches the given name 'android:Wind
- error: Error retrieving parent for item: No resource found that matches the given name 'Widget.AppCo
- Android应用出错 error: Error retrieving parent for item: No resource found that matches the
- error: Error retrieving parent for item: No resource found that matches the given name 'android:Them
- 解决error: Error retrieving parent for item: No resource found that matches the given name 'AppCompat'
- 解决error: Error retrieving parent for item: No resource found that matches the given name 'android:
- error: Error retrieving parent for item: No resource found that matches the given name 解决方法1
- error: Error retrieving parent for item: No resource found that matches the given name 'android:Win
- error: Error retrieving parent for item: No resource found that matches the
- Android应用出错 error: Error retrieving parent for item: No resource found that matches the
- error: Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCom
- error: Error retrieving parent for item: No resource found that matches the given name
- error: Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCom
- error: Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCom
- error: Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCom
- Dreamweaver & Flash & Photoshop网页设计基础与实例教程(职业白金版)
- 非编程天才参与开源项目的 14 种方式
- python学习第六章
- linux shell
- Eclipse Memory Analyzer(Mat) 堆转储文件分析
- Retrieving the Last-Error Code
- 1背景知识
- COBOL语言概述
- QT获取系统时间,以及设置日期格式
- MIME type类型
- cvFillConvexPoly cvFillPoly cvPolyLine
- EBS FORM開發總結(2)
- SQL SERVER 2008的错误日志清理
- Linux下网络相关命令和工具