Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / February 2008

Tip: Looking for answers? Try searching our database.

Write COM component in C# 2.0

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
bz - 18 Feb 2008 21:22 GMT
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
Suhredayan Panikkal - 21 Feb 2008 08:06 GMT
You need to give a GUID for .net framework class to behave like COM object.
use the GuidAttribute to associate a GUID, this is in
System.Runtime.InteropServices namespace.

The code should look like:

namespace MyPlugin
{
   [Guid("690AB25B-4E76-4e5e-9DF3-39D5E7A08242")]
   public interface IMyClass
   {
       bool Process(object session);
   }

   [Guid("274E67E5-6B00-4624-A721-782049101F91")]
   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;
       }
   }
}

-Suhredayan

> Hi,
>
[quoted text clipped - 54 lines]
>
> Thanks
PLS - 26 Feb 2008 04:26 GMT
You need to mark you method ComVisible(true) also.

   ++PLS

In article <81f8d71a-f4cd-43e0-a950-eee046ef55d4
@s12g2000prg.googlegroups.com>, bzamfir@gmail.com says...
> Hi,
>
[quoted text clipped - 45 lines]
>
> The message seems ok.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.