node.js addon - how to pass a string parameter to (nan) C++

4.5k views Asked by At

My node.js code does this :

var MyMQ = require( './build/release/mqconn' ) ;
var MyQmgrName = 'QM_CNT' ; // req.params.qmgrname ; 
MyMQ.connect ( MyQmgrName, function ( err, result ) { 

And my C++ code (uning nan) wants to receive the first string parameter :

NAN_METHOD( MQ_Connect ) {

    NanScope();
    Local<Value> szQMN( args[ 0 ] );
    printf( "(cc)>>>> qmn [%s].\n", szQMN ) ;

... but what I get is garbage.

Any clue on what am I doing wrong ? Sebastian.

1

There are 1 answers

0
mscdex On

First you should generally validate your arguments. Then you can get a string by calling ToString() on the argument. For example:

NAN_METHOD(MQ_Connect) {
  NanScope();

  if (args.Length() > 0) {
    if (args[0]->IsString()) {
      String::Utf8Value str(args[0]->ToString());
      printf("(cc)>>>> qmn [%s].\n", (const char*)(*str));
    }
  }