Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: error setting return value - buf.writeUInt8 is not a function when call EnumWindows #1

Open
xmsz opened this issue Apr 13, 2023 · 3 comments

Comments

@xmsz
Copy link

xmsz commented Apr 13, 2023

code:

var user32 = new ffi.Library('user32', {
  GetWindowThreadProcessId: ['long', ['long', 'pointer']],
  EnumWindows: ['bool', ['pointer', 'long']],
});
const callback = new ffi.Callback('bool', ['long', 'int32'], (hwnd, lParam) => {
  const lpdwProcessId = ref.alloc('int');
  user32.GetWindowThreadProcessId(hwnd, lpdwProcessId);
  if (lpdwProcessId == lParam) {
    return false;
  }
  return true;
});
user32.EnumWindows(callback, 0);

result

Uncaught Error: TypeError: error setting return value - buf.writeUInt8 is not a function
    at Object.proxy [as EnumWindows] (node_modules\.pnpm\@[email protected]\node_modules\@lwahonen\ffi-napi\lib\_foreign_function.js:61:14)
    at ./src/renderer/hooks/useRecordController.tsx (useRecordController.tsx:93:8)
    at options.factory (react refresh:6:1)
    at __webpack_require__ (bootstrap:24:1)
    at fn (hot module replacement:62:1)
    at ./src/renderer/components/Aside.tsx (Add.tsx:384:20)
    at options.factory (react refresh:6:1)
    at __webpack_require__ (bootstrap:24:1)
    at fn (hot module replacement:62:1)

I saw this issuse node-ffi-napi/node-ffi-napi#225 (comment)

And i change true to 1, false to 0, it works but no real a good solution

So how can i fix it

@lwahonen
Copy link
Owner

Either fix the project to support uint8 or use an int instead. I'd just use int instead of bool

@xmsz
Copy link
Author

xmsz commented Apr 13, 2023

Either fix the project to support uint8 or use an int instead. I'd just use int instead of bool

ok

@xieerduos
Copy link

xieerduos commented Aug 27, 2024

Either fix the project to support uint8 or use an int instead. I'd just use int instead of bool
Here’s the translation of your request:

@lwahonen @xmsz

I've encountered the error Error: TypeError: error setting return value - buf.writeUInt8 is not a function and am unsure how to resolve it.

Below is the related code:

const ffi = require("@breush/ffi-napi");
const ref = require("@breush/ref-napi");
function getUser32() {
  if (user32) {
    return user32;
  }

  user32 = ffi.Library("user32", {
    EnumWindows: ["bool", ["pointer", "int32"]],
    FindWindowW: ["int32", ["string", "string"]],
    EnumChildWindows: ["bool", ["int32", "pointer", "int32"]],
    GetWindowRect: ["bool", ["int32", "pointer"]],
    GetClassNameW: ["int32", ["int32", "pointer", "int32"]],
    IsWindowVisible: ["bool", ["int32"]],
    EnumDisplayMonitors: ["bool", ["pointer", "pointer", "pointer", "int32"]],
    GetMonitorInfoW: ["bool", ["int32", "pointer"]],
  });
  return user32;
}
// Method to get handles for all displays
function getAllDisplays() {
  const monitors = [];
  // Define MONITORINFO structure
  const MONITORINFO = Buffer.alloc(40);
  MONITORINFO.writeInt32LE(40, 0); // cbSize
  const monitorEnumProc = ffi.Callback(
    "bool",
    ["int32", "pointer", "pointer", "int32"],
    (hMonitor) => {
      if (getUser32().GetMonitorInfoW(hMonitor, MONITORINFO)) {
        const left = MONITORINFO.readInt32LE(4);
        const top = MONITORINFO.readInt32LE(8);
        const right = MONITORINFO.readInt32LE(12);
        const bottom = MONITORINFO.readInt32LE(16);
        monitors.push({
          ...convertToXYWH({ rect: { left, top, right, bottom } }),
          isPrimaryMonitor: isPrimaryMonitor(hMonitor),
          workArea: convertToXYWH({ rect: getMonitorWorkArea(hMonitor) }),
        });
      }

      return true; // Continue enumeration
    }
  );

  getUser32().EnumDisplayMonitors(null, null, monitorEnumProc, 0);
  return monitors;
}

console error

  Error: TypeError: error setting return value - buf.writeUInt8 is not a function
      at Object.proxy [as EnumDisplayMonitors] (D:\project\electron-screenshot\node_modules\@breush\ffi-napi\lib\_foreign_function.js:61:14)
      at Object.getAllDisplays (D:\project\electron-screenshot\main\desktopWindows.js:76:15)
      at MessagePort.<anonymous> (D:\project\electron-screenshot\main\worker-display.js:9:10)
      at [nodejs.internal.kHybridDispatch] (node:internal/event_target:647:20)
      at exports.emitMessage (node:internal/per_context/messageport:23:28)
]

How should I proceed to fix this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants