Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C##从缺少的类型库导入引用的类型'__缺少_类型';用作占位符_C#_Com - Fatal编程技术网

C##从缺少的类型库导入引用的类型'__缺少_类型';用作占位符

C##从缺少的类型库导入引用的类型'__缺少_类型';用作占位符,c#,com,C#,Com,在C#assembly中,我创建了以下类/接口并生成了a.tlb [ComVisible(true)] [Guid("9850B8CE-B71E-4C77-ABE6-94BEB8EA308E")] [InterfaceType(ComInterfaceType.InterfaceIsDual)] public interface ITest { [DispId(1)] string Foo(); } [ComVisible(true)] [Guid("E9625332-FEB

在C#assembly中,我创建了以下类/接口并生成了a.tlb

[ComVisible(true)]
[Guid("9850B8CE-B71E-4C77-ABE6-94BEB8EA308E")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface ITest
{
    [DispId(1)]
    string Foo();

}

[ComVisible(true)]
[Guid("E9625332-FEB3-4B9D-AB01-9BAAF844F8F3")]
[ClassInterface(ClassInterfaceType.None)]
public class Test : ITest
{
    public string Foo()
    {
        return "hi";
    }

}
在另一个托管程序集中,我创建了类/接口并生成了一个B.tlb

[ComVisible(true)]
[Guid("EF77F05A-B644-4E46-9AD9-5CB2E87EE89E")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IJunk
{
    ITest Foo();
}

[ComVisible(true)]
[Guid("D88BD554-4D58-49A2-9E0F-058D47CBE3CE")]
[ClassInterface(ClassInterfaceType.None)]
public class Junk : IJunk
{
    public ITest Foo()
    {
        var i = new Test();
        return i;

    }
}

最后,在一个本地C++中,我做了一个导入B.TLB

我越来越

错误2错误C4772:#从缺少的类型库导入引用的类型;'缺少\u类型,用作placehol

我看到了这个,但它看起来是直的C++ +COMP/P>


我尝试先导入A.tlb,但这并没有解决问题,因为您跳过了一个必需的步骤,所以出现了错误。在尝试生成B.tlb之前,必须向Regasm.exe注册.dll。这将在
Typelib
界面
注册表项中写入项。如果你不这样做,那么TLBEXP就不能确定A.tlb所描述的ITEST,并且将在B的类型库中省略必要的<代码> IncILB(“A.tlb”)< /C>指令,这使得C++编译器中的“导入”指令失败,因为它无法计算出ITest来自何处。首先导入A.tlb不是解决方法

除了手动运行Regasm之外,确保这一点的最简单方法是让构建系统来处理。右键单击项目、属性、生成选项卡,勾选“注册COM互操作”选项。但是,VS必须运行提升以具有注册表写入访问权限

将声明保存在一个项目中可以避免此问题