Hi I am trying to develop a COM using C# and Visual Studio 2005
First I did the interface “CSharpServer.cs”
<code>
using System;
using System.Runtime.InteropServices;
namespace CSharpServer
{
[ComVisible(true)]
[Guid("DBE0E8C4-1C61-41f3-B6A4-4E2F353D3D05")]
public interface IManagedInterface
{
int PrintHi();
}
[ComVisible(true)]
[Guid("C6659361-1625-4746-931C-36014B146679")]
public class InterfaceImplementation : IManagedInterface
{
public int PrintHi()
{
Console.WriteLine("Hello!");
return 33;
}
}
}
</code>
I compiled. The output is: csharp_server.dll. After, I did: regasm csharp_server.dll /tlb /codebase
The output is:
…
RegAsm : warning RA0000 : Registering an unsigned assembly with
/codebase can ca
use your assembly to interfere with other applications that may be
installed on
the same computer. 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 …
1ª Question: I have to sign? How?
2ª Question: Removed by the author.
3ª Question: I can only run “regasm” command prompt by visual studio. Soon if I develop a COM component using C#, and distribute to the client, it also has to have visual studio installed? If the customer has just installed Delphi, and want to use component, it will have to install visual studio?
4ª Inside Visual Studio C#, I trying to add COM reference but says:
A reference to ‘csharp_server’ could
not be added. The ActiveX type library
‘xxx\csharp_server.tlb’ was exported
from a .NET assembly and cannot be
added as a reference. Add a reference
to the .NET assembly instead.
But in “. NET reference”. I can not see the component “csharp_server.”
What am I doing wrong or forgetting?
Thanks in advance.
#1 Read up on sn.exe. Generate a strong name key file and then right-click your project in the solution explorer and edit its properties. Choose the “Signing” tab and add the key file. If your corporation has a key file you can delay-sign the assembly and then pass it to the keeper of the private key (you won’t have access to it because it is extremely locked-down because if it is public, anyone can sign app’s impersonating your company.) Don’t forget to open the “Properties” folder in solution explorer and edit the name/version/etc. of your assembly.
#2 Are you saying you can use a DLL after you’ve deleted it? That’s hard to believe.
#3 Installer packages such as Installshield have provisions for registering .NET assemblies as needed. Alternatively you can use regasm /regfile to generate a .reg that can be merged into the target registry. I really recommend either: 1) using a commercial tool to build your installer or 2) being a shining god of MSI technology, though. It is very difficult to write a correct installer with the SDK tools, and even with Wix.
#4 has no workaround as far as I know. I guess Microsoft decided it was perverse to have a managed assembly access another managed assembly through a managed wrapper assembly on top of a COM interface wrapper implemented by the 2nd managed assembly. Maybe they didn’t want it to become a common way to get around the private/global assembly binding rules.
Anyway, perhaps you could edit the type library such that it “fools” the managed wrapper generator (TlbImp.exe) into thinking it is a native COM type library, but is it really worth it? Most commercial applications with a public API have instructions for using the App’s API from COM and from managed clients, one or the other being a wrapper depending on the implementation of the Application. That technique has been working for many vendors, without issue, for years.