• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    Active Contributor
    Posts: 48
    Registered: ‎11-03-2003

    using COM API in .net dll

    640 Views, 9 Replies
    05-23-2005 07:18 PM
    Hi,

    I am trying to use the COM API in my .net dll (acad 2005) with C#
    I can get the code to work in VB.net but not C#


    to get the hosting acad application

    'in vba
    Public WithEvents m_Acad As AcadApplication
    Set m_Acad = AcadApplication

    'in vb.net dll
    Dim _AcadApp As Autodesk.AutoCAD.Interop.AcadApplication = Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication

    in c#.net dll
    //Yet converting the vb.net code to C#
    //Build gives error CS0029 cannot convert object to Autodesk.AutoCAD.Interop.AcadApplication
    private Autodesk.AutoCAD.Interop.AcadApplication _AcadApp = Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication;


    I assume the vb.net code is converting the '.net acad object' to the 'com acad object'
    I want to avoid using GetActiveObject("AutoCAD.Application.16")

    Thanks
    Mark
    Please use plain text.
    *Mike Tuersley

    Re: using COM API in .net dll

    05-23-2005 09:16 PM in reply to: markgardiner
    I'm sure there are lots of opinions and methods, but you should use the
    specific application ID which for 2005 is "16.1". Also learn to use the
    imports/using statements so you have less typing ;-)

    VB.NET
    Imports System.Windows.Forms
    Imports System.Runtime.InteropServices
    Imports Autodesk.AutoCAD.Interop
    Imports Autodesk.AutoCAD.Interop.Common

    Public Function Connect2Acad() As Boolean
    '+-- Retrieve or establish reference to AutoCAD [via COM]
    ' test to see if reference is already established
    If IsNothing(cadApp) Then
    ' no so try accessing existing session
    Try
    cadApp = Marshal.GetActiveObject("AutoCAD.Application.16.2")
    Catch
    ' existing session not found...fire up Acad
    Try
    cadApp = CreateObject("AutoCAD.Application.16.2")
    Catch AcadEx As Autodesk.AutoCAD.Runtime.Exception
    Dim sMsg As String = AcadEx.Message & " from: " & AcadEx.Source
    MessageBox.Show(sMsg, "AutoCAD_BasicCalls|Connect2Acad",
    MessageBoxButtons.OK, MessageBoxIcon.Error)
    Return False
    End Try
    End Try
    End If
    ' made it here so it worked
    Return True
    End Function

    C#
    using System;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    using Autodesk.AutoCAD.Interop;
    using Autodesk.AutoCAD.Interop.Common;

    class HitAcad_example
    {
    public AcadApplication cadApp;
    public AcadDocument cadDoc;

    public bool Connect2Acad()
    {
    try {
    cadApp =(Autodesk.AutoCAD.Interop.AcadApplication)
    Marshal.GetActiveObject("AutoCAD.Application.16.1");
    } catch {
    try {
    cadApp =new Autodesk.AutoCAD.Interop.AcadApplication();
    cadApp.Visible = true;
    } catch (System.Exception AcadEx) {
    string sMsg = AcadEx.Message + " from: " + AcadEx.Source;
    MessageBox.Show(sMsg, "AutoCAD_BasicCalls|Connect2Acad",
    MessageBoxButtons.OK, MessageBoxIcon.Error);
    return false;
    }
    }
    return true;
    }
    }

    -- Mike
    ___________________________
    Mike Tuersley
    ___________________________
    the trick is to realize that there is no spoon...
    Please use plain text.
    Active Contributor
    Posts: 48
    Registered: ‎11-03-2003

    Re: using COM API in .net dll

    05-23-2005 10:08 PM in reply to: markgardiner
    ks Mike

    I do use 'Imports' or 'using' but sometimes you need the fully qualified name to prevent ambiguity (ie application is in more than one of your imports)


    //c# I was missing the (AcadApplication)
    Autodesk.AutoCAD.Interop.AcadApplication _AcadApp = (AcadApplication)Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication;

    I didn't want to use GetActiveObject is this can return any running instance of acad and not necessarly the one my .net dll is loaded into.


    Mark
    Please use plain text.
    *Mike Tuersley

    Re: using COM API in .net dll

    05-24-2005 08:19 AM in reply to: markgardiner
    > I do use 'Imports' or 'using' but sometimes you need the fully qualified name to prevent ambiguity (ie application is in more than one of your imports)
    Haven't had that problem

    > I didn't want to use GetActiveObject is this can return any running instance of acad and not necessarly the one my .net dll is loaded into.
    Well you can't create a new instance from a dll already running in AutoCAD!
    Sorry I misread that portion of your post. You either need to use
    GetActiveObject or a bit of managed code to get the host something like:

    Imports AcadApp = Autodesk.AutoCAD.ApplicationServices.Application

    Public Shared m_CoAcadApp As Autodesk.AutoCAD.Interop.AcadApplication =
    CType(AcadApp.AcadApplication, AcadApplication)


    -- Mike
    ___________________________
    Mike Tuersley
    ___________________________
    the trick is to realize that there is no spoon...
    Please use plain text.
    *Paul Richardson

    Re: using COM API in .net dll

    05-24-2005 08:26 AM in reply to: markgardiner
    >I do use 'Imports' or 'using' but sometimes you need the fully qualified
    >name to prevent >ambiguity (ie application is in more than one of your
    >imports)

    Isn't avoiding this the idea behind Namespaces, while enjoying short
    typing with using directives? Mike help??

    wrote in message news:4854118@discussion.autodesk.com...
    ks Mike

    I do use 'Imports' or 'using' but sometimes you need the fully qualified
    name to prevent ambiguity (ie application is in more than one of your
    imports)


    //c# I was missing the (AcadApplication)
    Autodesk.AutoCAD.Interop.AcadApplication _AcadApp =
    (AcadApplication)Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication;

    I didn't want to use GetActiveObject is this can return any running instance
    of acad and not necessarly the one my .net dll is loaded into.


    Mark
    Please use plain text.
    *Mike Tuersley

    Re: using COM API in .net dll

    05-24-2005 08:54 AM in reply to: markgardiner
    > Isn't avoiding this the idea behind Namespaces, while enjoying short
    > typing with using directives? Mike help??

    Yes, and actually depending upon how you write your code there are a few
    places where you still need to type it all out but I've only ever run into
    it with the managed API [Graphics], never with COM.Try this to see it:

    Imports Autodesk.AutoCAD.DatabaseServices
    Imports Autodesk.AutoCAD.GraphicsInterface

    Then try declaring: Dim oVport As New Viewport

    You'll get the ambiguitity error. In my case here, I really wasn't using
    GraphicsInterface so I removed it =)

    -- Mike
    ___________________________
    Mike Tuersley
    ___________________________
    the trick is to realize that there is no spoon...
    Please use plain text.
    *Paul Richardson

    Re: using COM API in .net dll

    05-24-2005 09:28 AM in reply to: markgardiner
    Thanks..
    "Mike Tuersley" wrote in message
    news:4854721@discussion.autodesk.com...
    > Isn't avoiding this the idea behind Namespaces, while enjoying short
    > typing with using directives? Mike help??

    Yes, and actually depending upon how you write your code there are a few
    places where you still need to type it all out but I've only ever run into
    it with the managed API [Graphics], never with COM.Try this to see it:

    Imports Autodesk.AutoCAD.DatabaseServices
    Imports Autodesk.AutoCAD.GraphicsInterface

    Then try declaring: Dim oVport As New Viewport

    You'll get the ambiguitity error. In my case here, I really wasn't using
    GraphicsInterface so I removed it =)

    -- Mike
    ___________________________
    Mike Tuersley
    ___________________________
    the trick is to realize that there is no spoon...
    Please use plain text.
    New Member
    Posts: 2
    Registered: ‎12-07-2009

    Re: using COM API in .net dll

    12-07-2009 06:31 AM in reply to: markgardiner
    Hi,
    I have installed Architecture 2009 standalone version on my local machine. I am calling autocad document from Windows C# and getting the following error. Can you help me to fix this error?

    "Retrieving the COM class factory for component with CLSID {28B7AA99-C0F9-4C47-995E-8A8D729603A1} failed due to the following error: 80080005."

    My Code is:

    try
    {
    acadapp = (AcadApplication)Marshal.GetActiveObject("AutoCAD.Application");
    }
    catch
    {
    try
    {
    Type acType = Type.GetTypeFromProgID("AutoCAD.Application");
    acadapp = (AcadApplication)Activator.CreateInstance(acType, true);
    }
    catch (System.Exception ex)
    {

    };

    }


    If I use the following code then application is throwing an error as " it is not able to find out form(class) name"

    acadapp = (Autodesk.AutoCAD.Interop.AcadApplication)Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication;
    Please use plain text.
    *Tony Tanzillo

    Re: using COM API in .net dll

    12-07-2009 10:27 AM in reply to: markgardiner
    If the error happens when AutoCAD is running, then try
    "AutoCAD.Application.17.2" rather than just "AutoCAD.Application".

    To create a new instance of AutoCAD you can just use:

    AcadApplication app = new AcadApplication();

    --
    http://www.caddzone.com

    AcadXTabs: MDI Document Tabs for AutoCAD
    Supporting AutoCAD 2000 through 2010

    http://www.acadxtabs.com

    Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");

    wrote in message news:6300407@discussion.autodesk.com...
    Hi,
    I have installed Architecture 2009 standalone version on my local machine. I
    am calling autocad document from Windows C# and getting the following error.
    Can you help me to fix this error?

    "Retrieving the COM class factory for component with CLSID
    {28B7AA99-C0F9-4C47-995E-8A8D729603A1} failed due to the following error:
    80080005."

    My Code is:

    try
    {
    acadapp =
    (AcadApplication)Marshal.GetActiveObject("AutoCAD.Application");
    }
    catch
    {
    try
    {
    Type acType =
    Type.GetTypeFromProgID("AutoCAD.Application");
    acadapp =
    (AcadApplication)Activator.CreateInstance(acType, true);
    }
    catch (System.Exception ex)
    {

    };

    }


    If I use the following code then application is throwing an error as " it is
    not able to find out form(class) name"

    acadapp =
    (Autodesk.AutoCAD.Interop.AcadApplication)Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication;
    Please use plain text.
    New Member
    Posts: 2
    Registered: ‎12-07-2009

    Re: using COM API in .net dll

    12-08-2009 02:10 AM in reply to: markgardiner
    Thanks Tony. I opened AutoCAD 2009 through program menu and tried with the following code. It is working. My concern is like why should I run AutoCAD s/w first and run this program. This is the case through my C# program I need to kickoff autocad software and use the following code to create the instance for ACAD document. How Can I kickoff AutoCAD s/w?

    AndI have some autocad document(linked with oracle db(uid/pwd). Through C#, I need to pass UID/PWD to open the Autocad document. Is there any way we can do that?
    try
    {
    acadapp =
    (AcadApplication)Marshal.GetActiveObject("AutoCAD.Application");
    }
    catch
    {
    try
    {
    Type acType =
    Type.GetTypeFromProgID("AutoCAD.Application");
    acadapp =
    (AcadApplication)Activator.CreateInstance(acType, true);
    }
    catch (System.Exception ex)
    {

    };

    }

    How can I pass user id / password to autocad document. I have seen the following code accepts only pwd:
    acadapp.Documents.Open(dtFileList.Rows[intRowCount]["FileName"].ToString(), false, pwd);
    Here If I pass pwd as string then autocad document should not prompt to enter UID/PWD. Is it possible?

    Thanks and Regards,
    Palani. Edited by: palaniselvam on Dec 8, 2009 10:17 AM
    Please use plain text.