2010-11-30 66 views
1

tlb文件(使用regasm)是由dll(使用c#代码)制作的,我可以在C++中使用#import。一切工作正常。 有没有办法,我可以在C语言中使用它?我刚刚发现#import是特定于C++的。那么,有人可以告诉我如何在我的C程序中使用?如何在C中使用tlb文件?

我的主要目的是在我的C程序中使用C#开发的COM DLL。

感谢& RGDS, 〜卡尔文

+0

我得到c2773错误:#import和#using只能在C++编译器 – rplusg 2010-11-30 10:51:24

回答

7

COM编程C是痛苦,但并非不可能。但降压在这里停止。类型库的要点是让工具自动生成COM接口和联合类声明,以便您可以在代码中使用它们。非常类似于.h文件,但与语言无关。 .NET等价物是程序集中的元数据。

问题是,工具无法将.tlb转换为C声明。我相信你对#import很熟悉,这就是MSVC中使用的。但它会生成C++代码,智能指针,可帮助您创建COM对象,调用其接口方法并处理错误。如果有可用的工具生成C,那么它是一个非常好的隐藏的秘密。

有一招让人想起,您可以使用OleView.exe,File + View TypeLib来查看类型库的内容。这个视图被反编译成接口定义语言IDL。您可以将此文本复制并粘贴到.idl文件中,并使用midl.exe使用/ header命令行选项进行编译。这将生成一个.h文件,其中包含C++和接口的C声明。应该让你靠近,只要确保类型库相当稳定,所以你不必经常这样做。

+0

感谢您的见解。并且可以从oleviewer生成c文件和h文件。但是,当我包括然后,我得到很多错误。没有意义。 – rplusg 2010-11-30 14:28:33

-2

您可以使用regasm with /tlb option在Windows注册表中注册的类型。之后,您可以像C++代码中的常规COM调用一样创建实例。

从MSDN:

When you specify the /tlb option, Regasm.exe generates and registers a type library describing the types found in the assembly. Regasm.exe places the generated type libraries in the current working directory or the directory specified for the output file. 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). 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. 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. Using the two options together will unregister the type library and interface entries, which can clean the registry considerably.

+0

感谢您的回复,但我想从C使用它,而不是C++。对不起如果Iam不清楚的问题。 – rplusg 2010-11-30 10:50:29

0

既不Ç也不C++有#进口预处理器指令。

+1

这是一个微软扩展http://msdn.microsoft.com/en-us/library/8etzzkb6(VS.71).aspx – JeremyP 2010-11-30 11:30:08

相关问题