Reputation: 788
I want to get a specific window's size in node, using the node-ffi and user32 libraries. Can't make the Rect pointer work at all, I'm either getting NULL as output, or fatal errors.
https://msdn.microsoft.com/en-us/library/windows/desktop/ms633503(v=vs.85).aspx
Out LPRECT lpRect should contain the top left, and bottom right corner's coordinates.
var ffi = require('ffi');
var ref = require('ref');
var Struct = require('ref-struct');
var lpctstr = {
name: 'lpctstr',
indirection: 1,
size: ref.sizeof.pointer,
get: function(buffer, offset) {
var _buf = buffer.readPointer(offset);
if(_buf.isNull()) {
return null;
}
return _buf.readCString(0);
},
set: function(buffer, offset, value) {
var _buf = ref.allocCString(value, 'ucs2');
return buffer.writePointer(_buf, offset);
},
ffi_type: ffi.types.CString.ffi_type
};
var lpdwordPtr = ref.refType(ref.types.ulong);
var pointStruct = new Struct({
'x': 'long',
'y': 'long'
});
var rectStruct = new Struct({
left : pointStruct,
top : pointStruct,
right : pointStruct,
bottom : pointStruct
});
var rectPtr = ref.refType(rectStruct);
var user32 = ffi.Library('user32', {
FindWindowW: ['int', [lpctstr, lpctstr]],
GetWindowThreadProcessId: ['int', ['int', lpdwordPtr]],
SetForegroundWindow: ['bool', ['int']],
GetWindowRect: [rectPtr, ['int']]
});
var hwnd = user32.FindWindowW(null, 'Calculator');
user32.SetForegroundWindow(hwnd);
var Rect = user32.GetWindowRect(hwnd);
console.log(JSON.stringify(Rect));
Upvotes: 4
Views: 1934
Reputation: 17959
I couldn't get your example working using the ref-struct
approach, but I figured out a way to do it using a simple "pointer" type:
var user32 = new ffi.Library("user32", {
GetWindowRect: ["bool", ["int32", "pointer"]],
});
export function GetWindowRect(handle: number) {
var rectPointer = Buffer.alloc(4 * 4);
var success = user32.GetWindowRect(handle, rectPointer);
if (!success) return null;
let rect = RectPointerToRect(rectPointer);
console.log("Rect: " + JSON.stringify(rect));
return rect;
}
function RectPointerToRect(rectPointer: Buffer) {
let rect = {} as any;
rect.left = rectPointer.readUInt32LE(0);
rect.top = rectPointer.readUInt32LE(4);
rect.right = rectPointer.readUInt32LE(8);
rect.bottom = rectPointer.readUInt32LE(12);
return rect;
}
Upvotes: 3
Reputation: 460
I'm a bit late, but I think I finally figured out how GetWindowRect() works.
at first you need to define Rect Struct according to the Documentation:
const rect = Struct({
left : 'long',
top : 'long',
right : 'long',
bottom : 'long',
});
then you need to define your user32 library:
//I think This is something like (struct rect *)
//because you need to path pointer of struct rect to this function
const rectPtr = ref.refType(rect);
var user32 = ffi.Library('user32.dll', {
GetForegroundWindow : ['long',[]],
GetWindowRect : ['bool',['long',rectPtr]]
});
before calling GetWindowRect it's necessary to acquire handle of the window you want to know the size of. Lets just take currently active window for simplicity by calling GetForegroundWindow function:
const wind_handle = user32.GetForegroundWindow();
We have a window handle and now we need do create actual rect struct for GetWindowRect() to store result in:
const rec_struct = new rect;
if you log this object in console you'll see something like this:
{ left: 0, top: 0, right: 0, bottom: 0, 'ref.buffer': Buffer@0x000001C387A81E80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 }
as I mentioned GetWindowRect accepts buffer as it's second parameter and sets it's contents with the contents of actual C rectangle structure. so to get window information in our rec_struct object we need to path rec_struct['ref.buffer'] to the GetWindowRect :
user32.GetWindowRect(fhwnd,rec['ref.buffer']);
now if you log rec_struct in the console, you'll get it filled with the the coordinates of window's upper-left and lower-right corners:
{ left: 560, top: 220, right: 1360, bottom: 820, 'ref.buffer': Buffer@0x0000012F3751F270 30 02 00 00 dc 00 00 00 50 05 00 00 34 03 00 00 }
Now calculation of the window size will be trivial.
Upvotes: 4
Reputation: 3598
Try this. I believe, you have made four POINTs for RECT, but it needs only LONG.
var ffi = require('ffi');
var ref = require('ref');
var Struct = require('ref-struct');
var lpctstr = {
name: 'lpctstr',
indirection: 1,
size: ref.sizeof.pointer,
get: function(buffer, offset) {
var _buf = buffer.readPointer(offset);
if(_buf.isNull()) {
return null;
}
return _buf.readCString(0);
},
set: function(buffer, offset, value) {
var _buf = ref.allocCString(value, 'ucs2');
return buffer.writePointer(_buf, offset);
},
ffi_type: ffi.types.CString.ffi_type
};
var lpdwordPtr = ref.refType(ref.types.ulong);
var pointStruct = Struct({
'x': ffi.types.ulong,
'y': ffi.types.ulong
});
var rectStruct = Struct({
left : ffi.types.ulong,
top : ffi.types.ulong,
right : ffi.types.ulong,
bottom : ffi.types.ulong
});
var rectPtr = ref.refType(rectStruct);
var user32 = ffi.Library('user32', {
FindWindowW: ['int', [lpctstr, lpctstr]],
GetWindowThreadProcessId: ['int', ['int', lpdwordPtr]],
SetForegroundWindow: ['bool', ['int']],
GetWindowRect: ['bool', ['int',rectStruct]]
});
var hwnd = user32.FindWindowW(null, 'E');
user32.SetForegroundWindow(hwnd);
var Rect = new rectStruct;
var bool = user32.GetWindowRect(hwnd, Rect);
console.log(JSON.stringify(Rect), Rect);
Upvotes: 1