Ducky Ducky - 5 months ago 50
Node.js Question

Node-ffi Window List

I'd like to be able to use Node-ffi to get a list of all the currently open windows/programs in the Windows OS, presumably something with EnumWindows from User32.dll or something similar. I'd like to be able to get a handle, like from 'FindWindowW' in User32, and also a string of the processes name, so I know which it is.

Thanks to some kind soul I've got 'FindWindowW' working and know of this question about the EnumWindows, but I just can't get that solution to work,
always just get

Uncaught ReferenceError: libm is not defined
in node_modules\ffi\lib_foreign_function.js:59

Thank you, any help is much appreciated.

Answer

Alright, eventually I got this solved with EnumWindows like this:

var ref = require('ref');
var ffi = require('ffi');

var voidPtr = ref.refType(ref.types.void);
var stringPtr = ref.refType(ref.types.CString);

var user32 = ffi.Library('user32.dll', {
    EnumWindows: ['bool', [voidPtr, 'int32']],
    GetWindowTextA : ['long', ['long', stringPtr, 'long']]
});

windowProc = ffi.Callback('bool', ['long', 'int32'], function(hwnd, lParam) {
  var buf, name, ret;
  buf = new Buffer(255);
  ret = user32.GetWindowTextA(hwnd, buf, 255);
  name = ref.readCString(buf, 0);
  console.log(name);
  return true;
});

user32.EnumWindows(windowProc, 0);