C # when creating an academic application instance, sometimes the instance fails to be created. For a long time, refer to the following link to sort out the code. After testing, run OK.
Ref: http://through-the-interface.typepad.com/through_the_interface/2010/02/handling-com-calls-rejected-by-autocad-from-an-external-net-application.html
Ref: http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/1dab0874-bba5-481a-9986-a11a66efa0a0
Ref: http://msdn.microsoft.com/en-us/library/ms693740 (vs.85). aspx
Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; <br/> Using Autodesk. autoCAD. interOP; <br/> using system. runtime. interopservices; <br/> using system. diagnostics; <br/> using system. threading; <br/> using Microsoft. win32; </P> <p> namespace acadutils <br/> {<br/> Public Enum acadversion <br/>{< br/> acad2009, <br/> acad2010, <br/> unknow <br/>}</P> <p> [Comimport, <br/> interfacetype (cominterfacetype. interfaceisiunknown), <br/> GUID ("00000016-0000-0000-c000-000000000046")] </P> <p> Public interface imessagefilter <br/> {<br/> [preservesig] <br/> int handleincomingcall (<br/> int dwcalltype, intptr htaskcaller, <br/> int dwtickcount, intptr lpinterfaceinfo <br/>); </P> <p> [preservesig] <br/> int retryrejectedcall (<br/> intptr htaskcallee, int dwti Ckcount, int dwrejecttype <br/>); </P> <p> [preservesig] <br/> int messagepending (<br/> intptr htaskcallee, int dwtickcount, int dwpendingtype <br/>); <br/>}</P> <p> public class acadmgr: imessagefilter <br/>{< br/> static string progid; </P> <p> imessagefilter oldfilter; <br/> acadapplication acapp = NULL; </P> <p> [dllimport ("ole32.dll")] <br/> static extern int coregistermessagefilter (<br/> imess Agefilter lpmessagefilter, <br/> out imessagefilter lplpmessagefilter <br/>); </P> <p> static acadmgr () <br/> {<br/> If (acadver = acadversion. acad2009) <br/> progid = "AutoCAD. application.17 "; <br/> else if (acadver = acadversion. acad2010) <br/> progid = "AutoCAD. application.18 "; <br/> else <br/> progid =" unknown "; <br/>}</P> <p> Public acadmgr () <br/>{< br/> coregistermessagefilter (this, out Oldfilter); </P> <p> try <br/> {<br/> acapp = (acadapplication) Marshal. getactiveobject (progid); <br/>}< br/> catch <br/>{< br/> try <br/>{< br/> type actype = type. gettypefromprogid (progid); <br/> acapp = (acadapplication) activator. createinstance (actype, true); <br/>}< br/> catch <br/>{< br/> system. windows. forms. messageBox. show ("cannot create object type" + progid); <br/>}</P> <p> I F (acapp! = NULL) <br/>{< br/> acapp. visible = true; <br/>}</P> <p> Public acadapplication <br/>{< br/> Get <br/>{< br/> return acapp; <br/>}</P> <p> Public static acadversion acadver <br/> {<br/> Get <br/> {<br/> try <br/> {<br/> registrykey acadkey = registry. currentuser. opensubkey ("software // Autodesk // AutoCAD"); <br/> string acadvernum = acadkey. getvalue ("curver "). tostring (); </P> <p> If (acadvernum = "r18.0") <br/> return acadversion. acad2010; <br/> else if (acadvernum = "r1_") <br/> return acadversion. acad2009; <br/> else <br/> return acadversion. unknow; <br/>}< br/> catch <br/>{< br/> return acadversion. unknow; <br/>}</P> <p> int imessagefilter. handleincomingcall (<br/> int dwcalltype, intptr htaskcaller, <br/> int dwtickcount, intptr lpinterfaceinfo <br/>) <br/>{< br/> return 0; <br/>}</P> <p> int imessagefilter. retryrejectedcall (<br/> intptr htaskcallee, int dwtickcount, int dwrejecttype <br/>) <br/>{< br/> return 1000; <br/>}</P> <p> int imessagefilter. messagepending (<br/> intptr htaskcallee, int dwtickcount, int dwpendingtype <br/>) <br/>{< br/> return 1; <br/>}< br/>