Windows错误0“ERROR_SUCCESS”是什么意思?
我写了一个Python程序,它通过管道重定向来读取另一个进程的标准输出。不过,这个程序在这一行出问题了:
print "[Input Thread] ", self.inputPipe.readline(ii)
错误信息是IOError: [Errno 0] 错误
我查了一下Windows的errno 0的解释,结果让我很困惑,因为它的定义是:
操作成功完成。
为什么一个成功完成的操作会导致错误呢?
3 个回答
0
Windows的API(应用程序接口)可能会让人感到困惑。你提到的第二个程序很可能没有正确获取到错误编号。这个编号可能被覆盖了,或者根本没有被提取出来,所以它默认变成了0。
你没有说明另一个程序是什么;但比如在.net中,调用外部函数时很容易忘记加上'设置最后错误'的标志。
[DllImport('kernel32.dll', SetLastError = true)]
https://www.medo64.com/2013/03/error-the-operation-completed-successfully/
1
我知道这个问题有点老了,但我花了不少时间想找到一个完整的答案,结果没成功。所以我决定分享一下我了解到的内容。
这个问题的完整答案是,当你调用的pInvoke方法“失败”时,并不是因为发生了错误。
你可能会想,怎么会这样呢?
举个例子,假设你需要解除一个Windows钩子,但由于代码结构有点复杂,或者你的程序设计得过于小心,导致这个方法被调用了两次。
// hook assigned earlier
// now we call our clean up code
if (NativeMethods.UnhookWindowsHookEx(HookHandle) == 0)
{
// method succeeds normally so we do not get here
Log.ReportWin32Error("Error removing hook", Marshal.GetLastWin32Error());
}
// other code runs, but the hook is never reattached,
// due to paranoid defensive program you call your clean up code twice
if (NativeMethods.UnhookWindowsHookEx(HookHandle) == 0)
{
// pInvoke method failed (return zero) because there was no hook to remove
// however there was no error, the hook was already gone thus ERROR_SUCCESS (0)
// is our last error
Log.ReportWin32Error("Error removing hook", Marshal.GetLastWin32Error());
}
3
这个名字可能会让你误解,但ERROR_SUCCESS实际上是说没有错误发生。
来自 https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382.aspx:
ERROR_SUCCESS
0 (0x0)
这个操作成功完成了。