0

I'm trying to write a C++ library, using v8, for node.js. My only goal is to allow javascript to call EnumWindows, the win32 api function.

The EnumWindows method itself takes a callback function as a parameter. It will call that function for every enumerated window, passing it the window handle.

I'm trying to make it so that it calls a javascript function for every window handle, as well. Any ideas how to do this? libuv looked promising, but that looks like I have to be the one to be creating the thread. That's not the case here.

Ryan
  • 7,733
  • 10
  • 61
  • 106

1 Answers1

1

Use uv_async_init() and uv_async_send(). You can attach your own data pointer to the uv_async_t's data member (e.g. uv_async_t foo; foo.data = someptr;). This is where you could store any data you need (e.g. information about the enumerated windows in your case) when signalling the main thread with uv_async_send().

Once inside the uv_async callback on the main thread, you can read from the same data member and call to to javascript with the v8 API.

mscdex
  • 104,356
  • 15
  • 192
  • 153
  • you the man. ive accepted your answer, but ive since learned that the `EnumWindows` callback is not called in new thread. would this simplify things? i was just now going to pass in the callback function to the callback and call it each time, somehow. – Ryan Apr 22 '14 at 05:55
  • If you are absolutely sure it will always be called on the main node thread, then yes you should be able to make the v8 calls directly. However it's probably better to collect the window information in an array first and then call the JS callback. Not only does this ensure that a callback is only called once (unlike an event emitter), but then you only cross the C++<->JS boundary once, which is more performant. – mscdex Apr 22 '14 at 16:56