我是PInvoke电话的新手。我用谷歌搜索了这个问题似乎是一个简单的问题,但没有快乐。
我正在连续进行多个Windows Mobile API调用(以检测我的应用程序是否已在运行,然后重新激活它)。一切都很好,花花公子但是我想在没有工作的时候输入日志等等。
在编写此代码时,我发现即使我的调用显然返回有效结果并且后续调用成功使用了有效结果,我也会收到错误代码(证明它有效。)
E.g。我调用了CreateToolhelp32Snapshot,它为我提供了当前正在运行的进程快照的句柄。返回错误6后调用Marshal.GetLatWin32Error immediatley,这显然意味着无效句柄。但是后续调用其他方法会成功使用返回值,整个过程也可以正常工作。这肯定是这个特殊的调用设置错误6,因为如果你在调用之前调用Marshal.GetLatWin32Error它返回0。
调用Process32First和Process32Next表现出类似的行为,即他们愉快地给我处理信息但有时设置错误6.我(想)我知道这发生了,因为我在每次调用之前和之后调用Marshal.GetLatWin32Error immediatley,有时它是0之前和之后的6。目前我总是得到一个成功的结果(一个句柄适用于后续调用,或者一个值,如果int或者如果bool和进程信息成功复制到我的缓冲区则转换为true)。
所以......我应该关心吗?这一切都在工作....如果我找回其余进程成功使用的结果,我是否关心设置错误?我的直觉说是的,我当然可以吗?无论哪种方式,如果我显然得到一个成功的结果但是如果设置了错误代码,我怎么才能真正知道一切是否正常?关于发布一个不会给我有用的错误信息的不稳定系统我做噩梦........
更新
这是我的loggong代码的结果,试图说明我正在谈论的任何感兴趣的人。简而言之,代码检查已经运行的自身副本,然后检查已经运行的客户端副本,然后重新激活客户端(如果找到它)。
Method name: CreateToolhelp32Snapshot, Result: 605618176, Pre-call error code: 0, Post-call error code: 6
Method name: Process32First, Result: True, Pre-call error code: 6, Post-call error code: 6
Method name: Process32Next, Result: True, Pre-call error code: 6, Post-call error code: 6
Method name: Process32Next, Result: True, Pre-call error code: 6, Post-call error code: 0
Method name: Process32Next, Result: True, Pre-call error code: 0, Post-call error code: 0
Method name: Process32Next, Result: True, Pre-call error code: 0, Post-call error code: 0
Method name: Process32Next, Result: True, Pre-call error code: 0, Post-call error code: 0
Method name: Process32Next, Result: True, Pre-call error code: 0, Post-call error code: 0
Method name: Process32Next, Result: True, Pre-call error code: 0, Post-call error code: 0
Method name: Process32Next, Result: True, Pre-call error code: 0, Post-call error code: 0
Method name: Process32Next, Result: True, Pre-call error code: 0, Post-call error code: 0
Method name: Process32Next, Result: True, Pre-call error code: 0, Post-call error code: 0
Method name: Process32Next, Result: True, Pre-call error code: 0, Post-call error code: 0
Method name: Process32Next, Result: True, Pre-call error code: 0, Post-call error code: 0
Method name: Process32Next, Result: True, Pre-call error code: 0, Post-call error code: 0
Method name: Process32Next, Result: True, Pre-call error code: 0, Post-call error code: 0
Method name: Process32Next, Result: True, Pre-call error code: 0, Post-call error code: 0
Method name: Process32Next, Result: True, Pre-call error code: 0, Post-call error code: 0
Method name: Process32Next, Result: True, Pre-call error code: 0, Post-call error code: 0
Method name: Process32Next, Result: True, Pre-call error code: 0, Post-call error code: 0
Method name: Process32Next, Result: True, Pre-call error code: 0, Post-call error code: 0
Method name: Process32Next, Result: False, Pre-call error code: 0, Post-call error code: 18
Method name: CloseToolhelp32Snapshot, Result: True, Pre-call error code: 18, Post-call error code: 6
Method name: CreateToolhelp32Snapshot, Result: 605618176, Pre-call error code: 6, Post-call error code: 0
Method name: Process32First, Result: True, Pre-call error code: 0, Post-call error code: 0
Method name: Process32Next, Result: True, Pre-call error code: 0, Post-call error code: 0
Method name: Process32Next, Result: True, Pre-call error code: 0, Post-call error code: 0
Method name: Process32Next, Result: True, Pre-call error code: 0, Post-call error code: 0
Method name: Process32Next, Result: True, Pre-call error code: 0, Post-call error code: 0
Method name: Process32Next, Result: True, Pre-call error code: 0, Post-call error code: 0
Method name: Process32Next, Result: True, Pre-call error code: 0, Post-call error code: 0
Method name: Process32Next, Result: True, Pre-call error code: 0, Post-call error code: 0
Method name: Process32Next, Result: True, Pre-call error code: 0, Post-call error code: 0
Method name: Process32Next, Result: True, Pre-call error code: 0, Post-call error code: 0
Method name: Process32Next, Result: True, Pre-call error code: 0, Post-call error code: 0
Method name: Process32Next, Result: True, Pre-call error code: 0, Post-call error code: 0
Method name: Process32Next, Result: True, Pre-call error code: 0, Post-call error code: 0
Method name: Process32Next, Result: True, Pre-call error code: 0, Post-call error code: 0
Method name: Process32Next, Result: True, Pre-call error code: 0, Post-call error code: 0
Method name: Process32Next, Result: True, Pre-call error code: 0, Post-call error code: 0
Method name: Process32Next, Result: True, Pre-call error code: 0, Post-call error code: 0
Method name: Process32Next, Result: True, Pre-call error code: 0, Post-call error code: 0
Method name: Process32Next, Result: True, Pre-call error code: 0, Post-call error code: 0
Method name: Process32Next, Result: True, Pre-call error code: 0, Post-call error code: 0
Method name: Process32Next, Result: False, Pre-call error code: 0, Post-call error code: 18
Method name: CloseToolhelp32Snapshot, Result: True, Pre-call error code: 18, Post-call error code: 0
Method name: ShowWindow, Result: True, Pre-call error code: 0, Post-call error code: 120
Method name: SetForegroundWindow, Result: True, Pre-call error code: 120, Post-call error code: 6
结果始终为真,除了最后一次调用Process32Next,其中代码为18的失败表示已达到最后一个条目。我刚刚注意到ShowWindow导致错误120,这是不支持的,所以我会接受这个调用。
所以我的两个实际问题是:
为什么在我取得成功时出现错误?
并且,当预呼叫和呼叫后错误相同时,我如何知道这是旧错误还是第二次出现相同错误?
答案 0 :(得分:2)
除非CreateToolhelp32Snapshot返回INVALID_HANDLE_VALUE,否则不应调用GetLastWin32Error(GetLastError)。
请参阅 http://msdn.microsoft.com/en-us/library/ms682489(VS.85).aspx
答案 1 :(得分:1)
您希望GetLastError()在每次成功调用任何函数后返回0。但这意味着这些函数在其结尾中调用SetLastError(0)并不总是这样。这就是为什么你无法区分旧的错误代码和新的错误代码。仅针对MSDN告诉您这样做的情况调用GetLastError()。