Error while programmatically launching AutoCad

Error while programmatically launching AutoCad

larry.daubenspeck
Advocate Advocate
4,171 Views
17 Replies
Message 1 of 18

Error while programmatically launching AutoCad

larry.daubenspeck
Advocate
Advocate

We have had a VB.net program on a 32-bit machine for years. As part of its process, it was launching a standalone seat of AutoCad 2009 (which gives you an idea of how old it is). Recently, we upgraded to 64-bit AutoCad 2015 and migrated the program to a 64-bit machine. All works wonderfully from the Debugger, or when compiled as an executable and triggered manually. However, as with the original, we want to trigger the program with Task Scheduler. Here is where an error occurs. The error I get is that the “value does not fall within the expected range.” I have tried every different incantation to start AutoCad that I have come across:

  • acadApp = New AcadApplication
  • acadApp = CreateObject(“Autocad.Application.20.0”)
  • Dim acType As Type = Type.GetTypeFromProgID(“AutoCAD.Application.20”)
  • acadApp = Activator.CreateInstance(acType, True)

 

But nothing seems to solve the issue.

 

Any suggestions would be greatly appreciated.

Larry Daubenspeck
Software Programmer
Draper, Inc.
0 Likes
Accepted solutions (1)
4,172 Views
17 Replies
Replies (17)
Message 2 of 18

augusto.goncalves
Alumni
Alumni
Please make sure your code is using AutoCAD 2015 COM Type Lirabry, more specifically the 64 bit version. Also make sure your project is properly set to AnyCPU or 64 bit (if is set to 32 bit only, it can crash)
Regards,



Augusto Goncalves
Twitter @augustomaia
Autodesk Developer Network
0 Likes
Message 3 of 18

larry.daubenspeck
Advocate
Advocate

Thanks Augusto,

 

Both things you suggest are set correctly, yet still getting the error.  There are two interesting errors, which might be enlightening:

 

Warning               3              Processing COM reference "AutoCAD" from path "C:\Program Files\Common Files\Autodesk Shared\acax20enu.tlb". Interface 'IAcadShadowDisplay' is marked as [dual], but does not derive from IDispatch. It will be converted as an IUnknown-derived interface.  

 

Warning               4              Processing COM reference "AXDBLib" from path "C:\Program Files\Common Files\Autodesk Shared\axdb20enu.tlb". Interface 'IAcadShadowDisplay' is marked as [dual], but does not derive from IDispatch. It will be converted as an IUnknown-derived interface.  

 

I have no idea what they mean.

 

Thanks again.

Larry Daubenspeck
Software Programmer
Draper, Inc.
0 Likes
Message 4 of 18

augusto.goncalves
Alumni
Alumni
Got it!

Please use Autodesk.AutoCAD.Interop.dll and Autodesk.AutoCAD.Interop.Common.dll references from AutoCAD install folder (e.g. \Program Files\Autodesk\AutoCAD 2015)
Regards,



Augusto Goncalves
Twitter @augustomaia
Autodesk Developer Network
0 Likes
Message 5 of 18

larry.daubenspeck
Advocate
Advocate

Thanks again Augusto,

 

You're on a roll!  It eliminated one of the Visual Studio warnings; however, still getting this one (along with the original error about the value not falling within the expected range).

 

Warning 3 Processing COM reference "AXDBLib" from path "C:\Program Files\Common Files\Autodesk Shared\axdb20enu.tlb". Interface 'IAcadShadowDisplay' is marked as [dual], but does not derive from IDispatch. It will be converted as an IUnknown-derived interface. 

Larry Daubenspeck
Software Programmer
Draper, Inc.
0 Likes
Message 6 of 18

larry.daubenspeck
Advocate
Advocate

I got the other warning to go away by deleting a reference that wasn't being used.  However, with some different error-trapping, I was able to get the following information:

 

System.Runtime.InteropServices.COMException (0x80080005): Retrieving the COM class factory for component with CLSID {0B628DE4-07AD-4284-81CA-5B439F67C5E6} failed due to the following error: 80080005 Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)).

   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)

   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)

   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)

   at System.Activator.CreateInstance(Type type, Boolean nonPublic)

   at System.Activator.CreateInstance(Type type)

   at Draper_Draftsman.clsRolledScreens.Draw() in C:\Users\draftsman\Documents\Visual Studio 2012\Projects\Draper_Draftsman\Draper_Draftsman\clsRolledScreens.vb:line 331

 

Line 331 is simply this:  acadApp = New AcadApplication

 

As always, any suggestions would be greatly appreciated.

Larry Daubenspeck
Software Programmer
Draper, Inc.
0 Likes
Message 7 of 18

augusto.goncalves
Alumni
Alumni
I would suggest use Activator.CreateInstance instead

The "New" operator is different...
Regards,



Augusto Goncalves
Twitter @augustomaia
Autodesk Developer Network
0 Likes
Message 8 of 18

larry.daubenspeck
Advocate
Advocate

Augusto,

 

I used this, to no avail:

 

Dim acType As Type = Type.GetTypeFromProgID("Autocad.Application.20")

acadApp = Activator.CreateInstance(acType, True)

 

I saw that there are sixteen different versions of this method.  Is there perhaps a better version?

 

Thanks, as always.

Larry Daubenspeck
Software Programmer
Draper, Inc.
0 Likes
Message 9 of 18

augusto.goncalves
Alumni
Alumni
That's the version I use 🙂

