如何使C#COM DLL等效于ATL COM C ++ [英] How to make C# COM DLL equivalent to ATL COM C++

查看:54
本文介绍了如何使C#COM DLL等效于ATL COM C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用C#和Visual Studio 2005开发COM.

Hi I am trying to develop a COM using C# and Visual Studio 2005

首先,我创建了接口"CSharpServer.cs"

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>

我编译了.输出为:csharp_server.dll.之后,我做了:重新加气csharp_server.dll/tlb/codebase
输出为:

I compiled. The output is: csharp_server.dll. After, I did: regasm csharp_server.dll /tlb /codebase
The output is:

...RegAsm:警告RA0000:向以下对象注册未签名的程序集/codebase可以使用您的程序集来干扰可能安装在同一台计算机./codebase开关只能用于带签名程序集.请给您的大会起好名字,然后重新注册它.类型已成功注册...

... 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ª问题:我必须签名吗?如何?

1ª Question: I have to sign? How?

2ª问题:作者删除了.

2ª Question: Removed by the author.

3ª问题:我只能在Visual Studio中运行重新加气"命令提示符.很快,如果我使用C#开发COM组件并将其分发给客户端,那么还必须安装Visual Studio?如果客户刚刚安装了Delphi,并且想要使用组件,就必须安装Visual Studio吗?

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 Delp and want to use component, it will have to install visual studio?

4ª在Visual Studio C#中,我尝试添加COM引用,但是却说:

4ª Inside Visual Studio C#, I trying to add COM reference but says:

对"csharp_server"的引用可以未添加.ActiveX类型库'xxx \ csharp_server.tlb'已导出来自.NET程序集,不能添加为参考.添加参考改为.NET程序集.但是在".NET参考"中.我看不到组件"csharp_server".

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?

谢谢.

推荐答案

#1在sn.exe上进行阅读.生成一个强名称密钥文件,然后在解决方案资源管理器中右键单击您的项目并编辑其属性.选择签名"选项卡并添加密钥文件.如果您的公司有密钥文件,则可以延迟对程序集进行签名,然后将其传递给私钥的持有人(您将无法访问它,因为它被极度锁定,因为如果它是公开的,则任何人都可以签名)应用会冒充您的公司.)不要忘记在解决方案资源管理器中打开属性"文件夹并编辑名称/版本/等.您的程序集.

#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您是说删除DLL后就可以使用它吗?很难相信.

#2 Are you saying you can use a DLL after you've deleted it? That's hard to believe.

#3安装程序包(例如Installshield)具有根据需要注册.NET程序集的规定.或者,您可以使用regasm/regfile生成一个.reg,可以将其合并到目标注册表中.我真的建议:1)使用商业工具来构建安装程序,或者2)作为MSI技术的光辉之神.使用SDK工具甚至Wix编写正确的安装程序都是非常困难的.

#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没有解决方法.我猜微软认为让托管程序集通过第二托管程序集实现的COM接口包装程序之上的托管程序集程序集访问另一个托管程序集是不正确的.也许他们不希望它成为解决私有/全局程序集绑定规则的常用方法.

#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.

无论如何,也许您可​​以编辑类型库,以使其欺骗"托管包装器生成器(TlbImp.exe),使其认为它是本机COM类型库,但这真的值得吗?大多数具有公共API的商业应用程序都有从COM和受管客户端使用该应用程序的API的说明,取决于应用程序的实现,一个或另一个是包装器.多年来,这种技术一直在无数供应商中起作用.

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.

这篇关于如何使C#COM DLL等效于ATL COM C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