Code:
/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / ServiceModel / System / ServiceModel / Activation / SharedMemory.cs / 1 / SharedMemory.cs
//---------------------------------------------------------------------------- // Copyright (c) Microsoft Corporation. All rights reserved. //--------------------------------------------------------------------------- namespace System.ServiceModel.Activation { using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Security.Principal; using System.ServiceModel.Channels; using System.Threading; using System.ServiceModel; using System.ComponentModel; unsafe class SharedMemory : IDisposable { SafeFileMappingHandle fileMapping; SharedMemory(SafeFileMappingHandle fileMapping) { this.fileMapping = fileMapping; } public static unsafe SharedMemory Create(string name, Guid content, ListallowedSids) { int errorCode = UnsafeNativeMethods.ERROR_SUCCESS; byte[] binarySecurityDescriptor = SecurityDescriptorHelper.FromSecurityIdentifiers(allowedSids, UnsafeNativeMethods.GENERIC_READ); SafeFileMappingHandle fileMapping; UnsafeNativeMethods.SECURITY_ATTRIBUTES securityAttributes = new UnsafeNativeMethods.SECURITY_ATTRIBUTES(); fixed (byte* pinnedSecurityDescriptor = binarySecurityDescriptor) { securityAttributes.lpSecurityDescriptor = (IntPtr)pinnedSecurityDescriptor; fileMapping = UnsafeNativeMethods.CreateFileMapping((IntPtr)(-1), securityAttributes, UnsafeNativeMethods.PAGE_READWRITE, 0, sizeof(SharedMemoryContents), name); errorCode = Marshal.GetLastWin32Error(); } if (fileMapping.IsInvalid) { fileMapping.SetHandleAsInvalid(); fileMapping.Close(); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new Win32Exception(errorCode)); } SharedMemory sharedMemory = new SharedMemory(fileMapping); SafeViewOfFileHandle view; // Ignore return value. GetView(fileMapping, true, out view); try { SharedMemoryContents* contents = (SharedMemoryContents*)view.DangerousGetHandle(); contents->pipeGuid = content; Thread.MemoryBarrier(); contents->isInitialized = true; return sharedMemory; } finally { view.Close(); } } public void Dispose() { if (fileMapping != null) { fileMapping.Close(); fileMapping = null; } } static bool GetView(SafeFileMappingHandle fileMapping, bool writable, out SafeViewOfFileHandle handle) { handle = UnsafeNativeMethods.MapViewOfFile(fileMapping, writable ? UnsafeNativeMethods.FILE_MAP_WRITE : UnsafeNativeMethods.FILE_MAP_READ, 0, 0, (IntPtr)sizeof(SharedMemoryContents)); int errorCode = Marshal.GetLastWin32Error(); if (!handle.IsInvalid) { return true; } else { handle.SetHandleAsInvalid(); fileMapping.Close(); // Only return false when it's reading time. if (!writable && errorCode == UnsafeNativeMethods.ERROR_FILE_NOT_FOUND) { return false; } throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new Win32Exception(errorCode)); } } public static string Read(string name) { string content; if (Read(name, out content)) { return content; } throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new Win32Exception(UnsafeNativeMethods.ERROR_FILE_NOT_FOUND)); } public static bool Read(string name, out string content) { content = null; SafeFileMappingHandle fileMapping = UnsafeNativeMethods.OpenFileMapping(UnsafeNativeMethods.FILE_MAP_READ, false, ListenerConstants.GlobalPrefix + name); int errorCode = Marshal.GetLastWin32Error(); if (fileMapping.IsInvalid) { fileMapping.SetHandleAsInvalid(); fileMapping.Close(); if (errorCode == UnsafeNativeMethods.ERROR_FILE_NOT_FOUND) { return false; } throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new Win32Exception(errorCode)); } try { SafeViewOfFileHandle view; if (!GetView(fileMapping, false, out view)) { return false; } try { SharedMemoryContents* contents = (SharedMemoryContents*)view.DangerousGetHandle(); content = contents->isInitialized ? contents->pipeGuid.ToString() : null; return true; } finally { view.Close(); } } finally { fileMapping.Close(); } } [StructLayout(LayoutKind.Sequential)] struct SharedMemoryContents { public bool isInitialized; public Guid pipeGuid; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- QuaternionAnimationBase.cs
- CollectionAdapters.cs
- ItemCheckedEvent.cs
- EncoderExceptionFallback.cs
- Solver.cs
- RawStylusInputCustomData.cs
- DataGridViewToolTip.cs
- LicFileLicenseProvider.cs
- BindingMAnagerBase.cs
- XmlSchemaSimpleTypeRestriction.cs
- sqlcontext.cs
- TaskForm.cs
- Stackframe.cs
- InstancePersistenceEvent.cs
- SQLDateTimeStorage.cs
- WSDualHttpSecurityMode.cs
- ConnectionPoolRegistry.cs
- UIElement.cs
- RoutedEventArgs.cs
- IHttpResponseInternal.cs
- SpellCheck.cs
- Roles.cs
- MaterialGroup.cs
- EnumMember.cs
- DetailsViewInsertEventArgs.cs
- TreeViewEvent.cs
- TimeSpanConverter.cs
- TheQuery.cs
- MenuItemBinding.cs
- ConditionalBranch.cs
- SecurityContext.cs
- CommandField.cs
- LabelAutomationPeer.cs
- HtmlInputImage.cs
- CrossSiteScriptingValidation.cs
- WindowsImpersonationContext.cs
- Pkcs7Recipient.cs
- base64Transforms.cs
- SamlSubject.cs
- EndpointFilterProvider.cs
- ListViewHitTestInfo.cs
- PointLight.cs
- TextServicesLoader.cs
- SafeSystemMetrics.cs
- WSFederationHttpSecurityMode.cs
- ResourceManager.cs
- TreeNodeClickEventArgs.cs
- WindowClosedEventArgs.cs
- EntityTypeEmitter.cs
- EdmProperty.cs
- NotifyParentPropertyAttribute.cs
- ExecutionProperties.cs
- ApplicationServiceHelper.cs
- XmlReturnWriter.cs
- AttachedAnnotation.cs
- ManagedWndProcTracker.cs
- Logging.cs
- MetadataStore.cs
- WmlTextViewAdapter.cs
- AccessControlList.cs
- UpDownBase.cs
- DesignOnlyAttribute.cs
- MenuStrip.cs
- TraceLog.cs
- ConfigXmlElement.cs
- XmlSerializerVersionAttribute.cs
- XmlHelper.cs
- DataBoundControlParameterTarget.cs
- ImportOptions.cs
- WindowsSysHeader.cs
- ObjectConverter.cs
- CryptoProvider.cs
- CustomCredentialPolicy.cs
- WebSysDescriptionAttribute.cs
- DiscardableAttribute.cs
- SequenceFullException.cs
- LeaseManager.cs
- ProcessHostMapPath.cs
- safemediahandle.cs
- DrawingState.cs
- XXXOnTypeBuilderInstantiation.cs
- XmlSignificantWhitespace.cs
- SchemaNames.cs
- IdentityReference.cs
- ExtendedPropertyCollection.cs
- TableItemPatternIdentifiers.cs
- CheckableControlBaseAdapter.cs
- ButtonBaseAutomationPeer.cs
- InstanceCreationEditor.cs
- PositiveTimeSpanValidatorAttribute.cs
- GeometryCollection.cs
- StylusOverProperty.cs
- sqlstateclientmanager.cs
- IPEndPointCollection.cs
- PriorityChain.cs
- ComponentEditorForm.cs
- WhileDesigner.cs
- ItemsControl.cs
- DrawingContextWalker.cs
- DispatcherProcessingDisabled.cs