Restrictions.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //------------------------------------------------------------------------------
  2. // <copyright file="Restrictions.cs" company="Microsoft">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. //------------------------------------------------------------------------------
  6. using System;
  7. using System.Diagnostics.CodeAnalysis;
  8. using System.Runtime.InteropServices;
  9. namespace Experimental.System.Messaging.Interop
  10. {
  11. internal class Restrictions
  12. {
  13. private MQRESTRICTION restrictionStructure;
  14. public const int PRLT = 0;
  15. public const int PRLE = 1;
  16. public const int PRGT = 2;
  17. public const int PRGE = 3;
  18. public const int PREQ = 4;
  19. public const int PRNE = 5;
  20. public Restrictions(int maxRestrictions)
  21. {
  22. this.restrictionStructure = new MQRESTRICTION(maxRestrictions);
  23. }
  24. public virtual void AddGuid(int propertyId, int op, Guid value)
  25. {
  26. IntPtr data = Marshal.AllocHGlobal(16);
  27. Marshal.Copy(value.ToByteArray(), 0, data, 16);
  28. this.AddItem(propertyId, op, MessagePropertyVariants.VT_CLSID, data);
  29. }
  30. public virtual void AddGuid(int propertyId, int op)
  31. {
  32. IntPtr data = Marshal.AllocHGlobal(16);
  33. this.AddItem(propertyId, op, MessagePropertyVariants.VT_CLSID, data);
  34. }
  35. private void AddItem(int id, int op, short vt, IntPtr data)
  36. {
  37. Marshal.WriteInt32(restrictionStructure.GetNextValidPtr(0), op);
  38. Marshal.WriteInt32(restrictionStructure.GetNextValidPtr(4), id);
  39. Marshal.WriteInt16(restrictionStructure.GetNextValidPtr(8), vt);
  40. Marshal.WriteInt16(restrictionStructure.GetNextValidPtr(10), (short)0);
  41. Marshal.WriteInt16(restrictionStructure.GetNextValidPtr(12), (short)0);
  42. Marshal.WriteInt16(restrictionStructure.GetNextValidPtr(14), (short)0);
  43. Marshal.WriteIntPtr(restrictionStructure.GetNextValidPtr(16), data);
  44. Marshal.WriteIntPtr(restrictionStructure.GetNextValidPtr(16 + IntPtr.Size), (IntPtr)0);
  45. ++restrictionStructure.restrictionCount;
  46. }
  47. public virtual void AddI4(int propertyId, int op, int value)
  48. {
  49. this.AddItem(propertyId, op, MessagePropertyVariants.VT_I4, (IntPtr)value);
  50. }
  51. public virtual void AddString(int propertyId, int op, string value)
  52. {
  53. if (value == null)
  54. this.AddItem(propertyId, op, MessagePropertyVariants.VT_NULL, (IntPtr)0);
  55. else
  56. {
  57. IntPtr data = Marshal.StringToHGlobalUni(value);
  58. this.AddItem(propertyId, op, MessagePropertyVariants.VT_LPWSTR, data);
  59. }
  60. }
  61. public virtual MQRESTRICTION GetRestrictionsRef()
  62. {
  63. return this.restrictionStructure;
  64. }
  65. [StructLayout(LayoutKind.Sequential)]
  66. public class MQRESTRICTION
  67. {
  68. public int restrictionCount;
  69. [SuppressMessage("Microsoft.Reliability", "CA2006:UseSafeHandleToEncapsulateNativeResources")]
  70. public IntPtr restrinctions;
  71. public MQRESTRICTION(int maxCount)
  72. {
  73. this.restrinctions = Marshal.AllocHGlobal(maxCount * GetRestrictionSize());
  74. }
  75. ~MQRESTRICTION()
  76. {
  77. if (this.restrinctions != (IntPtr)0)
  78. {
  79. for (int index = 0; index < this.restrictionCount; ++index)
  80. {
  81. short vt = Marshal.ReadInt16((IntPtr)((long)this.restrinctions + (index * GetRestrictionSize()) + 8));
  82. if (vt != MessagePropertyVariants.VT_I4)
  83. {
  84. IntPtr dataPtr = (IntPtr)((long)this.restrinctions + (index * GetRestrictionSize()) + 16);
  85. IntPtr data = Marshal.ReadIntPtr(dataPtr);
  86. Marshal.FreeHGlobal(data);
  87. }
  88. }
  89. Marshal.FreeHGlobal(this.restrinctions);
  90. this.restrinctions = (IntPtr)0;
  91. }
  92. }
  93. public IntPtr GetNextValidPtr(int offset)
  94. {
  95. return (IntPtr)((long)restrinctions + restrictionCount * GetRestrictionSize() + offset);
  96. }
  97. public static int GetRestrictionSize()
  98. {
  99. return 16 + (IntPtr.Size * 2);
  100. }
  101. }
  102. }
  103. }