如何在 VS 代码扩展中引用 COM 类型库(互操作)?

How to reference a COM type library (interop) in a VS Code extension?

我想开发一个使用第三方应用程序的 COM 类型库(互操作)的 VS Code 扩展吗?

理想情况下,我想用 F# (Fsharp) 编写扩展,使用 Fable 将其编译为 JS,但这并不是完全必要的。在任何情况下,我都可以从 F#、C# 和 VB.NET 引用和使用 COM 库,并在需要时提供 .NET 库作为包装器。

上下文:我是 VS 代码扩展开发的新手。我设法在 JavaScript 和 F#+Fable 中编译了一个 hello world 扩展。我可能不明白的是如何使用 package.json 文件和 npm 包管理器来引用库。

编辑:我尝试了 node.js 库 winax,但没有成功。有替代品吗?

方法比较简单。你得写一个native node module。该模块是一个 C++ DLL,您可以使用它来为要使用的 COM 库导入类型库。它还可以提供一个访问层,将调用从 V8 转换为该 COM 库。

一位同事设法 Winax (Node.js addon) working within the VS Code extension. It's important to recompile winax against the same version of Node.js and Electron that are used in the current version VS Code. (At the time of writing, VS Code 1.16.0 使用 Node.js 7.4.0 和 Electron 1.7.3。)

在命令行中(在您的扩展项目文件夹中)执行

npm install --save winax
cd node_modules\winax
node-gyp configure
node-gyp build
cd ..\..
npm rebuild winax --runtime=electron --target=1.7.3 --disturl=https://atom.io/download/atom-shell --build-from-source

Electron 版本可能需要在 --target=1.7.3 中更新。

现在像这样在 VS Code 扩展中使用 Winax

var winax = require('winax');
var excel = new winax.Object("Excel.Application");
excel.visible = true;

对我来说就像一个魅力 =)