在使用基于 Kean Walmsley 的建议的 AutoCAD 互操作对象时,我总是实现 IMessageFilter:http: //adndevblog.typepad.com/autocad/2012/05/cannot-instantiate-autocad-2010-from-an-external-net-application -安装后-更新-1.html
// IMessageFilter Interface
[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("00000016-0000-0000-C000-000000000046")]
public interface IMessageFilter
{
[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 partial class Form1 : Form, IMessageFilter
{
public Form1()
{
InitializeComponent();
CoRegisterMessageFilter(this, null);
}
int IMessageFilter.HandleInComingCall(int dwCallType, IntPtr hTaskCaller, int dwTickCount, IntPtr lpInterfaceInfo)
{
// SERVERCALL_ISHANDLED
return 0;
}
int IMessageFilter.RetryRejectedCall(IntPtr hTaskCallee, int dwTickCount, int dwRejectType)
{
// Retry in a second
return 1000;
}
int IMessageFilter.MessagePending(IntPtr hTaskCallee, int dwTickCount, int dwPendingType)
{
// PENDINGMSG_WAITNOPROCESS
return 1;
}
[DllImport("ole32.dll")]
private static extern int CoRegisterMessageFilter(IMessageFilter lpMessageFilter, IMessageFilter lplpMessageFilter);
...
}
它似乎基本上是在告诉那些 COM 异常,让它们闭嘴,等待 AutoCAD COM 对象准备好。