Write COM component in C# 2.0

  • Thread starter Thread starter bz
  • Start date Start date
B

bz

Hi,

I need to write a COM component in C# (VS 2005)
I created the C# class library project, marked it "Make Assemble COM
visible"
I set the assembly to be signed with a key file.
The class contains a single method, as below:

namespace MyPlugin
{
public class MyClass
{
public bool Process(object session)
{
bool retVal = false;
try
{
// some processing here
retVal = true;
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex.Message);
}
return retVal;
}
}
}

I built the project and registered it with

c:\>regasm /tlb:mylib.tlb mylib.dll
Microsoft (R) .NET Framework Assembly Registration Utility
2.0.50727.1433
Copyright (C) Microsoft Corporation 1998-2004. All rights reserved.

Types registered successfully
Assembly exported to (...)MyLib.tlb', and the type library was
registered success
fully

c:\>gacutil /i mylib.dll
Microsoft (R) .NET Global Assembly Cache Utility. Version
2.0.50727.42
Copyright (c) Microsoft Corporation. All rights reserved.

Assembly successfully added to the cache

The message seems ok.

Still, in object browsert in VB6 or Access or other COM-enabled, I can
browse my class (by browsing the TLB) and I see only three methods
Equals, GetHashCode and GetType and a property ToString, but I cannot
see my method.

What am I doing wrong?

Thanks
 
I think this looks ok on a very quick glance. You'll need to add a default
constructor though and I don't see that in there. Another thing is to check
the assembly.cs file to see if comvisibility is actually set to true, the
IDE can be flaky and not update that flag. Or you can do this above your
class declaration [ComVisible(true)]

--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The O.W.C. Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $19.99
 
Thanks for answer
Actually I made it work in the end, (I had to restart IDE in between)
It might be something related with IDE

Regards

I think this looks ok on a very quick glance. You'll need to add a default
constructor though and I don't see that in there. Another thing is to check
the assembly.cs file to see if comvisibility is actually set to true, the
IDE can be flaky and not update that flag. Or you can do this above your
class declaration [ComVisible(true)]

--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The O.W.C. Black Book, 2nd Edition
Exclusively onwww.lulu.com/owc$19.99
-------------------------------------------------------




I need to write a COM component in C# (VS 2005)
I created the C# class library project, marked it "Make Assemble COM
visible"
I set the assembly to be signed with a key file.
The class contains a single method, as below:
namespace MyPlugin
{
   public class MyClass
   {
       public bool Process(object session)
       {
           bool retVal = false;
           try
           {
               // some processing here
               retVal = true;
           }
           catch (Exception ex)
           {
               MessageBox.Show("Error " + ex.Message);
           }
           return retVal;
       }
   }
}
I built the project and registered it with
c:\>regasm /tlb:mylib.tlb mylib.dll
Microsoft (R) .NET Framework Assembly Registration Utility
2.0.50727.1433
Copyright (C) Microsoft Corporation 1998-2004.  All rights reserved.
Types registered successfully
Assembly exported to (...)MyLib.tlb', and the type library was
registered success
fully
c:\>gacutil /i mylib.dll
Microsoft (R) .NET Global Assembly Cache Utility.  Version
2.0.50727.42
Copyright (c) Microsoft Corporation.  All rights reserved.
Assembly successfully added to the cache
The message seems ok.
Still, in object browsert in VB6 or Access or other COM-enabled, I can
browse my class (by browsing the TLB) and I see only three methods
Equals, GetHashCode and GetType and a property ToString, but I cannot
see my method.
What am I doing wrong?
Thanks- Ascunde citatul -

- Afiºare text în citat -
 
Back
Top