forked from Viruaithal/ScriptPro
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathMessageFilter.cs
More file actions
145 lines (134 loc) · 5.18 KB
/
MessageFilter.cs
File metadata and controls
145 lines (134 loc) · 5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
namespace ScriptUI
{
using System;
using System.Runtime.InteropServices;
/// <summary>
/// Message Handler demo implementation to preserve the E_REJECTED_XXX Errros (See Msdn documentation for Visual Studio).
/// </summary>
public class MessageFilter : IOleMessageFilter
{
/// <summary>
/// Start the filter.
/// </summary>
public static void Register()
{
IOleMessageFilter newFilter = new MessageFilter();
IOleMessageFilter oldFilter = null;
int test = CoRegisterMessageFilter(newFilter, out oldFilter);
if (test != 0)
{
Console.WriteLine(string.Format("CoRegisterMessageFilter failed with error : {0}", test));
}
}
/// <summary>
/// Done with the filter, close it.
/// </summary>
public static void Revoke()
{
IOleMessageFilter oldFilter = null;
int test = CoRegisterMessageFilter(null, out oldFilter);
}
/// <summary>
/// Handles the in coming thread requests.
/// </summary>
/// <param name="dwCallType">Type of the dw call.</param>
/// <param name="hTaskCaller">The h task caller.</param>
/// <param name="dwTickCount">The dw tick count.</param>
/// <param name="lpInterfaceInfo">The lp interface info.</param>
/// <returns>.</returns>
int IOleMessageFilter.HandleInComingCall(int dwCallType,
System.IntPtr hTaskCaller, int dwTickCount, System.IntPtr
lpInterfaceInfo)
{
//Return the flag SERVERCALL_ISHANDLED.
return 0;
}
/// <summary>
/// Retries the rejected call.
/// </summary>
/// <param name="hTaskCallee">The h task callee.</param>
/// <param name="dwTickCount">The dw tick count.</param>
/// <param name="dwRejectType">Type of the dw reject.</param>
/// <returns>.</returns>
int IOleMessageFilter.RetryRejectedCall(System.IntPtr hTaskCallee, int dwTickCount, int dwRejectType)
{
// Thread call was rejected, so try again.
if (dwRejectType == 2)
// flag = SERVERCALL_RETRYLATER.
{
// Retry the thread call immediately if return >=0 &
// <100.
return 99;
}
// Too busy; cancel call.
return -1;
}
/// <summary>
/// The MessagePending.
/// </summary>
/// <param name="hTaskCallee">The h task callee.</param>
/// <param name="dwTickCount">The dw tick count.</param>
/// <param name="dwPendingType">Type of the dw pending.</param>
/// <returns>.</returns>
int IOleMessageFilter.MessagePending(System.IntPtr hTaskCallee, int dwTickCount, int dwPendingType)
{
//Return the flag PENDINGMSG_WAITDEFPROCESS.
return 2;
}
// Implement the IOleMessageFilter interface.
/// <summary>
/// The CoRegisterMessageFilter.
/// </summary>
/// <param name="newFilter">The newFilter<see cref="IOleMessageFilter"/>.</param>
/// <param name="oldFilter">The oldFilter<see cref="IOleMessageFilter"/>.</param>
/// <returns>The <see cref="int"/>.</returns>
[DllImport("Ole32.dll")]
private static extern int CoRegisterMessageFilter(IOleMessageFilter newFilter, out IOleMessageFilter oldFilter);
}
/// <summary>
/// Definition of the IOleMessageFilter interface.
/// </summary>
[ComImport(), Guid("00000016-0000-0000-C000-000000000046"),
InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
interface IOleMessageFilter
{
/// <summary>
/// Handles the in coming call.
/// </summary>
/// <param name="dwCallType">Type of the dw call.</param>
/// <param name="hTaskCaller">The h task caller.</param>
/// <param name="dwTickCount">The dw tick count.</param>
/// <param name="lpInterfaceInfo">The lp interface info.</param>
/// <returns>.</returns>
[PreserveSig]
int HandleInComingCall(
int dwCallType,
IntPtr hTaskCaller,
int dwTickCount,
IntPtr lpInterfaceInfo);
/// <summary>
/// Retries the rejected call.
/// </summary>
/// <param name="hTaskCallee">The h task callee.</param>
/// <param name="dwTickCount">The dw tick count.</param>
/// <param name="dwRejectType">Type of the dw reject.</param>
/// <returns>.</returns>
[PreserveSig]
int RetryRejectedCall(
IntPtr hTaskCallee,
int dwTickCount,
int dwRejectType);
/// <summary>
/// Messages the pending.
/// </summary>
/// <param name="hTaskCallee">The h task callee.</param>
/// <param name="dwTickCount">The dw tick count.</param>
/// <param name="dwPendingType">Type of the dw pending.</param>
/// <returns>.</returns>
[PreserveSig]
int MessagePending(
IntPtr hTaskCallee,
int dwTickCount,
int dwPendingType);
}
}