Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / WCF / System.ServiceModel.Activation / System / ServiceModel / Activation / Interop / SafeHGlobalHandleCritical.cs / 1305376 / SafeHGlobalHandleCritical.cs
//------------------------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- namespace System.ServiceModel.Activation.Interop { using System; using System.Runtime; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Security; using System.Text; using Microsoft.Win32.SafeHandles; #pragma warning disable 618 // have not moved to the v4 security model yet [SecurityCritical(SecurityCriticalScope.Everything)] #pragma warning restore 618 sealed class SafeHGlobalHandleCritical : SafeHandleZeroOrMinusOneIsInvalid { SafeHGlobalHandleCritical() : base(true) { } // 0 is an Invalid Handle SafeHGlobalHandleCritical(IntPtr handle) : base(true) { Fx.Assert(handle == IntPtr.Zero, "SafeHGlobalHandleCritical constructor can only be called with IntPtr.Zero."); SetHandle(handle); } protected override bool ReleaseHandle() { Marshal.FreeHGlobal(handle); return true; } public static SafeHGlobalHandleCritical InvalidHandle { get { return new SafeHGlobalHandleCritical(IntPtr.Zero); } } public static SafeHGlobalHandleCritical AllocHGlobal(string s) { byte[] bytes = DiagnosticUtility.Utility.AllocateByteArray(checked((s.Length + 1) * 2)); Encoding.Unicode.GetBytes(s, 0, s.Length, bytes, 0); return AllocHGlobal(bytes); } public static SafeHGlobalHandleCritical AllocHGlobal(byte[] bytes) { SafeHGlobalHandleCritical result = AllocHGlobal(bytes.Length); Marshal.Copy(bytes, 0, result.DangerousGetHandle(), bytes.Length); return result; } public static SafeHGlobalHandleCritical AllocHGlobal(uint cb) { // The cast could overflow to minus. // Unfortunately, Marshal.AllocHGlobal only takes int. return AllocHGlobal((int)cb); } public static SafeHGlobalHandleCritical AllocHGlobal(int cb) { if (cb < 0) { throw FxTrace.Exception.ArgumentOutOfRange("cb", cb, SR.ValueMustBeNonNegative); } SafeHGlobalHandleCritical result = new SafeHGlobalHandleCritical(); // CER RuntimeHelpers.PrepareConstrainedRegions(); try { } finally { IntPtr ptr = Marshal.AllocHGlobal(cb); result.SetHandle(ptr); } return result; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //------------------------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- namespace System.ServiceModel.Activation.Interop { using System; using System.Runtime; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Security; using System.Text; using Microsoft.Win32.SafeHandles; #pragma warning disable 618 // have not moved to the v4 security model yet [SecurityCritical(SecurityCriticalScope.Everything)] #pragma warning restore 618 sealed class SafeHGlobalHandleCritical : SafeHandleZeroOrMinusOneIsInvalid { SafeHGlobalHandleCritical() : base(true) { } // 0 is an Invalid Handle SafeHGlobalHandleCritical(IntPtr handle) : base(true) { Fx.Assert(handle == IntPtr.Zero, "SafeHGlobalHandleCritical constructor can only be called with IntPtr.Zero."); SetHandle(handle); } protected override bool ReleaseHandle() { Marshal.FreeHGlobal(handle); return true; } public static SafeHGlobalHandleCritical InvalidHandle { get { return new SafeHGlobalHandleCritical(IntPtr.Zero); } } public static SafeHGlobalHandleCritical AllocHGlobal(string s) { byte[] bytes = DiagnosticUtility.Utility.AllocateByteArray(checked((s.Length + 1) * 2)); Encoding.Unicode.GetBytes(s, 0, s.Length, bytes, 0); return AllocHGlobal(bytes); } public static SafeHGlobalHandleCritical AllocHGlobal(byte[] bytes) { SafeHGlobalHandleCritical result = AllocHGlobal(bytes.Length); Marshal.Copy(bytes, 0, result.DangerousGetHandle(), bytes.Length); return result; } public static SafeHGlobalHandleCritical AllocHGlobal(uint cb) { // The cast could overflow to minus. // Unfortunately, Marshal.AllocHGlobal only takes int. return AllocHGlobal((int)cb); } public static SafeHGlobalHandleCritical AllocHGlobal(int cb) { if (cb < 0) { throw FxTrace.Exception.ArgumentOutOfRange("cb", cb, SR.ValueMustBeNonNegative); } SafeHGlobalHandleCritical result = new SafeHGlobalHandleCritical(); // CER RuntimeHelpers.PrepareConstrainedRegions(); try { } finally { IntPtr ptr = Marshal.AllocHGlobal(cb); result.SetHandle(ptr); } return result; } } } // 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
- ConstructorNeedsTagAttribute.cs
- ReflectionPermission.cs
- ThemeConfigurationDialog.cs
- ConnectionStringSettingsCollection.cs
- DeploymentSection.cs
- HtmlInputCheckBox.cs
- MimeMapping.cs
- ExpressionCopier.cs
- DirectionalLight.cs
- EntityParameter.cs
- WorkflowValidationFailedException.cs
- SettingsPropertyWrongTypeException.cs
- ConfigurationManager.cs
- UpDownBase.cs
- NamespaceList.cs
- TypeSemantics.cs
- TypeInfo.cs
- SHA256.cs
- OdbcStatementHandle.cs
- Message.cs
- BookmarkList.cs
- SingleTagSectionHandler.cs
- SessionPageStateSection.cs
- InputLanguageSource.cs
- MetricEntry.cs
- CriticalHandle.cs
- COAUTHINFO.cs
- XmlCharCheckingWriter.cs
- GroupBoxRenderer.cs
- ClosableStream.cs
- ComponentDispatcherThread.cs
- DbConnectionPoolGroupProviderInfo.cs
- MessageContractAttribute.cs
- KeyBinding.cs
- UpdatePanelTriggerCollection.cs
- SMSvcHost.cs
- XmlILConstructAnalyzer.cs
- FormViewRow.cs
- RotateTransform.cs
- UITypeEditor.cs
- ToolBarDesigner.cs
- DataGridViewDataErrorEventArgs.cs
- HttpClientCertificate.cs
- ObjectQuery_EntitySqlExtensions.cs
- MultiPageTextView.cs
- RuntimeDelegateArgument.cs
- DocumentXPathNavigator.cs
- MarkupCompilePass2.cs
- TableLayoutStyleCollection.cs
- SnapshotChangeTrackingStrategy.cs
- COAUTHIDENTITY.cs
- TypeUnloadedException.cs
- XPathSelectionIterator.cs
- BitmapInitialize.cs
- Merger.cs
- SHA384Managed.cs
- PackUriHelper.cs
- ZipIOLocalFileHeader.cs
- QuaternionConverter.cs
- TreeNodeMouseHoverEvent.cs
- GetFileNameResult.cs
- DbMetaDataFactory.cs
- ComplexObject.cs
- WindowsImpersonationContext.cs
- ControlParameter.cs
- DataBindingList.cs
- EntityType.cs
- ItemList.cs
- HtmlInputControl.cs
- EditorPartChrome.cs
- RoutedUICommand.cs
- DataGridViewToolTip.cs
- ObjectStorage.cs
- VScrollBar.cs
- DetailsViewPageEventArgs.cs
- BooleanAnimationUsingKeyFrames.cs
- HwndHostAutomationPeer.cs
- HMACMD5.cs
- HttpCapabilitiesEvaluator.cs
- OptimizerPatterns.cs
- TextBoxBaseDesigner.cs
- GridViewEditEventArgs.cs
- SystemMulticastIPAddressInformation.cs
- CopyNamespacesAction.cs
- RightsManagementEncryptionTransform.cs
- TextSelection.cs
- SchemaElementLookUpTable.cs
- BindingExpressionUncommonField.cs
- RuleRefElement.cs
- HttpListener.cs
- RelatedEnd.cs
- DefaultDiscoveryService.cs
- TreeNodeCollection.cs
- BuildProviderUtils.cs
- FormatterServices.cs
- ResolveNameEventArgs.cs
- SecurityTokenParametersEnumerable.cs
- Normalization.cs
- GeneratedContractType.cs
- RequestSecurityTokenResponse.cs