GetObject与edge.js

我目前正在通过运行edge.js的基于node.js的REST-APIreplaceIE中的ActiveX实现

到目前为止,页面实现的基本示例工作得很好。 我有我的index.js设置为

var edge = require('edge'); var edgeVb = require('edge-vb'); var path = require('path'); var helloVb = edge.func('vb', path.join(__dirname, 'simpleVbFunction.vb')); helloVb('Testing with String', (err, res) => { if(err) throw err; console.log(res); }); 

和simpleVbFunction.vb一样

 Async Function(Input As Object) As Task(Of Object) Return Await Task.Run(Function() Return "NodeJS Welcomes: " & Input.ToString() End Function) End Function 

到现在为止还挺好。 现在我想能够访问在运行node.js的机器上运行的应用程序。 在这种情况下,Catia(也可以是Excel)

通常为此,我会使用这样的simpleVbFunction.vb

 Async Function(Input As Object) As Task(Of Object) Return Await Task.Run(Function() Dim CATIA as Object set CATIA = GetObject(, "CATIA.Application") Return CATIA.ActiveDocument.Application.FullName End Function) End Function 

但是这不起作用。 我收到以下错误。

错误:无法编译VB代码。 —->编译为CLR库时出错:C:\ Users \ xxxxxx \ AppData \ Local \ Temp \ 4hf2uw3z.0.vb(1,0):error BC30203:Bezeichner erwartet。 —->编译为CLRasynchronouslambdaexpression式时出现错误:C:\ Users \ xxxxxx \ AppData \ Local \ Temp \ cypj4ltp.0.vb(7,0):错误BC30451:“GetObject”ist nicht deklariert。 Auf das Objekt kann aufgrund der Schutzstufemöglicherweisenicht zugegriffen werden。 在对象上的Object.exports.func(C:\ Users \ xxxxxx \ coding \ node \ tst_win32ole \ node_modules \ edge \ lib \ edge.js:169:17)上的错误(本机)。 (module.js:579)处的Module._compile(module.js:570:32)处的文件(C:\ Users \ xxxxxx \ coding \ node \ tst_win32ole \ index.js:6:20) :10)在Module.load(module.js:487:32)在tryModuleLoad(module.js:446:12)在Function.Module._load(module.js:438:3)在Timeout.Module.runMain [as _onTimeout](module.js:604:10)ontimeout(timers.js:386:14)

现在我必须pipe理我的VB.NET技能有点生疏,这可能只是在我的VB代码中的错误,但我认为这是另一回事。 有没有人能够通过edge.js访问COM对象,如果是的话,这是可能的?

好的。 我通过从VB切换到C#(至less代码注释为访问Excel应用程序的名称)来了一点点。 但是这又带来另一个问题。 我的代码在这里看起来像这样

 using System.Threading.Tasks; using System; using System.Runtime.InteropServices; // using INFITF; public class Startup { public async Task<object> Invoke(object input) { // dynamic xl = Activator.CreateInstance(Type.GetTypeFromProgID("Excel.Application")); // return xl.Name; object CATIA0 = Marshal.GetActiveObject("CATIA.Application"); INFITF.Application CATIA = CATIA0 as INFITF.Application; } } 

现在的问题是,当使用Visual Studio和编译DLL自动包括(当设置CATIA为INFITF.Application时)。 但与edge.js我会得到命名空间INFITF找不到的错误。 任何想法如何得到这个工作?

对不起,很长的问题。 解决之后,我会整理一下。

[/编辑]

好。 我想出了大部分。 首先,因为您不使用IDE,而且不会像以前那样添加Ressources,所以这将与edge.js不同。 你将不得不find你的DLL并执行它。 在我的情况下,我不得不find

Interop.INFITF.dll

从Catia目录。

之后,你的node.js代码将如下所示。 (我现在testing的function就是每秒一次,我将在Catia中logging当前打开的部分的名称)

 var edge = require('edge'); var path = require('path'); var getCat = edge.func({ source: path.join(__dirname, 'accessCatia.cs') , references :[ './Resources/Interop.INFITF.dll' ]}); setInterval(function(){ getCat('someInputString', (err, res)=>{ if(err){ console.log('ERROR FOUND: '); console.log(err); return; } console.log(res); }); },1000); 

我的accessCatia.cs将如下所示。

 using System.Threading.Tasks; using System.Runtime.InteropServices; using System; public class Startup { public async Task<object> Invoke(object input) { object cat0 = Marshal.GetActiveObject("Catia.Application"); INFITF.Application cat = cat0 as INFITF.Application; INFITF.Document doc = cat.ActiveDocument as INFITF.Document; return doc.FullName; } } 

现在仍然开放的是,接收到的对象似乎与使用VB.net或通过Internet Explorer中的ActiveX获得的结构不同。 例如

doc.FullName作为常规存在,但使用C#时不会finddoc.Name,而是必须使用doc.get_Name()。

不幸的是,DassaultSystems在文档方面相当糟糕,但这可能是另一个话题。