SafeNativeMethods.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //------------------------------------------------------------------------------
  2. // <copyright file="SafeNativeMethods.cs" company="Microsoft">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. //------------------------------------------------------------------------------
  6. using System;
  7. using System.Runtime.ConstrainedExecution; //for ReliabilityContract
  8. using System.Runtime.InteropServices;
  9. using System.Security;
  10. using System.Text;
  11. using System.Threading;
  12. namespace Experimental.System.Messaging.Interop
  13. {
  14. [ComVisible(false),
  15. SuppressUnmanagedCodeSecurityAttribute()]
  16. internal static class SafeNativeMethods
  17. {
  18. public unsafe delegate void ReceiveCallback(int result, IntPtr handle, int timeout, int action, IntPtr propertiesPointer, NativeOverlapped* overlappedPointer, IntPtr cursorHandle);
  19. [DllImport(ExternDll.Mqrt, EntryPoint = "MQBeginTransaction", CharSet = CharSet.Unicode)]
  20. public static extern int IntMQBeginTransaction(out ITransaction refTransaction);
  21. public static int MQBeginTransaction(out ITransaction refTransaction)
  22. {
  23. try
  24. {
  25. return IntMQBeginTransaction(out refTransaction);
  26. }
  27. catch (DllNotFoundException)
  28. {
  29. throw new InvalidOperationException(Res.GetString(Res.MSMQNotInstalled));
  30. }
  31. }
  32. [DllImport(ExternDll.Mqrt, CharSet = CharSet.Unicode)]
  33. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
  34. public static extern int MQCloseQueue(IntPtr handle);
  35. [DllImport(ExternDll.Mqrt, EntryPoint = "MQPathNameToFormatName", CharSet = CharSet.Unicode)]
  36. private static extern int IntMQPathNameToFormatName(string pathName, StringBuilder formatName, ref int count);
  37. public static int MQPathNameToFormatName(string pathName, StringBuilder formatName, ref int count)
  38. {
  39. try
  40. {
  41. return IntMQPathNameToFormatName(pathName, formatName, ref count);
  42. }
  43. catch (DllNotFoundException)
  44. {
  45. throw new InvalidOperationException(Res.GetString(Res.MSMQNotInstalled));
  46. }
  47. }
  48. [DllImport(ExternDll.Mqrt, EntryPoint = "MQInstanceToFormatName", CharSet = CharSet.Unicode)]
  49. public static extern int IntMQInstanceToFormatName(byte[] id, StringBuilder formatName, ref int count);
  50. public static int MQInstanceToFormatName(byte[] id, StringBuilder formatName, ref int count)
  51. {
  52. try
  53. {
  54. return IntMQInstanceToFormatName(id, formatName, ref count);
  55. }
  56. catch (DllNotFoundException)
  57. {
  58. throw new InvalidOperationException(Res.GetString(Res.MSMQNotInstalled));
  59. }
  60. }
  61. [DllImport(ExternDll.Mqrt, CharSet = CharSet.Unicode)]
  62. public static extern int MQCreateCursor(MessageQueueHandle handle, out CursorHandle cursorHandle);
  63. [DllImport(ExternDll.Mqrt, CharSet = CharSet.Unicode)]
  64. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
  65. public static extern int MQCloseCursor(IntPtr cursorHandle);
  66. [DllImport(ExternDll.Mqrt, CharSet = CharSet.Unicode)]
  67. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
  68. public static extern void MQFreeSecurityContext(IntPtr handle);
  69. [DllImport(ExternDll.Mqrt, CharSet = CharSet.Unicode)]
  70. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
  71. public static extern int MQLocateEnd(IntPtr enumHandle);
  72. [DllImport(ExternDll.Mqrt, CharSet = CharSet.Unicode)]
  73. public static extern int MQLocateNext(LocatorHandle enumHandle, ref int propertyCount, [Out] MQPROPVARIANTS[] variantArray);
  74. [DllImport(ExternDll.Mqrt, CharSet = CharSet.Unicode)]
  75. public static extern void MQFreeMemory(IntPtr memory);
  76. [DllImport(ExternDll.Kernel32, ExactSpelling = true, CharSet = CharSet.Auto, SetLastError = true)]
  77. public static extern bool GetHandleInformation(SafeHandle handle, out int handleInformation);
  78. [DllImport(ExternDll.Kernel32)]
  79. public static extern IntPtr LocalFree(IntPtr hMem);
  80. [DllImport(ExternDll.Advapi32)]
  81. public static extern int SetEntriesInAclW(int count,
  82. //[MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0, ArraySubType = ExplicitAccess)]
  83. //ExplicitAccess[] entries,
  84. IntPtr entries,
  85. IntPtr oldacl,
  86. out IntPtr newAcl);
  87. [DllImport(ExternDll.Kernel32, CharSet = CharSet.Auto)]
  88. public static extern bool GetComputerName(StringBuilder lpBuffer, int[] nSize);
  89. public const int FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100,
  90. FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200,
  91. FORMAT_MESSAGE_FROM_STRING = 0x00000400,
  92. FORMAT_MESSAGE_FROM_HMODULE = 0x00000800,
  93. FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000,
  94. FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x00002000,
  95. FORMAT_MESSAGE_MAX_WIDTH_MASK = 0x000000FF;
  96. [DllImport(ExternDll.Kernel32, CharSet = CharSet.Auto)]
  97. public static extern int FormatMessage(int dwFlags, IntPtr lpSource, int dwMessageId,
  98. int dwLanguageId, StringBuilder lpBuffer, int nSize, IntPtr arguments);
  99. }
  100. }