UnsafeNativeMethods.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. //------------------------------------------------------------------------------
  2. // <copyright file="UnsafeNativeMethods.cs" company="Microsoft">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. //------------------------------------------------------------------------------
  6. using System;
  7. using System.Runtime.InteropServices;
  8. using System.Security;
  9. using System.Text;
  10. using System.Threading;
  11. namespace Experimental.System.Messaging.Interop
  12. {
  13. [ComVisible(false),
  14. SuppressUnmanagedCodeSecurityAttribute()]
  15. internal static class UnsafeNativeMethods
  16. {
  17. [DllImport(ExternDll.Mqrt, EntryPoint = "MQOpenQueue", CharSet = CharSet.Unicode)]
  18. private static extern int IntMQOpenQueue(string formatName, int access, int shareMode, out MessageQueueHandle handle);
  19. public static int MQOpenQueue(string formatName, int access, int shareMode, out MessageQueueHandle handle)
  20. {
  21. try
  22. {
  23. return IntMQOpenQueue(formatName, access, shareMode, out handle);
  24. }
  25. catch (DllNotFoundException)
  26. {
  27. throw new InvalidOperationException(Res.GetString(Res.MSMQNotInstalled));
  28. }
  29. }
  30. [DllImport(ExternDll.Mqrt, CharSet = CharSet.Unicode)]
  31. public static extern int MQSendMessage(MessageQueueHandle handle, MessagePropertyVariants.MQPROPS properties, IntPtr transaction);
  32. [DllImport(ExternDll.Mqrt, CharSet = CharSet.Unicode)]
  33. public static extern int MQSendMessage(MessageQueueHandle handle, MessagePropertyVariants.MQPROPS properties, ITransaction transaction);
  34. [DllImport(ExternDll.Mqrt, CharSet = CharSet.Unicode)]
  35. public unsafe static extern int MQReceiveMessage(MessageQueueHandle handle, uint timeout, int action, MessagePropertyVariants.MQPROPS properties, NativeOverlapped* overlapped,
  36. SafeNativeMethods.ReceiveCallback receiveCallback, CursorHandle cursorHandle, IntPtr transaction);
  37. [DllImport(ExternDll.Mqrt, CharSet = CharSet.Unicode)]
  38. public unsafe static extern int MQReceiveMessage(MessageQueueHandle handle, uint timeout, int action, MessagePropertyVariants.MQPROPS properties, NativeOverlapped* overlapped,
  39. SafeNativeMethods.ReceiveCallback receiveCallback, CursorHandle cursorHandle, ITransaction transaction);
  40. [DllImport(ExternDll.Mqrt, EntryPoint = "MQReceiveMessageByLookupId", CharSet = CharSet.Unicode)]
  41. private unsafe static extern int IntMQReceiveMessageByLookupId(MessageQueueHandle handle, long lookupId, int action, MessagePropertyVariants.MQPROPS properties, NativeOverlapped* overlapped,
  42. SafeNativeMethods.ReceiveCallback receiveCallback, IntPtr transaction);
  43. public unsafe static int MQReceiveMessageByLookupId(MessageQueueHandle handle, long lookupId, int action, MessagePropertyVariants.MQPROPS properties, NativeOverlapped* overlapped,
  44. SafeNativeMethods.ReceiveCallback receiveCallback, IntPtr transaction)
  45. {
  46. try
  47. {
  48. return IntMQReceiveMessageByLookupId(handle, lookupId, action, properties, overlapped, receiveCallback, transaction);
  49. }
  50. catch (EntryPointNotFoundException)
  51. {
  52. throw new PlatformNotSupportedException(Res.GetString(Res.PlatformNotSupported));
  53. }
  54. }
  55. [DllImport(ExternDll.Mqrt, EntryPoint = "MQReceiveMessageByLookupId", CharSet = CharSet.Unicode)]
  56. private unsafe static extern int IntMQReceiveMessageByLookupId(MessageQueueHandle handle, long lookupId, int action, MessagePropertyVariants.MQPROPS properties, NativeOverlapped* overlapped,
  57. SafeNativeMethods.ReceiveCallback receiveCallback, ITransaction transaction);
  58. public unsafe static int MQReceiveMessageByLookupId(MessageQueueHandle handle, long lookupId, int action, MessagePropertyVariants.MQPROPS properties, NativeOverlapped* overlapped,
  59. SafeNativeMethods.ReceiveCallback receiveCallback, ITransaction transaction)
  60. {
  61. try
  62. {
  63. return IntMQReceiveMessageByLookupId(handle, lookupId, action, properties, overlapped, receiveCallback, transaction);
  64. }
  65. catch (EntryPointNotFoundException)
  66. {
  67. throw new PlatformNotSupportedException(Res.GetString(Res.PlatformNotSupported));
  68. }
  69. }
  70. [DllImport(ExternDll.Mqrt, EntryPoint = "MQCreateQueue", CharSet = CharSet.Unicode)]
  71. private static extern int IntMQCreateQueue(IntPtr securityDescriptor, MessagePropertyVariants.MQPROPS queueProperties, StringBuilder formatName, ref int formatNameLength);
  72. public static int MQCreateQueue(IntPtr securityDescriptor, MessagePropertyVariants.MQPROPS queueProperties, StringBuilder formatName, ref int formatNameLength)
  73. {
  74. try
  75. {
  76. return IntMQCreateQueue(securityDescriptor, queueProperties, formatName, ref formatNameLength);
  77. }
  78. catch (DllNotFoundException)
  79. {
  80. throw new InvalidOperationException(Res.GetString(Res.MSMQNotInstalled));
  81. }
  82. }
  83. [DllImport(ExternDll.Mqrt, EntryPoint = "MQDeleteQueue", CharSet = CharSet.Unicode)]
  84. private static extern int IntMQDeleteQueue(string formatName);
  85. public static int MQDeleteQueue(string formatName)
  86. {
  87. try
  88. {
  89. return IntMQDeleteQueue(formatName);
  90. }
  91. catch (DllNotFoundException)
  92. {
  93. throw new InvalidOperationException(Res.GetString(Res.MSMQNotInstalled));
  94. }
  95. }
  96. [DllImport(ExternDll.Mqrt, EntryPoint = "MQLocateBegin", CharSet = CharSet.Unicode)]
  97. private static extern int IntMQLocateBegin(string context, Restrictions.MQRESTRICTION Restriction, Columns.MQCOLUMNSET columnSet, IntPtr sortSet, out LocatorHandle enumHandle);
  98. public static int MQLocateBegin(string context, Restrictions.MQRESTRICTION Restriction, Columns.MQCOLUMNSET columnSet, out LocatorHandle enumHandle)
  99. {
  100. try
  101. {
  102. return IntMQLocateBegin(context, Restriction, columnSet, IntPtr.Zero, out enumHandle);
  103. }
  104. catch (DllNotFoundException)
  105. {
  106. throw new InvalidOperationException(Res.GetString(Res.MSMQNotInstalled));
  107. }
  108. }
  109. [DllImport(ExternDll.Mqrt, EntryPoint = "MQGetMachineProperties", CharSet = CharSet.Unicode)]
  110. private static extern int IntMQGetMachineProperties(string machineName, IntPtr machineIdPointer, MessagePropertyVariants.MQPROPS machineProperties);
  111. public static int MQGetMachineProperties(string machineName, IntPtr machineIdPointer, MessagePropertyVariants.MQPROPS machineProperties)
  112. {
  113. try
  114. {
  115. return IntMQGetMachineProperties(machineName, machineIdPointer, machineProperties);
  116. }
  117. catch (DllNotFoundException)
  118. {
  119. throw new InvalidOperationException(Res.GetString(Res.MSMQNotInstalled));
  120. }
  121. }
  122. [DllImport(ExternDll.Mqrt, EntryPoint = "MQGetQueueProperties", CharSet = CharSet.Unicode)]
  123. private static extern int IntMQGetQueueProperties(string formatName, MessagePropertyVariants.MQPROPS queueProperties);
  124. public static int MQGetQueueProperties(string formatName, MessagePropertyVariants.MQPROPS queueProperties)
  125. {
  126. try
  127. {
  128. return IntMQGetQueueProperties(formatName, queueProperties);
  129. }
  130. catch (DllNotFoundException)
  131. {
  132. throw new InvalidOperationException(Res.GetString(Res.MSMQNotInstalled));
  133. }
  134. }
  135. [DllImport(ExternDll.Mqrt, EntryPoint = "MQMgmtGetInfo", CharSet = CharSet.Unicode)]
  136. private static extern int IntMQMgmtGetInfo(string machineName, string objectName, MessagePropertyVariants.MQPROPS queueProperties);
  137. public static int MQMgmtGetInfo(string machineName, string objectName, MessagePropertyVariants.MQPROPS queueProperties)
  138. {
  139. try
  140. {
  141. return IntMQMgmtGetInfo(machineName, objectName, queueProperties);
  142. }
  143. catch (EntryPointNotFoundException)
  144. {
  145. throw new InvalidOperationException(Res.GetString(Res.MSMQInfoNotSupported));
  146. }
  147. catch (DllNotFoundException)
  148. {
  149. throw new InvalidOperationException(Res.GetString(Res.MSMQNotInstalled));
  150. }
  151. }
  152. [DllImport(ExternDll.Mqrt, CharSet = CharSet.Unicode)]
  153. public static extern int MQPurgeQueue(MessageQueueHandle handle);
  154. [DllImport(ExternDll.Mqrt, EntryPoint = "MQSetQueueProperties", CharSet = CharSet.Unicode)]
  155. private static extern int IntMQSetQueueProperties(string formatName, MessagePropertyVariants.MQPROPS queueProperties);
  156. public static int MQSetQueueProperties(string formatName, MessagePropertyVariants.MQPROPS queueProperties)
  157. {
  158. try
  159. {
  160. return IntMQSetQueueProperties(formatName, queueProperties);
  161. }
  162. catch (DllNotFoundException)
  163. {
  164. throw new InvalidOperationException(Res.GetString(Res.MSMQNotInstalled));
  165. }
  166. }
  167. // This method gets us the current security descriptor In "self-relative" format - so it contains offsets instead of pointers,
  168. // and we don't know how big the return buffer is, so we just use an IntPtr parameter
  169. [DllImport(ExternDll.Mqrt, CharSet = CharSet.Unicode, SetLastError = true)]
  170. public static extern int MQGetQueueSecurity(string formatName, int SecurityInformation, IntPtr SecurityDescriptor, int length, out int lengthNeeded);
  171. // This method takes a security descriptor In "absolute" formate - so it will always be the same size and
  172. // we can just use the SECURITY_DESCRIPTOR class.
  173. [DllImport(ExternDll.Mqrt, CharSet = CharSet.Unicode, SetLastError = true)]
  174. public static extern int MQSetQueueSecurity(string formatName, int SecurityInformation, NativeMethods.SECURITY_DESCRIPTOR SecurityDescriptor);
  175. [DllImport(ExternDll.Advapi32, SetLastError = true)]
  176. public static extern bool GetSecurityDescriptorDacl(IntPtr pSD, out bool daclPresent, out IntPtr pDacl, out bool daclDefaulted);
  177. [DllImport(ExternDll.Advapi32, SetLastError = true)]
  178. public static extern bool SetSecurityDescriptorDacl(NativeMethods.SECURITY_DESCRIPTOR pSD, bool daclPresent, IntPtr pDacl, bool daclDefaulted);
  179. [DllImport(ExternDll.Advapi32, SetLastError = true)]
  180. public static extern bool InitializeSecurityDescriptor(NativeMethods.SECURITY_DESCRIPTOR SD, int revision);
  181. [DllImport(ExternDll.Advapi32, CharSet = CharSet.Auto, SetLastError = true)]
  182. public static extern bool LookupAccountName(string lpSystemName,
  183. string lpAccountName,
  184. IntPtr sid,
  185. ref int sidSize,
  186. StringBuilder DomainName,
  187. ref int DomainSize,
  188. out int pUse);
  189. }
  190. }