我正在编写 NPAPI 插件来访问当前页面的 DOM。我能够构建插件。现在我想调用 javascript 函数 console.debug("hello from c++"); 来自 NPAPI 插件。我采用了以下代码,我正在使用来自谷歌的 helloworld 示例代码来构建 npapi 插件:代码:
bool ScriptablePluginObject::Invoke(NPObject* obj, NPIdentifier methodName, const NPVariant* args,uint32_t argCount, NPVariant* result)
{
// The message i want to send.
char* message = "Hello from C++";
// Get window object.
NPObject* window = NULL;
NPN_GetValue(npp_, NPNVWindowNPObject, &window);
// Get console object.
NPVariant consoleVar;
NPIdentifier id = NPN_GetStringIdentifier("console");
NPN_GetProperty(npp_, window, id, &consoleVar);
NPObject* console = NPVARIANT_TO_OBJECT(consoleVar);
// Get the debug object.
id = NPN_GetStringIdentifier("debug");
// Invoke the call with the message!
NPVariant type;
STRINGZ_TO_NPVARIANT(message, type);
NPVariant args[] = { type };
NPVariant voidResponse;
NPN_Invoke(npp_, console, id, args,sizeof(args) / sizeof(args[0]),&voidResponse);
// Cleanup all allocated objects, otherwise, reference count and
// memory leaks will happen.
NPN_ReleaseObject(window);
NPN_ReleaseVariantValue(&consoleVar);
NPN_ReleaseVariantValue(&voidResponse);
}
但是在我调用 test.html 时加载后它崩溃了。请让我知道“我是否在正确的位置调用此代码”和“我如何测试此代码”。
谢谢...