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

COM Interop again

8 REPLIES 8
Reply
Message 1 of 9
Anonymous
433 Views, 8 Replies

COM Interop again

Hi all

I've created a .NET dll registered and visible for COM Interop and loads the assembly in AutoCAD.

Then "Out-of-process" I run the following code

AcadApplication AcApp = (AcadApplication)System.Runtime.InteropServices.Marshal.GetActiveObject("Autocad.Application");
iDataTransfer DataTransfer = (iDataTransfer)AcApp.GetInterfaceObject("NOVAcadCOM.DataTransfer");
DataTransfer.DoDataTransfer();

During GETINTERFACEOBJECT the constructor for DataTransfer class gets called and here any code works. I can writemesseges to commandline or append objects to drawing database etc.

During the function call DODATATRANSFER any interaction with AutoCAD throws an exeption.

System.AccessViolationException was unhandled
Message="Attempted to read or write protected memory. This is often an indication that other memory is corrupt."


Any ideas
Tore
8 REPLIES 8
Message 2 of 9
Anonymous
in reply to: Anonymous

We can't guess what your ActiveX server does.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

wrote in message news:5661201@discussion.autodesk.com...
Hi all

I've created a .NET dll registered and visible for COM Interop and loads the assembly in AutoCAD.

Then "Out-of-process" I run the following code

AcadApplication AcApp = (AcadApplication)System.Runtime.InteropServices.Marshal.GetActiveObject("Autocad.Application");
iDataTransfer DataTransfer = (iDataTransfer)AcApp.GetInterfaceObject("NOVAcadCOM.DataTransfer");
DataTransfer.DoDataTransfer();

During GETINTERFACEOBJECT the constructor for DataTransfer class gets called and
here any code works. I can writemesseges to commandline or append objects to drawing database etc.

During the function call DODATATRANSFER any interaction with AutoCAD throws an exeption.

System.AccessViolationException was unhandled
Message="Attempted to read or write protected memory. This is often an indication that other memory is corrupt."


Any ideas
Tore
Message 3 of 9
Anonymous
in reply to: Anonymous

See attached file "Class1.txt".


Tore
Message 4 of 9
Anonymous
in reply to: Anonymous

To further clarify.

"COM Visible" and "Register for COM Interop" is checked in the "In-Proc" assembly.

In the "Out-of-proc" assembly I reference the "In-Proc" DLL.


Cheers
Tore
Message 5 of 9
jbooth
in reply to: Anonymous

From the looks of it, you are trying to use the managed .NET wrappers out of process (which to my knowledge is impossible).

Since you are out of process you are restricted to COM interop, and should stick with the Autodesk.Autocad.Interop and Autodesk.Autocad.Interop.Common namespaces in your application.


The only way that I know of to use the managed API via an out of process COM is to send a command string to Autocad using the AcadApplication.ActiveDocument.SendCommand() method. You simply write an entry point to your application as a command in a .NET dll, and get the dll demand loaded by AutoCAD during application startup.
Message 6 of 9
Anonymous
in reply to: Anonymous

Thanks

But the .NET dll exposing the interface is loaded in-proc by AutoCAD. In the class constructor I have tested to start a transaction, create a new line and added it to drawing database. All in the Autodesk.AutoCAD.ApplicationServices namespace. This works within the constructor but the same code fails in one of the interface exposed methods.

My knowledge about COM is very limited, but could it be something like this. During the AcApp.GetInterfaceObject() the interface gets instanceiated "In-proc" and then passed to the "Out-of-proc". Later any methods not using the COM Interop namespace fails since it is "Out-of-proc"


Any ides
Tore
Message 7 of 9
jbooth
in reply to: Anonymous

Makes sense to me, but I would recommend that you refrain from hacking the constructor, and write a true COM wrapper for your application. I am sure any behaviour you would get doing otherwise is completely unintended.

The managed wrappers were designed for use inside the AutoCAD process, and trying to call them out of process is just asking for trouble.

In my opinion, anyways... 🙂
Message 8 of 9
Anonymous
in reply to: Anonymous

In your code:

// Exeption is thrown before this point.
documentLock.Dispose();

What exactly do you mean by 'before this point' ?

Does the exception occur when you lock the document?

If there is code that executes in place of the comment
above, before the call to dispose the document lock,
then why haven't you bothered to show that?

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

wrote in message news:5662743@discussion.autodesk.com...
Thanks

But the .NET dll exposing the interface is loaded in-proc by AutoCAD. In the class constructor I have tested to start a transaction, create a new line and added it to drawing database. All in the Autodesk.AutoCAD.ApplicationServices namespace. This works within the constructor but the same code fails in one of the interface exposed methods.

My knowledge about COM is very limited, but could it be something like this. During the AcApp.GetInterfaceObject() the interface gets instanceiated "In-proc" and then passed to the "Out-of-proc". Later any methods not using the COM Interop namespace fails since it is "Out-of-proc"


Any ides
Tore
Message 9 of 9
Anonymous
in reply to: Anonymous

Got this answer from Autodesk DevTech :


The problem is that your COM object is not in the STA. So you are trying to access AutoCAD from a thread other than its main thread. This won't work. i.e. this is a user error.

You have 2 ways to solve this problem:

1. Derive your COM object from System.EnterpriseServices.ServicedComponent

2. Or transition into the main thread "manually" in each method. Something like this:
control.Invoke(realMethod);





I tried solution 1 and it works PERFECT. This mean I can write methods using the .NET wrappers. Make sure they are loaded in ACAD and COM exposed. In the end order them from "Out-of-proc". The BIG advantage is of course speed "In-proc" makes the speed of light seem slow compared to similar "Out-of-proc" code.


Cheers
Tore

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