Windows Mobile API呼び出し-成功したが、GetLastWin32Errorはエラーコードを返します-心配する必要がありますか?

StackOverflow https://stackoverflow.com/questions/606048

質問

私はPInvoke呼び出しの初心者です。私はこれをグーグルで検索しましたが、それは十分に単純な質問のようですが、喜びではありません。

複数のWindows Mobile API呼び出しを連続して実行しています(アプリが既に実行されているかどうかを検出し、再度アクティブにするため)。すべてがうまく動作しますが、うまく動作しないときのためにログなどを入れたいと思いました。

このコードを書いている間に、呼び出しが明らかに有効な結果を返し、有効な結果が後続の呼び出しで正常に使用された場合でもエラーコードを取得することがわかりました(有効だと思います)

E.g。 CreateToolhelp32Snapshotを呼び出して、現在実行中のプロセスのスナップショットへのハンドルを返します。すぐにMarshal.GetLatWin32Errorを呼び出すと、明らかに無効なハンドルを意味するエラー6が返されます。ただし、返された値は他のメソッドへの後続の呼び出しで正常に使用され、プロセス全体が機能します。エラー6を設定するのは間違いなくこの呼び出しです。呼び出しの直前にMarshal.GetLatWin32Errorを呼び出すと、0が返されるためです。

Process32FirstとProcess32Nextの呼び出しは同様の動作を示します。つまり、プロセス情報を喜んで提供しますが、エラー6を設定することもあります。前と後6。現在、私は常に成功した結果を取得しています(後続の呼び出しで機能するハンドル、またはintの場合は1にキャストする値、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        

結果は、コード18の失敗が最後のエントリに到達したことを示すProcess32Nextの最後の呼び出しを除き、常に真です。そして、ShowWindowがサポートされていないエラー120になっていることに気付いたので、その呼び出しを取り上げます。

つまり、私の2つの質問は次のとおりです。

成功したときにエラーが発生するのはなぜですか?
また、呼び出し前のエラーと呼び出し後のエラーが同じ場合、これが古いエラーであるか、同じエラーの2回目の発生であるかをどのようにして確認できますか?

役に立ちましたか?

解決

CreateToolhelp32SnapshotがINVALID_HANDLE_VALUEを返さない限り、GetLastWin32Error(GetLastError)を呼び出すべきではありません。

http://msdn.microsoft.com/en-us/library/ms682489( VS.85).aspx

他のヒント

GetLastError()は、関数の呼び出しが成功するたびに0を返すことを期待しています。しかし、これは、これらの関数がエピローグでSetLastError(0)を呼び出すことを意味しますが、これは常に実行されるわけではありません。そのため、古いエラーコードと新しいエラーコードを区別できません。 MSDNから指示された場合にのみGetLastError()を呼び出してください。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top