Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / whidbey / NetFxQFE / ndp / clr / src / BCL / System / Reflection / Emit / UnmanagedMarshal.cs / 1 / UnmanagedMarshal.cs
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
namespace System.Reflection.Emit
{
using System.Runtime.InteropServices;
using System;
using System.Security.Permissions;
// This class is describing the fieldmarshal.
[Serializable()]
[HostProtection(MayLeakOnAbort = true)]
[System.Runtime.InteropServices.ComVisible(true)]
[Obsolete("An alternate API is available: Emit the MarshalAs custom attribute instead. http://go.microsoft.com/fwlink/?linkid=14202")]
public sealed class UnmanagedMarshal
{
/******************************
[System.Runtime.InteropServices.ComVisible(true)]
* public static constructors. You can only construct
* UnmanagedMarshal using these static constructors.
******************************/
public static UnmanagedMarshal DefineUnmanagedMarshal(UnmanagedType unmanagedType)
{
if (unmanagedType == UnmanagedType.ByValTStr ||
#if FEATURE_COMINTEROP
unmanagedType == UnmanagedType.SafeArray ||
#endif //FEATURE_COMINTEROP
unmanagedType == UnmanagedType.ByValArray ||
unmanagedType == UnmanagedType.LPArray ||
unmanagedType == UnmanagedType.CustomMarshaler)
{
// not a simple native marshal
throw new ArgumentException(Environment.GetResourceString("Argument_NotASimpleNativeType"));
}
return new UnmanagedMarshal(unmanagedType, Guid.Empty, 0, (UnmanagedType) 0);
}
public static UnmanagedMarshal DefineByValTStr(int elemCount)
{
return new UnmanagedMarshal(UnmanagedType.ByValTStr, Guid.Empty, elemCount, (UnmanagedType) 0);
}
#if FEATURE_COMINTEROP
public static UnmanagedMarshal DefineSafeArray(UnmanagedType elemType)
{
return new UnmanagedMarshal(UnmanagedType.SafeArray, Guid.Empty, 0, elemType);
}
#endif //FEATURE_COMINTEROP
public static UnmanagedMarshal DefineByValArray(int elemCount)
{
return new UnmanagedMarshal(UnmanagedType.ByValArray, Guid.Empty, elemCount, (UnmanagedType) 0);
}
public static UnmanagedMarshal DefineLPArray(UnmanagedType elemType)
{
return new UnmanagedMarshal(UnmanagedType.LPArray, Guid.Empty, 0, elemType);
}
// accessor function for the native type
public UnmanagedType GetUnmanagedType
{
get { return m_unmanagedType; }
}
public Guid IIDGuid
{
get
{
if (m_unmanagedType != UnmanagedType.CustomMarshaler)
{
// throw exception here. There is Guid only if CustomMarshaler
throw new ArgumentException(Environment.GetResourceString("Argument_NotACustomMarshaler"));
}
return m_guid;
}
}
public int ElementCount
{
get
{
if (m_unmanagedType != UnmanagedType.ByValArray &&
m_unmanagedType != UnmanagedType.ByValTStr)
{
// throw exception here. There is NumElement only if NativeTypeFixedArray
throw new ArgumentException(Environment.GetResourceString("Argument_NoUnmanagedElementCount"));
}
return m_numElem;
}
}
public UnmanagedType BaseType
{
get
{
#if FEATURE_COMINTEROP
if (m_unmanagedType != UnmanagedType.LPArray && m_unmanagedType != UnmanagedType.SafeArray)
#else
if (m_unmanagedType != UnmanagedType.LPArray)
#endif //!FEATURE_COMINTEROP
{
// throw exception here. There is NestedUnmanagedType only if LPArray or SafeArray
throw new ArgumentException(Environment.GetResourceString("Argument_NoNestedMarshal"));
}
return m_baseType;
}
}
private UnmanagedMarshal(UnmanagedType unmanagedType, Guid guid, int numElem, UnmanagedType type)
{
m_unmanagedType = unmanagedType;
m_guid = guid;
m_numElem = numElem;
m_baseType = type;
}
/************************
*
* Data member
*
*************************/
internal UnmanagedType m_unmanagedType;
internal Guid m_guid;
internal int m_numElem;
internal UnmanagedType m_baseType;
/************************
* this function return the byte representation of the marshal info.
*************************/
internal byte[] InternalGetBytes()
{
byte[] buf;
#if FEATURE_COMINTEROP
if (m_unmanagedType == UnmanagedType.SafeArray || m_unmanagedType == UnmanagedType.LPArray)
#else
if (m_unmanagedType == UnmanagedType.LPArray)
#endif //!FEATURE_COMINTEROP
{
// syntax for NativeTypeSafeArray is
//
//
int cBuf = 2;
buf = new byte[cBuf];
buf[0] = (byte) (m_unmanagedType);
buf[1] = (byte) (m_baseType);
return buf;
}
else
if (m_unmanagedType == UnmanagedType.ByValArray ||
m_unmanagedType == UnmanagedType.ByValTStr)
{
//
//
int cBuf;
int iBuf = 0;
if (m_numElem <= 0x7f)
cBuf = 1;
else if (m_numElem <= 0x3FFF)
cBuf = 2;
else
cBuf = 4;
// the total buffer size is the one byte + encoded integer size
cBuf = cBuf + 1;
buf = new byte[cBuf];
buf[iBuf++] = (byte) (m_unmanagedType);
if (m_numElem <= 0x7F)
{
buf[iBuf++] = (byte)(m_numElem & 0xFF);
} else if (m_numElem <= 0x3FFF)
{
buf[iBuf++] = (byte)((m_numElem >> 8) | 0x80);
buf[iBuf++] = (byte)(m_numElem & 0xFF);
} else if (m_numElem <= 0x1FFFFFFF)
{
buf[iBuf++] = (byte)((m_numElem >> 24) | 0xC0);
buf[iBuf++] = (byte)((m_numElem >> 16) & 0xFF);
buf[iBuf++] = (byte)((m_numElem >> 8) & 0xFF);
buf[iBuf++] = (byte)((m_numElem) & 0xFF);
}
return buf;
}
buf = new byte[1];
buf[0] = (byte) (m_unmanagedType);
return buf;
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
namespace System.Reflection.Emit
{
using System.Runtime.InteropServices;
using System;
using System.Security.Permissions;
// This class is describing the fieldmarshal.
[Serializable()]
[HostProtection(MayLeakOnAbort = true)]
[System.Runtime.InteropServices.ComVisible(true)]
[Obsolete("An alternate API is available: Emit the MarshalAs custom attribute instead. http://go.microsoft.com/fwlink/?linkid=14202")]
public sealed class UnmanagedMarshal
{
/******************************
[System.Runtime.InteropServices.ComVisible(true)]
* public static constructors. You can only construct
* UnmanagedMarshal using these static constructors.
******************************/
public static UnmanagedMarshal DefineUnmanagedMarshal(UnmanagedType unmanagedType)
{
if (unmanagedType == UnmanagedType.ByValTStr ||
#if FEATURE_COMINTEROP
unmanagedType == UnmanagedType.SafeArray ||
#endif //FEATURE_COMINTEROP
unmanagedType == UnmanagedType.ByValArray ||
unmanagedType == UnmanagedType.LPArray ||
unmanagedType == UnmanagedType.CustomMarshaler)
{
// not a simple native marshal
throw new ArgumentException(Environment.GetResourceString("Argument_NotASimpleNativeType"));
}
return new UnmanagedMarshal(unmanagedType, Guid.Empty, 0, (UnmanagedType) 0);
}
public static UnmanagedMarshal DefineByValTStr(int elemCount)
{
return new UnmanagedMarshal(UnmanagedType.ByValTStr, Guid.Empty, elemCount, (UnmanagedType) 0);
}
#if FEATURE_COMINTEROP
public static UnmanagedMarshal DefineSafeArray(UnmanagedType elemType)
{
return new UnmanagedMarshal(UnmanagedType.SafeArray, Guid.Empty, 0, elemType);
}
#endif //FEATURE_COMINTEROP
public static UnmanagedMarshal DefineByValArray(int elemCount)
{
return new UnmanagedMarshal(UnmanagedType.ByValArray, Guid.Empty, elemCount, (UnmanagedType) 0);
}
public static UnmanagedMarshal DefineLPArray(UnmanagedType elemType)
{
return new UnmanagedMarshal(UnmanagedType.LPArray, Guid.Empty, 0, elemType);
}
// accessor function for the native type
public UnmanagedType GetUnmanagedType
{
get { return m_unmanagedType; }
}
public Guid IIDGuid
{
get
{
if (m_unmanagedType != UnmanagedType.CustomMarshaler)
{
// throw exception here. There is Guid only if CustomMarshaler
throw new ArgumentException(Environment.GetResourceString("Argument_NotACustomMarshaler"));
}
return m_guid;
}
}
public int ElementCount
{
get
{
if (m_unmanagedType != UnmanagedType.ByValArray &&
m_unmanagedType != UnmanagedType.ByValTStr)
{
// throw exception here. There is NumElement only if NativeTypeFixedArray
throw new ArgumentException(Environment.GetResourceString("Argument_NoUnmanagedElementCount"));
}
return m_numElem;
}
}
public UnmanagedType BaseType
{
get
{
#if FEATURE_COMINTEROP
if (m_unmanagedType != UnmanagedType.LPArray && m_unmanagedType != UnmanagedType.SafeArray)
#else
if (m_unmanagedType != UnmanagedType.LPArray)
#endif //!FEATURE_COMINTEROP
{
// throw exception here. There is NestedUnmanagedType only if LPArray or SafeArray
throw new ArgumentException(Environment.GetResourceString("Argument_NoNestedMarshal"));
}
return m_baseType;
}
}
private UnmanagedMarshal(UnmanagedType unmanagedType, Guid guid, int numElem, UnmanagedType type)
{
m_unmanagedType = unmanagedType;
m_guid = guid;
m_numElem = numElem;
m_baseType = type;
}
/************************
*
* Data member
*
*************************/
internal UnmanagedType m_unmanagedType;
internal Guid m_guid;
internal int m_numElem;
internal UnmanagedType m_baseType;
/************************
* this function return the byte representation of the marshal info.
*************************/
internal byte[] InternalGetBytes()
{
byte[] buf;
#if FEATURE_COMINTEROP
if (m_unmanagedType == UnmanagedType.SafeArray || m_unmanagedType == UnmanagedType.LPArray)
#else
if (m_unmanagedType == UnmanagedType.LPArray)
#endif //!FEATURE_COMINTEROP
{
// syntax for NativeTypeSafeArray is
//
//
int cBuf = 2;
buf = new byte[cBuf];
buf[0] = (byte) (m_unmanagedType);
buf[1] = (byte) (m_baseType);
return buf;
}
else
if (m_unmanagedType == UnmanagedType.ByValArray ||
m_unmanagedType == UnmanagedType.ByValTStr)
{
//
//
int cBuf;
int iBuf = 0;
if (m_numElem <= 0x7f)
cBuf = 1;
else if (m_numElem <= 0x3FFF)
cBuf = 2;
else
cBuf = 4;
// the total buffer size is the one byte + encoded integer size
cBuf = cBuf + 1;
buf = new byte[cBuf];
buf[iBuf++] = (byte) (m_unmanagedType);
if (m_numElem <= 0x7F)
{
buf[iBuf++] = (byte)(m_numElem & 0xFF);
} else if (m_numElem <= 0x3FFF)
{
buf[iBuf++] = (byte)((m_numElem >> 8) | 0x80);
buf[iBuf++] = (byte)(m_numElem & 0xFF);
} else if (m_numElem <= 0x1FFFFFFF)
{
buf[iBuf++] = (byte)((m_numElem >> 24) | 0xC0);
buf[iBuf++] = (byte)((m_numElem >> 16) & 0xFF);
buf[iBuf++] = (byte)((m_numElem >> 8) & 0xFF);
buf[iBuf++] = (byte)((m_numElem) & 0xFF);
}
return buf;
}
buf = new byte[1];
buf[0] = (byte) (m_unmanagedType);
return buf;
}
}
}
// 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
- LocalizationComments.cs
- OutputCacheEntry.cs
- Symbol.cs
- TextClipboardData.cs
- LinqDataSource.cs
- CommandBinding.cs
- AsyncOperationManager.cs
- ChineseLunisolarCalendar.cs
- XmlBaseReader.cs
- ContextDataSource.cs
- RuntimeResourceSet.cs
- ShortcutKeysEditor.cs
- AdjustableArrowCap.cs
- FontUnit.cs
- Cursor.cs
- DSACryptoServiceProvider.cs
- ParameterCollection.cs
- UserControl.cs
- EnvelopedSignatureTransform.cs
- BamlBinaryReader.cs
- InternalDispatchObject.cs
- IISMapPath.cs
- HandleExceptionArgs.cs
- ExtentKey.cs
- sitestring.cs
- RelatedEnd.cs
- InkCanvasInnerCanvas.cs
- DynamicMetaObjectBinder.cs
- UniqueIdentifierService.cs
- ColorBlend.cs
- X509ThumbprintKeyIdentifierClause.cs
- SimpleExpression.cs
- ListItemsCollectionEditor.cs
- ZipIOExtraFieldPaddingElement.cs
- SqlDataRecord.cs
- TogglePattern.cs
- Quad.cs
- RootProfilePropertySettingsCollection.cs
- WebPartRestoreVerb.cs
- IdentityManager.cs
- InputLanguageManager.cs
- InputReport.cs
- MetadataUtil.cs
- LinqDataSourceSelectEventArgs.cs
- ReturnValue.cs
- TextEncodedRawTextWriter.cs
- HScrollBar.cs
- SetStateDesigner.cs
- AstNode.cs
- DoubleAnimationBase.cs
- GridViewAutomationPeer.cs
- ActivitySurrogateSelector.cs
- StringValidator.cs
- DoWorkEventArgs.cs
- WeakEventManager.cs
- PropertyFilterAttribute.cs
- LogRestartAreaEnumerator.cs
- ToolStripItemDataObject.cs
- SingleAnimationBase.cs
- StylesEditorDialog.cs
- XmlEnumAttribute.cs
- StyleCollection.cs
- VectorCollection.cs
- GiveFeedbackEventArgs.cs
- DataServiceConfiguration.cs
- HwndSourceParameters.cs
- InfoCardRSAPKCS1SignatureDeformatter.cs
- CompositeActivityTypeDescriptor.cs
- ClientEventManager.cs
- OleDbMetaDataFactory.cs
- CodeTypeDeclarationCollection.cs
- SequenceNumber.cs
- XmlWriter.cs
- _HeaderInfo.cs
- _NestedMultipleAsyncResult.cs
- ReplyChannelAcceptor.cs
- RuleDefinitions.cs
- SqlDependencyListener.cs
- DetailsViewRow.cs
- ImageListImageEditor.cs
- NonVisualControlAttribute.cs
- ChameleonKey.cs
- controlskin.cs
- X509CertificateChain.cs
- InfoCardHelper.cs
- XmlMemberMapping.cs
- UntypedNullExpression.cs
- MessageBuffer.cs
- TitleStyle.cs
- CodeCommentStatement.cs
- SiteMapDataSourceDesigner.cs
- WpfKnownMember.cs
- AsyncDataRequest.cs
- XmlDownloadManager.cs
- InteropBitmapSource.cs
- RepeatBehavior.cs
- BaseDataBoundControl.cs
- _LocalDataStoreMgr.cs
- InfoCardRSAPKCS1SignatureDeformatter.cs
- ObjectContextServiceProvider.cs