MessagePropertyVariants.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. //------------------------------------------------------------------------------
  2. // <copyright file="MessagePropertyVariants.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. // definition for tagMQPROPVARIANT
  12. [StructLayout(LayoutKind.Explicit)]
  13. internal struct MQPROPVARIANTS
  14. {
  15. // definition for struct tagCAUB
  16. [StructLayout(LayoutKind.Sequential)]
  17. internal struct CAUB
  18. {
  19. internal uint cElems;
  20. [SuppressMessage("Microsoft.Reliability", "CA2006:UseSafeHandleToEncapsulateNativeResources")]
  21. internal IntPtr pElems;
  22. }
  23. [FieldOffset(0)]
  24. internal short vt;
  25. [FieldOffset(2)]
  26. internal short wReserved1;
  27. [FieldOffset(4)]
  28. internal short wReserved2;
  29. [FieldOffset(6)]
  30. internal short wReserved3;
  31. [FieldOffset(8)]
  32. internal byte bVal;
  33. [FieldOffset(8)]
  34. internal short iVal;
  35. [FieldOffset(8)]
  36. internal int lVal;
  37. [FieldOffset(8)]
  38. internal long hVal;
  39. [FieldOffset(8)]
  40. [SuppressMessage("Microsoft.Reliability", "CA2006:UseSafeHandleToEncapsulateNativeResources")]
  41. internal IntPtr ptr;
  42. [FieldOffset(8)]
  43. internal CAUB caub;
  44. }
  45. internal class MessagePropertyVariants
  46. {
  47. private const short VT_UNDEFINED = 0;
  48. public const short VT_EMPTY = short.MaxValue; //this is hack, VT_EMPTY is really 0,
  49. //but redefining VT_UNDEFINED is risky since 0 is a good semantic default for it
  50. public const short VT_ARRAY = 0x2000;
  51. public const short VT_BOOL = 11;
  52. public const short VT_BSTR = 8;
  53. public const short VT_CLSID = 72;
  54. public const short VT_CY = 6;
  55. public const short VT_DATE = 7;
  56. public const short VT_I1 = 16;
  57. public const short VT_I2 = 2;
  58. public const short VT_I4 = 3;
  59. public const short VT_I8 = 20;
  60. public const short VT_LPSTR = 30;
  61. public const short VT_LPWSTR = 31;
  62. public const short VT_NULL = 1;
  63. public const short VT_R4 = 4;
  64. public const short VT_R8 = 5;
  65. public const short VT_STREAMED_OBJECT = 68;
  66. public const short VT_STORED_OBJECT = 69;
  67. public const short VT_UI1 = 17;
  68. public const short VT_UI2 = 18;
  69. public const short VT_UI4 = 19;
  70. public const short VT_UI8 = 21;
  71. public const short VT_VECTOR = 0x1000;
  72. private int MAX_PROPERTIES = 61;
  73. private int basePropertyId = NativeMethods.MESSAGE_PROPID_BASE + 1;
  74. private int propertyCount;
  75. private GCHandle handleVectorProperties;
  76. private GCHandle handleVectorIdentifiers;
  77. private GCHandle handleVectorStatus;
  78. private MQPROPS reference;
  79. private int[] vectorIdentifiers;
  80. private int[] vectorStatus;
  81. private MQPROPVARIANTS[] vectorProperties;
  82. private short[] variantTypes;
  83. private object[] objects;
  84. private object[] handles;
  85. internal MessagePropertyVariants(int maxProperties, int baseId)
  86. {
  87. reference = new MQPROPS();
  88. MAX_PROPERTIES = maxProperties;
  89. basePropertyId = baseId;
  90. variantTypes = new short[MAX_PROPERTIES];
  91. objects = new object[MAX_PROPERTIES];
  92. handles = new object[MAX_PROPERTIES];
  93. }
  94. public MessagePropertyVariants()
  95. {
  96. reference = new MQPROPS();
  97. variantTypes = new short[MAX_PROPERTIES];
  98. objects = new object[MAX_PROPERTIES];
  99. handles = new object[MAX_PROPERTIES];
  100. }
  101. public byte[] GetGuid(int propertyId)
  102. {
  103. return (byte[])objects[propertyId - basePropertyId];
  104. }
  105. public void SetGuid(int propertyId, byte[] value)
  106. {
  107. if (variantTypes[propertyId - basePropertyId] == VT_UNDEFINED)
  108. {
  109. variantTypes[propertyId - basePropertyId] = VT_CLSID;
  110. ++propertyCount;
  111. }
  112. objects[propertyId - basePropertyId] = value;
  113. }
  114. public short GetI2(int propertyId)
  115. {
  116. return (short)objects[propertyId - basePropertyId];
  117. }
  118. public void SetI2(int propertyId, short value)
  119. {
  120. if ((variantTypes[propertyId - basePropertyId]) == VT_UNDEFINED)
  121. {
  122. variantTypes[propertyId - basePropertyId] = VT_I2;
  123. ++propertyCount;
  124. }
  125. objects[propertyId - basePropertyId] = value;
  126. }
  127. public int GetI4(int propertyId)
  128. {
  129. return (int)objects[propertyId - basePropertyId];
  130. }
  131. public void SetI4(int propertyId, int value)
  132. {
  133. if (variantTypes[propertyId - basePropertyId] == VT_UNDEFINED)
  134. {
  135. variantTypes[propertyId - basePropertyId] = VT_I4;
  136. ++propertyCount;
  137. }
  138. objects[propertyId - basePropertyId] = value;
  139. }
  140. public IntPtr GetStringVectorBasePointer(int propertyId)
  141. {
  142. return (IntPtr)handles[propertyId - basePropertyId];
  143. }
  144. public uint GetStringVectorLength(int propertyId)
  145. {
  146. return (uint)objects[propertyId - basePropertyId];
  147. }
  148. public byte[] GetString(int propertyId)
  149. {
  150. return (byte[])objects[propertyId - basePropertyId];
  151. }
  152. public void SetString(int propertyId, byte[] value)
  153. {
  154. if (variantTypes[propertyId - basePropertyId] == VT_UNDEFINED)
  155. {
  156. variantTypes[propertyId - basePropertyId] = VT_LPWSTR;
  157. ++propertyCount;
  158. }
  159. objects[propertyId - basePropertyId] = value;
  160. }
  161. public byte GetUI1(int propertyId)
  162. {
  163. return (byte)objects[propertyId - basePropertyId];
  164. }
  165. public void SetUI1(int propertyId, byte value)
  166. {
  167. if (variantTypes[propertyId - basePropertyId] == VT_UNDEFINED)
  168. {
  169. variantTypes[propertyId - basePropertyId] = VT_UI1;
  170. ++propertyCount;
  171. }
  172. objects[propertyId - basePropertyId] = value;
  173. }
  174. public byte[] GetUI1Vector(int propertyId)
  175. {
  176. return (byte[])objects[propertyId - basePropertyId];
  177. }
  178. public void SetUI1Vector(int propertyId, byte[] value)
  179. {
  180. if (variantTypes[propertyId - basePropertyId] == VT_UNDEFINED)
  181. {
  182. variantTypes[propertyId - basePropertyId] = (short)(VT_VECTOR | VT_UI1);
  183. ++propertyCount;
  184. }
  185. objects[propertyId - basePropertyId] = value;
  186. }
  187. public short GetUI2(int propertyId)
  188. {
  189. return (short)objects[propertyId - basePropertyId];
  190. }
  191. public void SetUI2(int propertyId, short value)
  192. {
  193. if (variantTypes[propertyId - basePropertyId] == VT_UNDEFINED)
  194. {
  195. variantTypes[propertyId - basePropertyId] = VT_UI2;
  196. ++propertyCount;
  197. }
  198. objects[propertyId - basePropertyId] = value;
  199. }
  200. public int GetUI4(int propertyId)
  201. {
  202. return (int)objects[propertyId - basePropertyId];
  203. }
  204. public void SetUI4(int propertyId, int value)
  205. {
  206. if (variantTypes[propertyId - basePropertyId] == VT_UNDEFINED)
  207. {
  208. variantTypes[propertyId - basePropertyId] = VT_UI4;
  209. ++propertyCount;
  210. }
  211. objects[propertyId - basePropertyId] = value;
  212. }
  213. public long GetUI8(int propertyId)
  214. {
  215. return (long)objects[propertyId - basePropertyId];
  216. }
  217. public void SetUI8(int propertyId, long value)
  218. {
  219. if (variantTypes[propertyId - basePropertyId] == VT_UNDEFINED)
  220. {
  221. variantTypes[propertyId - basePropertyId] = VT_UI8;
  222. ++propertyCount;
  223. }
  224. objects[propertyId - basePropertyId] = value;
  225. }
  226. public IntPtr GetIntPtr(int propertyId)
  227. {
  228. //
  229. // Bug 379376: Property access might have been unsuccessful (e.g., getting Id of a private queue),
  230. // in which case the object stored at objects[propertyId - basePropertyId] will be object{uint}
  231. // instead of object{IntPtr}.
  232. // We will return IntPtr.Zero in that case
  233. //
  234. object obj = objects[propertyId - basePropertyId];
  235. if (obj.GetType() == typeof(IntPtr))
  236. return (IntPtr)obj;
  237. return IntPtr.Zero;
  238. }
  239. public virtual void AdjustSize(int propertyId, int size)
  240. {
  241. //I'm going to reuse this field to store the size temporarily.
  242. //Later I'll be storing the handle.
  243. handles[propertyId - basePropertyId] = (uint)size;
  244. }
  245. public virtual void Ghost(int propertyId)
  246. {
  247. if (variantTypes[propertyId - basePropertyId] != VT_UNDEFINED)
  248. {
  249. variantTypes[propertyId - basePropertyId] = VT_UNDEFINED;
  250. --propertyCount;
  251. }
  252. }
  253. public virtual MQPROPS Lock()
  254. {
  255. int[] newVectorIdentifiers = new int[propertyCount];
  256. int[] newVectorStatus = new int[propertyCount];
  257. MQPROPVARIANTS[] newVectorProperties = new MQPROPVARIANTS[propertyCount];
  258. int usedProperties = 0;
  259. for (int i = 0; i < MAX_PROPERTIES; ++i)
  260. {
  261. short vt = variantTypes[i];
  262. if (vt != VT_UNDEFINED)
  263. {
  264. //Set PropertyId
  265. newVectorIdentifiers[usedProperties] = i + basePropertyId;
  266. //Set VariantType
  267. newVectorProperties[usedProperties].vt = vt;
  268. if (vt == (short)(VT_VECTOR | VT_UI1))
  269. {
  270. if (handles[i] == null)
  271. newVectorProperties[usedProperties].caub.cElems = (uint)((byte[])objects[i]).Length;
  272. else
  273. newVectorProperties[usedProperties].caub.cElems = (uint)handles[i];
  274. GCHandle handle = GCHandle.Alloc(objects[i], GCHandleType.Pinned);
  275. handles[i] = handle;
  276. newVectorProperties[usedProperties].caub.pElems = handle.AddrOfPinnedObject();
  277. }
  278. else if (vt == VT_UI1 || vt == VT_I1)
  279. newVectorProperties[usedProperties].bVal = (byte)objects[i];
  280. else if (vt == VT_UI2 || vt == VT_I2)
  281. newVectorProperties[usedProperties].iVal = (short)objects[i];
  282. else if (vt == VT_UI4 || vt == VT_I4)
  283. newVectorProperties[usedProperties].lVal = (int)objects[i];
  284. else if (vt == VT_UI8 || vt == VT_I8)
  285. newVectorProperties[usedProperties].hVal = (long)objects[i];
  286. else if (vt == VT_LPWSTR || vt == VT_CLSID)
  287. {
  288. GCHandle handle = GCHandle.Alloc(objects[i], GCHandleType.Pinned);
  289. handles[i] = handle;
  290. newVectorProperties[usedProperties].ptr = handle.AddrOfPinnedObject();
  291. }
  292. else if (vt == VT_EMPTY)
  293. newVectorProperties[usedProperties].vt = 0; //real value for VT_EMPTY
  294. ++usedProperties;
  295. if (propertyCount == usedProperties)
  296. break;
  297. }
  298. }
  299. handleVectorIdentifiers = GCHandle.Alloc(newVectorIdentifiers, GCHandleType.Pinned);
  300. handleVectorProperties = GCHandle.Alloc(newVectorProperties, GCHandleType.Pinned);
  301. handleVectorStatus = GCHandle.Alloc(newVectorStatus, GCHandleType.Pinned);
  302. vectorIdentifiers = newVectorIdentifiers;
  303. vectorStatus = newVectorStatus;
  304. vectorProperties = newVectorProperties;
  305. reference.propertyCount = propertyCount;
  306. reference.propertyIdentifiers = handleVectorIdentifiers.AddrOfPinnedObject();
  307. reference.propertyValues = handleVectorProperties.AddrOfPinnedObject();
  308. reference.status = handleVectorStatus.AddrOfPinnedObject();
  309. return reference;
  310. }
  311. public virtual void Remove(int propertyId)
  312. {
  313. if (variantTypes[propertyId - basePropertyId] != VT_UNDEFINED)
  314. {
  315. variantTypes[propertyId - basePropertyId] = VT_UNDEFINED;
  316. objects[propertyId - basePropertyId] = null;
  317. handles[propertyId - basePropertyId] = null;
  318. --propertyCount;
  319. }
  320. }
  321. public virtual void SetNull(int propertyId)
  322. {
  323. if (variantTypes[propertyId - basePropertyId] == VT_UNDEFINED)
  324. {
  325. variantTypes[propertyId - basePropertyId] = VT_NULL;
  326. ++propertyCount;
  327. }
  328. objects[propertyId - basePropertyId] = null;
  329. }
  330. public virtual void SetEmpty(int propertyId)
  331. {
  332. if (variantTypes[propertyId - basePropertyId] == VT_UNDEFINED)
  333. {
  334. variantTypes[propertyId - basePropertyId] = VT_EMPTY;
  335. ++propertyCount;
  336. }
  337. objects[propertyId - basePropertyId] = null;
  338. }
  339. public virtual void Unlock()
  340. {
  341. for (int i = 0; i < vectorIdentifiers.Length; ++i)
  342. {
  343. short vt = (short)vectorProperties[i].vt;
  344. if (variantTypes[vectorIdentifiers[i] - basePropertyId] == VT_NULL)
  345. {
  346. if (vt == (short)(VT_VECTOR | VT_UI1) || vt == VT_NULL)
  347. //Support for MSMQ self memory allocation.
  348. objects[vectorIdentifiers[i] - basePropertyId] = vectorProperties[i].caub.cElems;
  349. else if (vt == (short)(VT_VECTOR | VT_LPWSTR))
  350. {
  351. //Support for MSMQ management apis.
  352. objects[vectorIdentifiers[i] - basePropertyId] = vectorProperties[i * 4].caub.cElems;
  353. handles[vectorIdentifiers[i] - basePropertyId] = vectorProperties[i * 4].caub.pElems;
  354. }
  355. else
  356. objects[vectorIdentifiers[i] - basePropertyId] = vectorProperties[i].ptr;
  357. }
  358. else if (vt == VT_LPWSTR || vt == VT_CLSID || vt == (short)(VT_VECTOR | VT_UI1))
  359. {
  360. ((GCHandle)handles[vectorIdentifiers[i] - basePropertyId]).Free();
  361. handles[vectorIdentifiers[i] - basePropertyId] = null;
  362. }
  363. else if (vt == VT_UI1 || vt == VT_I1)
  364. objects[vectorIdentifiers[i] - basePropertyId] = (byte)vectorProperties[i].bVal;
  365. else if (vt == VT_UI2 || vt == VT_I2)
  366. objects[vectorIdentifiers[i] - basePropertyId] = (short)vectorProperties[i].iVal;
  367. else if (vt == VT_UI4 || vt == VT_I4)
  368. objects[vectorIdentifiers[i] - basePropertyId] = vectorProperties[i].lVal;
  369. else if (vt == VT_UI8 || vt == VT_I8)
  370. objects[vectorIdentifiers[i] - basePropertyId] = vectorProperties[i].hVal;
  371. }
  372. handleVectorIdentifiers.Free();
  373. handleVectorProperties.Free();
  374. handleVectorStatus.Free();
  375. }
  376. [StructLayout(LayoutKind.Sequential)]
  377. public class MQPROPS
  378. {
  379. internal int propertyCount;
  380. [SuppressMessage("Microsoft.Reliability", "CA2006:UseSafeHandleToEncapsulateNativeResources")]
  381. internal IntPtr propertyIdentifiers;
  382. [SuppressMessage("Microsoft.Reliability", "CA2006:UseSafeHandleToEncapsulateNativeResources")]
  383. internal IntPtr propertyValues;
  384. [SuppressMessage("Microsoft.Reliability", "CA2006:UseSafeHandleToEncapsulateNativeResources")]
  385. internal IntPtr status;
  386. }
  387. }
  388. }