Code:
/ DotNET / DotNET / 8.0 / untmp / WIN_WINDOWS / lh_tools_devdiv_wpf / Windows / wcp / Core / System / Windows / Media / Imaging / PropVariant.cs / 1 / PropVariant.cs
//------------------------------------------------------------------------------
// Microsoft Avalon
// Copyright (c) Microsoft Corporation, All Rights Reserved
//
// File: PropVariant.cs
//
//-----------------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Reflection;
using MS.Internal;
using MS.Win32.PresentationCore;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Xml;
using System.IO;
using System.Security;
using System.Security.Permissions;
using System.Windows.Media.Imaging;
using System.Windows.Media.Composition;
using System.Text;
using MS.Internal.PresentationCore;
using System.Diagnostics.CodeAnalysis;
#pragma warning disable 1634, 1691 // suppressing PreSharp warnings
//
// This class wraps a PROPVARIANT type for interop with the unmanaged metadata APIs. Only
// the capabilities used by this API are supported (so for example, there is no SAFEARRAY, IDispatch,
// general VT_UNKNOWN support, etc.)
//
// The types are mapped to C# types as follows:
//
// byte <=> VT_UI1
// sbyte <=> VT_I1
// char <=> VT_LPSTR (size 1)
// ushort <=> VT_UI2
// short <=> VT_I2
// String <=> VT_LPWSTR
// uint <=> VT_UI4
// int <=> VT_I4
// UInt64 <=> VT_UI8
// Int64 <=> VT_I8
// float <=> VT_R4
// double <=> VT_R8
// Guid <=> VT_CLSID
// bool <=> VT_BOOL
// BitmapMetadata <=> VT_UNKNOWN (IWICMetadataQueryReader)
// BitmapMetadataBlob <=> VT_BLOB
//
// For array types:
//
// byte[] <=> VT_UI1|VT_VECTOR
// sbyte[] <=> VT_I1|VT_VECTOR
// char[] <=> VT_LPSTR (size is length of array - treated as ASCII string) - read back is String, use ToCharArray().
// char[][] <=> VT_LPSTR|VT_VECTOR (array of ASCII strings)
// ushort[] <=> VT_UI2|VT_VECTOR
// short[] <=> VT_I2|VT_VECTOR
// String[] <=> VT_LPWSTR|VT_VECTOR
// uint[] <=> VT_UI4|VT_VECTOR
// int[] <=> VT_I4|VT_VECTOR
// UInt64[] <=> VT_UI8|VT_VECTOR
// Int64[] <=> VT_I8|VT_VECTOR
// float[] <=> VT_R4|VT_VECTOR
// double[] <=> VT_R8|VT_VECTOR
// Guid[] <=> VT_CLSID|VT_VECTOR
// bool[] <=> VT_BOOL|VT_VECTOR
//
namespace System.Windows.Media.Imaging
{
#region PropVariant
[StructLayout(LayoutKind.Sequential, Pack=0)]
internal struct PROPARRAY
{
internal UInt32 cElems;
internal IntPtr pElems;
}
[StructLayout(LayoutKind.Explicit, Pack=1)]
internal struct PROPVARIANT
{
[FieldOffset(0)] internal ushort varType;
[FieldOffset(2)] internal ushort wReserved1;
[FieldOffset(4)] internal ushort wReserved2;
[FieldOffset(6)] internal ushort wReserved3;
[FieldOffset(8)] internal byte bVal;
[FieldOffset(8)] internal sbyte cVal;
[FieldOffset(8)] internal ushort uiVal;
[FieldOffset(8)] internal short iVal;
[FieldOffset(8)] internal UInt32 uintVal;
[FieldOffset(8)] internal Int32 intVal;
[FieldOffset(8)] internal UInt64 ulVal;
[FieldOffset(8)] internal Int64 lVal;
[FieldOffset(8)] internal float fltVal;
[FieldOffset(8)] internal double dblVal;
[FieldOffset(8)] internal short boolVal;
[FieldOffset(8)] internal IntPtr pclsidVal; //this is for GUID ID pointer
[FieldOffset(8)] internal IntPtr pszVal; //this is for ansi string pointer
[FieldOffset(8)] internal IntPtr pwszVal; //this is for Unicode string pointer
[FieldOffset(8)] internal IntPtr punkVal; //this is for punkVal (interface pointer)
[FieldOffset(8)] internal PROPARRAY ca;
[FieldOffset(8)] internal System.Runtime.InteropServices.ComTypes.FILETIME filetime;
///
/// CopyBytes - Poor man's mem copy. Copies cbData from pbFrom to pbTo.
///
/// byte* pointing to the "to" array.
/// int - count of bytes of receiving buffer.
/// byte* pointing to the "from" array.
/// int - count of bytes to copy from buffer.
///
/// Critical - Accesses unmanaged memory for copying
///
[SecurityCritical]
private static unsafe void CopyBytes(
byte* pbTo,
int cbTo,
byte* pbFrom,
int cbFrom
)
{
if (cbFrom>cbTo)
{
throw new InvalidOperationException(SR.Get(SRID.Image_InsufficientBufferSize));
}
byte* pCurFrom = (byte*)pbFrom;
byte* pCurTo = (byte*)pbTo;
for (int i = 0; i < cbFrom; i++)
{
pCurTo[i] = pCurFrom[i];
}
}
///
/// Critical -Accesses unmanaged code
/// TreatAsSafe - inputs are verified or safe
///
[SecurityCritical, SecurityTreatAsSafe]
internal void InitVector(Array array, Type type, VarEnum varEnum)
{
Init(array, type, varEnum | VarEnum.VT_VECTOR);
}
///
/// Critical -Accesses unmanaged code and structure is overlapping in memory
/// TreatAsSafe - inputs are verified or safe
///
[SecurityCritical, SecurityTreatAsSafe]
internal void Init(Array array, Type type, VarEnum vt)
{
varType = (ushort) vt;
ca.cElems = 0;
ca.pElems = IntPtr.Zero;
int length = array.Length;
if (length > 0)
{
long size = Marshal.SizeOf(type) * length;
IntPtr destPtr =IntPtr.Zero;
GCHandle handle = new GCHandle();
try
{
destPtr = Marshal.AllocCoTaskMem((int) size);
handle = GCHandle.Alloc(array, GCHandleType.Pinned);
unsafe
{
CopyBytes((byte *) destPtr, (int)size, (byte *)handle.AddrOfPinnedObject(), (int)size);
}
ca.cElems = (uint)length;
ca.pElems = destPtr;
destPtr = IntPtr.Zero;
}
finally
{
if (handle.IsAllocated)
{
handle.Free();
}
if (destPtr != IntPtr.Zero)
{
Marshal.FreeCoTaskMem(destPtr);
}
}
}
}
///
/// Critical -Accesses unmanaged code and structure is overlapping in memory
/// TreatAsSafe - inputs are verified or safe
///
[SecurityCritical, SecurityTreatAsSafe]
internal void Init(String[] value, bool fAscii)
{
varType = (ushort) (fAscii ? VarEnum.VT_LPSTR : VarEnum.VT_LPWSTR);
varType |= (ushort) VarEnum.VT_VECTOR;
ca.cElems = 0;
ca.pElems = IntPtr.Zero;
int length = value.Length;
if (length > 0)
{
IntPtr destPtr = IntPtr.Zero;
int sizeIntPtr = 0;
unsafe
{
sizeIntPtr = sizeof(IntPtr);
}
long size = sizeIntPtr * length;
int index = 0;
try
{
IntPtr pString = IntPtr.Zero;
destPtr = Marshal.AllocCoTaskMem((int)size);
for (index=0; index
/// Critical -Accesses unmanaged code and structure is overlapping in memory
/// TreatAsSafe - inputs are verified or safe
///
[SecurityCritical, SecurityTreatAsSafe]
internal void Init(object value)
{
if (value == null)
{
varType = (ushort)VarEnum.VT_EMPTY;
}
else if (value is Array)
{
Type type = value.GetType();
if (type == typeof(sbyte[]))
{
InitVector(value as Array, typeof(sbyte), VarEnum.VT_I1);
}
else if (type == typeof(byte[]))
{
InitVector(value as Array, typeof(byte), VarEnum.VT_UI1);
}
else if (value is char[])
{
varType = (ushort) VarEnum.VT_LPSTR;
pszVal = Marshal.StringToCoTaskMemAnsi(new String(value as char[]));
}
else if (value is char[][])
{
char[][] charArray = value as char[][];
String[] strArray = new String[charArray.GetLength(0)];
for (int i=0; i
/// Critical -Accesses unmanaged code and structure is overlapping in memory
/// TreatAsSafe - there are no inputs
///
[SecurityCritical, SecurityTreatAsSafe]
internal void Clear()
{
VarEnum vt = (VarEnum) varType;
if ((vt & VarEnum.VT_VECTOR) != 0 || vt == VarEnum.VT_BLOB)
{
if (ca.pElems != IntPtr.Zero)
{
vt = vt & ~VarEnum.VT_VECTOR;
if (vt == VarEnum.VT_UNKNOWN)
{
IntPtr punkPtr = ca.pElems;
int sizeIntPtr = 0;
unsafe
{
sizeIntPtr = sizeof(IntPtr);
}
for (uint i=0; i
/// Critical -Accesses unmanaged code and structure is overlapping in memory
/// TreatAsSafe - inputs are verified or safe
///
[SecurityCritical, SecurityTreatAsSafe]
internal object ToObject(object syncObject)
{
VarEnum vt = (VarEnum) varType;
if ((vt & VarEnum.VT_VECTOR) != 0)
{
switch (vt & (~VarEnum.VT_VECTOR))
{
case VarEnum.VT_EMPTY:
return null;
case VarEnum.VT_I1:
{
sbyte[] array = new sbyte[ca.cElems];
for (int i=0; i
/// Critical - Accesses a structure that contains overlapping data
/// TreatAsSafe - there are no inputs
///
internal bool RequiresSyncObject
{
[SecurityCritical, SecurityTreatAsSafe]
get
{
return (varType == (ushort) VarEnum.VT_UNKNOWN);
}
}
}
#endregion
}
// 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
- Substitution.cs
- XmlSchemaSimpleTypeRestriction.cs
- DelegateBodyWriter.cs
- UpdateExpressionVisitor.cs
- ChannelBuilder.cs
- Margins.cs
- COM2ColorConverter.cs
- DifferencingCollection.cs
- WindowsToolbarAsMenu.cs
- SessionStateContainer.cs
- XNameConverter.cs
- EncoderExceptionFallback.cs
- HintTextMaxWidthConverter.cs
- ColumnTypeConverter.cs
- XslTransform.cs
- EdmComplexTypeAttribute.cs
- ellipse.cs
- ObfuscationAttribute.cs
- SmtpAuthenticationManager.cs
- columnmapkeybuilder.cs
- DiagnosticTraceSource.cs
- IgnoreFileBuildProvider.cs
- OleDbConnection.cs
- AssemblyResourceLoader.cs
- SymbolPair.cs
- Process.cs
- RectangleF.cs
- GridViewDeleteEventArgs.cs
- ReturnEventArgs.cs
- XamlGridLengthSerializer.cs
- BaseCodePageEncoding.cs
- Function.cs
- ThemeDictionaryExtension.cs
- CachedCompositeFamily.cs
- ByteStack.cs
- CopyOnWriteList.cs
- ReadOnlyCollectionBase.cs
- ComboBoxItem.cs
- XmlSchemaComplexContentExtension.cs
- SessionParameter.cs
- BaseTemplateBuildProvider.cs
- XmlAttributeOverrides.cs
- PasswordBoxAutomationPeer.cs
- SqlFactory.cs
- RNGCryptoServiceProvider.cs
- _CommandStream.cs
- PhysicalOps.cs
- Image.cs
- FontNamesConverter.cs
- precedingsibling.cs
- JsonSerializer.cs
- PropertySegmentSerializationProvider.cs
- XmlUtilWriter.cs
- SimplePropertyEntry.cs
- VectorAnimation.cs
- TransformValueSerializer.cs
- RawContentTypeMapper.cs
- CustomAttributeSerializer.cs
- FormViewPagerRow.cs
- QfeChecker.cs
- WebScriptMetadataMessageEncodingBindingElement.cs
- BoolExpression.cs
- DataServices.cs
- XmlSignificantWhitespace.cs
- SoapProtocolImporter.cs
- HwndStylusInputProvider.cs
- SecurityBindingElement.cs
- TypeForwardedToAttribute.cs
- SqlUnionizer.cs
- MD5HashHelper.cs
- DependencyPropertyDescriptor.cs
- InheritanceAttribute.cs
- RectAnimationBase.cs
- ProcessManager.cs
- LinqDataSourceView.cs
- DrawingGroupDrawingContext.cs
- OdbcError.cs
- DeferredSelectedIndexReference.cs
- ButtonFieldBase.cs
- BaseProcessProtocolHandler.cs
- IntSecurity.cs
- ActivityValidationServices.cs
- SystemIcmpV6Statistics.cs
- StringToken.cs
- CompositeCollectionView.cs
- InvalidateEvent.cs
- NamespaceDecl.cs
- Int64.cs
- StreamAsIStream.cs
- PkcsMisc.cs
- DecimalAnimationBase.cs
- TypeGeneratedEventArgs.cs
- TextStore.cs
- Root.cs
- ObjectDataSource.cs
- ListViewCommandEventArgs.cs
- EventNotify.cs
- TimelineGroup.cs
- XmlRawWriter.cs
- CodePageUtils.cs