angular-electron icon indicating copy to clipboard operation
angular-electron copied to clipboard

calling c++ addons (.node generated file)

Open dali-gharbi opened this issue 8 years ago • 4 comments

Hi i want to call a c++ function from angular service. i have the .node file already generated.

i added a folder named cpp-addons in src folder containing this c++ file

// addon.cc
#include <v8.h>
#include <v8-debug.h>
#include <node.h>
#include <windows.h>
namespace demo
{

using v8::Function;
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Null;
using v8::Object;
using v8::String;
using v8::Value;

void RunCallback(const FunctionCallbackInfo<Value> &args)
{
  Isolate *isolate = args.GetIsolate();
  Local<Function> cb = Local<Function>::Cast(args[0]);
  const unsigned argc = 1;
  Local<Value> argv[argc] = {String::NewFromUtf8(isolate, "pause/play")};
  keybd_event(VK_MEDIA_PLAY_PAUSE, 0x8f, 0, 0);               // PLAY/PAUSE Press
  keybd_event(VK_MEDIA_PLAY_PAUSE, 0x8f, KEYEVENTF_KEYUP, 0); // PLAY/PAUSE Release
  cb->Call(Null(isolate), argc, argv);
}

void Init(Local<Object> exports, Local<Object> module)
{
  NODE_SET_METHOD(module, "exports", RunCallback);
}

NODE_MODULE(NODE_GYP_MODULE_NAME, Init)

} // namespace demo

at the root of the project i added addons folder wich contain

binding.gyp :

{
  "targets": [
    {
      "target_name": "keybd_event",
      "sources": [ "../src/cpp-addons/keybd_event.cpp" ]
    }
  ]
}

and keybd_event.js

modue.exports = require('./build/Release/keybd_event.node'); i generated the .node file and called it using node test.js and it's working fine but when i try to import or require the keybd_event i got cannot find module error ..

thanks .

dali-gharbi avatar Jan 27 '18 03:01 dali-gharbi

Hey, did you get anywhere with this?

sublimator avatar Jul 11 '18 05:07 sublimator

Hi, any solution for this?

sanchezpablo avatar Jul 13 '18 11:07 sanchezpablo

any progress? unable to add native modules for electron.

calidion avatar Jul 14 '18 20:07 calidion

it worked for me using c++ code to pause / play sound and auto shutdown computer. the only problem was when i use the app on different computer who is not running the same nodejs version : you can find here the v8 based solution : https://github.com/dali-gharbi/angular-electron/tree/master/keybd_event

dali-gharbi avatar Jul 18 '18 13:07 dali-gharbi