精通
英语
和
开源
,
擅长
开发
与
培训
,
胸怀四海
第一信赖
As you have seen, it does not really take a lot to implement and use objects based on the programming model introduced by COM. The techniques are very valuable on their own, even if you do not intend to use the COM Libraries for whatever (strange!) reason.如上所述,基于COM编程,并不需要写很多代码来使用对象。技术本事有价值,即使你不倾向于使用COM库来开发。
Taking advantage of COM allows you to: 使用COM的优点来:
Some general tips for implementing your objects using COM: 使用COM来实现对象时可以用一些技巧,比如:
Making an object "COM compliant" requires a very low overhead, in terms of both implementation cost and overhead incurred.让对象“COM兼容”需要非常低的开销,在实现和执行开支上都低。
To implement a COM object, simply reuse all the "adorning" code, such as class factory objects, IUnknown implementation, Registration code (you could even make it a macro or a C++ template), and start implementing your object's functionality!为了实现COM对象,简单重用所有“修饰”代码,比如类工厂对象,IUnknown实现,注册代码(你甚至能让它们成为宏或C++模板),且开始实现你对象的功能。
Overhead in memory is very small: You basically have a 4- byte overhead per interface (the vtable pointers in the object's instance data) plus 4 bytes for the reference counter. Even the smallest object can live with this overhead (8 bytes), and you get all the advantages of making it a COM component.内存开销非常小:基本上每个接口需要额外4个字节(对象实例数据里虚函数表指针),加上引用计数的4个字节。甚至最小的对象依赖于这8个字节生存,这样你能得到COM组件的好处了。
Performance overhead in the in-process case can be beaten only by direct function calls, but the overhead is irrelevant for any real-world use. In the local case, you basically have to implement your own remoting infrastructure, either on top of RPC (like COM) or through windows messages, shared memory, and semaphores, or whatever you wish. You could probably be a little more efficient if you optimized your implementation for your kind of interface and interface usage. But usually COM's performance will be more than sufficient and will save you a lot of work!
Just spend some time thinking about which of your objects or libraries you could implement as COM objects. Which would be in-process servers, which could be local servers? How would your overall design gain, if you could use local servers?
COM can make your life a lot easier today, and prepares you for a very exciting future!