123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447 |
- //------------------------------------------------------------------------------
- // <copyright file="MessagePropertyVariants.cs" company="Microsoft">
- // Copyright (c) Microsoft Corporation. All rights reserved.
- // </copyright>
- //------------------------------------------------------------------------------
- using System;
- using System.Diagnostics.CodeAnalysis;
- using System.Runtime.InteropServices;
- namespace Experimental.System.Messaging.Interop
- {
- // definition for tagMQPROPVARIANT
- [StructLayout(LayoutKind.Explicit)]
- internal struct MQPROPVARIANTS
- {
- // definition for struct tagCAUB
- [StructLayout(LayoutKind.Sequential)]
- internal struct CAUB
- {
- internal uint cElems;
- [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)]
- [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;
- [SuppressMessage("Microsoft.Reliability", "CA2006:UseSafeHandleToEncapsulateNativeResources")]
- internal IntPtr propertyIdentifiers;
- [SuppressMessage("Microsoft.Reliability", "CA2006:UseSafeHandleToEncapsulateNativeResources")]
- internal IntPtr propertyValues;
- [SuppressMessage("Microsoft.Reliability", "CA2006:UseSafeHandleToEncapsulateNativeResources")]
- internal IntPtr status;
- }
- }
- }
|