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
- MatrixUtil.cs
- ReadContentAsBinaryHelper.cs
- DesignerVerbCollection.cs
- BoundColumn.cs
- MaskedTextBoxDesignerActionList.cs
- XmlSchemaSimpleTypeList.cs
- TemplatedWizardStep.cs
- CodeCommentStatement.cs
- GridItemCollection.cs
- HitTestFilterBehavior.cs
- TemplatedMailWebEventProvider.cs
- TimeZone.cs
- ValuePatternIdentifiers.cs
- ContextQuery.cs
- ContactManager.cs
- GeneralTransform3D.cs
- ModelVisual3D.cs
- DbMetaDataFactory.cs
- WorkflowQueueInfo.cs
- BindUriHelper.cs
- DbParameterHelper.cs
- CompiledRegexRunner.cs
- GeometryDrawing.cs
- PresentationTraceSources.cs
- OleDbTransaction.cs
- FileDialogCustomPlacesCollection.cs
- StatusBarItem.cs
- TypeRestriction.cs
- InheritanceContextChangedEventManager.cs
- FontEditor.cs
- StyleXamlParser.cs
- PeerCredential.cs
- WebBrowserEvent.cs
- BamlResourceDeserializer.cs
- StructuredTypeEmitter.cs
- ListViewInsertionMark.cs
- RegexWriter.cs
- OutputCacheProfile.cs
- localization.cs
- InputDevice.cs
- ComAdminWrapper.cs
- DataGridViewRowPostPaintEventArgs.cs
- PageTheme.cs
- formatstringdialog.cs
- TextRunCache.cs
- ConfigXmlDocument.cs
- TransformFinalBlockRequest.cs
- Buffer.cs
- CachedCompositeFamily.cs
- AttributeCallbackBuilder.cs
- SqlUtils.cs
- SocketElement.cs
- ContainerActivationHelper.cs
- RoleGroupCollectionEditor.cs
- PerformanceCountersElement.cs
- ConstraintEnumerator.cs
- Bidi.cs
- _NetRes.cs
- BufferedReceiveElement.cs
- _TimerThread.cs
- SqlNodeAnnotations.cs
- SqlConnectionString.cs
- ObfuscateAssemblyAttribute.cs
- UnsafeNativeMethods.cs
- PageContent.cs
- ToolStripOverflowButton.cs
- MetadataItemCollectionFactory.cs
- HtmlEncodedRawTextWriter.cs
- ExpressionEditorAttribute.cs
- DSASignatureDeformatter.cs
- FileDialogPermission.cs
- TraceContextRecord.cs
- PolyLineSegment.cs
- PropertySourceInfo.cs
- EntityTypeEmitter.cs
- Vector3DCollection.cs
- RegexWriter.cs
- IndexerNameAttribute.cs
- BamlRecordWriter.cs
- HttpResponseHeader.cs
- SchemaCompiler.cs
- VisualTreeHelper.cs
- SrgsRuleRef.cs
- MenuItem.cs
- querybuilder.cs
- TableCell.cs
- QuotedPairReader.cs
- ObjectViewFactory.cs
- AnchoredBlock.cs
- ReaderContextStackData.cs
- BitmapEffectrendercontext.cs
- Parser.cs
- CancelEventArgs.cs
- IxmlLineInfo.cs
- RectangleF.cs
- ComplexBindingPropertiesAttribute.cs
- ScaleTransform.cs
- SqlBuilder.cs
- PageDeviceFont.cs
- HtmlLinkAdapter.cs