使用C ++或C访问应用程序的COM接口

时间:2021-10-12 15:08:01

In response to question, how can I (or find more information to) automate certain functionality without user intervention, from a C++ (or C) using:

在回答问题时,我如何(或找到更多信息)在没有用户干预的情况下自动化某些功能,使用C ++(或C):

  • ATL
  • Or Automation code directly in C/C++
  • 或自动化代码直接在C / C ++中

Regards

2 个解决方案

#1


If the application exposes a type library (and Microsoft Office applications do), then you can get at it from Microsoft C++ by using the #import keyword. This will create C++ wrappers for the COM interfaces exposed by the application.

如果应用程序公开类型库(以及Microsoft Office应用程序),则可以使用#import关键字从Microsoft C ++获取它。这将为应用程序公开的COM接口创建C ++包装器。

Type libraries are often .TLB files, but they are regularly embedded as Win32 resources in executable files.

类型库通常是.TLB文件,但它们通常作为Win32资源嵌入可执行文件中。

To find out if an application exposes its functionality in this way, you'll probably need to consult the documentation for the specific application, because discovering (and understanding) type libraries is quite complicated.

要确定应用程序是否以这种方式公开其功能,您可能需要查阅特定应用程序的文档,因为发现(和理解)类型库非常复杂。

The Windows SDK includes a tool, "OLE/COM Viewer" that can display typelibrary information for a COM object in the registry, or from a .TLB file or resource.

Windows SDK包含一个工具“OLE / COM查看器”,可以在注册表中显示COM对象的类型库信息,也可以显示.TLB文件或资源。

If you want to play with an application, VBScript (or JScript) give you a quick way to experiment. They're both capable of consuming OLE Automation interfaces.

如果您想使用应用程序,VBScript(或JScript)可以为您提供快速的实验方法。它们都能够使用OLE自动化接口。

#2


By automate you mean be able to have a script that runs an application, communicates with it, and performs some functions. Something like an automated test script of sorts or something like that.

通过自动化,您意味着能够拥有一个运行应用程序,与之通信并执行某些功能的脚本。类似自动测试脚本或类似的东西。

For that the simplest way (if the application already exists, such as Word, Excel, Visual Studio, etc) is to write a script in a language that supports DCOM (such as Ruby with its WIN32OLE library) and use that to call into the application.

为此,最简单的方法(如果应用程序已经存在,如Word,Excel,Visual Studio等)是用支持DCOM的语言编写脚本(例如Ruby及其WIN32OLE库)并使用它来调用应用。

If you're developing an application then that becomes trickier, you could probably embed an existing scripting language into your executable and provide a way to interface into that. Perhaps have a command line option to automatically run a script within the context of your application. Or if you want to do some serious development work you could try adding in DCOM (I have not tried this so I don't know how much work is involved).

如果您正在开发一个应用程序然后变得更加棘手,您可以将现有的脚本语言嵌入到您的可执行文件中,并提供一种与之接口的方法。也许有一个命令行选项可以在应用程序的上下文中自动运行脚本。或者如果你想做一些认真的开发工作,你可以尝试添加DCOM(我没有尝试过,所以我不知道涉及多少工作)。

#1


If the application exposes a type library (and Microsoft Office applications do), then you can get at it from Microsoft C++ by using the #import keyword. This will create C++ wrappers for the COM interfaces exposed by the application.

如果应用程序公开类型库(以及Microsoft Office应用程序),则可以使用#import关键字从Microsoft C ++获取它。这将为应用程序公开的COM接口创建C ++包装器。

Type libraries are often .TLB files, but they are regularly embedded as Win32 resources in executable files.

类型库通常是.TLB文件,但它们通常作为Win32资源嵌入可执行文件中。

To find out if an application exposes its functionality in this way, you'll probably need to consult the documentation for the specific application, because discovering (and understanding) type libraries is quite complicated.

要确定应用程序是否以这种方式公开其功能,您可能需要查阅特定应用程序的文档,因为发现(和理解)类型库非常复杂。

The Windows SDK includes a tool, "OLE/COM Viewer" that can display typelibrary information for a COM object in the registry, or from a .TLB file or resource.

Windows SDK包含一个工具“OLE / COM查看器”,可以在注册表中显示COM对象的类型库信息,也可以显示.TLB文件或资源。

If you want to play with an application, VBScript (or JScript) give you a quick way to experiment. They're both capable of consuming OLE Automation interfaces.

如果您想使用应用程序,VBScript(或JScript)可以为您提供快速的实验方法。它们都能够使用OLE自动化接口。

#2


By automate you mean be able to have a script that runs an application, communicates with it, and performs some functions. Something like an automated test script of sorts or something like that.

通过自动化,您意味着能够拥有一个运行应用程序,与之通信并执行某些功能的脚本。类似自动测试脚本或类似的东西。

For that the simplest way (if the application already exists, such as Word, Excel, Visual Studio, etc) is to write a script in a language that supports DCOM (such as Ruby with its WIN32OLE library) and use that to call into the application.

为此,最简单的方法(如果应用程序已经存在,如Word,Excel,Visual Studio等)是用支持DCOM的语言编写脚本(例如Ruby及其WIN32OLE库)并使用它来调用应用。

If you're developing an application then that becomes trickier, you could probably embed an existing scripting language into your executable and provide a way to interface into that. Perhaps have a command line option to automatically run a script within the context of your application. Or if you want to do some serious development work you could try adding in DCOM (I have not tried this so I don't know how much work is involved).

如果您正在开发一个应用程序然后变得更加棘手,您可以将现有的脚本语言嵌入到您的可执行文件中,并提供一种与之接口的方法。也许有一个命令行选项可以在应用程序的上下文中自动运行脚本。或者如果你想做一些认真的开发工作,你可以尝试添加DCOM(我没有尝试过,所以我不知道涉及多少工作)。