Just replace ("Autocad.Application")
- no version number
Regards,



Augusto Goncalves
Twitter @augustomaia
Autodesk Developer Network
0 Likes
Message 10 of 18

larry.daubenspeck
Advocate
Advocate

Augusto,

 

Still no change.  I'm pulling at straws now.  Would it make a difference that this is running on Windows Server 2008 R2 Standard?

 

Thanks.

Larry Daubenspeck
Software Programmer
Draper, Inc.
0 Likes
Message 11 of 18

augusto.goncalves
Alumni
Alumni
Accepted solution
It may be a permission problem...

Win Server is not listed as supported: http://knowledge.autodesk.com/support/autocad/troubleshooting/caas/sfdcarticles/sfdcarticles/System-...

Sorry...
Regards,



Augusto Goncalves
Twitter @augustomaia
Autodesk Developer Network
0 Likes
Message 12 of 18

larry.daubenspeck
Advocate
Advocate

Augusto,

 

We moved this project to a new virtual machine, with an OS of Windows 7 Professional.  As before, the program works fine when launched manually, but it seems that AutoCad doesn't like to be launched via Windows Task Scheduler.  Can you shed any light on this?

 

Larry

Larry Daubenspeck
Software Programmer
Draper, Inc.
0 Likes
Message 13 of 18

augusto.goncalves
Alumni
Alumni
Larry

The main point is around permissions. When you launch AutoCAD (acad.exe/accoreconsole.exe) via a non-user interface, like Task Schedule, it will probably not have a user-permission on it.

If you search for "Task Schedule Impersonate" there are some reports and suggestions of workarounds, like link to an account, turn of UAC, set admin as default, etc...

http://stackoverflow.com/questions/482905/error-when-impersonating-a-user-in-net-in-a-scheduled-task

https://social.msdn.microsoft.com/forums/vstudio/en-US/4d83911f-3db9-4d78-ac2b-dc0b4403cb96/uac-and-...
Regards,



Augusto Goncalves
Twitter @augustomaia
Autodesk Developer Network
0 Likes
Message 14 of 18

larry.daubenspeck
Advocate
Advocate

Augusto,

 

Thanks, but I still am having no luck whatsoever. 

 

I have gone into Component Services, the DCOM Config folder, under AutoCAD Application, properties.  There, under the Security tab, I have added any and all users I can think of, and given them full rights.  And then under the Identity tab, I have tried "The interactive user," "The launching user," and finally "This user", and given the username and password of a user with admin rights. 

 

I have gone to C:\Program Files\Autodesk\AutoCAD 2015\ and right-clicked on acad.exe to access its properties.  Under the Security tab, I have given all the users full access.

 

Is there another "mystery user" at work when using automation such as this?

 

Nothing I have tried seems to work.  I am beginning to think AutoCAD can't be launched with Task Scheduler. 

 

 

Larry Daubenspeck
Software Programmer
Draper, Inc.
0 Likes
Message 15 of 18

augusto.goncalves
Alumni
Alumni
Indeed as it was not tested, we cannot guarantee it will work. The COM engine behind AutoCAD Automation is an old technology.

There is a Microsoft article about Office Automation (that uses a similar COM engine): https://support.microsoft.com/en-us/kb/257757
Regards,



Augusto Goncalves
Twitter @augustomaia
Autodesk Developer Network
0 Likes
Message 16 of 18

larry.daubenspeck
Advocate
Advocate

Augusto,

 

Here is something interesting, and I think it points to permissions again:

 

I scrapped the idea of using Windows Task Scheduler.  I wrote a batch file to launch my executable, and put it into a loop and timer so it fires every 30 seconds.  It works fine, but the batch file has to be continuously running.  So, I created a Windows Service to do the same thing.  However, it's back to the same old error when trying to launch AutoCAD.  AutoCAD will launch just fine under the bat file, but not under the service.

 

I know it's just a matter of finding the right combination of accounts and permissions.  I can't imagine that I am the only person in the world trying to do this sort of thing.  Surely there is an easy answer(?).  It's like I'm 99% there - I just need this one piece of the puzzle to complete this thing.

 

Any help would be appreciated.  Again, thanks for your time!

Larry Daubenspeck
Software Programmer
Draper, Inc.
0 Likes
Message 17 of 18

augusto.goncalves
Alumni
Alumni
Some time ago I wrote a small sample app that watch a specific folder. When a new .dwg file is copied/moved to that folder, the app launch AutoCAD Console and process the file, using a .scr that contains the sequence of commands.

Would that work? I'll place it on our blog and send you a link. Give me a couple hours..
Regards,



Augusto Goncalves
Twitter @augustomaia
Autodesk Developer Network
0 Likes
Message 18 of 18

larry.daubenspeck
Advocate
Advocate

Augusto,

 

My batch file is actually a modification of a sample from the Console examples.  I tried working with Console for a few days last week, but my program is fairly extensive and may be beyond the scope of Console.  I have found very little documentation on it, and converting my program from the Acad API to the Console API is daunting, given that there is little, if any, documentation.  I will say that Console looks very useful for some things - I just wish it were better documented.

 

Given that I'm so close to having it done, were it not for this permissions thing, it's all very frustrating to think of having to re-write my program, simply because AutoCad's security has tightened up over the last few versions, and now doesn't play nice with others.

Larry Daubenspeck
Software Programmer
Draper, Inc.
0 Likes