Using getNativeWindowHandle in a native app


#1

I’m writing a native app on Windows, and this is the first native app I’m writing. The reason I’m writing this app is because I need to call a win32 API and using the Window handle.

I’m not sure how to get the HWND value into my native app from getNativeWindowHandle()

Basically, what I want is this, in my js file, I have the following line (With win being a BrowserWindow):

addonFunction(win.getNativeWindowHandle());

What I’m asking for is in my C++ file, how do I use it like this, where winAPIFunction is a function that takes a HWND parameter?

winAPIFunction(arg[0]);

#2

@miquelfire Have you resolved it? I will do the same thing. I think you should bind c++ to JS or execute JS code in cpp with JS bindings.


#3

I haven’t resolved it yet. (I thought I did at one point, but didn’t help that it seems there was a bug with the getNativeWindowHandle function, so I may have gotten right, but the value was wrong, either because of that bug, or I actually did it wrong. Unfortunately, all code I had is gone now as the C++ was under source control yet when I had something)

At the state I’m in, it seems it would be easier to ditch Electron and use something like QT, as my knowledge of using C++ with JS is limited enough that what I want might be too advanced for me to handle with Electron.


#4

The release note of v0.36.8 said: Fix BrowserWindow.getNativeWindowHandle() returning corrupted buffer. So i think you should use v0.36.8 or upper.


#5

The question I’m asking is still the same. I’m already using at least that version now. When I might have had working code, it may have been because I was waiting for that version, or I did wrong anyway.


#6

@miquelfire May be you can use pepper plugin for your native code. But the communication between c++ and JS is asynchronous.


#7

The nature of the code I want to write happens to be in my first post. Switching to the plugin would mean I need to write even more code than I am now.

This is a case where the documentation is not giving enough info for me to know what to do. As of now, I feel like I bought some furniture at IKEA and it doesn’t have instructions (or at least, the instructions I got is not helpful, and I am unable to know if I’m doing it right)

The documentation I’m able to read basically tells me to use a ->toType() function, but there’s no toHWND() function I can use.


#8

Hi, did you manage to fix the issue? cuz I am facing the same problem.


#9

I basically gave up. I found I could do what I wanted by using edge.js
instead.


#10

I noticed that getNativeWindowHandle() returns the buffer which contains HWND in reversed.
e.g.
if my creating window HWND is 000906C4.
window.getNativeWindowHandle() returns me “c406090000000000” when i called

let hwnd = mainWindow.getNativeWindowHandle();
hwnd.toString();

this maybe the reason it did not work for you all cases.


#11

I managed to find it online.

v8::Localv8::Object bufferObj = args[0].Asv8::Object();
unsigned char *bufferData = (unsigned char *)node::Buffer::Data(bufferObj);
unsigned long handle = *reinterpret_cast<unsigned long *>(bufferData);
HWND hwnd = (HWND)handle;