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

Need simple example of making com class with c#

4 REPLIES 4
Reply
Message 1 of 5
Anonymous
451 Views, 4 Replies

Need simple example of making com class with c#

I have looked through about 3 books and 10 articles on how to make a class in c#, and make it com callable.
They all seem a bit different, so I am getting confused.
The couple examples I have are in VB.net and I can't seem to find their equivalents in c#.

I'd just like to make a class with one property, and one method, something simple. Feel free to point me somewhere, as
I know this is one of those asking for tutoring posts a particular person hates.
thx
James Maeding
Civil Engineer and Programmer
jmaeding - at - hunsaker - dotcom
4 REPLIES 4
Message 2 of 5
Anonymous
in reply to: Anonymous

James,

Am Fri, 3 Aug 2007 23:49:21 +0000 schrieb James Maeding:

> I have looked through about 3 books and 10 articles on how to make a class in c#, and make it com callable.
> They all seem a bit different, so I am getting confused.
> The couple examples I have are in VB.net and I can't seem to find their equivalents in c#.
>

Have a look at http://www.codeproject.com/dotnet/nettocom.asp. The
information there were a great help for me.

> thx
> James Maeding
> Civil Engineer and Programmer
> jmaeding - at - hunsaker - dotcom

Bye

Herbert
Message 3 of 5
Anonymous
in reply to: Anonymous

ah, I shall study....

Herbert Putz
|>James,
|>
|>Am Fri, 3 Aug 2007 23:49:21 +0000 schrieb James Maeding:
|>
|>> I have looked through about 3 books and 10 articles on how to make a class in c#, and make it com callable.
|>> They all seem a bit different, so I am getting confused.
|>> The couple examples I have are in VB.net and I can't seem to find their equivalents in c#.
|>>
|>
|>Have a look at http://www.codeproject.com/dotnet/nettocom.asp. The
|>information there were a great help for me.
|>
|>> thx
|>> James Maeding
|>> Civil Engineer and Programmer
|>> jmaeding - at - hunsaker - dotcom
|>
|>Bye
|>
|>Herbert
James Maeding
Civil Engineer and Programmer
jmaeding - at - hunsaker - dotcom
Message 4 of 5
Mikko
in reply to: Anonymous

This might be of some help.

Here is a very very basic example of calling a netloaded
function within AutoCad from a windows application.
The DLL and EXE need to be in the same folder

Attached is the VB Class Application.

--VB.NET Class Application

Imports System.Runtime.InteropServices
Imports Autodesk.AutoCAD.Runtime

Namespace AcadVbMgd

<Guid("4BD6BCF0-80B8-47f2-969A-F015DDF9FFE9")> _
Public Interface _Example
<DispId(1)> Function msgTest() As String
End Interface

<Guid("3DC53537-D18D-42f0-9D94-9ED977921A0B"), ClassInterface(ClassInterfaceType.None)> _
Public Class Example
Inherits System.EnterpriseServices.ServicedComponent
Implements _Example

Public Function msgTest() As String Implements _Example.msgTest
Return "Works for me"
End Function

End Class

--VB.NET Windows Application

Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Interop.Common
Imports AcadVbMgd

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim aa As AcadApplication = CType(System.Runtime.InteropServices.Marshal.GetActiveObject("Autocad.Application.17.1"), AcadApplication)
Dim d As AcadVbMgd.AcadVbMgd._Example = CType(aa.GetInterfaceObject("AcadVbMgd.AcadVbMgd.Example"), AcadVbMgd.AcadVbMgd._Example)
Dim test As String = d.msgTest()
MessageBox.Show(test)
End Sub

End Class

--C# Class Application

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using Autodesk.AutoCAD.Runtime;

[assembly: CommandClass(typeof(AcadCsMgd.Demo))]

namespace AcadCsMgd
{
[Guid("78FB2976-E6F5-460b-BD94-730D7B90F286")]
public interface _Demo
{
[DispId(1)]
string msgTest();
}
[Guid("3E10E0DD-82A1-4438-BB9F-B988842C3A70"), ClassInterface(ClassInterfaceType.None)]
public class Demo : System.EnterpriseServices.ServicedComponent, _Demo
{
public string msgTest()
{
return "Looking Good....";
}
}
}

--C# Windows Application

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;
using AcadCsMgd;

namespace AcadCsMgdTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
AcadApplication aa = (AcadApplication)System.Runtime.InteropServices.Marshal.GetActiveObject("Autocad.Application.17.1");
AcadCsMgd._Demo d = (AcadCsMgd._Demo)aa.GetInterfaceObject("AcadCsMgd.Demo");
string test = d.msgTest();
MessageBox.Show(test);
}
}
}
Message 5 of 5
alexandrescoelho
in reply to: Anonymous

Mikko,

Thanks for help me. I am getting a ComException below this line

Dim aa As Autodesk.AutoCAD.Interop.AcadApplication = CType(System.Runtime.InteropServices.Marshal.GetActiveObject("Autocad.Application.17.1"), Autodesk.AutoCAD.Interop.AcadApplication)

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