简体   繁体   中英

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

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.

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));
    }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM