.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Unable to create instance of AutoCAD 2014 using Windows task Scheduler

9 REPLIES 9
SOLVED
Reply
Message 1 of 10
wrenchinternal
1912 Views, 9 Replies

Unable to create instance of AutoCAD 2014 using Windows task Scheduler

Hi,

 

  I have a windows application to convert AutoCAD drawing files to pdf. Inorder to do that i create an instance of Autocad in application like shown below

     

     oClassType = Type.GetTypeFromProgID("AutoCAD.Application.19");
     moAcadApp = Activator.CreateInstance(oClassType);

 

I Schedule this application to run periodicaly using Windows Task scheduler. But when i do that  i get an error like shown below(while creating an instance)

   

    Retrieving the COM class factory for component with CLSID {BD0DEB94-63DB-4392-9420-6EEE05094B1F} failed due to the following error:    80080005        Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)).

 

But if i run the Application manually I am able to create the instance successfully. A liitle help from the community would be appreciated.

Thanks in advance

 

OS: Windows 7

FrameWork: .netframework4

Visual Studio: VS 2010

Language: C#

 

Regards,

Manosh Jacob

9 REPLIES 9
Message 2 of 10

Well sometime i get this error, when i run the application manually.

Message 3 of 10

The correct ProgID string for AutoCAD2014 is "AutoCAD.Application.19.1", if you want to start a version-specific AutoCAD. Or you can simply use "AutoCAD.Application" as generic AutoCAD ProgID, in this case, the version of AutoCAD of last running AutoCAD instance under the same loggin user profile will be started, if the computer has multiple AutoCAD versions installed.

 

There could be issue if your app runs as scheduled task without a user logs in: AutoCAD may not start start correctly without user logs in. Running full AutoCAD whitout user attending is a problematic practice, at least, is not desired.

Norman Yuan

Drive CAD With Code

EESignature

Message 4 of 10

Thank you very much norman for the quick response.

 

As you said i have changed the ProgId to "AutoCAD.Application" as I am using multiple instances of Autocad (13,14). This information is very helpful and now i am using this in my code.

 

I am running my task under a user profile and the same has got "Admin" privilages. But there is something i want to point out here.

 

   A. That is if i run my application as "Run as Admin" by right clicking the exe, the same exception is thrown. The logged in user is same as the user profile             which i have given in my task.Same exception is thrown if i try to run my application through task scheduler.

 

   B. But if i run by application without using "Run as Admin", I am able to create the instance of the AutoCAD and no exception is thrown.

 

From this we can conclude that there is some security issues? but i dont knw what that is. The user is an admin user and i have given full permission to the user by right clicking the acad.exe.

 

Any idea?

 

 

 

 

Message 5 of 10
hgasty1001
in reply to: wrenchinternal

Hi,

 

I'm not sure what the problem is, but maybe you should consider using accoreconsole as it was designed for that kind of batch processing, a starter here:accoreconsole

 

Gaston Nunez

Message 6 of 10
wrenchinternal
in reply to: hgasty1001

Thanks gasty. But changing the implementaion is a big hurdle for me. Is there any other way?

I am using a 64 bit sysyem.

Message 7 of 10
hgasty1001
in reply to: wrenchinternal

Hi,

 

Are you aware that interop it's platform (32 or 64 bit) sensitive?, maybe you have the wrong references, you should reference interop dll from the 64 bit include folder.

 

Gaston Nunez

 

 

Message 8 of 10
wrenchinternal
in reply to: hgasty1001

Hai,

 

  I am not using interop dll. I use reflection to create the instance.

Message 9 of 10
bjhuffine
in reply to: wrenchinternal

I don't know about AutoCAD 2014 or if this may be addressing your specific issue with starting it up, so this may be all I can do to help, but who knows! 🙂

 

I had an issue using this approach with 2015 and found out the issue was related to the fact that Microsoft has chosen not to support nested message loops in WPF.... Kean Wamsley wrote about it here

 

Below is some sample code for startup which may or may not address your particular issue.  Note though that what gasty was getting at is that you don't have to have the COM Interop references if you don't plan to interact with the session, but be aware that without them, AutoCAD will start in the background and you can't make it visible without them.  See my code sample below... Hope this helps...

 

