简体   繁体   中英

Using Excel through vtable interface

I am learning COM programming via C++. As I understand, on the client side of dual interfaces you have two choices:

  1. Acquire an IDispatch interface, query DISPIDs with GetIDsOfNames, and use Invoke to access methods and properties.
  2. Include the .h header files with interface definitions and the .c source files with GUIDs created by MIDL in your project and call the functions directly through the vtable, which is known for the compiler from the .h files.

I would like to create a quite complex Excel Workbook from a C++ program (and I insist on using C++ instead of C# or anything else). Using the 1. way I was able to write a program which runs correctly. However, I have two problems: (A) the code is quite clumsy because of the calls to Invoke, (B) it is quite fast but I would like it to be even faster.

So I would like to try the 2. way. I am just missing the .h and .c files because unlike in the examples in the books I read, these files are not created by another example project but by Microsoft.

My questions are:

  • Where can I find these files?
  • How much performance improvement can I hope from way 2. compared to way 1.?

I recommend to not do it. And, I have my reasons...

For simple things, just spinning up excel.exe and marshaling data from one process to another process eats up most of the time. Those things are a magnitude greater than what you might gain in using C++ interfaces.

However, the big reason is this: Sometimes Office doesn't get installed or registered correctly on a client machine...for whatever reason. What happens sometimes is that the interfaces do not get registered correctly. If the interfaces do not get registered correctly, you will be pulling your hair out trying to figure out why your program is failing. Eventually you might figure it out. Then your only recourse is to tell your customer to re-install Office and hope it installs correctly, or to create a .reg file and have the customer apply the reg file to fix the interfaces if he has administrator privileges and you know which are the missing interfaces.

If you use IDispatch, it doesn't matter if the interfaces are missing. I've learned this the hard way with Word. You already have it working...

If you insist, then you can try:

#import "progid:Excel.Sheet" // plus a bunch of other options like rename() etc...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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