Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.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#类库导出为COM,MIDL编译期间出错_C#_Winapi_Com_Interop - Fatal编程技术网

C#类库导出为COM,MIDL编译期间出错

C#类库导出为COM,MIDL编译期间出错,c#,winapi,com,interop,C#,Winapi,Com,Interop,我正试图从现有的C#库中创建一个COM库。为了做到这一点,我将我的C#类定义如下: [Guid("830D0BFA-8045-455B-AAB7-2A7D4A16455C")] [ComVisible(true)] public interface IMyInterface { uint Start(); } [Guid("22B91F20-1CC3-4276-BB51-0AE75D7DA5BB")] [ClassInterfac

我正试图从现有的C#库中创建一个COM库。为了做到这一点,我将我的C#类定义如下:

[Guid("830D0BFA-8045-455B-AAB7-2A7D4A16455C")]    
[ComVisible(true)]
public interface IMyInterface
{
        uint Start();
}

[Guid("22B91F20-1CC3-4276-BB51-0AE75D7DA5BB")]
[ClassInterface(ClassInterfaceType.None)]
[ComVisible(true)]
public class MyInterface_Impl : IMyInterface, IDisposable
{        
 public uint Start()
 {
 return 0;
 }
}
此外,我在项目的构建属性中启用了“注册COM互操作”。允许Win32应用程序(实际上是MFC应用程序)使用它的下一步是为程序集生成.tlb。我使用以下生成后命令执行此操作:

“$(FrameworkSdkDir)\bin\NETFX 4.8 Tools\tlbexp.exe” “$(TargetFileName)”“$(TargetName).tlb”/win32

我在build文件夹中看到了.tlb,然后继续将其添加到MFC项目中,右键单击它并将其设置为Item Type:MIDL tool。然后,当我构建此项目时,我会收到以下警告和错误:

1>------ Build started: Project: My_Dll_Test_MFC, Configuration: Debug Win32 ------ 
1>Processing ..\My_Dll\bin\x86\Debug\My_Dll.tlb 1>My_Dll.tlb 
1>..\My_Dll\bin\x86\Debug\My_Dll.tlb(1): warning C4335: Mac file format detected: please convert the source file to either DOS or UNIX format 
1>..\My_Dll\bin\x86\Debug\My_Dll.tlb(1): error MIDL2025: syntax error : expecting an interface name or DispatchInterfaceName or CoclassName or ModuleName or LibraryName or ContractName or a type specification near "MSFT" 
1>..\My_Dll\bin\x86\Debug\My_Dll.tlb(1): error MIDL2026: cannot recover from earlier syntax errors; aborting compilation 1>Done building project "My_Dll_Test_MFC.vcxproj" -- FAILED.

我在这里犯了什么错误?

您不应该将MIDL与tlb一起使用(MIDL需要idl输入,而不是tlb)。为了在C++项目中使用TLB,您只需要

我不在C++中使用COM,但我记得MIDL需要IDL输入,而不是TLB。要消耗C++项目中的TLB,需要导入指令()哦,孩子,我知道这是愚蠢的。我删除了tlb(你说得对,它需要idl),只使用了带有原始接口的#import指令#,它都是构建的。你能补充一下答案吗?我会接受的。