Question

The program I'm working on will be used on AutoCAD 2013 and 2002. So, what I'm doing is checking if the user has 2013 and if it is not present to try 2002. The problem comes when the code links the AcadApplication object to the opened instance of 2002.

code:

_progID_2002 = "AutoCAD.Application.15";
_progID_2013 = "AutoCAD.Application.19";

try
{
    Type acType = Type.GetTypeFromProgID(_progID_2002);
    _acadApp = (AcadApplication)Activator.CreateInstance(acType, true);
}
catch
{
    // Try other version, or exit
}

So this works perfectly when I use _progID_2013. It opens AutoCAD 2013 and _acadApp gets linked. When I try it with _progID_2002 it opens AutoCAD 2002, but when I set _acadApp to the opened instance it throws the exception:

InvalidCastException
Unable to cast COM object of the type 'System._ComObject' to interface type   
'AutoCAD.AcadApplication'. This operation failed because the QueryInterface 
call on the COM component for the interface with IID '{070AA05D-DFC1-4E64-
8379-432269B48B07}' failed due to the following error: No such interface
supported (Exception from HRESULT:0x80004002 (E_NOINTERFACE)).

I've tried using both the 2000 and 2013 interop libraries with no luck.

Was it helpful?

Solution

The problem is not the version, since you're using COM to get AutoCAD. The GetObject calls worked back in VB6 days, so it's not that the AutoCAD.Application.15 object can't be created, it's that you're trying to cast it into a defined type it won't match. IN your project, what assembly do you have loaded that defines the AcadApplication type? I can guarantee you the 2002 and 2013 versions don't play nice together.

I would build this project in .NET 4.0 and have _acadApp be a Dynamic variable.

Dynamic _acadApp;
try
{
    Type acType = Type.GetTypeFromProgID(_progID_2002);
    _acadApp = Activator.CreateInstance(acType, true);
}

It will mean you won't have any intellisense while writing, and it will compile even if you've typed in methods/properties that don't exists. That means you may have methods that work for 2002 and not 2013 and vice versa. If you have to use .NET 3.5 or lower, Reflection would be the way to go.

OTHER TIPS

While messing around with the answer that Locke gave, I found out if I ran the app as a x64 instead of x86 the app would work with Acad 2002. I have to use the 2000 object library and it won't open 2013 anymore because of the same error. So, I have to build two different versions of the app. One for 2002 version run as x64 and one for the 2013 version which will work whether it's x64 or x86. Someone smarter than me could probably tell us why.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top