Skip to content

Getting a cryptic error while trying to import user32.dll with Node18 #244

@mykola-mokhnach

Description

@mykola-mokhnach

I've created a fairly simple import statement like

import ffi from 'ffi-napi';

const user32 = ffi.Library('user32.dll', {
  // UINT SendInput(
  //   _In_ UINT cInputs,                     // number of input in the array
  //   _In_reads_(cInputs) LPINPUT pInputs,  // array of inputs
  //   _In_ int cbSize);                      // sizeof(INPUT)
  SendInput: ['uint32', ['uint32', 'pointer', 'int32']],
  // int GetSystemMetrics(
  //  [in] int nIndex
  // );
  GetSystemMetrics: ['int32', ['int32']],
});

Although execution of unit tests always fail on my Windows 10 machine with the cryptic stacktrace:

TypeError: Cannot read properties of null (reading 'match')
    at new DynamicLibrary (F:\workspace\appium-windows-driver\node_modules\ffi-napi\lib\dynamic_library.js:67:21)
    at Object.Library (F:\workspace\appium-windows-driver\node_modules\ffi-napi\lib\library.js:47:10)
    at Object.<anonymous> (F:\workspace\appium-windows-driver\lib\commands\winapi\/user32.js:11:20)
    at Module._compile (node:internal/modules/cjs/loader:1226:14)
    at Module._compile (F:\workspace\appium-windows-driver\node_modules\pirates\lib\index.js:136:24)
    at Module._extensions..js (node:internal/modules/cjs/loader:1280:10)
    at Object.newLoader [as .js] (F:\workspace\appium-windows-driver\node_modules\pirates\lib\index.js:141:7)
    at Module.load (node:internal/modules/cjs/loader:1089:32)
    at Function.Module._load (node:internal/modules/cjs/loader:930:12)
    at Module.require (node:internal/modules/cjs/loader:1113:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (F:\workspace\appium-windows-driver\lib\commands\/gestures.js:2:1)
    at Module._compile (node:internal/modules/cjs/loader:1226:14)
    at Module._compile (F:\workspace\appium-windows-driver\node_modules\pirates\lib\index.js:136:24)
    at Module._extensions..js (node:internal/modules/cjs/loader:1280:10)
    at Object.newLoader [as .js] (F:\workspace\appium-windows-driver\node_modules\pirates\lib\index.js:141:7)
    at Module.load (node:internal/modules/cjs/loader:1089:32)
    at Function.Module._load (node:internal/modules/cjs/loader:930:12)
    at Module.require (node:internal/modules/cjs/loader:1113:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (F:\workspace\appium-windows-driver\lib\commands\/index.js:9:1)
    at Module._compile (node:internal/modules/cjs/loader:1226:14)
    at Module._compile (F:\workspace\appium-windows-driver\node_modules\pirates\lib\index.js:136:24)
    at Module._extensions..js (node:internal/modules/cjs/loader:1280:10)
    at Object.newLoader [as .js] (F:\workspace\appium-windows-driver\node_modules\pirates\lib\index.js:141:7)
    at Module.load (node:internal/modules/cjs/loader:1089:32)
    at Function.Module._load (node:internal/modules/cjs/loader:930:12)
    at Module.require (node:internal/modules/cjs/loader:1113:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (F:\workspace\appium-windows-driver\lib\/driver.js:6:1)
    at Module._compile (node:internal/modules/cjs/loader:1226:14)
    at Module._compile (F:\workspace\appium-windows-driver\node_modules\pirates\lib\index.js:136:24)
    at Module._extensions..js (node:internal/modules/cjs/loader:1280:10)
    at Object.newLoader [as .js] (F:\workspace\appium-windows-driver\node_modules\pirates\lib\index.js:141:7)
    at Module.load (node:internal/modules/cjs/loader:1089:32)
    at Function.Module._load (node:internal/modules/cjs/loader:930:12)
    at Module.require (node:internal/modules/cjs/loader:1113:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (F:\workspace\appium-windows-driver\test\unit\/driver-specs.js:3:1)
    at Module._compile (node:internal/modules/cjs/loader:1226:14)
    at Module._compile (F:\workspace\appium-windows-driver\node_modules\pirates\lib\index.js:136:24)
    at Module._extensions..js (node:internal/modules/cjs/loader:1280:10)
    at Object.newLoader [as .js] (F:\workspace\appium-windows-driver\node_modules\pirates\lib\index.js:141:7)
    at Module.load (node:internal/modules/cjs/loader:1089:32)
    at Function.Module._load (node:internal/modules/cjs/loader:930:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:169:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25)

From checking the source code it looks like dlerror call returns null, which causes the above exception.

The complete pull request could be found at appium/appium-windows-driver#163

Environment info:
OS: Windows 10 x64
Node.js: x64 v 18.14.0
npm: 9.3.1
ffi-napi: 4.0.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions