Requirement is to get all windows open in current desktop. I am trying to invoke EnumWindows from node-ffi which gives handler length as 0.
node reference link: node
I do not take credit for any of this code, it is merely the other answer on here, fix and then converted to regular Javascript instead of Coffee Script
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);
You seem to be treating 'hwnd' as a pointer, think you can simply use a int or long. This worked for me (coffeescript) :
ref = require 'ref'
ffi = require 'ffi
voidPtr = ref.refType(ref.types.void)
stringPtr = ref.refType(ref.types.CString)
bindings =
EnumWindows: ['bool', [voidPtr, 'int32']]
GetWindowTextA : ['long', ['long', stringPtr, 'long']]
user32 = ffi.Library('user32.dll', bindings)
windowProc = ffi.Callback 'bool', ['long', 'int32'], (hwnd, lParam) ->
buf = new Buffer 255
ret = libm.GetWindowTextA hwnd, buf, 255
name = ref.readCString(buf, 0)
console.log name
true
user32.EnumWindows windowProc, 0