如何从 NodeJS 访问 COM 对象

How to access COM objects from NodeJS

如何从 Node.js 访问 COM 对象?

我已经尝试过使用 Edge 的实现和使用 C# 的简单 DLL。当我调用 invoke 函数时,DLL 中的一切都按预期工作,但 DLL 找不到指定的 COM 对象。然而,如果我在同一个 VS 解决方案中构建一个测试程序,并且 运行 它从那里运行得很好。

这是我的代码(这是我代码中第一次实际使用 COM 对象,之前只是类型):

using QBXMLRP2Lib;
...
try { rp = new RequestProcessor2(); } catch (Exception e) { return e }

它 returns 这个错误:

{ Error: Retrieving the COM class factory for component with CLSID {45F5708E-3B43-4FA8-BE7E-A5F1849214CB} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
    at Error (native)
  ErrorCode: -2147221164,
  Message: 'Retrieving the COM class factory for component with CLSID {45F5708E-3B43-4FA8-BE7E-A5F1849214CB} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).',
  Data: {},
  InnerException: null,
  TargetSite: {},
  StackTrace: 
   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at System.Activator.CreateInstance(Type type)
   at Startup.DoQBQuery(Object options) in c:\path\to\my\source\code\Startup.cs:line 35
  HelpLink: null,
  Source: 'mscorlib',
  HResult: -2147221164,
  message: 'Retrieving the COM class factory for component with CLSID {45F5708E-3B43-4FA8-BE7E-A5F1849214CB} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).',
  name: 'System.Runtime.InteropServices.COMException' }

0x80040154 或 REGDB_E_CLASSNOTREG "Class not registered" 错误是一个非常常见的错误,表示 COM 对象注册问题。 COM 基本上告诉您它找不到您正在寻找的对象。

在当今世界,大多数时候 x86/x64 不匹配:客户端和服务器(当它是进程内 DLL 时)未使用相同的位数编译。

如果使用 .NET 作为 .DLL(进程内服务器),您还需要确保至少在 dev/test 阶段使用 RegAsm /codebase 注册了它。