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 / May 2006

Tip: Looking for answers? Try searching our database.

How to create registered COM Interface (not a class) ?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dule - 27 May 2006 01:09 GMT
Hi all,

How can I create a visible COM interface? For i.e., I have my interface
IUserList : IList , and the the COM class Users : IUserList. Now I want to
have IUserList in my COM application, so I can implement it with some other
classes (my goal is just to have interface, so I don't have to have
implementation in .Net)...

a little code (again, my goal is to have IUserList visible in my MTS
application !; Thanks is advance!):

//==============================================
//MY INTERFACE
[Guid("74FA5E41-E12F-4ce7-ABEB-371ED269F4B6")]

[InterfaceType(ComInterfaceType.InterfaceIsDual)]

public interface IUserList : IList

{

[DispId(1)]

int Add(IUser user);

}

//============================================
//MY COM CLASS:
[ComVisible(true)]

[Guid("CE3B3FD1-ACD7-4f60-B13F-742CA88BCA5B")]

[ClassInterface(ClassInterfaceType.AutoDual)]

[ProgId("Users")]

public class Users : ArrayList, IUserList

{

public int Add(IUser user)

{

return base.Add(User);

}

}
Bob Eaton - 27 May 2006 02:43 GMT
Assuming that I understand what you are asking (i.e. can you create an
interface definition in .Net which can be used in unmanaged code?), then
just define the interface in a .Net assembly (c#) using the regular
"interface" syntax. E.g.

// in C# .Net managed assembly (schmaboogle.dll):
namespace schmaboogle
{
 public interface IFoo
 {
     string bar
     {
         [return : MarshalAs(UnmanagedType.BStr)]
         get;
      }
 }
}

Then to use it in your unmanaged code, use an "#import" statement to ...
well... import that definition for use:

// in your, let's say C++, unmanaged code:
#import "schmaboogle.tlb" raw_interfaces_only
using namespace schmaboogle;

// use the interface defined above
typedef CComPtr<IFoo> ptrIfoo;

Bob

> Hi all,
>
[quoted text clipped - 46 lines]
>
> }
Egbert Nierop (MVP for IIS) - 27 May 2006 11:42 GMT
> Assuming that I understand what you are asking (i.e. can you create an
> interface definition in .Net which can be used in unmanaged code?), then
> just define the interface in a .Net assembly (c#) using the regular
> "interface" syntax. E.g.

Hi!

I'm missing some attributes...

> // in C# .Net managed assembly (schmaboogle.dll):
> namespace schmaboogle
> {

[GuidAttribute ("9DD12939-DC08-4f6d-9E64-A3EC6CFE0317"),

InterfaceType(ComInterfaceType.InterfaceIsIUnknown),

TypeLibType(TypeLibTypeFlags.FOleAutomation), ComVisible(true)]
>  public interface IFoo
>  {
[quoted text clipped - 5 lines]
>  }
> }

use RegAsm.exe to create a TLB from your interface...

And the   [return : MarshalAs(UnmanagedType.BStr)] is optionally since this
is assumed by default.

> Then to use it in your unmanaged code, use an "#import" statement to ...
> well... import that definition for use:
[quoted text clipped - 59 lines]
>>
>> }
Dule - 30 May 2006 18:21 GMT
Hi Bob,

You got my question right. However, this still doesn't want to work for me
the way I want. Actually, my goal is to have interface declared in my COM
object, everything in managed code.

Imagine situation where I have IAccount interface, and then IUserList under
that IAccount interface. Then I have Account class implementing IAccount,
and Users class implementing IUserList. Now I want to have the Account COM
object, which will return the read-only property IUserList, but my goal is
to have HIDDEN Users class in COM, and still able to marshall IUserList as a
variant to the client.

In addition, I'm having all of this as DCOM. DCOM registration works fine,
but I'm not sure what should be registered on the client side. Basically, if
I setup Users class to be visible, it works fine. However, if I hide it from
COM, my DCOM is lost (DCOM client is working with Variants, with late
binding, if it helps).

Thanks a lot in advance,

Dusko
P.S. Below is the code snipet.

namespace Admin.System
{
//=========================================
// Account part
//IAccount interface
   [Guid("B9724F5C-4A75-406d-8176-D852A76203E0")]
   [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
   public interface IAccount
   {
       [DispId(1)]
       IUserList Users
       {
           get;
       }
   }

//--------------------------------------------------
   [ComVisible(true)]
   [Guid("53C74A10-6FFE-49c7-99C5-6E6D05B623C9")]
   [ClassInterface(ClassInterfaceType.None)]
   [ProgId("Admin.Account")]
   public class Account : IAccount
   {
       private IUserList users = null;

       public IUserList Users
       {
           get
           {
               if (users == null)
               {
                   users = new Users();
               }
               return users;
           }
       }
   }

//==========================================
// Users part
   [ComVisible(true)]
   [Guid("74FA5E41-E12F-4ce7-ABEB-371ED269F4B5")]
   [InterfaceType(ComInterfaceType.InterfaceIsDual)]//InterfaceIsIUnknown
// I tried both
   [TypeLibType(TypeLibTypeFlags.FOleAutomation)]
   public interface IUserList : IList
   {
       [DispId(1)]
       int Add(IUser user);
   }

//--------------------------------------------------
//    [ComVisible(true)] // I don't want to have visible this class
   [Guid("CE3B3FD1-ACD7-4f60-B13F-742CA88BCA4B")]
   [ClassInterface(ClassInterfaceType.AutoDual)]
   [TypeLibType(TypeLibTypeFlags.FControl)]
//    [ProgId("Admin.Account.Passengers")]
   public sealed class Users : ArrayList, IUserList
   {
       public int Add(IUser user)
       {
           return base.Add(user);
       }
   }
}

//======== END OF POST

----- Original Message -----
From: "Bob Eaton" <pete_dembrowski@hotmail.com>
Newsgroups: microsoft.public.dotnet.framework.interop
Sent: Friday, May 26, 2006 6:43 PM
Subject: Re: How to create registered COM Interface (not a class) ?

> Assuming that I understand what you are asking (i.e. can you create an
> interface definition in .Net which can be used in unmanaged code?), then
[quoted text clipped - 77 lines]
>>
>> }

Rate this thread:







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.