简体   繁体   English

如何在C中使用tlb文件?

[英]How to use tlb file in C?

tlb file(using regasm) is made from a dll(using c# code), and i can use this in c++ using #import. tlb文件(使用regasm)是由dll(使用c#代码)制成的,我可以使用#import在c ++中使用它。 and everything is working fine. 而且一切正常。 Is there a way, i can use this in C language? 有没有办法,我可以用C语言使用它? I just found that #import is c++ specific. 我刚刚发现#import是c ++特有的。 So, can someone please tell me how can i use in my C program? 那么,有人可以告诉我如何在我的C程序中使用吗?

my main intention is to use COM DLL developed in C# in my C program. 我的主要目的是在我的C程序中使用C#开发的COM DLL。

Thanks & Rgds, ~calvin 谢谢,谢谢,〜calvin

COM programming in C is very painful, but not impossible. 用C语言进行COM编程非常痛苦,但并非不可能。 The buck stops here though. 雄鹿到此为止。 The point of a type library is to have a tool auto-generate the COM interface and co-class declarations so that you can use them in your code. 类型库的重点是让工具自动生成COM接口和共同类声明,以便您可以在代码中使用它们。 Quite similar to a .h file, but language independent. 与.h文件非常相似,但与语言无关。 The .NET equivalent is the metadata in an assembly. .NET等效项是程序集中的元数据。

Problem is, the tooling isn't available to convert a .tlb to C declarations. 问题是,无法使用该工具将.tlb转换为C声明。 I'm sure you are familiar with #import, that's what gets used in MSVC. 我确定您熟悉#import,这就是MSVC中使用的内容。 But it generates C++ code, smart pointers that help you create the COM object, call its interface methods and deal with errors. 但是它会生成C ++代码,智能指针,这些指针可以帮助您创建COM对象,调用其接口方法并处理错误。 If there is a tool available that generates C then it is a very well hidden secret. 如果有可用的生成C的工具,那么这是一个非常隐秘的秘密。

One trick jumps to mind, you can use OleView.exe, File + View TypeLib to view the contents of the type library. 想到一个窍门,可以使用OleView.exe,File + View TypeLib来查看类型库的内容。 This view is decompiled into IDL, the interface definition language. 该视图被反编译为接口定义语言IDL。 You can copy and paste this text into an .idl file and compile it with midl.exe with the /header command line option. 您可以将此文本复制并粘贴到.idl文件中,并使用带有/ header命令行选项的midl.exe对其进行编译。 This generates a .h file that contains both C++ and the C declarations for the interfaces. 这将生成一个.h文件,其中包含接口的C ++和C声明。 Ought to get you close, just make sure that the type library is reasonably stable so you don't have to do this very often. 应该使您接近,只需确保类型库相当稳定即可,因此您不必经常这样做。

I also faced the same problem as I need to use tlb file in C. And I think you can check the below link which helps 我也遇到了相同的问题,因为我需要在C中使用tlb文件。我认为您可以检查以下链接,这将对您有所帮助

How to call a COM component in C 如何在C中调用COM组件

Thanks, sveerap 谢谢,sveerap

C和C ++都没有#import预处理程序指令。

You can use regasm with /tlb option to register the types in windows registry. 您可以将regasm与/ tlb选项一起使用以在Windows注册表中注册类型。 After that, you can create your instances like regular COM calls from C++ code. 之后,您可以创建实例,例如从C ++代码进行常规COM调用。

From MSDN: 从MSDN:

When you specify the /tlb option, Regasm.exe generates and registers a type library describing the types found in the assembly. 当您指定/ tlb选项时,Regasm.exe会生成并注册一个描述在程序集中找到的类型的类型库。 Regasm.exe places the generated type libraries in the current working directory or the directory specified for the output file. Regasm.exe将生成的类型库放置在当前工作目录或为输出文件指定的目录中。 Generating a type library for an assembly that references other assemblies may cause several type libraries to be generated at once. 为引用其他程序集的程序集生成类型库可能会导致一次生成多个类型库。 You can use the type library to provide type information to development tools like Visual Studio 2005. You should not use the /tlb option if the assembly you are registering was produced by the Type Library Importer (Tlbimp.exe). 您可以使用类型库为开发工具(如Visual Studio 2005)提供类型信息。如果要注册的程序集是由类型库导入程序(Tlbimp.exe)生成的,则不应使用/ tlb选项。 You cannot export a type library from an assembly that was imported from a type library. 您不能从从类型库导入的部件中导出类型库。 Using the /tlb option has the same effect as using the Type Library Exporter (Tlbexp.exe) and Regasm.exe, with the exception that Tlbexp.exe does not register the type library it produces. 使用/ tlb选项与使用类型库导出器(Tlbexp.exe)和Regasm.exe具有相同的效果,不同之处在于Tlbexp.exe不注册它生成的类型库。 If you use the /tlb option to registered a type library, you can use /tlb option with the /unregister option to unregistered the type library. 如果使用/ tlb选项注册类型库,则可以将/ tlb选项与/ unregister选项一起使用以注销类型库。 Using the two options together will unregister the type library and interface entries, which can clean the registry considerably. 同时使用这两个选项将取消注册类型库和接口条目,从而可以大幅度清除注册表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C ++汇编中使用`#import`和`tlb`文件在C ++中实现COM回调接口? - How to implement COM callback interface in C++, from C# assembly, using `#import` and `tlb` file? 如何在Visual Studio 2012 C#中使用InstallShield LE注册类型库文件(.tlb) - How to register type library file (.tlb) with InstallShield LE in Visual Studio 2012 C# 在COM INTEROP的C#DLL中注册.tlb文件时出错 - Error during registration of the .tlb file in a C# DLL for COM INTEROP 将TLB导入C# - Import TLB into C# 如何从C#中读取非托管代码的TLB(类型库)? - How to read TLB (type libraries) of unmanaged code from C#? 如何将.tlb作为资源文件嵌入到.NET程序集DLL中? - How to embed .tlb as a resource file into .NET Assembly DLL? 将 C# .NET 6.0 Core DLL 导入到没有 TF6F87C9FDCF8B3C3F07F93F1EE8712 文件的 ZF6F87C9FDCF8B3C3F07F93F1EE8712 中 - Importing a C# .NET 6.0 Core DLL into a C++ without TLB file 生成的.TLB文件输出目录 - Generated .TLB file output directory Excel 2010 VSE 2012 C#dll和tlb-无法创建对指定文件的引用 - Excel 2010 VSE 2012 C# dll and tlb - Can't create reference to specified file 在 VB 6.0 中通过将其导出为 .tlb 文件来查找 C# dll 的命名空间 - Finding the namespace of a C# dll while using it in VB 6.0 through exporting it as .tlb file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM