Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Services / Messaging / System / Messaging / Interop / MessagePropertyVariants.cs / 1305376 / MessagePropertyVariants.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Messaging.Interop {
using System.Diagnostics;
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;
// definition for tagMQPROPVARIANT
[StructLayout(LayoutKind.Explicit)]
internal struct MQPROPVARIANTS {
// definition for struct tagCAUB
[StructLayout(LayoutKind.Sequential)]
internal struct CAUB
{
internal uint cElems;
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2006:UseSafeHandleToEncapsulateNativeResources")]
internal IntPtr pElems;
}
[FieldOffset(0)]
internal short vt;
[FieldOffset(2)]
internal short wReserved1;
[FieldOffset(4)]
internal short wReserved2;
[FieldOffset(6)]
internal short wReserved3;
[FieldOffset(8)]
internal byte bVal;
[FieldOffset(8)]
internal short iVal;
[FieldOffset(8)]
internal int lVal;
[FieldOffset(8)]
internal long hVal;
[FieldOffset(8)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2006:UseSafeHandleToEncapsulateNativeResources")]
internal IntPtr ptr;
[FieldOffset(8)]
internal CAUB caub;
}
internal class MessagePropertyVariants {
private const short VT_UNDEFINED = 0;
public const short VT_EMPTY = short.MaxValue; //this is hack, VT_EMPTY is really 0,
//but redefining VT_UNDEFINED is risky since 0 is a good semantic default for it
public const short VT_ARRAY = 0x2000;
public const short VT_BOOL = 11;
public const short VT_BSTR = 8;
public const short VT_CLSID = 72;
public const short VT_CY = 6;
public const short VT_DATE = 7;
public const short VT_I1 = 16;
public const short VT_I2 = 2;
public const short VT_I4 = 3;
public const short VT_I8 = 20;
public const short VT_LPSTR = 30;
public const short VT_LPWSTR = 31;
public const short VT_NULL = 1;
public const short VT_R4 = 4;
public const short VT_R8 = 5;
public const short VT_STREAMED_OBJECT = 68;
public const short VT_STORED_OBJECT = 69;
public const short VT_UI1 = 17;
public const short VT_UI2 = 18;
public const short VT_UI4 = 19;
public const short VT_UI8 = 21;
public const short VT_VECTOR = 0x1000;
private int MAX_PROPERTIES = 61;
private int basePropertyId = NativeMethods.MESSAGE_PROPID_BASE + 1;
private int propertyCount;
private GCHandle handleVectorProperties;
private GCHandle handleVectorIdentifiers;
private GCHandle handleVectorStatus;
private MQPROPS reference;
private int[] vectorIdentifiers;
private int[] vectorStatus;
private MQPROPVARIANTS[] vectorProperties;
private short[] variantTypes;
private object[] objects;
private object[] handles;
internal MessagePropertyVariants(int maxProperties, int baseId) {
reference = new MQPROPS();
MAX_PROPERTIES = maxProperties;
basePropertyId = baseId;
variantTypes = new short[MAX_PROPERTIES];
objects = new object[MAX_PROPERTIES];
handles = new object[MAX_PROPERTIES];
}
public MessagePropertyVariants() {
reference = new MQPROPS();
variantTypes = new short[MAX_PROPERTIES];
objects = new object[MAX_PROPERTIES];
handles = new object[MAX_PROPERTIES];
}
public byte[] GetGuid(int propertyId) {
return(byte[])objects[propertyId - basePropertyId];
}
public void SetGuid(int propertyId, byte[] value) {
if (variantTypes[propertyId - basePropertyId] == VT_UNDEFINED) {
variantTypes[propertyId - basePropertyId] = VT_CLSID;
++ propertyCount;
}
objects[propertyId - basePropertyId] = value;
}
public short GetI2(int propertyId) {
return(short)objects[propertyId - basePropertyId];
}
public void SetI2(int propertyId, short value) {
if ((variantTypes[propertyId - basePropertyId]) == VT_UNDEFINED) {
variantTypes[propertyId - basePropertyId] = VT_I2;
++ propertyCount;
}
objects[propertyId - basePropertyId] = value;
}
public int GetI4(int propertyId) {
return(int)objects[propertyId - basePropertyId];
}
public void SetI4(int propertyId, int value) {
if (variantTypes[propertyId - basePropertyId] == VT_UNDEFINED) {
variantTypes[propertyId - basePropertyId] = VT_I4;
++ propertyCount;
}
objects[propertyId - basePropertyId] = value;
}
public IntPtr GetStringVectorBasePointer(int propertyId) {
return (IntPtr)handles[propertyId - basePropertyId];
}
public uint GetStringVectorLength(int propertyId) {
return (uint)objects[propertyId - basePropertyId];
}
public byte[] GetString(int propertyId) {
return(byte[])objects[propertyId - basePropertyId];
}
public void SetString(int propertyId, byte[] value) {
if (variantTypes[propertyId - basePropertyId] == VT_UNDEFINED) {
variantTypes[propertyId - basePropertyId] = VT_LPWSTR;
++ propertyCount;
}
objects[propertyId - basePropertyId] = value;
}
public byte GetUI1(int propertyId) {
return(byte)objects[propertyId - basePropertyId];
}
public void SetUI1(int propertyId, byte value) {
if (variantTypes[propertyId - basePropertyId] == VT_UNDEFINED) {
variantTypes[propertyId - basePropertyId] = VT_UI1;
++ propertyCount;
}
objects[propertyId - basePropertyId] = value;
}
public byte[] GetUI1Vector(int propertyId) {
return(byte[])objects[propertyId - basePropertyId];
}
public void SetUI1Vector(int propertyId, byte[] value) {
if (variantTypes[propertyId - basePropertyId] == VT_UNDEFINED) {
variantTypes[propertyId - basePropertyId] = (short)(VT_VECTOR | VT_UI1);
++ propertyCount;
}
objects[propertyId - basePropertyId] = value;
}
public short GetUI2(int propertyId) {
return(short)objects[propertyId - basePropertyId];
}
public void SetUI2(int propertyId, short value) {
if (variantTypes[propertyId - basePropertyId] == VT_UNDEFINED) {
variantTypes[propertyId - basePropertyId] = VT_UI2;
++ propertyCount;
}
objects[propertyId - basePropertyId] = value;
}
public int GetUI4(int propertyId) {
return(int)objects[propertyId - basePropertyId];
}
public void SetUI4(int propertyId, int value) {
if (variantTypes[propertyId - basePropertyId] == VT_UNDEFINED) {
variantTypes[propertyId - basePropertyId] = VT_UI4;
++ propertyCount;
}
objects[propertyId - basePropertyId] = value;
}
public long GetUI8(int propertyId) {
return(long)objects[propertyId - basePropertyId];
}
public void SetUI8(int propertyId, long value) {
if (variantTypes[propertyId - basePropertyId] == VT_UNDEFINED) {
variantTypes[propertyId - basePropertyId] = VT_UI8;
++ propertyCount;
}
objects[propertyId - basePropertyId] = value;
}
public IntPtr GetIntPtr(int propertyId) {
//
// Bug 379376: Property access might have been unsuccessful (e.g., getting Id of a private queue),
// in which case the object stored at objects[propertyId - basePropertyId] will be object{uint}
// instead of object{IntPtr}.
// We will return IntPtr.Zero in that case
//
object obj = objects[propertyId - basePropertyId];
if (obj.GetType() == typeof(IntPtr))
return (IntPtr)obj;
return IntPtr.Zero;
}
public virtual void AdjustSize(int propertyId, int size) {
//I'm going to reuse this field to store the size temporarily.
//Later I'll be storing the handle.
handles[propertyId - basePropertyId] = (uint)size;
}
public virtual void Ghost(int propertyId) {
if (variantTypes[propertyId - basePropertyId] != VT_UNDEFINED) {
variantTypes[propertyId - basePropertyId] = VT_UNDEFINED;
--propertyCount;
}
}
public virtual MQPROPS Lock() {
int[] newVectorIdentifiers = new int[propertyCount];
int[] newVectorStatus = new int[propertyCount];
MQPROPVARIANTS[] newVectorProperties = new MQPROPVARIANTS[propertyCount];
int usedProperties = 0;
for (int i = 0; i < MAX_PROPERTIES; ++ i) {
short vt = variantTypes[i];
if (vt != VT_UNDEFINED) {
//Set PropertyId
newVectorIdentifiers[usedProperties] = i + basePropertyId;
//Set VariantType
newVectorProperties[usedProperties].vt = vt;
if (vt == (short)(VT_VECTOR | VT_UI1)) {
if (handles[i] == null)
newVectorProperties[usedProperties].caub.cElems = (uint)((byte[])objects[i]).Length;
else
newVectorProperties[usedProperties].caub.cElems = (uint)handles[i];
GCHandle handle = GCHandle.Alloc(objects[i], GCHandleType.Pinned);
handles[i] = handle;
newVectorProperties[usedProperties].caub.pElems = handle.AddrOfPinnedObject();
}
else if (vt == VT_UI1 || vt == VT_I1)
newVectorProperties[usedProperties].bVal = (byte)objects[i];
else if (vt == VT_UI2 || vt == VT_I2)
newVectorProperties[usedProperties].iVal = (short)objects[i];
else if (vt == VT_UI4 || vt == VT_I4)
newVectorProperties[usedProperties].lVal = (int)objects[i];
else if (vt == VT_UI8 || vt == VT_I8)
newVectorProperties[usedProperties].hVal = (long)objects[i];
else if (vt == VT_LPWSTR || vt == VT_CLSID) {
GCHandle handle = GCHandle.Alloc(objects[i], GCHandleType.Pinned);
handles[i] = handle;
newVectorProperties[usedProperties].ptr = handle.AddrOfPinnedObject();
}
else if (vt == VT_EMPTY)
newVectorProperties[usedProperties].vt = 0; //real value for VT_EMPTY
++usedProperties;
if (propertyCount == usedProperties)
break;
}
}
handleVectorIdentifiers = GCHandle.Alloc(newVectorIdentifiers, GCHandleType.Pinned);
handleVectorProperties = GCHandle.Alloc(newVectorProperties, GCHandleType.Pinned);
handleVectorStatus = GCHandle.Alloc(newVectorStatus, GCHandleType.Pinned);
vectorIdentifiers = newVectorIdentifiers;
vectorStatus = newVectorStatus;
vectorProperties = newVectorProperties;
reference.propertyCount = propertyCount;
reference.propertyIdentifiers = handleVectorIdentifiers.AddrOfPinnedObject();
reference.propertyValues = handleVectorProperties.AddrOfPinnedObject();
reference.status = handleVectorStatus.AddrOfPinnedObject();
return reference;
}
public virtual void Remove(int propertyId) {
if (variantTypes[propertyId - basePropertyId] != VT_UNDEFINED) {
variantTypes[propertyId - basePropertyId] = VT_UNDEFINED;
objects[propertyId - basePropertyId] = null;
handles[propertyId - basePropertyId] = null;
--propertyCount;
}
}
public virtual void SetNull(int propertyId) {
if (variantTypes[propertyId - basePropertyId] == VT_UNDEFINED) {
variantTypes[propertyId - basePropertyId] = VT_NULL;
++ propertyCount;
}
objects[propertyId - basePropertyId] = null;
}
public virtual void SetEmpty(int propertyId) {
if (variantTypes[propertyId - basePropertyId] == VT_UNDEFINED) {
variantTypes[propertyId - basePropertyId] = VT_EMPTY;
++ propertyCount;
}
objects[propertyId - basePropertyId] = null;
}
public virtual void Unlock() {
for (int i = 0; i < vectorIdentifiers.Length; ++ i) {
short vt = (short)vectorProperties[i].vt;
if (variantTypes[vectorIdentifiers[i] - basePropertyId] == VT_NULL) {
if (vt == (short)(VT_VECTOR | VT_UI1) || vt == VT_NULL)
//Support for MSMQ self memory allocation.
objects[vectorIdentifiers[i] - basePropertyId] = vectorProperties[i].caub.cElems;
else if (vt == (short)(VT_VECTOR | VT_LPWSTR)) {
//Support for MSMQ management apis.
objects[vectorIdentifiers[i] - basePropertyId] = vectorProperties[i * 4].caub.cElems;
handles[vectorIdentifiers[i] - basePropertyId] = vectorProperties[i * 4].caub.pElems;
}
else
objects[vectorIdentifiers[i] - basePropertyId] = vectorProperties[i].ptr;
}
else if (vt == VT_LPWSTR || vt == VT_CLSID || vt == (short)(VT_VECTOR | VT_UI1)) {
((GCHandle)handles[vectorIdentifiers[i] - basePropertyId]).Free();
handles[vectorIdentifiers[i] - basePropertyId] = null;
}
else if (vt == VT_UI1 || vt == VT_I1)
objects[vectorIdentifiers[i] - basePropertyId] = (byte)vectorProperties[i].bVal;
else if (vt == VT_UI2 || vt == VT_I2)
objects[vectorIdentifiers[i] - basePropertyId] = (short)vectorProperties[i].iVal;
else if (vt == VT_UI4 || vt == VT_I4)
objects[vectorIdentifiers[i] - basePropertyId] = vectorProperties[i].lVal;
else if (vt == VT_UI8 || vt == VT_I8)
objects[vectorIdentifiers[i] - basePropertyId] = vectorProperties[i].hVal;
}
handleVectorIdentifiers.Free();
handleVectorProperties.Free();
handleVectorStatus.Free();
}
[StructLayout(LayoutKind.Sequential)]
public class MQPROPS {
internal int propertyCount;
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2006:UseSafeHandleToEncapsulateNativeResources")]
internal IntPtr propertyIdentifiers;
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2006:UseSafeHandleToEncapsulateNativeResources")]
internal IntPtr propertyValues;
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2006:UseSafeHandleToEncapsulateNativeResources")]
internal IntPtr status;
}
}
}
// 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
- FrugalList.cs
- Int64Storage.cs
- HashMembershipCondition.cs
- Track.cs
- HtmlEmptyTagControlBuilder.cs
- ToolStripSettings.cs
- SoapFault.cs
- LinkedList.cs
- OleDbTransaction.cs
- RepeatBehavior.cs
- CorrelationResolver.cs
- HybridDictionary.cs
- Journal.cs
- AutomationProperties.cs
- TypeSystem.cs
- StylusLogic.cs
- controlskin.cs
- RowsCopiedEventArgs.cs
- HtmlEncodedRawTextWriter.cs
- XmlSchemaInferenceException.cs
- MouseButton.cs
- TemplatedWizardStep.cs
- DataGridViewTopRowAccessibleObject.cs
- DataColumnChangeEvent.cs
- ConnectionManager.cs
- ResolveMatchesMessageCD1.cs
- XmlSchemaValidationException.cs
- tooltip.cs
- SqlTypesSchemaImporter.cs
- PageHandlerFactory.cs
- SmtpMail.cs
- ExplicitDiscriminatorMap.cs
- AppSecurityManager.cs
- VectorValueSerializer.cs
- DataServiceExpressionVisitor.cs
- FilterFactory.cs
- ProtocolsConfiguration.cs
- JoinElimination.cs
- SystemException.cs
- PerspectiveCamera.cs
- SmiSettersStream.cs
- Signature.cs
- ParameterCollection.cs
- OleDbErrorCollection.cs
- HttpMethodAttribute.cs
- ReversePositionQuery.cs
- WebDescriptionAttribute.cs
- ComponentResourceKey.cs
- PixelShader.cs
- StaticExtensionConverter.cs
- RegexCode.cs
- Model3D.cs
- CurrencyWrapper.cs
- CompiledQuery.cs
- CommandID.cs
- WindowsFormsHostAutomationPeer.cs
- HiddenField.cs
- SoapEnumAttribute.cs
- Filter.cs
- AccessDataSource.cs
- GeometryHitTestResult.cs
- SingleStorage.cs
- DataSourceSelectArguments.cs
- _SslSessionsCache.cs
- XmlCharCheckingWriter.cs
- SessionEndedEventArgs.cs
- ToolBarPanel.cs
- SQLRoleProvider.cs
- SvcMapFileSerializer.cs
- OleDbSchemaGuid.cs
- SqlComparer.cs
- ProcessManager.cs
- WCFModelStrings.Designer.cs
- OdbcConnectionStringbuilder.cs
- FileDialog.cs
- ListBindingHelper.cs
- WebConfigurationHostFileChange.cs
- MatchSingleFxEngineOpcode.cs
- MenuItemStyleCollection.cs
- UpDownBaseDesigner.cs
- PropertyFilter.cs
- CapabilitiesRule.cs
- WinInet.cs
- UnauthorizedAccessException.cs
- BindingList.cs
- ToolboxComponentsCreatingEventArgs.cs
- InstanceKeyCompleteException.cs
- __FastResourceComparer.cs
- UIPermission.cs
- PageCodeDomTreeGenerator.cs
- ToggleProviderWrapper.cs
- Int32.cs
- TableMethodGenerator.cs
- RefreshEventArgs.cs
- ViewLoader.cs
- documentation.cs
- SectionInput.cs
- MultipleViewProviderWrapper.cs
- BitmapEffect.cs
- MouseGestureConverter.cs