node-ffiでWindows APIでUnicode版のAPIを使って文字列を指定する

node-ffiでWindows APIでUnicode版のAPIを使って文字列を取得する - Qiitaの反対に、UNICODE版のAPIに文字列を指定して呼び出す

const ref = require('ref');
const ffi = require('ffi');
const wchar_t = require('ref-wchar');
const {Iconv} = require('iconv');

const wchar_string = wchar_t.string;
const voidPtr = ref.refType(ref.types.void);

const user32 = ffi.Library('user32.dll', {
    EnumWindows: ['bool', [voidPtr, 'int32']],
    IsWindowVisible: ['bool', ['long']],
    FindWindowW: ['long',[wchar_string, wchar_string]],
    GetWindowTextW: ['long', ['long', wchar_string, 'long']]
});

const windowProc =
    ffi.Callback('bool', ['long', 'int32'], function (hwnd, lParam) {
        if (user32.IsWindowVisible(hwnd)) {
            const buf = Buffer.alloc(255);
            const ret = user32.GetWindowTextW(hwnd, buf, 255);
            const s = ref.reinterpretUntilZeros(buf, wchar_t.size);
            const title = wchar_t.toString(s);
            if (title.length > 0) {
                console.log(title);
            }
        }
        return true;
    });

//user32.EnumWindows(windowProc, 0);
const title = 'ピクチャ'; // <- 適当なタイトル名で
myconv = new Iconv('UTF-8','UTF-16LE');
rmyconv = new Iconv('UTF-16LE','UTF-8');
const _buf = myconv.convert(title);
const __buf = rmyconv.convert(_buf);
console.log(__buf.toString());
wtitle=_buf;
const hwnd = user32.FindWindowW(null,wtitle);
console.log(hwnd);
const buf = Buffer.alloc(255);
const ret = user32.GetWindowTextW(hwnd, buf, 255);
const s = ref.reinterpretUntilZeros(buf, wchar_t.size);
console.log(wchar_t.toString(s));