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 / June 2005

Tip: Looking for answers? Try searching our database.

Using .NET Component from COM Object

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mike McIntyre [MVP] - 24 May 2005 21:46 GMT
Is it at all possible to use a .NET component from a COM object without
strongly naming the .NET component?
Frederik Mangelsdorf - 26 May 2005 17:21 GMT
> Is it at all possible to use a .NET component from a COM object without
> strongly naming the .NET component?

Mike, I don't know of a way to do this.  As far as I'm aware you must
register the .NET class for COM use (ProgID, CLSID, etc.).  This is usually
done either by the installer or by regasm.

The help on regasm states this:

/codebase Creates a Codebase entry in the registry. The Codebase entry
specifies the file path for an assembly that is not installed in the global
assembly cache. You should not specify this option if you will subsequently
install the assembly that you are registering into the global assembly cache.
The assemblyFile argument that you specify with the /codebase option must be
a strong-named assembly.

Essentially this means that you must either have the assembly in the GAC,
which requires it to be strong named -OR- it must be strong named to use the
/codebase switch on regasm.

So in all possible cases (with regasm) the assembly must be strong named.

Hope this helps
Frederik Mangelsdorf - 27 May 2005 09:57 GMT
Mike,

I've just ran a test with another project of mine (CWW, strong named) and
bingo !

Yes it *IS* possible to register and use a .NET assembly as a COM component
even if it isn't strong named.  In spite of the documentation and my previous
reply to your post it does work.

I've replaced the attribute that signs my assembly like this:
/*[assembly: AssemblyKeyFile(@"..\..\PrivateKey.snk")]*/
[assembly: AssemblyKeyFile("")]

(but I've left the explicit version number in AssemblyInfo.cs - although I
don't expect that this will break the ability to register and use from COM -
you will however end up with a lot of registry entries for every build of the
.NET assembly that you make)

I've then first uninstalled/unregistered my old, still strong named,
assembly.  After rebuilding the assembly it isn't strong named anymore and
I've changed my install script like this:

rem gacutil /i protocol.dll
rem regasm protocol.dll
regasm /codebase protocol.dll

When I run it regasm does make some noise (warnings), I get:

Microsoft (R) .NET Framework Assembly Registration Utility 1.1.4322.573
Copyright (C) Microsoft Corporation 1998-2002.  All rights reserved.

RegAsm warning: Registering an unsigned assembly with /codebase can cause
your a
ssembly to interfere with other applications that may be installed on the
same c
omputer. The /codebase switch is intended to be used only with signed
assemblies
. Please give your assembly a strong name and re-register it.
Types registered successfully

And after all of this the registry entries just look fine and it does indeed
work identical to the strong named assembly.

So essentially you're expected, because of the old DLL hell, to alway strong
name your assemblies if you're going to do COM.  But you can still stay
unsafe if you want and not use strong names for CCW .NET assemblies.

As a note to the internal workings, I've observed that the registry entry
for my CCW .NET assembly got a string value Assembly which looks like this:

MyAssembly, Version=0.1.0.1, Culture=neutral, PublicKeyToken=null

Please note the end of the value specifying null for the public key token,
that's because the assembly isn't strong named anymore.
Mike McIntyre [MVP] - 27 May 2005 14:20 GMT
Fredrick,

Thank you for such a thorough response in this and your previous emails.

What you have found and done corresponds to the research and testing I have
done. (which helps!)

The reason I asked this question is because of an issue I am trying to
resolve.  The scenario is:

A .NET component has been created to be used by a C++ ATL COM service
application.

Using VS.NET to create the library on the development computer (Windows XP),
the .NET component works - including raising events that the C++ COM service
does receive.

The Issue

The C++ COM application and the .NET component are deployed to another
computer (a Windows 2000) computer.

The .NET component is not strongly typed.  It is installed using regasm.

On the Windows 2000 computer everything BUT THE EVENTS works between the C++
COM service and the .NET component.  There are no erros but events from the
.NET component are not received by the C++ COM service - as nearly as I can
tell.

This lead me to think that a not strongly named .NET component installed
using regasm with the \codebase option might not be able to receive events
correctly.

So I havn't resolve the issue but I am a little saner thanks to the
confirmations you provided.

Thanks!

Signature

Mike

Mike McIntyre
Visual Basic MVP
www.getdotnetcode.com

> Mike,
>
[quoted text clipped - 57 lines]
> Please note the end of the value specifying null for the public key token,
> that's because the assembly isn't strong named anymore.
Frederik Mangelsdorf - 27 May 2005 14:52 GMT
Mike,

There are a number of possible explanations for the symptoms just described:

1. The events are sourced into the C++ ATL COM service.  This requires at a
minimum a registration of the outgoing (as seen from the .NET assembly)
interface that is used to sink the events into.  If the IID isn't identical
on both sides then you'll never receive events because the interfaces won't
be recognised as being the same.  You can define (and register) this outgoing
interface from both sides, but usually you'll want just one side to define &
register it.  If that side is .NET, make sure that you're using attributes to
assign it manuall an IID.  If you're defining and registering the event
interface from the ATL side, then you must make sure that that components
type library gets registered as well.  This is important for any marshalling
that may have to be done (even if everything takes place in-process, you may
still be crossing apartment boundaries and thus need marshalling).  Your XP
machine will have this registration because it's done automatically by VS as
you build the application.  Your W2K machine will certainly not have any
registration of the interface used.  If you've copied the interface
definition so that both sides hold a copy of the source code that defines the
interface (I do this sometimes and as the interface is immutable, you
actually can do this), then you should make sure that both sides will use the
same IID (as explained above).

2. If all of the registration checks fail then I remember having had some
(many) problems in using IConnectionPointContainer and IConnectionPoint.  
Since this code will be supplied for you by the .NET implementation of your
component it is effective hidden code, which is always very hard to debug.  
You can check the ATL side for the use of the IConnectionPoint* interface and
check to see if the registration is successfull.

3. Finally you could have troubles delivering the events.  Again, like point
2 above, this is hidden code produced by the .NET implementation of your
component.  You'd be specifically looking at the delegate that is to deliver
the event.

Have fun!

> Fredrick,
>
[quoted text clipped - 95 lines]
> > Please note the end of the value specifying null for the public key token,
> > that's because the assembly isn't strong named anymore.
Mike McIntyre [MVP] - 03 Jun 2005 06:10 GMT
Frederik,

I am beginning to see what I have been doing wrong on the Win2000 side.  I
am off to have some 'fun' learning and trying the new things you have
introduced to me.

Thank you very much,

Mike

> Mike,
>
[quoted text clipped - 165 lines]
>> > token,
>> > that's because the assembly isn't strong named anymore.

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.