btw... equivalent 2012 Interop references (COM tab of VS) is "AutoCAD 2012 Type Library" and "AcObjClassImp 1.0 Type Library".  I'm not sure how this varies on 2014 if there's been changes, but something to look for...

 

 /*
    For more information on IMessageFilter:  http://msdn.microsoft.com/en-us/library/ms693740(VS.85).aspx
    This is used to handle a “problem” that was introduced as Autodesk 
    addressed an issue with the WPF components in AutoCAD handle inbound messages, 
    largely due to Microsoft’s decision not to support nested message loops in WPF.
    If WPF is in the middle of performing some kind of layout processing operation 
    (which leads to Dispatcher.DisableProcessing() being called) and there’s an incoming COM call, 
    then AutoCAD was respecting it which could lead to a crash. 
    Now they do the right thing and reject the COM call: effectively asking the caller to try again later.
    The problem is that – while the VB6 runtime was very good at automatically retrying calls such as CreateObject() – WinForm applications are not. 
    We need to implement an additional interface from our WinForm application to make sure it can handle failure
     */

    [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 class StartAutoCAD : IMessageFilter
    {

        #region P/Invoke Declarations and Delegates

        //IMessageFilter support, see the IMessageFilter interface file for more information
        //Note:  The IMessage helps support opening AutoCAD externally while supporting conflict issues
        [DllImport("ole32.dll")]
        static extern int CoRegisterMessageFilter(IMessageFilter lpMessageFilter, out IMessageFilter lplpMessageFilter);

        #endregion

        #region Constructors

        public StartAutoCAD()
        {
            //IMessageFilter support
            IMessageFilter oldFilter;
            CoRegisterMessageFilter(this, out oldFilter);
        }

        #endregion

        #region Starting new instance

        public void StartNew()
        {

            //Trying to get a currently open instance first
            object objectACADApplication=null;

            try
            {
                objectACADApplication = Marshal.GetActiveObject("AutoCAD.Application.18");
            }
            catch (COMException)
            {

                //If not found, try getting with reflection
                if (objectACADApplication == null)
                {
                    Type acadSessionType = Type.GetTypeFromProgID("AutoCAD.Application.18");
                    objectACADApplication = Activator.CreateInstance(acadSessionType, true);

                    //I give a little time for it to settle out, probably not necessary though
                    Thread.Sleep(125);
                }
            }

            //Reference the AutoCAD Interop dll's so that you can cast acadApplication as AcadApplication 
            //if you want to communicate with the instance directly with COM...  Shouldn't be necessary otherwise

            //Make visible... sorry, but to my knowledge this does require the interop dlls otherwise AutoCAD 
            //remains in the background
            if (objectACADApplication != null)
            {
                AcadApplication acadApplication = (AcadApplication)objectACADApplication;
                acadApplication.Visible = true;
            }


        }

        #endregion


        #region IMessageFilter Members

        //For more information, see comments in the IMessageFilter file.

        int IMessageFilter.HandleInComingCall(int dwCallType, IntPtr hTaskCaller, int dwTickCount, IntPtr lpInterfaceInfo)
        {
            return 0; // SERVERCALL_ISHANDLED
        }

        int IMessageFilter.RetryRejectedCall(IntPtr hTaskCallee, int dwTickCount, int dwRejectType)
        {
            /* return values:
             * -1: the call should be cancelled.  COM then returns RPC_E_CALL_REJECTED from the original method call
             * value >= 0 and < 100: the call is to be retried immediately
             * value >= 100:  COM will wait for this many milliseconds and then retry the call
             */

            return 1000; // Retry in a second
        }

        int IMessageFilter.MessagePending(IntPtr hTaskCallee, int dwTickCount, int dwPendingType)
        {
            return 1; // PENDINGMSG_WAITNOPROCESS
        }

        #endregion
    }

 

 

 

Message 10 of 10
wrenchinternal
in reply to: bjhuffine

Hello guys

  

  Now i am using the version specific program id to cerate instances of autocad. That kind of solved my problem . Sorry for the late reply.

 

Thank you very much for your help. 

 

Regards,

Manosh Jacob

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost