import ffi from "@inigolabs/ffi-napi"; const knl32 = ffi.Library("kernel32.dll", { GetLastError: ["uint32", []], FormatMessageW: [ "uint", ["uint", "pointer", "uint", "uint", "pointer", "uint", "pointer"], ], }); export default function getLastErrMsg() { const errcode = knl32.GetLastError(); let errMsg = ""; if (errcode) { const len = 255; const buf = Buffer.alloc(len); // eslint-disable-next-line no-bitwise const p = 0x00001000 | 0x00000200; // FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS const langid = 0x0409; // 0x0409: US, 0x0000: Neutral locale language const msglen = knl32.FormatMessageW( p, null, errcode, langid, buf, len, null ); errMsg = msglen ? `${errcode}: ${buf.toString("ucs2")}` : `${errcode}: unknown error message`; } return errMsg; }