Code:
/ 4.0 / 4.0 / untmp / 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.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- PersonalizationAdministration.cs
- AppSettingsReader.cs
- DynamicPropertyHolder.cs
- Span.cs
- EntityRecordInfo.cs
- XPathArrayIterator.cs
- ServiceObjectContainer.cs
- UnsafeNativeMethodsTablet.cs
- IPEndPoint.cs
- RsaSecurityTokenAuthenticator.cs
- PropertyItem.cs
- FileInfo.cs
- TextEffectCollection.cs
- MessagePropertyFilter.cs
- EndpointDiscoveryBehavior.cs
- ConsoleKeyInfo.cs
- TextBoxBase.cs
- COM2Properties.cs
- SerialErrors.cs
- wmiprovider.cs
- infer.cs
- EdmTypeAttribute.cs
- TextProperties.cs
- ExpandCollapseProviderWrapper.cs
- GregorianCalendarHelper.cs
- Metafile.cs
- IsolatedStorageFilePermission.cs
- RtfNavigator.cs
- JulianCalendar.cs
- ArgumentValidation.cs
- IPEndPoint.cs
- MarkerProperties.cs
- MessagePartDescription.cs
- ScriptModule.cs
- JsonFormatReaderGenerator.cs
- MetafileHeader.cs
- SafeProcessHandle.cs
- Pen.cs
- StyleXamlTreeBuilder.cs
- IntSecurity.cs
- TrailingSpaceComparer.cs
- HttpProtocolReflector.cs
- PropertyToken.cs
- Span.cs
- CuspData.cs
- WindowsFormsSynchronizationContext.cs
- Calendar.cs
- Pkcs7Signer.cs
- MaskInputRejectedEventArgs.cs
- RowVisual.cs
- DataGridViewRowErrorTextNeededEventArgs.cs
- SmiConnection.cs
- CompiledQuery.cs
- WorkflowViewService.cs
- SynchronizedInputProviderWrapper.cs
- ActiveXMessageFormatter.cs
- SpellCheck.cs
- ChainOfResponsibility.cs
- HttpApplication.cs
- WsdlInspector.cs
- TableItemStyle.cs
- RestClientProxyHandler.cs
- RegexCompilationInfo.cs
- DocumentEventArgs.cs
- securitycriticaldata.cs
- CompositeDataBoundControl.cs
- CollectionViewGroupInternal.cs
- InternalTypeHelper.cs
- ContainerFilterService.cs
- HttpException.cs
- DataGridViewAccessibleObject.cs
- VisualStyleInformation.cs
- HeaderLabel.cs
- SortFieldComparer.cs
- WindowsTab.cs
- TypeBuilderInstantiation.cs
- PolicyStatement.cs
- Page.cs
- StringFreezingAttribute.cs
- PrintPageEvent.cs
- DataServiceExpressionVisitor.cs
- Brush.cs
- Timer.cs
- DependencyObjectPropertyDescriptor.cs
- UnsafeNativeMethods.cs
- InheritanceRules.cs
- TextTrailingCharacterEllipsis.cs
- TemplateLookupAction.cs
- DiagnosticEventProvider.cs
- exports.cs
- UIPermission.cs
- Int32CAMarshaler.cs
- ApplicationInterop.cs
- XmlDigitalSignatureProcessor.cs
- TextParaLineResult.cs
- Funcletizer.cs
- HtmlInputImage.cs
- SafeMILHandle.cs
- WebConfigurationHost.cs
- PageResolution.cs