我用VS2008,建立一个工程ATLProject1,里面先有一个VC建立的ATL工程,有一个组件叫做Fun,接口叫做IFun,函数叫做Add([in] int n1,[in] int n2,[out,retval] int* pVal). 编译成功,自动注册。然后我建立一个C#的工程,敲入以下代码:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ATLProject1Lib;
namespace cs_ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            IFun f = new FunClass();
            Console.WriteLine(f.Add(2, 3));
        }
    }
}运行调试到了IFun f=new FunClass();这一句出错了,如下:
Additional information: Retrieving the COM class factory for component with CLSID {6FB1F141-A978-420B-892B-6DB29610F314} failed due to the following error: 80040154.'cs_ConsoleApplication1.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll'
The program '[520] cs_ConsoleApplication1.vshost.exe: Managed' has exited with code 0 (0x0).我把这句改成了这样也不行:            IFun f = new Fun();这是为什么呢?

解决方案 »

  1.   

    我重新添加了引用,如下:但是发现还是不行:为什么呢?用一个VC的客户端是可以使用这个com组件的啊。
      

  2.   

    代码改成了:using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using ATLProject2Lib;
    namespace cs_ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Fun f = new Fun();
                IFun fi = new Fun();// f as IFun;
                Console.WriteLine(fi.Add(3, 7));
    我重新创建了一个ATLProject2生成一个com组件
      

  3.   

    估计你的C#程序是64位的。你有COM组件源代码的话,编译一个32位版本,一个64位版本,然后注册到对应的注册表位置里面去。
      

  4.   


    谢谢。问题是:64位的com组件是用什么命令进行注册的? regsvr32是注册32位组件的吧?但是我的win7 64位系统里面并不能找到名叫regsvr64的程序。然后我按照你说的,devenv /useenv启动VS,进入VS打开工程新建一个x64的配置,编译ATL工程以及C#工程。都能编过,但是这次运行提示连库类都找不到了:Unhandled Exception: System.Runtime.InteropServices.COMException: Retrieving the
     COM class factory for component with CLSID {2BFFD823-9C32-4661-87F2-6B453E0C3F6
    9} failed due to the following error: 80040154 Class not registered (Exception f
    rom HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
       at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOn
    ly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Bo
    olean& bNeedSecurityCheck)
       at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipChec
    kThis, Boolean fillCache, StackCrawlMark& stackMark)
       at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean s
    kipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
       at System.Activator.CreateInstance(Type type, Boolean nonPublic)
       at System.Activator.CreateInstance(Type type)
       at cs_ConsoleApplication1.Program.Main(String[] args) in d:\Documents\Visual...
      

  5.   

    试试下面的方法:
    1.在编译你的dll时,将其定义为com组件;
    2.生产dll之前,对程序集进行签名(Choose a strong name key 下选择new,写入自己的key file name)
    3.生成dll
    4.注册dll
      a.以管理员运行cmd,转到你的RegAsm.exe所在的.net 框架文件夹,并运行如下命令,如:我的.net 框架在(C:\Windows\Microsoft.NET\Framework64\v2.0.50727)——(dll路径:C:\xx\xx.dll),如下:
      C:\Windows\Microsoft.NET\Framework64\v2.0.50727>regasm /codebase C:\xx\xx.dll  
    ok 了
      

  6.   

    64位系统下有两个regsvr32,64位的那个在system32下,一个32位在wow目录里面
      

  7.   

    我自己解决了:
    在工程配置里面要把Any CPU 改成X86,像这样:
    然后就能输出结果16了