Author Topic: Why I get the null for AcadApplication?  (Read 2921 times)

0 Members and 1 Guest are viewing this topic.

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Why I get the null for AcadApplication?
« on: March 27, 2013, 08:48:32 AM »
Code - C#: [Select]
  1. // AutoCAD 2009 - 2013
  2. using cad = Autodesk.AutoCAD.ApplicationServices.Application;
  3. ...
  4. Autodesk.AutoCAD.Interop.AcadApplication acadInt = cad.AcadApplication as Autodesk.AutoCAD.Interop.AcadApplication;
  5.  
Why I get the null for acadInt variable on the some machines???

This code works fine in the AutoCAD 2009 (at our company), and in the AutoCAD 2010-2013 on virtual machines at my home. But it ain't working in the AutoCAD 2013 on machine at my office. Why???

I see it:

Code - C#: [Select]
  1. Boolean x0 = cad.AcadApplication == null; // false
  2. Boolean x1 = cad.AcadApplication is Autodesk.AutoCAD.Interop.AcadApplication; // false
  3. Boolean x2 = cad.AcadApplication is Autodesk.AutoCAD.Interop.IAcadApplication; // false
  4. Boolean x3 = cad.AcadApplication is Autodesk.AutoCAD.Interop._DAcadApplicationEvents_Event; // false
  5.  
« Last Edit: March 27, 2013, 09:12:19 AM by Andrey »

TheMaster

  • Guest
Re: Why I get the null for AcadApplication?
« Reply #1 on: March 27, 2013, 08:51:07 PM »
The same build will not work across all of those releases if it is referencing AutoCAD's COM interop assemblies (for which release?).

COM interop is release-dependent, and it isn't wise to be dependent on them, if you can avoid it.

You can use System.Dynamic in .NET 4.0 to make late-bound calls to AutoCAD COM methods without having to reference the COM assemblies.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8706
  • AKA Daniel
Re: Why I get the null for AcadApplication?
« Reply #2 on: March 28, 2013, 12:33:19 AM »
In theory, that call should never return null as I think the property calls acedGetIDispatch.  Unless of course the class AcadApplication has changed between versions as Tony mentioned.  Try a C style cast and see if you get a bad cast exception
« Last Edit: March 28, 2013, 12:36:26 AM by nullptr »

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: Why I get the null for AcadApplication?
« Reply #3 on: March 28, 2013, 01:05:28 AM »
It is strangely: I removed this topic yesterday, because I found out a cause of error... But today I see it again and with answers.

All thanks for answers.

Earlier I compiled a code for AutoCAD 2009. I thought that if for assembly of Interop (the Autodesk.AutoCAD.Interop.Common.dll, and the Autodesk.AutoCAD.Interop.dll) "Copy Local"  is equal "false",  will be used the version  found in work process (x86/x64). My program worked in AutoCAD 2009 x86 and AutoCAD 2009 x64 without recompilation (I compiled as "AnyCPU"). But when I compiled it for AutoCAD 2012 and AutoCAD 2013 it ceased to work. If I specify for the Autodesk.AutoCAD.Interop.Common.dll, and for the Autodesk.AutoCAD.Interop.dll the exact versions of libraries (x86 or x64) then everything works fine.

But I still compile my program as "AnyCPU". Whether there can be problems because of it? Or it is better to me to recompile such programs as "x68" and "x64"?

Best Regards

TheMaster

  • Guest
Re: Why I get the null for AcadApplication?
« Reply #4 on: March 28, 2013, 01:08:18 AM »
In theory, that call should never return null as I think the property calls acedGetIDispatch.  Unless of course the class AcadApplication has changed between versions as Tony mentioned.  Try a C style cast and see if you get a bad cast exception

In the case of an RCW, using the 'as' operator leads to a call to IUnknown::QueryInterface() with the interface type specified in the [ComImport()] attribute on the RCW class. If it returns E_NOINTERFACE, 'as' doesn't throw any exception, it just returns null, so that is most-likely what's happening.