我在dll中有这个功能
static COMMANDERDLL_API int InsertCodeBar(const char* pszBuffer);
在我的节点插件中我有这个功能
void InsertCodeBarWrapper(const FunctionCallbackInfo<Value>& args){
Isolate* isolate = args.GetIsolate();
Local<Function> cb = Local<Function>::Cast(args[1]);
Local<String> bar = args[0]->ToString();
const unsigned argc = 1;
Local<Value> argv[argc] = { CSGPCommander::InsertCodeBar(bar) };
cb->Call(isolate->GetCurrentContext()->Global(), argc, argv);
}
当我尝试编译时,node-gyp返回错误:“无法将参数1从'v8 :: Local'转换为'const char *'
如何将v8 :: String转换为const char *?
答案 0 :(得分:3)
解决
创建一个函数ToCString,将V8 :: String转换为const char *
use namespace v8;
const char* ToCString(const String::Utf8Value& value) {
return *value ? *value : "<string conversion failed>";
}
用法:
void InsertCodeBarWrapper(const FunctionCallbackInfo<Value>& args){
Isolate* isolate = args.GetIsolate();
Local<Function> cb = Local<Function>::Cast(args[1]);
String::Utf8Value str(args[0]);
const char* bar = ToCString(str);
const unsigned argc = 1;
Local<Value> argv[argc] = { CSGPCommander::InsertCodeBar(bar) };
cb->Call(isolate->GetCurrentContext()->Global(), argc, argv);
}
答案 1 :(得分:2)
改善@Matheus的回答:
use namespace v8;
const char* ToCString(Local<String> str) {
String::Utf8Value value(str);
return *value ? *value : "<string conversion failed>";
}
直接使用:
const char* bar = ToCString(info[0]);