Nguyen
Nguyen16mo ago

VuNguyen – 09-08 Apr 14

How do get process id using ffi-napi? I'm using this code, which is generated by ChatGPT.
const ffi = require("ffi-napi");
const ref = require("ref-napi");
const iconv = require("iconv-lite");

const user32 = ffi.Library("user32", {
EnumWindows: ["bool", ["pointer", "int32"]],
IsWindowVisible: ["bool", ["int32"]],
GetWindowTextW: ["int", ["int", "pointer", "int"]],
GetWindowThreadProcessId: ["int32", ["int", "pointer"]],
});

const MAX_TITLE_LENGTH = 255;
const windowInfos = [];

const callback = ffi.Callback("bool", ["int32", "int32"], (hwnd, lParam) => {
const isVisible = user32.IsWindowVisible(hwnd);
if (isVisible) {
const buf = Buffer.alloc(MAX_TITLE_LENGTH * 2); // multiply by 2 for wide char support
user32.GetWindowTextW(hwnd, buf, buf.length);
const nullTerminatorIndex = buf.indexOf("\0\0"); // wide char null terminator is two bytes long
const titleBuffer = buf.slice(0, nullTerminatorIndex);
const title = iconv.decode(titleBuffer, "utf-16le").trim();

if (!title) return true;

try {
const pid = ref.alloc("int");
user32.GetWindowThreadProcessId(hwnd, pid);
const id = pid.deref();
const windowInfo = {
id: id,
title: title,
};
windowInfos.push(windowInfo);

console.log(windowInfo);
} catch (error) {
console.error("Error adding window:", error);
}
}
return true;
});

try {
user32.EnumWindows(callback, 0);
} catch (error) {
console.error("Error enumerating windows:", error);
}

console.log(windowInfos);
const ffi = require("ffi-napi");
const ref = require("ref-napi");
const iconv = require("iconv-lite");

const user32 = ffi.Library("user32", {
EnumWindows: ["bool", ["pointer", "int32"]],
IsWindowVisible: ["bool", ["int32"]],
GetWindowTextW: ["int", ["int", "pointer", "int"]],
GetWindowThreadProcessId: ["int32", ["int", "pointer"]],
});

const MAX_TITLE_LENGTH = 255;
const windowInfos = [];

const callback = ffi.Callback("bool", ["int32", "int32"], (hwnd, lParam) => {
const isVisible = user32.IsWindowVisible(hwnd);
if (isVisible) {
const buf = Buffer.alloc(MAX_TITLE_LENGTH * 2); // multiply by 2 for wide char support
user32.GetWindowTextW(hwnd, buf, buf.length);
const nullTerminatorIndex = buf.indexOf("\0\0"); // wide char null terminator is two bytes long
const titleBuffer = buf.slice(0, nullTerminatorIndex);
const title = iconv.decode(titleBuffer, "utf-16le").trim();

if (!title) return true;

try {
const pid = ref.alloc("int");
user32.GetWindowThreadProcessId(hwnd, pid);
const id = pid.deref();
const windowInfo = {
id: id,
title: title,
};
windowInfos.push(windowInfo);

console.log(windowInfo);
} catch (error) {
console.error("Error adding window:", error);
}
}
return true;
});

try {
user32.EnumWindows(callback, 0);
} catch (error) {
console.error("Error enumerating windows:", error);
}

console.log(windowInfos);
It works, but it just throw the error below randomly after getting some process id. I believe that this error is related to memory allocations.
#
# Fatal error in , line 0
# Check failed: result.second.
#
#
#
#FailureMessage Object: 000000B259F4CD70
1: 00007FF68D20BD2F v8::internal::CodeObjectRegistry::~CodeObjectRegistry+112159
2: 00007FF68D12B11F v8::CFunctionInfo::HasOptions+7039
3: 00007FF68DDFFD72 V8_Fatal+162
4: 00007FF68D855EBD v8::internal::BackingStore::Reallocate+621
5: 00007FF68DAC4149 v8::ArrayBuffer::GetBackingStore+137
6: 00007FF68D1DFDB9 napi_get_typedarray_info+393
7: 00007FFE9BA18828
8: 00007FFE9BA17C19
9: 00007FFE9BA1D003
10: 00007FFE9BA1ED79
11: 00007FFE9BA1DD09
12: 00007FFE9BA1949E
13: 00007FFE9BA180B7
14: 00007FFE9BA1EF63
15: 00007FF68D1DA696 node::Stop+36246
16: 00007FF68DA92F10 v8::internal::Builtins::builtin_handle+320880
17: 00007FF68DA9251F v8::internal::Builtins::builtin_handle+318335
18: 00007FF68DA9280F v8::internal::Builtins::builtin_handle+319087
19: 00007FF68DA92653 v8::internal::Builtins::builtin_handle+318643
... So on
#
# Fatal error in , line 0
# Check failed: result.second.
#
#
#
#FailureMessage Object: 000000B259F4CD70
1: 00007FF68D20BD2F v8::internal::CodeObjectRegistry::~CodeObjectRegistry+112159
2: 00007FF68D12B11F v8::CFunctionInfo::HasOptions+7039
3: 00007FF68DDFFD72 V8_Fatal+162
4: 00007FF68D855EBD v8::internal::BackingStore::Reallocate+621
5: 00007FF68DAC4149 v8::ArrayBuffer::GetBackingStore+137
6: 00007FF68D1DFDB9 napi_get_typedarray_info+393
7: 00007FFE9BA18828
8: 00007FFE9BA17C19
9: 00007FFE9BA1D003
10: 00007FFE9BA1ED79
11: 00007FFE9BA1DD09
12: 00007FFE9BA1949E
13: 00007FFE9BA180B7
14: 00007FFE9BA1EF63
15: 00007FF68D1DA696 node::Stop+36246
16: 00007FF68DA92F10 v8::internal::Builtins::builtin_handle+320880
17: 00007FF68DA9251F v8::internal::Builtins::builtin_handle+318335
18: 00007FF68DA9280F v8::internal::Builtins::builtin_handle+319087
19: 00007FF68DA92653 v8::internal::Builtins::builtin_handle+318643
... So on
5 Replies
Nguyen
Nguyen16mo ago
These 3 lines of code caused the problem, but I have no idea how to fix it
const pid = ref.alloc("int");
user32.GetWindowThreadProcessId(hwnd, pid);
const id = pid.deref();
const pid = ref.alloc("int");
user32.GetWindowThreadProcessId(hwnd, pid);
const id = pid.deref();
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Nguyen
Nguyen16mo ago
hmm seen few issues like this and there are no solutions on there Okay I just upgraded node to v18 and it worked <:dKN_weird4:1079865640141193276>
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
reactibot
reactibot16mo ago
This thread hasn’t had any activity in 36 hours, so it’s now locked. Threads are closed automatically after 36 hours. If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/1096361353993793536