123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- //------------------------------------------------------------------------------
- // <copyright file="UnsafeNativeMethods.cs" company="Microsoft">
- // Copyright (c) Microsoft Corporation. All rights reserved.
- // </copyright>
- //------------------------------------------------------------------------------
- using System;
- using System.Runtime.InteropServices;
- using System.Security;
- using System.Text;
- using System.Threading;
- namespace Experimental.System.Messaging.Interop
- {
- [ComVisible(false),
- SuppressUnmanagedCodeSecurityAttribute()]
- internal static class UnsafeNativeMethods
- {
- [DllImport(ExternDll.Mqrt, EntryPoint = "MQOpenQueue", CharSet = CharSet.Unicode)]
- private static extern int IntMQOpenQueue(string formatName, int access, int shareMode, out MessageQueueHandle handle);
- public static int MQOpenQueue(string formatName, int access, int shareMode, out MessageQueueHandle handle)
- {
- try
- {
- return IntMQOpenQueue(formatName, access, shareMode, out handle);
- }
- catch (DllNotFoundException)
- {
- throw new InvalidOperationException(Res.GetString(Res.MSMQNotInstalled));
- }
- }
- [DllImport(ExternDll.Mqrt, CharSet = CharSet.Unicode)]
- public static extern int MQSendMessage(MessageQueueHandle handle, MessagePropertyVariants.MQPROPS properties, IntPtr transaction);
- [DllImport(ExternDll.Mqrt, CharSet = CharSet.Unicode)]
- public static extern int MQSendMessage(MessageQueueHandle handle, MessagePropertyVariants.MQPROPS properties, ITransaction transaction);
- [DllImport(ExternDll.Mqrt, CharSet = CharSet.Unicode)]
- public unsafe static extern int MQReceiveMessage(MessageQueueHandle handle, uint timeout, int action, MessagePropertyVariants.MQPROPS properties, NativeOverlapped* overlapped,
- SafeNativeMethods.ReceiveCallback receiveCallback, CursorHandle cursorHandle, IntPtr transaction);
- [DllImport(ExternDll.Mqrt, CharSet = CharSet.Unicode)]
- public unsafe static extern int MQReceiveMessage(MessageQueueHandle handle, uint timeout, int action, MessagePropertyVariants.MQPROPS properties, NativeOverlapped* overlapped,
- SafeNativeMethods.ReceiveCallback receiveCallback, CursorHandle cursorHandle, ITransaction transaction);
- [DllImport(ExternDll.Mqrt, EntryPoint = "MQReceiveMessageByLookupId", CharSet = CharSet.Unicode)]
- private unsafe static extern int IntMQReceiveMessageByLookupId(MessageQueueHandle handle, long lookupId, int action, MessagePropertyVariants.MQPROPS properties, NativeOverlapped* overlapped,
- SafeNativeMethods.ReceiveCallback receiveCallback, IntPtr transaction);
- public unsafe static int MQReceiveMessageByLookupId(MessageQueueHandle handle, long lookupId, int action, MessagePropertyVariants.MQPROPS properties, NativeOverlapped* overlapped,
- SafeNativeMethods.ReceiveCallback receiveCallback, IntPtr transaction)
- {
- try
- {
- return IntMQReceiveMessageByLookupId(handle, lookupId, action, properties, overlapped, receiveCallback, transaction);
- }
- catch (EntryPointNotFoundException)
- {
- throw new PlatformNotSupportedException(Res.GetString(Res.PlatformNotSupported));
- }
- }
- [DllImport(ExternDll.Mqrt, EntryPoint = "MQReceiveMessageByLookupId", CharSet = CharSet.Unicode)]
- private unsafe static extern int IntMQReceiveMessageByLookupId(MessageQueueHandle handle, long lookupId, int action, MessagePropertyVariants.MQPROPS properties, NativeOverlapped* overlapped,
- SafeNativeMethods.ReceiveCallback receiveCallback, ITransaction transaction);
- public unsafe static int MQReceiveMessageByLookupId(MessageQueueHandle handle, long lookupId, int action, MessagePropertyVariants.MQPROPS properties, NativeOverlapped* overlapped,
- SafeNativeMethods.ReceiveCallback receiveCallback, ITransaction transaction)
- {
- try
- {
- return IntMQReceiveMessageByLookupId(handle, lookupId, action, properties, overlapped, receiveCallback, transaction);
- }
- catch (EntryPointNotFoundException)
- {
- throw new PlatformNotSupportedException(Res.GetString(Res.PlatformNotSupported));
- }
- }
- [DllImport(ExternDll.Mqrt, EntryPoint = "MQCreateQueue", CharSet = CharSet.Unicode)]
- private static extern int IntMQCreateQueue(IntPtr securityDescriptor, MessagePropertyVariants.MQPROPS queueProperties, StringBuilder formatName, ref int formatNameLength);
- public static int MQCreateQueue(IntPtr securityDescriptor, MessagePropertyVariants.MQPROPS queueProperties, StringBuilder formatName, ref int formatNameLength)
- {
- try
- {
- return IntMQCreateQueue(securityDescriptor, queueProperties, formatName, ref formatNameLength);
- }
- catch (DllNotFoundException)
- {
- throw new InvalidOperationException(Res.GetString(Res.MSMQNotInstalled));
- }
- }
- [DllImport(ExternDll.Mqrt, EntryPoint = "MQDeleteQueue", CharSet = CharSet.Unicode)]
- private static extern int IntMQDeleteQueue(string formatName);
- public static int MQDeleteQueue(string formatName)
- {
- try
- {
- return IntMQDeleteQueue(formatName);
- }
- catch (DllNotFoundException)
- {
- throw new InvalidOperationException(Res.GetString(Res.MSMQNotInstalled));
- }
- }
- [DllImport(ExternDll.Mqrt, EntryPoint = "MQLocateBegin", CharSet = CharSet.Unicode)]
- private static extern int IntMQLocateBegin(string context, Restrictions.MQRESTRICTION Restriction, Columns.MQCOLUMNSET columnSet, IntPtr sortSet, out LocatorHandle enumHandle);
- public static int MQLocateBegin(string context, Restrictions.MQRESTRICTION Restriction, Columns.MQCOLUMNSET columnSet, out LocatorHandle enumHandle)
- {
- try
- {
- return IntMQLocateBegin(context, Restriction, columnSet, IntPtr.Zero, out enumHandle);
- }
- catch (DllNotFoundException)
- {
- throw new InvalidOperationException(Res.GetString(Res.MSMQNotInstalled));
- }
- }
- [DllImport(ExternDll.Mqrt, EntryPoint = "MQGetMachineProperties", CharSet = CharSet.Unicode)]
- private static extern int IntMQGetMachineProperties(string machineName, IntPtr machineIdPointer, MessagePropertyVariants.MQPROPS machineProperties);
- public static int MQGetMachineProperties(string machineName, IntPtr machineIdPointer, MessagePropertyVariants.MQPROPS machineProperties)
- {
- try
- {
- return IntMQGetMachineProperties(machineName, machineIdPointer, machineProperties);
- }
- catch (DllNotFoundException)
- {
- throw new InvalidOperationException(Res.GetString(Res.MSMQNotInstalled));
- }
- }
- [DllImport(ExternDll.Mqrt, EntryPoint = "MQGetQueueProperties", CharSet = CharSet.Unicode)]
- private static extern int IntMQGetQueueProperties(string formatName, MessagePropertyVariants.MQPROPS queueProperties);
- public static int MQGetQueueProperties(string formatName, MessagePropertyVariants.MQPROPS queueProperties)
- {
- try
- {
- return IntMQGetQueueProperties(formatName, queueProperties);
- }
- catch (DllNotFoundException)
- {
- throw new InvalidOperationException(Res.GetString(Res.MSMQNotInstalled));
- }
- }
- [DllImport(ExternDll.Mqrt, EntryPoint = "MQMgmtGetInfo", CharSet = CharSet.Unicode)]
- private static extern int IntMQMgmtGetInfo(string machineName, string objectName, MessagePropertyVariants.MQPROPS queueProperties);
- public static int MQMgmtGetInfo(string machineName, string objectName, MessagePropertyVariants.MQPROPS queueProperties)
- {
- try
- {
- return IntMQMgmtGetInfo(machineName, objectName, queueProperties);
- }
- catch (EntryPointNotFoundException)
- {
- throw new InvalidOperationException(Res.GetString(Res.MSMQInfoNotSupported));
- }
- catch (DllNotFoundException)
- {
- throw new InvalidOperationException(Res.GetString(Res.MSMQNotInstalled));
- }
- }
- [DllImport(ExternDll.Mqrt, CharSet = CharSet.Unicode)]
- public static extern int MQPurgeQueue(MessageQueueHandle handle);
- [DllImport(ExternDll.Mqrt, EntryPoint = "MQSetQueueProperties", CharSet = CharSet.Unicode)]
- private static extern int IntMQSetQueueProperties(string formatName, MessagePropertyVariants.MQPROPS queueProperties);
- public static int MQSetQueueProperties(string formatName, MessagePropertyVariants.MQPROPS queueProperties)
- {
- try
- {
- return IntMQSetQueueProperties(formatName, queueProperties);
- }
- catch (DllNotFoundException)
- {
- throw new InvalidOperationException(Res.GetString(Res.MSMQNotInstalled));
- }
- }
- // This method gets us the current security descriptor In "self-relative" format - so it contains offsets instead of pointers,
- // and we don't know how big the return buffer is, so we just use an IntPtr parameter
- [DllImport(ExternDll.Mqrt, CharSet = CharSet.Unicode, SetLastError = true)]
- public static extern int MQGetQueueSecurity(string formatName, int SecurityInformation, IntPtr SecurityDescriptor, int length, out int lengthNeeded);
- // This method takes a security descriptor In "absolute" formate - so it will always be the same size and
- // we can just use the SECURITY_DESCRIPTOR class.
- [DllImport(ExternDll.Mqrt, CharSet = CharSet.Unicode, SetLastError = true)]
- public static extern int MQSetQueueSecurity(string formatName, int SecurityInformation, NativeMethods.SECURITY_DESCRIPTOR SecurityDescriptor);
- [DllImport(ExternDll.Advapi32, SetLastError = true)]
- public static extern bool GetSecurityDescriptorDacl(IntPtr pSD, out bool daclPresent, out IntPtr pDacl, out bool daclDefaulted);
- [DllImport(ExternDll.Advapi32, SetLastError = true)]
- public static extern bool SetSecurityDescriptorDacl(NativeMethods.SECURITY_DESCRIPTOR pSD, bool daclPresent, IntPtr pDacl, bool daclDefaulted);
- [DllImport(ExternDll.Advapi32, SetLastError = true)]
- public static extern bool InitializeSecurityDescriptor(NativeMethods.SECURITY_DESCRIPTOR SD, int revision);
- [DllImport(ExternDll.Advapi32, CharSet = CharSet.Auto, SetLastError = true)]
- public static extern bool LookupAccountName(string lpSystemName,
- string lpAccountName,
- IntPtr sid,
- ref int sidSize,
- StringBuilder DomainName,
- ref int DomainSize,
- out int pUse);
- }
- }
|