Windows核心编程【1】小结
2012-04-26 20:57
260 查看
第一章 错误处理
1、调用Windows函数时,它会先验证你传给它的参数,然后再开始执行任务。如果传入的参数无效,或者由于其他原因导致操作无法执行,则函数的返回值将支出函数在某个方面失败了。
2、每种错误都有三种表示:一个消息ID(一个可在源代码中使用的宏,用于与GetLastError的返回值进行比较)、消息文本(描述错误的英文文本)和一个编号(应该避免使用此编号,尽量使用消息ID)。
3、调试程序时,在VS的Watch监视窗口中查看$err,hr则可以看到函数错误码。‘,hr’限定符可以让我们进一步看到错误码的消息文本。
4、Windows提供了一个函数,可以将错误代码转换为相应的文本描述。函数为FormatMessage。(http://msdn.microsoft.com/en-us/library/windows/desktop/ms679351(v=vs.85).aspx)配合MAKELANGID宏使用更佳。MAKELANGID宏http://msdn.microsoft.com/en-us/library/windows/desktop/dd373908(v=vs.85).aspx可以获取系统本地默认语言。最后贴出段代码来作为实例。
Formats a message string. The function requires a message definition as input. The message definition can come from a buffer passed
into the function. It can come from a message table resource in an already-loaded module. Or the caller can ask the function to search the system's message table resource(s) for the message definition. The function finds the message definition in a message
table resource based on a message identifier and a language identifier. The function copies the formatted message text to an output buffer, processing any embedded insert sequences if requested.
5、定义自己的错误代码,SetLastError函数。(http://msdn.microsoft.com/en-us/library/windows/desktop/ms680627(v=vs.85).aspx)
Sets the last-error code for the calling thread.
实例代码段如下:
1、调用Windows函数时,它会先验证你传给它的参数,然后再开始执行任务。如果传入的参数无效,或者由于其他原因导致操作无法执行,则函数的返回值将支出函数在某个方面失败了。
2、每种错误都有三种表示:一个消息ID(一个可在源代码中使用的宏,用于与GetLastError的返回值进行比较)、消息文本(描述错误的英文文本)和一个编号(应该避免使用此编号,尽量使用消息ID)。
3、调试程序时,在VS的Watch监视窗口中查看$err,hr则可以看到函数错误码。‘,hr’限定符可以让我们进一步看到错误码的消息文本。
4、Windows提供了一个函数,可以将错误代码转换为相应的文本描述。函数为FormatMessage。(http://msdn.microsoft.com/en-us/library/windows/desktop/ms679351(v=vs.85).aspx)配合MAKELANGID宏使用更佳。MAKELANGID宏http://msdn.microsoft.com/en-us/library/windows/desktop/dd373908(v=vs.85).aspx可以获取系统本地默认语言。最后贴出段代码来作为实例。
Formats a message string. The function requires a message definition as input. The message definition can come from a buffer passed
into the function. It can come from a message table resource in an already-loaded module. Or the caller can ask the function to search the system's message table resource(s) for the message definition. The function finds the message definition in a message
table resource based on a message identifier and a language identifier. The function copies the formatted message text to an output buffer, processing any embedded insert sequences if requested.
Syntax
DWORD WINAPI FormatMessage( __in DWORD dwFlags, __in_opt LPCVOID lpSource, __in DWORD dwMessageId, __in DWORD dwLanguageId, __out LPTSTR lpBuffer, __in DWORD nSize, __in_opt va_list *Arguments );
5、定义自己的错误代码,SetLastError函数。(http://msdn.microsoft.com/en-us/library/windows/desktop/ms680627(v=vs.85).aspx)
Sets the last-error code for the calling thread.
Syntax
void WINAPI SetLastError( __in DWORD dwErrCode );
实例代码段如下:
// Get the error code DWORD dwError = GetDlgItemInt(hwnd, IDC_ERRORCODE, NULL, FALSE); HLOCAL hlocal = NULL; // Buffer that gets the error message string // Use the default system locale since we look for Windows messages. DWORD systemLocale = MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL); // Get the error code's textual description BOOL fOk = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, dwError, systemLocale, (PTSTR) &hlocal, 0, NULL); if (!fOk) { // Is it a network-related error? HMODULE hDll = LoadLibraryEx(TEXT("netmsg.dll"), NULL, DONT_RESOLVE_DLL_REFERENCES); if (hDll != NULL) { fOk = FormatMessage( FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ALLOCATE_BUFFER, hDll, dwError, systemLocale, (PTSTR) &hlocal, 0, NULL); FreeLibrary(hDll); } } if (fOk && (hlocal != NULL)) { SetDlgItemText(hwnd, IDC_ERRORTEXT, (PCTSTR) LocalLock(hlocal)); LocalFree(hlocal); } else { SetDlgItemText(hwnd, IDC_ERRORTEXT, TEXT("No text found for this error number.")); }
相关文章推荐
- Windows核心编程【8】小结
- Windows核心编程【21】小结
- Windows核心编程小结3
- 核心编程边读边小结(1)
- windows核心编程心解--内核对象
- Windows编程【7】小结
- windows核心编程知识点
- Windows 核心编程之10 IO完成端口
- windows 编程杂谈 (小结)
- 基于visual c++之windows核心编程代码分析(26)实现文件关联
- 基于visual c++之windows核心编程代码分析(34)WinIo驱动级模拟按键的实现
- 基于visual c++之windows核心编程代码分析(37)实践信息安全隐患监测-嗅探
- 基于visual c++之windows核心编程代码分析(48)编程实现远程登录3389
- 基于visual c++之windows核心编程代码分析(55)使用GoogleUrl方便安全地解析URL
- 基于visual c++之windows核心编程代码分析(63)无模块dll进程注射
- windows 核心编程之在应用程序中使用虚拟内存
- windows核心编程-8.用户模式下的线程同步
- 读Windows核心编程 - 4
- 读Windows核心编程 - 6
- Windows核心编程学习笔记----第二章 UNICODE