I’ve re-created the _DAcadApplicationEvents interface from an AutoCAD interop .dll using .NET Reflector, and have my code working for AutoCAD 2010. The problem is that I need this to be compatible with AutoCAD 2006 all the way up to 2012 and forward. I am running .NET 4.0, and am therefore using Embed Interop Type set to true to make sure my Application object resolves properly to runtime without needing reflection invoke methods, but I can’t seem to find a way to get the event interface working without hard-coding in the GUID (which is assembly specific).
Here is the custom interface I created from the original AutoDesk.AutoCAD.Interop.dll:
<ComImport(), Guid("1F893620-C96D-4361-BBAF-A61D4144B7F8"), TypeLibType(CShort(&H1010))>
Public Interface _DAcadApplicationEventsClone
<MethodImpl(MethodImplOptions.InternalCall, MethodCodeType:=MethodCodeType.Runtime), DispId(1)>
Sub SysVarChanged(<[In](), MarshalAs(UnmanagedType.BStr)> ByVal SysvarName As String, <[In](),
MarshalAs(UnmanagedType.Struct)> ByVal newVal As Object)
<MethodImpl(MethodImplOptions.InternalCall, MethodCodeType:=MethodCodeType.Runtime), DispId(8)>
Sub BeginQuit(<[In](), Out()> ByRef Cancel As Boolean)
End Interface
The Application object gets created in this manner:
Public Application As Object
Application = Marshal.GetActiveObject("AutoCAD.Application")
Next I am connecting to the cloned interface using IConnectionPoint:
Dim acGUID As New Guid(GetInterfaceGUID())
Dim acConnectionPoint As ComTypes.IConnectionPoint = Nothing
TryCast(Application, ComTypes.IConnectionPointContainer).FindConnectionPoint(acGUID, acConnectionPoint)
Dim acSinkCookie As Integer
acConnectionPoint.Advise(Me, acSinkCookie)
As you can see, I’m running a function called ‘GetInterfaceGUID’ to dynamically search for the right GUID depending on what version of AutoCAD is installed on the system. However, if the GUID returned doesn’t match the hard-coded GUID on the interface, it won’t work anyway. I’ve tried different methods of reflection to work with a System._ComObject, and this is the only method that I’ve gotten to even come close. I feel like I’m fairly close to succeeding, and would appreciate any input at this point. Thanks in advance.
-Locke
As you found, the AutoCAD COM Interop is version and even bitness (32bit/64bit) dependant. Why not use the AutoCAD .NET Application Events to get rid of these trouble as follows?
Here is the event registration code in the chosen IExtensionApplication implementation by the way: