Skip to content

Commit 6dc5f11

Browse files
committed
fix: getting window process path on Windows
1 parent d6fe961 commit 6dc5f11

File tree

5 files changed

+19
-34
lines changed

5 files changed

+19
-34
lines changed

example.js

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,4 @@ console.time("getActiveWindow");
44
const window = windowManager.getActiveWindow();
55
console.timeEnd("getActiveWindow");
66

7-
console.time("getTitle");
8-
console.log(window.getTitle());
9-
console.timeEnd("getTitle");
10-
11-
console.time("getBounds");
12-
console.log(window.getBounds());
13-
console.timeEnd("getBounds");
14-
15-
console.time("setBounds");
16-
window.setBounds({ x: 0, y: 0 });
17-
console.timeEnd("setBounds");
18-
19-
windowManager.on("window-activated", window => {
20-
console.log(window.getTitle());
21-
});
22-
23-
console.log(windowManager.getWindows());
7+
console.log(window.path);

lib/windows.cc

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,15 @@ Process getWindowProcess(HWND handle) {
4242
DWORD pid{0};
4343
GetWindowThreadProcessId(handle, &pid);
4444

45+
HANDLE pHandle{OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, pid)};
46+
4547
DWORD dwSize{MAX_PATH};
4648
wchar_t exeName[MAX_PATH]{};
4749

48-
QueryFullProcessImageNameW(handle, 0, exeName, &dwSize);
50+
QueryFullProcessImageNameW(pHandle, 0, exeName, &dwSize);
4951

50-
std::wstring wspath(exeName);
51-
std::string path(wspath.begin(), wspath.end());
52+
auto wspath(exeName);
53+
auto path = toUtf8(wspath);
5254

5355
return {static_cast<int>(pid), path};
5456
}
@@ -186,11 +188,11 @@ Napi::Boolean setWindowBounds(const Napi::CallbackInfo &info) {
186188
Napi::Env env{info.Env()};
187189

188190
Napi::Object bounds{info[1].As<Napi::Object>()};
191+
auto handle{getValueFromCallbackData<HWND>(info, 0)};
189192

190-
BOOL b{MoveWindow(getValueFromCallbackData<HWND>(info, 0),
191-
bounds.Get("x").ToNumber(), bounds.Get("y").ToNumber(),
192-
bounds.Get("width").ToNumber(),
193-
bounds.Get("height").ToNumber(), true)};
193+
BOOL b{MoveWindow(
194+
handle, bounds.Get("x").ToNumber(), bounds.Get("y").ToNumber(),
195+
bounds.Get("width").ToNumber(), bounds.Get("height").ToNumber(), true)};
194196

195197
return Napi::Boolean::New(env, b);
196198
}

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
"files": [
4141
"dist/",
4242
"binding.gyp",
43-
"compile.js",
4443
"lib/"
4544
]
4645
}

src/classes/window.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ export class Window {
5353
if (platform() === "win32") {
5454
const sf = windowManager.getScaleFactor(this.getMonitor());
5555

56-
bounds.x = Math.round(bounds.x / sf);
57-
bounds.y = Math.round(bounds.y / sf);
58-
bounds.width = Math.round(bounds.width / sf);
59-
bounds.height = Math.round(bounds.height / sf);
56+
bounds.x = Math.floor(bounds.x / sf);
57+
bounds.y = Math.floor(bounds.y / sf);
58+
bounds.width = Math.floor(bounds.width / sf);
59+
bounds.height = Math.floor(bounds.height / sf);
6060
}
6161

6262
return bounds;
@@ -70,10 +70,10 @@ export class Window {
7070
if (platform() === "win32") {
7171
const sf = windowManager.getScaleFactor(this.getMonitor());
7272

73-
newBounds.x = Math.round(newBounds.x * sf);
74-
newBounds.y = Math.round(newBounds.y * sf);
75-
newBounds.width = Math.round(newBounds.width * sf);
76-
newBounds.height = Math.round(newBounds.height * sf);
73+
newBounds.x = Math.floor(newBounds.x * sf);
74+
newBounds.y = Math.floor(newBounds.y * sf);
75+
newBounds.width = Math.floor(newBounds.width * sf);
76+
newBounds.height = Math.floor(newBounds.height * sf);
7777

7878
addon.setWindowBounds(this.id, newBounds);
7979
} else if (platform() === "darwin") {

0 commit comments

Comments
 (0)