Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / ndp / fx / src / SystemNet / Net / UnsafeMethods.cs / 1 / UnsafeMethods.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace System.Net { using System; using System.Collections.Generic; using System.Text; using Microsoft.Win32.SafeHandles; using System.Security; using System.Runtime.InteropServices; using System.Runtime.ConstrainedExecution; using System.Threading; using System.Net.Sockets; [Flags] internal enum FormatMessageFlags : uint { FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100, //FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200, //FORMAT_MESSAGE_FROM_STRING = 0x00000400, FORMAT_MESSAGE_FROM_HMODULE = 0x00000800, FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000, FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x00002000 } [System.Security.SuppressUnmanagedCodeSecurityAttribute()] internal static class UnsafeSystemNativeMethods { private const string KERNEL32 = "KERNEL32.dll"; [System.Security.SecurityCritical] [DllImport(KERNEL32, ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)] internal static extern unsafe SafeLoadLibrary LoadLibraryExW( string lpwLibFileName, [In] void* hFile, uint dwFlags); [DllImport(KERNEL32, ExactSpelling = true, SetLastError = true)] [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] [return: MarshalAs(UnmanagedType.Bool)] internal static extern unsafe bool FreeLibrary(IntPtr hModule); [System.Security.SecurityCritical] [DllImport(KERNEL32, EntryPoint = "GetProcAddress", SetLastError = true, BestFitMapping = false)] internal extern static IntPtr GetProcAddress(SafeLoadLibrary hModule, string entryPoint); [DllImport(KERNEL32, CharSet = CharSet.Unicode)] internal extern static uint FormatMessage( FormatMessageFlags dwFlags, IntPtr lpSource, UInt32 dwMessageId, UInt32 dwLanguageId, ref IntPtr lpBuffer, UInt32 nSize, IntPtr vaArguments ); [DllImport(KERNEL32, CharSet = CharSet.Unicode)] internal extern static uint LocalFree(IntPtr lpMem); } //// [System.Security.SecurityCritical(System.Security.SecurityCriticalScope.Everything)] [System.Security.SuppressUnmanagedCodeSecurityAttribute()] internal sealed class SafeLoadLibrary : SafeHandleZeroOrMinusOneIsInvalid { private const string KERNEL32 = "kernel32.dll"; private SafeLoadLibrary() : base(true) { } //private SafeLoadLibrary(bool ownsHandle) : base(ownsHandle) { } //internal static readonly SafeLoadLibrary Zero = new SafeLoadLibrary(false); internal unsafe static SafeLoadLibrary LoadLibraryEx(string library) { SafeLoadLibrary result = UnsafeSystemNativeMethods.LoadLibraryExW(library, null, 0); if (result.IsInvalid) { //NOTE: //IsInvalid tests the numeric value of the handle. //SetHandleAsInvalid sets the handle as closed, so that further closing //does not have to take place in the critical finalizer thread. // //You would think that when you assign 0 or -1 to an instance of //SafeHandleZeroOrMinusoneIsInvalid, the handle will not be closed, since after all it is invalid //It turns out that the SafeHandleZetoOrMinusOneIsInvalid overrides only the IsInvalid() method //It does not do anything to automatically close it. //So we have to SetHandleAsInvalid --> Which means mark it closed -- so that //we will not eventually call CloseHandle on 0 or -1 result.SetHandleAsInvalid(); } return result; } protected override bool ReleaseHandle() { return UnsafeSystemNativeMethods.FreeLibrary(handle); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //------------------------------------------------------------------------------ //// // Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace System.Net { using System; using System.Collections.Generic; using System.Text; using Microsoft.Win32.SafeHandles; using System.Security; using System.Runtime.InteropServices; using System.Runtime.ConstrainedExecution; using System.Threading; using System.Net.Sockets; [Flags] internal enum FormatMessageFlags : uint { FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100, //FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200, //FORMAT_MESSAGE_FROM_STRING = 0x00000400, FORMAT_MESSAGE_FROM_HMODULE = 0x00000800, FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000, FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x00002000 } [System.Security.SuppressUnmanagedCodeSecurityAttribute()] internal static class UnsafeSystemNativeMethods { private const string KERNEL32 = "KERNEL32.dll"; [System.Security.SecurityCritical] [DllImport(KERNEL32, ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)] internal static extern unsafe SafeLoadLibrary LoadLibraryExW( string lpwLibFileName, [In] void* hFile, uint dwFlags); [DllImport(KERNEL32, ExactSpelling = true, SetLastError = true)] [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] [return: MarshalAs(UnmanagedType.Bool)] internal static extern unsafe bool FreeLibrary(IntPtr hModule); [System.Security.SecurityCritical] [DllImport(KERNEL32, EntryPoint = "GetProcAddress", SetLastError = true, BestFitMapping = false)] internal extern static IntPtr GetProcAddress(SafeLoadLibrary hModule, string entryPoint); [DllImport(KERNEL32, CharSet = CharSet.Unicode)] internal extern static uint FormatMessage( FormatMessageFlags dwFlags, IntPtr lpSource, UInt32 dwMessageId, UInt32 dwLanguageId, ref IntPtr lpBuffer, UInt32 nSize, IntPtr vaArguments ); [DllImport(KERNEL32, CharSet = CharSet.Unicode)] internal extern static uint LocalFree(IntPtr lpMem); } //// [System.Security.SecurityCritical(System.Security.SecurityCriticalScope.Everything)] [System.Security.SuppressUnmanagedCodeSecurityAttribute()] internal sealed class SafeLoadLibrary : SafeHandleZeroOrMinusOneIsInvalid { private const string KERNEL32 = "kernel32.dll"; private SafeLoadLibrary() : base(true) { } //private SafeLoadLibrary(bool ownsHandle) : base(ownsHandle) { } //internal static readonly SafeLoadLibrary Zero = new SafeLoadLibrary(false); internal unsafe static SafeLoadLibrary LoadLibraryEx(string library) { SafeLoadLibrary result = UnsafeSystemNativeMethods.LoadLibraryExW(library, null, 0); if (result.IsInvalid) { //NOTE: //IsInvalid tests the numeric value of the handle. //SetHandleAsInvalid sets the handle as closed, so that further closing //does not have to take place in the critical finalizer thread. // //You would think that when you assign 0 or -1 to an instance of //SafeHandleZeroOrMinusoneIsInvalid, the handle will not be closed, since after all it is invalid //It turns out that the SafeHandleZetoOrMinusOneIsInvalid overrides only the IsInvalid() method //It does not do anything to automatically close it. //So we have to SetHandleAsInvalid --> Which means mark it closed -- so that //we will not eventually call CloseHandle on 0 or -1 result.SetHandleAsInvalid(); } return result; } protected override bool ReleaseHandle() { return UnsafeSystemNativeMethods.FreeLibrary(handle); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.//
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- SeverityFilter.cs
- CompilerErrorCollection.cs
- Root.cs
- SerializerProvider.cs
- LazyTextWriterCreator.cs
- FamilyCollection.cs
- StringResourceManager.cs
- ProgressiveCrcCalculatingStream.cs
- WebControlAdapter.cs
- COM2IProvidePropertyBuilderHandler.cs
- FamilyTypeface.cs
- HtmlTableRow.cs
- TransformerConfigurationWizardBase.cs
- XmlnsDictionary.cs
- AncillaryOps.cs
- NetTcpBinding.cs
- FusionWrap.cs
- CornerRadiusConverter.cs
- InfoCardRSAPKCS1SignatureFormatter.cs
- Bits.cs
- ZipIOCentralDirectoryDigitalSignature.cs
- DataRowCollection.cs
- BindingNavigator.cs
- DataRowExtensions.cs
- AnyAllSearchOperator.cs
- FontWeight.cs
- ToolStripProgressBar.cs
- DataContractAttribute.cs
- UnsafeNativeMethods.cs
- CodeSubDirectoriesCollection.cs
- CalendarDay.cs
- SqlTypesSchemaImporter.cs
- clipboard.cs
- ServiceDesigner.xaml.cs
- GACMembershipCondition.cs
- EmbeddedMailObject.cs
- ThreadExceptionEvent.cs
- ReadOnlyKeyedCollection.cs
- CursorConverter.cs
- ConnectionProviderAttribute.cs
- OutOfMemoryException.cs
- DataKeyArray.cs
- Membership.cs
- XmlSchemaAnyAttribute.cs
- BCLDebug.cs
- BindingNavigator.cs
- XamlWriter.cs
- DependencyObjectCodeDomSerializer.cs
- PolyQuadraticBezierSegment.cs
- HttpProcessUtility.cs
- PointHitTestParameters.cs
- StatusBarItem.cs
- SimpleRecyclingCache.cs
- WebResponse.cs
- GridViewRowCollection.cs
- ScrollChangedEventArgs.cs
- RectAnimationUsingKeyFrames.cs
- FormViewModeEventArgs.cs
- VideoDrawing.cs
- CalendarDataBindingHandler.cs
- HorizontalAlignConverter.cs
- JsonEnumDataContract.cs
- OleDbParameterCollection.cs
- NeutralResourcesLanguageAttribute.cs
- ReferencedCollectionType.cs
- AsymmetricKeyExchangeDeformatter.cs
- ListItem.cs
- Int64AnimationUsingKeyFrames.cs
- LabelEditEvent.cs
- UIElementCollection.cs
- GridView.cs
- ZipIOExtraFieldPaddingElement.cs
- TableParagraph.cs
- ResolvePPIDRequest.cs
- XamlRtfConverter.cs
- HealthMonitoringSectionHelper.cs
- EventLog.cs
- RequestQueue.cs
- CheckBox.cs
- ObjectPersistData.cs
- ByValueEqualityComparer.cs
- DefaultExpression.cs
- SchemaImporterExtensionsSection.cs
- Stream.cs
- RowType.cs
- Rotation3DAnimation.cs
- DbConnectionPoolGroupProviderInfo.cs
- NameTable.cs
- StorageEntityContainerMapping.cs
- DataGridViewCellEventArgs.cs
- DocumentPageHost.cs
- ConfigXmlWhitespace.cs
- Journal.cs
- DbConnectionStringCommon.cs
- AppDomain.cs
- SqlRowUpdatedEvent.cs
- HealthMonitoringSectionHelper.cs
- ConditionValidator.cs
- DiscoveryMessageSequence.cs
- TextEditorTyping.cs