4

I am building a Windows Electron app that will move and resize an active window.
I am using ffi-napi to access user32 specific functions such as GetForegroundWindow, ShowWindow, & SetWindowPos.

const ffi = require('ffi-napi');

// create foreign function
const user32 = new ffi.Library('user32', {
  'GetForegroundWindow': ['long', []],
  'ShowWindow': ['bool', ['long', 'int']],
  'SetWindowPos': ['bool', ['long', 'long', 'int', 'int', 'int', 'int', 'uint']]
});

// get active window
const activeWindow = user32.GetForegroundWindow();
// force active window to restore mode
user32.ShowWindow(activeWindow, 9);
// set window position
user32.SetWindowPos(
  activeWindow,
  0,
  0, // 0 left have margin on left 
  0, // 0 top have margin on top 
  1024,
  768,
  0x4000 | 0x0020 | 0x0020 | 0x0040
);

Now to my problem
I need to get the active window dimension. I am searching the web and I found GetWindowRect.
The problem is when I add it to the user32 functions, I am not sure what is the 2nd param (RECT) requires.

// create foreign function
const user32 = new ffi.Library('user32', {
  'GetForegroundWindow': ['long', []],
  'ShowWindow': ['bool', ['long', 'int']],
+ 'GetWindowRect': ['bool', ['int', 'rect']],
  'SetWindowPos': ['bool', ['long', 'long', 'int', 'int', 'int', 'int', 'uint']]
});
...
// get active window dimensions
user32.GetWindowRect(activeWindow, 0);
...

This is the error I am getting:

A javascript error occurred in the main process

Uncaught Exemption:
TypeError: error setting argument 2 - writePointer: Buffer instance expected as
third argument at Object.writePointer

Hoping anyone can help me. Thank you in advance.

Neo Genesis
  • 922
  • 11
  • 26
  • Looks like you have to pass a "RECT" instance as 2 argument: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getwindowrect#parameters – Marc Oct 18 '20 at 19:26
  • I tried creating a rect by `const rect = { top: 0, left: 0, right: 0, bottom: 0 };` and assign it to **GetWindowRect** like `user32.GetWindowRect(handle, rect);` same error is showing – Neo Genesis Oct 18 '20 at 20:08
  • Thats not a buffer, try it with a buffer instance, but i have no clue what size it should be: https://learn.microsoft.com/en-us/windows/win32/api/windef/ns-windef-rect Perhaps there is a method for creating a "RECT" structure? – Marc Oct 18 '20 at 20:09

1 Answers1

5

This is how I solve my issue

...
// create rectangle from pointer
const pointerToRect = function (rectPointer) {
  const rect = {};
  rect.left = rectPointer.readInt16LE(0);
  rect.top = rectPointer.readInt16LE(4);
  rect.right = rectPointer.readInt16LE(8);
  rect.bottom = rectPointer.readInt16LE(12);
  return rect;
}

// obtain window dimension
const getWindowDimensions = function (handle) {
  const rectPointer = Buffer.alloc(16);
  const getWindowRect = user32.GetWindowRect(handle, rectPointer);
  return !getWindowRect
    ? null
    : pointerToRect(rectPointer);
}

// get active window
const activeWindow = user32.GetForegroundWindow();

// get window dimension
const activeWindowDimensions = getWindowDimensions(activeWindow);

// get active window width and height
const activeWindowWidth = activeWindowDimensions.right - activeWindowDimensions.left;
const activeWindowHeight = activeWindowDimensions.bottom - activeWindowDimensions.top;
...

I use this code on my small project called Sōkan.

Neo Genesis
  • 922
  • 11
  • 26