-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageFilter.cs
More file actions
72 lines (61 loc) · 2.28 KB
/
Copy pathMessageFilter.cs
File metadata and controls
72 lines (61 loc) · 2.28 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
using System.Runtime.InteropServices;
namespace VisualStudioMcpServer;
/// <summary>
/// COM Message Filter to handle Visual Studio's busy state.
/// VS is a single-threaded COM server and will reject calls with RPC_E_CALL_REJECTED when busy.
/// This filter implements retry logic to gracefully handle those cases.
/// </summary>
[ComImport]
[Guid("00000016-0000-0000-C000-000000000046")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IOleMessageFilter
{
[PreserveSig]
int HandleInComingCall(int dwCallType, IntPtr hTaskCaller, int dwTickCount, IntPtr lpInterfaceInfo);
[PreserveSig]
int RetryRejectedCall(IntPtr hTaskCallee, int dwTickCount, int dwRejectType);
[PreserveSig]
int MessagePending(IntPtr hTaskCallee, int dwTickCount, int dwPendingType);
}
public class MessageFilter : IOleMessageFilter
{
private const int SERVERCALL_ISHANDLED = 0;
private const int PENDINGMSG_WAITDEFPROCESS = 2;
private const int SERVERCALL_RETRYLATER = 2;
[DllImport("Ole32.dll")]
private static extern int CoRegisterMessageFilter(IOleMessageFilter? newFilter, out IOleMessageFilter? oldFilter);
/// <summary>
/// Registers the message filter to handle COM retry logic.
/// Must be called from an STA thread before any COM operations.
/// </summary>
public static void Register()
{
IOleMessageFilter newFilter = new MessageFilter();
CoRegisterMessageFilter(newFilter, out _);
}
/// <summary>
/// Unregisters the message filter. Call on application exit.
/// </summary>
public static void Revoke()
{
CoRegisterMessageFilter(null, out _);
}
int IOleMessageFilter.HandleInComingCall(int dwCallType, IntPtr hTaskCaller, int dwTickCount, IntPtr lpInterfaceInfo)
{
return SERVERCALL_ISHANDLED;
}
int IOleMessageFilter.RetryRejectedCall(IntPtr hTaskCallee, int dwTickCount, int dwRejectType)
{
// If server is busy (SERVERCALL_RETRYLATER), retry after 99ms
if (dwRejectType == SERVERCALL_RETRYLATER)
{
return 99;
}
// Otherwise, cancel the call
return -1;
}
int IOleMessageFilter.MessagePending(IntPtr hTaskCallee, int dwTickCount, int dwPendingType)
{
return PENDINGMSG_WAITDEFPROCESS;
}
}