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

1.6k views Asked by At

I'd like to develop a VS Code extension that consumes a COM type library (interop) of a third-party application?

Ideally, I'd like to write the extension in F# (Fsharp) using Fable to compile it to JS, but that's not totally essential. In any case, I can reference and use the COM library from F#, C# and VB.NET and provide a .NET library as a wrapper if needed.

Context: I'm new to VS Code extension development. I managed to compile a hello world extension in JavaScript and in F#+Fable. What I probably don't understand is how to use the package.json file and the npm package manager to reference the library.

Edit: I tried the node.js library winax, but without success. Is there an alternative?

2

There are 2 answers

1
lukasimir On BEST ANSWER

A colleague managed to get 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 uses Node.js 7.4.0 and Electron 1.7.3.)

In the command line (in your extension project folder) do

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

The Electron version may need updating in --target=1.7.3.

Now use Winax in the VS Code extension for instance like this

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

Works like a charm for me =)

3
Mike Lischke On

The approach is relatively simple. You have to write a native node module. This module is a C++ DLL which you can use to import the typelib for the COM library you want to use. It also can provide an access layer that translates calls from V8 to that COM lib.