如何使 C# COM DLL 等同于 ATL COM C++
您好,我正在尝试使用 C# 和 Visual Studio 2005 开发 COM
。首先,我编译了接口“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>
。输出是:csharp_server.dll。之后,我做了:regasm csharp_server.dll /tlb /codebase
输出是:
... RegAsm:警告 RA0000:注册未签名的程序集 /代码库可以 使用您的程序集干扰其他可能的应用程序 安装在 同一台计算机。 /codebase 开关仅供使用 有签名的 组件。请为您的程序集指定一个强名称并重新注册 它。 类型注册成功...
1ª 问:我必须签字吗?如何?
2ª 问题:已被作者删除。
3ª 问题:我只能通过 Visual Studio 运行“regasm”命令提示符。很快如果我用C#开发一个COM组件,并分发给客户端,它也必须安装Visual Studio吗?如果客户刚刚安装了Delphi,想要使用组件,就必须安装Visual Studio吗?
4ª 在 Visual Studio C# 中,我尝试添加 COM 引用,但提示:
对“csharp_server”的引用可以 不被添加。 ActiveX 类型库 'xxx\csharp_server.tlb' 已导出 来自 .NET 程序集并且不能 添加作为参考。添加参考 改为 .NET 程序集。 但在《.NET参考》中。我看不到组件“csharp_server”。
我做错了什么或忘记了什么?
提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
#1 阅读 sn.exe。生成强名称密钥文件,然后在解决方案资源管理器中右键单击您的项目并编辑其属性。选择“签名”选项卡并添加密钥文件。如果您的公司有密钥文件,您可以延迟签署程序集,然后将其传递给私钥的保管者(您将无法访问它,因为它是高度锁定的,因为如果它是公开的,任何人都可以签署应用程序正在冒充您的公司。)不要忘记在解决方案资源管理器中打开“属性”文件夹并编辑名称/版本/等。你的集会。
#2 你是说删除DLL后还可以使用它吗?这很难相信。
#3 安装程序包(例如 Installshield)具有根据需要注册 .NET 程序集的规定。或者,您可以使用 regasm /regfile 生成可以合并到目标注册表中的 .reg。我真的建议:1)使用商业工具来构建安装程序,或者 2)成为 MSI 技术的闪亮之神。使用 SDK 工具编写正确的安装程序非常困难,甚至使用 Wix 也是如此。
据我所知,#4 没有解决方法。我猜微软认为让一个托管程序集通过第二个托管程序集实现的 COM 接口包装器之上的托管包装器程序集访问另一个托管程序集是不正当的。也许他们不希望它成为绕过私有/全局程序集绑定规则的常见方法。
不管怎样,也许您可以编辑类型库,使其“欺骗”托管包装生成器 (TlbImp.exe),使其认为它是本机 COM 类型库,但这真的值得吗?大多数具有公共 API 的商业应用程序都具有从 COM 和托管客户端使用应用程序 API 的说明,其中之一是包装器,具体取决于应用程序的实现。该技术多年来一直为许多供应商所用,没有出现任何问题。
#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.