我正在尝试读取一个文件。调用是从 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/