Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
66 Programming Microsoft Visual C# 2005: The Base Class Library have to write code that deals with errors in the UnmanagedResourceWrapper constructor. If the constructor fails, the value of the Handle field continues to be equal to the InvalidHandle constant; the Dispose method can detect this condition and skip the cleanup code. Finally, the UnmanagedResourceWrapper class is so simple and generic that often you can copy and paste its code (with minor edits) inside other types that must manage unmanaged resources. Because it is nested inside another class, you don't even need to worry about name collisions. Note Version 2.0 of the .NET Framework introduces the SafeHandle abstract class, which makes it simpler to author classes that use unmanaged resources. Basically, a SafeHandle object is a wrapper for a Windows handle and is vaguely similar to the UnmanagedResource- Wrapper in the previous example but with many additional features, such as the protection from a kind of attack known as handle recycle attacks. You can find more information in MSDN documentation and a few articles from the BCL Team, such as https://blogs.msdn.com/ bclteam/archive/2005/03/15/396335.aspx and http://blogs.msdn.com/cbrumme/archive/2004/ 02/20/77460.aspx.