You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Casting to NSView* from getNativeWindowHandle() #7460
I've been trying to get NSView* from getNativeWindowHandle() in a C++ addon, as the docs suggest the handle is NSView* on MacOS, but with no luck, I've been getting seg faults. I've seen #4369 and thought maybe the pointer returned on Mac is invalid too ? If someone did this successfully, I'd like to know how you can retrieve NSView* from getNativeWindowHandle().
v8::Local<v8::Object> bufferObj = info[0].As<v8::Object>();
unsigned char* bufferData = (unsigned char*)node::Buffer::Data(bufferObj);
NSView* view = reinterpret_cast<NSView*>(bufferData);
//Seg fault if you try to do anything with the NSView*
I've converted the buffer to hex using toString('hex') and then print hex on C++ side to verify that they are the same.
NSLog prints 2016-10-03 03:40:33.734 node[1816:18569] <50255153 e07f0000>,
and the developer console when you type require('electron').remote.getCurrentWindow().getNativeWindowHandle().toString('hex') prints 50255153e07f0000.
Note that everything works if I short-circuit the C++ function by setting the result and jumping to set_ret (meaning the result shows up in the HTML view).
How is anyone getting this to work?
Worth mentioning this is impossible to debug seeing as how Electron outputs nothing to the terminal and LLDB can't attach to it.
@arkenthera Doesn't work for me, same thing happens.
Is this because I'm trying to use the NSView* from within the browser window using require('electron').remote? Is that not supported? I didn't think you could modify the window of another process (since electron is multi-process, right?) directly using a pointer to an NSView on MacOS, hence why I'm doing it this way. The documentation doesn't specify either way.
EDIT: Yeah getNativeWindowHandle() should only be called by the main electron process. It cannot be called from the browser windows (well, it can, but you can't use the result reliably). This seems like something that should be included in the documentation. Would you agree @zcbenz?
It's not, though. I was under the assumption that, like Chrome, each window is created in its own process. That would mean there's a good change the NSWindow is created in the child process, thus resulting in a process-specific pointer that can't be shared like a windows HWND or something.
Further, I didn't know that cocoa windows could be modified between processes, since the main process can still manipulate the child process's window after it forks. Hence why I thought it was a pretty natural assumption that you had to use the pointer from within the browser process.
I always thought like if we are creating a BrowserWindow on the main process' js file, then the actual pointer address will be in main process as well, thus calling it obvious.
@arkenthera but like I said, it's not obvious - because that's not how pointers work. The window belongs to the child process, so referencing the window handle by pointer in the parent process shouldn't work since the window isn't in the parent process.
There's some other sort of IPC going on here.
EDIT: and even if the window is created prior to forking, which means the pointer would be valid in the child process, you'd expect that the pointer would work if referenced in the child process too. It does not. So I'm not sure how it's working.
error: reinterpret_cast from 'Napi::Buffer<unsigned char>' to 'void **' is not allowed
void **handle = reinterpret_cast<void **>(buffer);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
However, I managed with a lot of try and error to produce a solution that works! (tested with "node-addon-api": "^6.0.0"):
It should be able to return the title of your BrowserWindow without crashing: console.log( nativeAddon.GetWindowTitle(window.getNativeWindowHandle()) );
Activity
zcbenz commentedon Oct 20, 2016
I think it should be
NSView* view = *reinterpret_cast<NSView**>(bufferData).Qix- commentedon Sep 9, 2018
Sorry to necro but this doesn't work. The following test code compiles fine but segfaults.
module/binding.gypmodule/hello.mmmodule/index.jsapp/index.htmlNote that everything works if I short-circuit the C++ function by setting the
resultand jumping toset_ret(meaning the result shows up in the HTML view).How is anyone getting this to work?
Worth mentioning this is impossible to debug seeing as how Electron outputs nothing to the terminal and LLDB can't attach to it.
0x61726b commentedon Sep 9, 2018
Can you try my native module and see if it works? Because it worked unless a recent electron update broke it?
https://github.com/arkenthera/electron-vibrancy/
Qix- commentedon Sep 9, 2018
@arkenthera Doesn't work for me, same thing happens.
Is this because I'm trying to use the
NSView*from within the browser window usingrequire('electron').remote? Is that not supported? I didn't think you could modify the window of another process (since electron is multi-process, right?) directly using a pointer to anNSViewon MacOS, hence why I'm doing it this way. The documentation doesn't specify either way.EDIT: Yeah
getNativeWindowHandle()should only be called by the main electron process. It cannot be called from the browser windows (well, it can, but you can't use the result reliably). This seems like something that should be included in the documentation. Would you agree @zcbenz?0x61726b commentedon Sep 9, 2018
I think it's kind of obvious that the actual OS window that is created is done so in the main process.
Qix- commentedon Sep 9, 2018
It's not, though. I was under the assumption that, like Chrome, each window is created in its own process. That would mean there's a good change the
NSWindowis created in the child process, thus resulting in a process-specific pointer that can't be shared like a windowsHWNDor something.Further, I didn't know that cocoa windows could be modified between processes, since the main process can still manipulate the child process's window after it forks. Hence why I thought it was a pretty natural assumption that you had to use the pointer from within the browser process.
0x61726b commentedon Sep 9, 2018
I always thought like if we are creating a
BrowserWindowon the main process' js file, then the actual pointer address will be in main process as well, thus calling it obvious.Qix- commentedon Sep 9, 2018
@arkenthera but like I said, it's not obvious - because that's not how pointers work. The window belongs to the child process, so referencing the window handle by pointer in the parent process shouldn't work since the window isn't in the parent process.
There's some other sort of IPC going on here.
EDIT: and even if the window is created prior to forking, which means the pointer would be valid in the child process, you'd expect that the pointer would work if referenced in the child process too. It does not. So I'm not sure how it's working.
Lijian1122 commentedon Jan 26, 2022
Hi, Has the problem been solved? If so, could you please tell me how it was solved? Thank you very much. I am looking forward for your reply.
nornagon commentedon Jan 26, 2022
If using a module like
objc, see lukaskollmer/objc#16 (comment):BB-fat commentedon Mar 29, 2022
This is my solution.
ZeroMax-dev commentedon Mar 28, 2023
@BB-fat Your solution gives me an
However, I managed with a lot of try and error to produce a solution that works! (tested with
"node-addon-api": "^6.0.0"):It should be able to return the title of your BrowserWindow without crashing:
console.log( nativeAddon.GetWindowTitle(window.getNativeWindowHandle()) );lockieluke commentedon May 20, 2023
i'm trying to pass the C++ value to Swift