11

P /呼び出し時にC#の「errno」変数にアクセスすることは可能ですか?これは、Win32 GetLastError()に似ています。

4

4 に答える 4

10

方法はあると確信していますが、それはおそらく悪い考えです。ランタイムが内部処理中に影響を及ぼしたCRT関数を呼び出さなかったことをどのように保証しますerrnoか?

GetLastError同じ理由で、直接電話することもできません。はプロパティDllImportAttributeを提供するSetLastErrorため、ランタイムは最後のエラーをすぐにキャプチャし、マネージコードがを使用して読み取ることができる場所に保存することができますMarshal.GetLastWin32Error

この場合に実行できる最も堅牢なことは、実際のC作業とのキャプチャの両方を実行するCDLLを作成することだと思いますerrno。(キャプチャの周りにラッパーを作成するだけでerrnoも、上記の懸念事項があることに注意してください。)

于 2010-03-21T02:45:52.500 に答える
2

Yes, it is possible - GetLastError does exactly that. However, as binarycoder pointed out, you should not do this directly - instead, set SetLastError on your DllImport to have this performed and cached automatically (and to avoid multithreading problems or runtime-invoked functions modifying the errno value) - then, on invoking the P/Invoked function, check it's return status, and if it's showing an error condition - throw Win32Exception, which reads the value of last error automatically. Yes, even on Mono on Linux.

于 2010-03-21T10:31:53.573 に答える