GetWindowTextW を使ってみた
以下のコードで、日本語のウィンドウ名もコマンドプロンプトに
文字化けせずに表示できた。
const ref = require('ref');
const ffi = require('ffi');
const wchar_t = require('ref-wchar');
const wchar_string = wchar_t.string;
const voidPtr = ref.refType(ref.types.void);
const user32 = ffi.Library('user32.dll', {
EnumWindows: ['bool', [voidPtr, 'int32']],
GetWindowTextW : ['long', ['long', wchar_string, 'long']]
});
const windowProc = ffi.Callback('bool', ['long', 'int32'], function(hwnd, lParam) {
const buf = new Buffer(255);
const ret = user32.GetWindowTextW(hwnd, buf, 255);
const s = ref.reinterpretUntilZeros(buf, wchar_t.size);
console.log(wchar_t.toString(s));
return true;
});
user32.EnumWindows(windowProc, 0);
wchar_t*型を使うには
wchar_t*型に関係するコードを抜粋すると以下のとおり。
const wchar_t = require('ref-wchar');
// node側はwchar_t*型の指定をwchar_stringで行う
const wchar_string = wchar_t.string;
// wchar_t*型を扱うAPIの利用準備
const user32 = ffi.Library('user32.dll', {
GetWindowTextW : ['long', ['long', wchar_string, 'long']]
});
// wchar_t*型に対応する変数(というか領域)を用意
const buf = new Buffer(255);
// wchar_t*型を扱うAPIの呼び出し
const ret = user32.GetWindowTextW(hwnd, buf, 255);
// wchar_t*型の変数から文字列を取り出す
const s = ref.reinterpretUntilZeros(buf, wchar_t.size);
console.log(wchar_t.toString(s))