ffi查找窗口句柄
第一种写法:
function u16ptr(s) {
const t = ffi.Buffer("u16string")
const b = new t()
b.write(s)
const v = new Uint32Array(b)
return FFI.BigIntToPointer(v[0])
}
function findWindowExample(){
const { FindWindowExW } = ffi.LoadLibrary("user32", {
FindWindowExW: { returnType: "uint32",parameters: [ "uint32", "uint32", "pointer", "pointer" ]}
})
console.log(FindWindowExW(0, 0, null, u16ptr("WPS 宏编辑器")))
}第二种写法:
function findWindowExample() {
let { FindWindowW } = ffi.LoadLibrary("user32", {
FindWindowW: {returnType: "uint32", parameters: ["pointer","pointer"]} });
let mtitle = "无标题 - Notepad";
let mclass = "Notepad";
let buft = strToArr(mtitle);
let bufc = strToArr(mclass);
let Hwnd = FindWindowW(bufc.buffer, buft.buffer);
console.log(Hwnd);
}
function strToArr(str) {
const l = str.length;
const int16Arr = new Int16Array(l+2);
for (let i = 0; i < l; i++) {
int16Arr[i] = str.charCodeAt(i);
}
return int16Arr;
}在jsaIDE环境下,winAPI函数尽量用W类型名称的。目前看,比A的好调用一些。
创作者俱乐部成员