简体   繁体   English

COM exe,C ++和MinGW

[英]COM exe, C++, and MinGW

Have abit of an odd question; 有一个奇怪的问题; I'm using a tool supplied by a large company that, for reasons I find somewhat baffling, uses a COM interface defined inside the exe itself. 我使用的是一家大公司提供的工具,出于某种原因,我觉得有点莫名其妙,而是使用了exe本身定义的COM接口。 In the example code they provide, it looks alittle like this. 在他们提供的示例代码中,看起来有点像这样。

#import "C:\\\\Path_To_Exe\\\\the.exe" rename_namespace ("exe_namespace");

From what I understand, this is the way Microsoft Visual C++ compiler understands the COM and works with it, and I have had the example code working before (currently, it doesn't compile due to fiddling with my build environment). 据我了解,这是Microsoft Visual C ++编译器理解COM并与其一起工作的方式,并且我之前有示例代码可以工作(当前,由于对我的构建环境的偏爱,它无法编译)。

My question is, is there a way to do the same with MinGW? 我的问题是,有没有办法对MinGW进行同样的处理? The project I'm working on is mainly using that; 我正在研究的项目主要是使用它。 we can use MSVC if required, but I'd ideally like to avoid using multiple compilers if possible. 我们可以根据需要使用MSVC,但理想情况下,我希望避免使用多个编译器。 I'm currently using cmake to build with, but I'm willing to use a script to build the items that need the COM interface if needed. 我目前正在使用cmake进行构建,但是如果需要,我愿意使用脚本来构建需要COM接口的项目。

Thanks for your time. 谢谢你的时间。

The COM subsystem is part of the Windows API, and you can access it using C calls to that API. COM子系统是Windows API的一部分,您可以使用对该API的C调用来访问它。

However there is a huge amount of boilerplate involved in this. 但是,这涉及大量样板。 The compilers which support COM "out of the box" have written all this boilerplate, and packaged it up in some combination of compiled libraries, template headers, and so on. 支持COM“开箱即用”的编译器已经编写了所有这些样板,并将其打包为已编译的库,模板标头等的某种组合。

Another part of the usual suite of tools offered by these compilers is one that can read COM interface definitions out of an existing compiled object. 这些编译器提供的常用工具套件的另一部分是可以从现有的编译对象中读取COM接口定义的工具。 COM objects usually contain a binary representation of their interface, for this reason. 因此,COM对象通常包含其接口的二进制表示形式。

There are a few ways you could proceed here in order to use g++; 您可以通过几种方法来使用g ++。 one option is following this broad outline: 一种选择是遵循以下大致概述:

  • Use your MSVC installation to read the COM object and produce a C header file describing the interface. 使用您的MSVC安装程序读取COM对象并生成一个描述该接口的C头文件。
  • Pick out the enumerations and GUIDs from that header file. 从该头文件中选择枚举和GUID。
  • In g++, use the Windows API to invoke the object, using those enumerations and GUIDs. 在g ++中,使用Windows API来使用这些枚举和GUID调用对象。

If you want to author objects in g++ then there is a lot more work to do as you need to implement a bunch of things, but it is possible. 如果您想用g ++编写对象,那么您还需要做很多工作来实现一堆东西,但这是可能的。

I have done this successfully in the past with g++ (as part of testing COM objects I'd developed). 过去,我已经使用g ++成功地做到了这一点(作为测试我开发的COM对象的一部分)。 Probably somebody could develop a nice open-source suite for using COM objects, or even for authoring, that does not depend on MSVC but I'm not aware of such a thing. 可能有人可以开发一个不错的开源套件来使用COM对象,甚至用于创作,它不依赖于MSVC,但我不知道这种事情。

I would recommend reading the books by Don Box, they fill in a lot of gaps in understanding that you will have if you've only learned about COM by working with it and reading the internet. 我建议阅读Don Box的书,如果您仅通过与COM一起工作并阅读互联网来了解COM,它们将填补您在理解方面的许多空白。

The answer to "is there a way to do the same with MinGW" is no. 答案是“是否可以使用MinGW进行相同的处理”。 #import is an optional tool that reads a COM type library (embedded in a binary or not, the TLB corresponds in general to an .idl file, but that also is optional), and generates C/C++ code that's heavily dependent on .c and .h files that only Visual Studio provides. #import是一个可选工具 ,可读取COM 类型库 (无论是否嵌入二进制文件,TLB通常对应于.idl文件,但也是可选的),并生成严重依赖于.c的C / C ++代码。和仅Visual Studio提供的.h文件。

The answer to "can I do COM with MinGW" is of course yes. 当然,“我可以用MinGW做COM”的答案是肯定的。 I don't know much about MinGW and tools, but you can do COM with any compiler since COM is (just) a binary standard . 我对MinGW和工具了解不多,但是您可以使用任何编译器来实现COM,因为COM(只是) 二进制标准

If you get rid of #import, you'll have to change the code that uses what was generated (in the .TLH file resulting of the #import directive), COM helper, wrappers, etc. It can be a lot of work, but it's technically possible. 如果您放弃了#import,则必须更改使用生成的代码(在#import指令产生的.TLH文件中),COM帮助程序,包装程序等的代码。这可能需要大量工作,但这在技术上是可能的。

Now, in your context, I suppose it really depends how big the .exe's type library (the description of your COM classes, interfaces, etc.) is. 现在,在您的上下文中,我想它实际上取决于.exe的类型库(您的COM类,接口等的描述)的大小。 Visual Studio's #import adds value, so you'll have to assess how much value it added for you. Visual Studio的#import增加了价值,因此您必须评估它为您增加了多少价值。

If it's just one class, one interface for example, then it can be interesting to get rid of the #import. 如果只是一个类,例如一个接口,那么摆脱#import可能会很有趣。 If the .exe already has .h files that correspond to the tlb, then you can use them, otherwise you'll have to redeclare some by yourself (and again, change the code that was using generated wrappers). 如果.exe已经具有与tlb对应的.h文件,则可以使用它们,否则,您必须自己重新声明一些文件(然后再次更改使用生成的包装器的代码)。

The sole fact that you ask the question makes me wonder if you have enough knowledge of COM (no offense :-) to get rid of Visual Studio. 您提出问题的唯一事实使我想知道您是否具有足够的COM知识(没有冒犯:-)以摆脱Visual Studio。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM