Accessing an application's COM interface using

2019-06-04 05:50发布

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

  • ATL
  • Or Automation code directly in C/C++

Regards

2条回答
姐就是有狂的资本
2楼-- · 2019-06-04 06:37

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.

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).

查看更多
乱世女痞
3楼-- · 2019-06-04 06:50

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.

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

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.

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.

查看更多
登录 后发表回答