node-ffi-napi : Promise not resolved inside ffi.Cllback

254 views Asked by At

When I run the following code, I can see that inside self invoking function in ffi.Callback that 'await psList()' promise not resolved. It should be resolved. Why the promise not resolved inside the ffi.Callback, should it be resolved? Is there a way to resolve the promise? What could be the reason? Attaching the script. Using Windows os.

import ref from "ref-napi";
import wchar from "ref-wchar-napi";
// const si = require("systeminformation");
import psList from "ps-list";

let pidA = 0o1;

if (process.platform === "win32") {
    const intPtr = ref.refType(ref.types.int32);
    const msgType = ref.types.void;
    const msgPtr = ref.refType(msgType);
    const EVENT_SYSTEM_FOREGROUND = 3;
    const WINEVENT_OUTOFCONTEXT = 0;
    const WINEVENT_SKIPOWNPROCESS = 2;
    const user32 = ffi.Library("user32", {
        GetForegroundWindow: ["int", []],
        GetWindowThreadProcessId: ["int", [ref.types.int32, intPtr]],
        SetWinEventHook: ["int", ["int", "int", "pointer", "pointer", "int", "int", "int"]],
        GetMessageA: ["bool", [msgPtr, "int", "uint", "uint"]],
        GetWindowTextW: ["int", ["pointer", "pointer", "int"]],
        GetWindowTextLengthW: ["int", ["pointer"]],
    });

    function getMessage() {
        return user32.GetMessageA(ref.alloc(msgPtr), null, 0, 0);
    }

    const pfnWinEventProc = ffi.Callback(
    "void",["pointer", "int", "pointer", "long", "long", "int", "int"],
    function (hWinEventHook, event, hwnd, idObject, idChild, idEventThread, dwmsEventTime) {
        const hwnd2 = user32.GetForegroundWindow();
        const pid = ref.alloc(ref.types.int32);
        const result = user32.GetWindowThreadProcessId(hwnd2, pid);
        const windowTitleLength = user32.GetWindowTextLengthW(hwnd);
        const bufferSize = windowTitleLength * 2 + 4;
        const titleBuffer = Buffer.alloc(bufferSize);
        user32.GetWindowTextW(hwnd, titleBuffer, bufferSize);
        const titleText = ref.reinterpretUntilZeros(titleBuffer, wchar.size);
        const finallyWindowTitle = wchar.toString(titleText);
        pidA = pid.deref();

        (async () => {
            let list = await psList();
            list.forEach((data) => {
                console.log(data);
                data.list.forEach((item) => {
                    if (item.pid === pidA) {
                    console.log(`Foreground window ${JSON.stringify(item)}`);
                    }
                });
            });
        })().catch(err => {
            console.error(err);
        });
        console.log(`timestamp {finallyWindowTitle}, result:${result}, pid: ${pid.deref()}`);
    });

    user32.SetWinEventHook( EVENT_SYSTEM_FOREGROUND,EVENT_SYSTEM_FOREGROUND, null,pfnWinEventProc, 0, 0,
    WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS );

    let res = getMessage();

    while (res != 0) {
        switch (res) {
            case -1:
                console.log("Invalid GetMessageA arguments or something!");
                break;
        default:
            console.log("Got a message!");
        }
        
        res = getMessage();
    }
} else {
    throw new Error("Plat not supported: " + process.platform);
}```
Thanks.
0

There are 0 answers