c++ - 如何为不同的 HRESULT 值正确使用 On Error Goto

标签 c++ vb6

我正在尝试读取一个文件。调用是从 VB 到 CPP dll。

这是我的示例代码片段

VB调用

  Private Sub ReadFile(...)
    On Error GoTo Problem
    Dim errorString As String
    Sample.ReadFile basepath, filename, register, errorString
    GoTo Completed
Problem:
    WriteToLogFile basepath + filename + errorString //error string contains the formatted hresult message from cpp dll

我的 CPP 功能:

HRESULT ReadFile(...)
{

hr= actualread(...)
if(FAILED(hr)
{
return E_FAIL // 
}

如果我将 E_FAIL 更改为 ERROR_FILE_NOT_FOUND VB 不会记录错误消息。

E_FAIL 消息描述为未指定错误。这对用户没有太大帮助。

它记录 E_POINTER、E_HANDLE 等任何以 E_ 开头而不是以 ERROR_ 开头的内容

最佳答案

COM 指定 HRESULT 中的哪些值被视为错误,哪些不是。引用自Structure of COM Error Codes :

The high-order bit in the HRESULT or SCODE indicates whether the return value represents success or failure. If set to 0, SEVERITY_SUCCESS, the value indicates success. If set to 1, SEVERITY_ERROR, it indicates failure.

E_ 常量被设计为以这种方式使用,并具有适当的值以确保它们被视为错误。 ERROR_ 常量不是,它们用于报告错误的不同约定。

正如 WhozCraig 在评论中指出的那样,有一个 HRESULT_FROM_WIN32 函数可用于将 Win32 错误代码转换为 HRESULT

关于c++ - 如何为不同的 HRESULT 值正确使用 On Error Goto,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28318043/

相关文章:

c++ - 缓存未命中的代价是什么

c++ - 对 2dim vector 进行排序并保留索引

c# - 我可以在 .Net 和 WPF 中这样做吗?

python - 无法使用 VB6 程序从 VB6 COM DLL 创建对象

c++ - iOS:如何在运行时使用音频单元对音频(PCM 数据)进行重新采样?

c++ - Casablanca 可以用作网络服务器来为 Assets 提供服务吗?

c++ - 使用 tbb 从数组中进行并行保序选择

VB6 局部变量作用域

sql-server - 如何保护 Vb6 应用程序和 mssql 服务器之间的连接?

C#/VB.Net 问题 : VB.Net 填充字符串数组但 C# 没有