简体   繁体   English

通过vtable界面使用Excel

[英]Using Excel through vtable interface

I am learning COM programming via C++. 我正在通过C ++学习COM编程。 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. 获取IDispatch接口,使用GetIDsOfNames查询DISPID,并使用Invoke访问方法和属性。
  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. 在项目中包括带有接口定义的.h头文件和带有MIDL创建的GUID的.c源文件,并直接通过vtable调用函数,vtable对于.h文件来说是众所周知的。

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). 我想通过C ++程序创建一个非常复杂的Excel工作簿(我坚持使用C ++代替C#或其他任何东西)。 Using the 1. way I was able to write a program which runs correctly. 使用1.方法,我能够编写一个可以正常运行的程序。 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. 但是,我有两个问题:(A)由于调用了Invoke,所以代码很笨拙;(B)相当快,但是我希望它更快。

So I would like to try the 2. way. 所以我想尝试2.方式。 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. 我只是缺少.h和.c文件,因为与我阅读的书籍中的示例不同,这些文件不是由另一个示例项目而是由Microsoft创建的。

My questions are: 我的问题是:

  • Where can I find these files? 在哪里可以找到这些文件?
  • How much performance improvement can I hope from way 2. compared to way 1.? 与方法1相比,我希望方法2的性能提高多少?

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. 对于简单的事情,仅将excel.exe分解并将数据从一个进程编组到另一个进程就消耗了大多数时间。 Those things are a magnitude greater than what you might gain in using C++ interfaces. 这些事情比使用C ++接口所获得的结果要大得多。

However, the big reason is this: Sometimes Office doesn't get installed or registered correctly on a client machine...for whatever reason. 但是,最大的原因是:有时出于某些原因,有时无法在客户端计算机上正确安装或注册Office。 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. 然后,唯一的办法就是告诉客户重新安装Office并希望它能够正确安装,或者创建一个.reg文件,并让客户应用reg文件来修复接口(如果他具有管理员特权,并且您知道哪个是Windows XP的)。缺少接口。

If you use IDispatch, it doesn't matter if the interfaces are missing. 如果使用IDispatch,则缺少接口都没有关系。 I've learned this the hard way with Word. 我已经用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...

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

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