| | | 1 | | using System; |
| | | 2 | | using System.Runtime.InteropServices; |
| | | 3 | | using System.Runtime.InteropServices.Marshalling; |
| | | 4 | | using System.Threading; |
| | | 5 | | |
| | | 6 | | namespace SolidEdgeCommunity; |
| | | 7 | | |
| | | 8 | | internal enum SERVERCALL |
| | | 9 | | { |
| | | 10 | | SERVERCALL_ISHANDLED = 0, |
| | | 11 | | SERVERCALL_REJECTED = 1, |
| | | 12 | | SERVERCALL_RETRYLATER = 2 |
| | | 13 | | } |
| | | 14 | | |
| | | 15 | | internal enum PENDINGMSG |
| | | 16 | | { |
| | | 17 | | PENDINGMSG_CANCELCALL = 0, |
| | | 18 | | PENDINGMSG_WAITNOPROCESS = 1, |
| | | 19 | | PENDINGMSG_WAITDEFPROCESS = 2 |
| | | 20 | | } |
| | | 21 | | |
| | | 22 | | [GeneratedComInterface] |
| | | 23 | | [Guid("00000016-0000-0000-C000-000000000046")] |
| | | 24 | | [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] |
| | | 25 | | internal partial interface IMessageFilter |
| | | 26 | | { |
| | | 27 | | [PreserveSig] |
| | | 28 | | int HandleInComingCall(int dwCallType, IntPtr hTaskCaller, int dwTickCount, IntPtr lpInterfaceInfo); |
| | | 29 | | |
| | | 30 | | [PreserveSig] |
| | | 31 | | int MessagePending(IntPtr hTaskCallee, int dwTickCount, int dwPendingType); |
| | | 32 | | |
| | | 33 | | [PreserveSig] |
| | | 34 | | int RetryRejectedCall(IntPtr hTaskCallee, int dwTickCount, int dwRejectType); |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Class that implements the OLE IMessageFilter interface. |
| | | 39 | | /// </summary> |
| | | 40 | | [GeneratedComClass] |
| | | 41 | | public sealed partial class OleMessageFilter : IMessageFilter |
| | | 42 | | { |
| | | 43 | | [LibraryImport("Ole32.dll")] |
| | | 44 | | private static partial int CoRegisterMessageFilter(IMessageFilter newFilter, out IMessageFilter oldFilter); |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Private constructor. |
| | | 48 | | /// </summary> |
| | | 49 | | /// <remarks> |
| | | 50 | | /// Instance of this class is only created by Register(). |
| | | 51 | | /// </remarks> |
| | 4 | 52 | | private OleMessageFilter() |
| | | 53 | | { |
| | 4 | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Destructor. |
| | | 58 | | /// </summary> |
| | | 59 | | ~OleMessageFilter() |
| | | 60 | | { |
| | | 61 | | // Call Unregister() for good measure. It's fine if this gets called twice. |
| | 0 | 62 | | Unregister(); |
| | 0 | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Registers this instance of IMessageFilter with OLE to handle concurrency issues on the current thread. |
| | | 67 | | /// </summary> |
| | | 68 | | /// <remarks> |
| | | 69 | | /// Only one message filter can be registered for each thread. |
| | | 70 | | /// Threads in multithreaded apartments cannot have message filters. |
| | | 71 | | /// Thread.CurrentThread.GetApartmentState() must equal ApartmentState.STA. In console applications, this can |
| | | 72 | | /// be achieved by applying the STAThreadAttribute to the Main() method. In WinForm applications, it is default. |
| | | 73 | | /// </remarks> |
| | | 74 | | public static void Register() |
| | | 75 | | { |
| | | 76 | | // 1st check the current thread's apartment state. It must be STA! |
| | 1 | 77 | | if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA) |
| | | 78 | | { |
| | 0 | 79 | | System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(CoRegisterMessageFilter(newFilter: new OleMessage |
| | | 80 | | } |
| | | 81 | | else |
| | | 82 | | { |
| | 1 | 83 | | throw new Exception("The current thread's apartment state must be STA."); |
| | | 84 | | } |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Unregisters a previous instance of IMessageFilter with OLE on the current thread. |
| | | 89 | | /// </summary> |
| | | 90 | | /// <remarks> |
| | | 91 | | /// It is not necessary to call Unregister() unless you need to explicitly do so as it is handled |
| | | 92 | | /// in the destructor. |
| | | 93 | | /// </remarks> |
| | 1 | 94 | | public static void Unregister() => CoRegisterMessageFilter(newFilter: null, oldFilter: out _); // Call CoRegisterMes |
| | | 95 | | |
| | | 96 | | #region IMessageFilter |
| | | 97 | | |
| | 1 | 98 | | public int HandleInComingCall(int dwCallType, IntPtr hTaskCaller, int dwTickCount, IntPtr lpInterfaceInfo) => (int)S |
| | | 99 | | |
| | | 100 | | // Cancel the outgoing call. This should be returned only under extreme conditions. Canceling a call that |
| | | 101 | | // has not replied or been rejected can create orphan transactions and lose resources. COM fails the original |
| | | 102 | | // call and returns RPC_E_CALL_CANCELLED. |
| | | 103 | | //return (int)NativeMethods.PENDINGMSG.PENDINGMSG_CANCELCALL; |
| | | 104 | | |
| | | 105 | | // Continue waiting for the reply, and do not dispatch the message unless it is a task-switching or |
| | | 106 | | // window-activation message. A subsequent message will trigger another call to MessagePending. |
| | | 107 | | //return (int)NativeMethods.PENDINGMSG.PENDINGMSG_WAITNOPROCESS; |
| | | 108 | | |
| | | 109 | | // Keyboard and mouse messages are no longer dispatched. However there are some cases where mouse and |
| | | 110 | | // keyboard messages could cause the system to deadlock, and in these cases, mouse and keyboard messages |
| | | 111 | | // are discarded. WM_PAINT messages are dispatched. Task-switching and activation messages are handled as before. |
| | 1 | 112 | | public int MessagePending(IntPtr hTaskCallee, int dwTickCount, int dwPendingType) => (int)PENDINGMSG.PENDINGMSG_WAIT |
| | | 113 | | |
| | | 114 | | // 0 ≤ value < 100 |
| | | 115 | | // The call is to be retried immediately. |
| | | 116 | | |
| | | 117 | | // 100 ≤ value |
| | | 118 | | // COM will wait for this many milliseconds and then retry the call. |
| | | 119 | | |
| | | 120 | | // -1 | The call should be canceled. COM then returns RPC_E_CALL_REJECTED from the original method call. |
| | | 121 | | public int RetryRejectedCall(IntPtr hTaskCallee, int dwTickCount, int dwRejectType) |
| | 2 | 122 | | => (dwRejectType == (int)SERVERCALL.SERVERCALL_RETRYLATER) ? 99 : -1; |
| | | 123 | | |
| | | 124 | | #endregion IMessageFilter |
| | | 125 | | } |