简体   繁体   中英

Using JavaScript in a NodeJS environment, can you find a window handle value of a running application?

I'm writing some tests for my React-Native application (using JS) in a NodeJS environment. In one scenario, I need to attach to an already-running Windows application. In order to attach to this Application, I need to know the NativeWindowHandle value .

For example, if you open Inspect.exe on a window, you'll find the "NativeWindowHandle" hex value.

Is there anyway I can find this value programmatically?

What I've Tried: I'm able to find the PID of the app using:

const exec = require('child_process').exec;
exec('tasklist', function (err, stdout) {
....
}}

However, I haven't been able to turn that into the window handle. Does anyone have any ideas here? Is this possible?

This can be reliably accomplished by writing a native (C++) node addon which calls the appropriate Windows API functions and passes the results back to JS land.

eg you might want to call FindWindowEx and Windows will find and return the HWND (native window handle) of the matching open window. Or use one of the enumeration functions if you need to do the search yourself.

I did a quick search of npm and it looks like there might be a few packages that have done this work already, but you'll need to evaluate them.

If none of the npm packages will work, you'll need to write it yourself. This isn't too hard if you have a little C++ knowledge, but alternatively you might be able to get away with using node-ffi , which lets you write everything in JS and marshals the native calls for you.

(Using ffi will be a little slower than writing the native module yourself, but for your purposes that doesn't really matter. Either native or ffi will be much faster than spawning child processes.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM