Handling HRESULTs

本文详细介绍了HRESULT的结构及其在组件间表示成功或失败的作用。HRESULT由三个部分组成:严重性位、设施代码和状态代码。文章还提供了查找HRESULT含义的方法,并通过实例展示了如何在调试器中查看HRESULT描述。

I've already shown some simple error handling, using the SUCCEEDED and FAILED macros. Now I'll give some more details on what to do with the HRESULTs returned from COM methods.

An HRESULT is a 32-bit signed integer, with nonnegative values indicating success, and negative values indicating failure. An HRESULT has three fields: the severity bit (to indicate success or failure), the facility code, and the status code. The "facility" indicates what component or program theHRESULT is coming from. Microsoft assigns facility codes to the various components, for exampleCOM has one, the Task Scheduler has one, and so on. The "code" is a 16-bit field that has no intrinsic meaning; the codes are just an arbitrary association between a number and a meaning, just like the values returned by GetLastError().

If you look up error codes in the winerror.h file, you'll see a lot of HRESULTs listed, with the naming convention [facility]_[severity]_[description]. Generic HRESULTs that can be returned by anycomponent (like E_OUTOFMEMORY) have no facility in their name. Examples:

  • REGDB_E_READREGDB: Facility = REGDB, for "registry database"; E = error; READREGDB is a description of the error (couldn't read the database).
  • S_OK: Facility = generic; S = success; OK is a description of the status (everything's OK).

Fortunately, there are easier ways to determine the meaning of an HRESULT than looking throughwinerror.hHRESULTs for built-in facilities can be looked up with the Error Lookup tool. For example, say you forgot to call CoInitialize() before CoCreateInstance().CoCreateInstance() will return a value of 0x800401F0. You can enter that value into Error Lookup and you'll see the description: "CoInitialize has not been called."

 [Error Lookup screen shot - 7K]

You can also look up HRESULT descriptions in the debugger. If you have an HRESULT variable calledhres, you can view the description in the Watch window by entering "hres,hr" as the value to watch. The ",hr" tells VC to display the value as an HRESULT description.

 [Watch window - 4K]

eventmgrlib.dll 是一个与 Windows 操作系统事件管理相关的动态链接库文件,通常用于处理系统级别的事件日志(Event Logging)功能。该库属于 Windows 的一部分,常见于 Windows Vista 及其后续版本中,用于支持事件日志服务(Event Log Service)的扩展功能,例如事件日志的订阅、转发和远程管理。 ### 功能介绍 eventmgrlib.dll 的主要功能包括: - **事件日志订阅管理**:允许应用程序订阅本地或远程计算机上的事件日志,以便在特定事件发生时接收通知。 - **事件日志转发**:支持将事件日志从一个计算机转发到另一个计算机,便于集中管理和分析。 - **事件日志查询**:提供对事件日志的查询接口,支持通过 XPath 表达式筛选特定事件。 - **事件日志记录**:允许应用程序将自定义事件写入事件日志系统中。 这些功能通常通过 Windows 的事件日志 API(如 `EvtSubscribe`、`EvtQuery` 等函数)进行调用,而 eventmgrlib.dll 作为这些 API 的实现模块之一,提供了底层支持[^1]。 ### 使用方法 使用 eventmgrlib.dll 通常涉及以下几个步骤: 1. **加载动态链接库**:在程序中通过 `LoadLibrary` 函数加载 eventmgrlib.dll。 2. **获取函数指针**:使用 `GetProcAddress` 获取所需的函数地址。 3. **调用相关函数**:通过函数指针调用 eventmgrlib.dll 提供的功能函数。 以下是一个简单的示例代码,展示如何动态加载 eventmgrlib.dll 并获取一个函数地址: ```cpp #include <windows.h> #include <iostream> typedef EVT_HANDLE (*EvtQueryFunc)(EVT_HANDLE, LPCWSTR, LPCWSTR, DWORD); int main() { HMODULE hModule = LoadLibrary(L"eventmgrlib.dll"); if (hModule == NULL) { std::cerr << "Failed to load eventmgrlib.dll" << std::endl; return 1; } EvtQueryFunc pEvtQuery = (EvtQueryFunc)GetProcAddress(hModule, "EvtQuery"); if (pEvtQuery == NULL) { std::cerr << "Failed to get EvtQuery function" << std::endl; FreeLibrary(hModule); return 1; } // 调用 EvtQuery 函数 EVT_HANDLE hResults = pEvtQuery(NULL, L"System", L"*", EvtQueryChannelPath); if (hResults != NULL) { std::cout << "Successfully queried events" << std::endl; // 进一步处理事件结果... } else { std::cerr << "EvtQuery failed" << std::endl; } FreeLibrary(hModule); return 0; } ``` ### 注意事项 - **权限要求**:访问事件日志通常需要管理员权限,尤其是在访问远程日志或系统级事件时。 - **兼容性**:eventmgrlib.dll 是 Windows Vista 及以后版本的组件,不适用于 Windows XP 或更早版本。 - **错误处理**:在调用 eventmgrlib.dll 中的函数时,务必检查返回值并处理可能的错误代码,确保程序的健壮性。 ### 常见问题 在使用 eventmgrlib.dll 时,可能会遇到一些常见问题,例如: - **找不到 eventmgrlib.dll**:这可能是由于系统文件损坏或缺失导致的。可以尝试运行系统文件检查工具(如 `sfc /scannow`)来修复系统文件。 - **函数调用失败**:某些函数可能因权限不足或参数错误而失败,需确保调用时具有足够的权限,并正确设置参数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值