0

我必须在我的项目的 C++ 应用程序中导入一个 NPAPI DLL。我遵循优秀的教程:http ://colonelpanic.net/2009/03/building-a-firefox-plugin-part-one/

但是我在访问 dll 的方法时遇到了一些麻烦。(注意:在浏览器中,dll 是完全正常的)。在调用主要函数:NP_GetEntryPoints 和 NP_Initialize 并检索 ScriptableNPObject 后,方法的调用没有返回值,没有错误。属性或方法名称与浏览器中的 javascript 中使用的相同(功能案例)。

有关信息,此示例中的属性和方法名称以及 mime 类型已被替换。

任何人都知道通过模拟浏览器的功能来调用 dll 的方法吗?

这是主程序的一部分:

if (hDLL == 0)
{
    std::cout << "DLL failed to load!" << std::endl;
}
else
{
    std::cout << "DLL loaded!" << std::endl;

    //WRAP NP_GETENTRYPOINTS FUNCTION:
    _GetEntryPointsFunc = (GetEntryPointsFunc)GetProcAddress(hDLL, "NP_GetEntryPoints");
    if (_GetEntryPointsFunc)
    {
        std::cout << "Get NP_GetEntryPoints Function!" << std::endl;
        status = _GetEntryPointsFunc(pFuncs);

    }

    //WRAP NP_INITIALIZE FUNCTION:
    _InitializeFunc = (InitializeFunc)GetProcAddress(hDLL, "NP_Initialize");
    if (_InitializeFunc)
    {   
        std::cout << "Get NP_Initialize Function!" << std::endl;
        status = _InitializeFunc(&sBrowserFuncs);
    }


    int32_t       mode = NP_EMBED; 
    int32_t       argc = 7;
    static const char mimetype[] = "application/x-mime_type_of_my_plugin";

    char * argn[] = {"param1", "param2", "param3", "param4", "param5", "param6", "param7"};
    char * argv[] = { "value1", "value2", "value3", "value4", "value5", "value6", "value7" };



    NPObject np_object;
    uint16_t size;
    char* descritpionString;
    char* nameString;

  instance = &(plugin_instance.mNPP);
    status = pFuncs->newp((char*)mimetype, instance, (uint16_t)mode, argc, argn, argv, &saved);
    status = pFuncs->version; //OK  
    status = pFuncs->getvalue(instance,NPPVpluginDescriptionString,&descritpionString); //OK
    status = pFuncs->getvalue(instance,NPPVpluginNameString,&nameString); //OK

    status = pFuncs->getvalue(instance,NPPVpluginScriptableNPObject,&np_object); //ISSUE STARTS HERE

    std::cin.get();

}

这是使用 getvalue 函数获取可编写脚本的 NPObject 后调用的 create_object 函数:

NPObject* _createobject(NPP npp, NPClass* aClass)
{

if (!npp) {
    return nullptr;
}

if (!aClass) {
    return nullptr;
}

NPObject *npobj;

if (aClass->allocate) {
    npobj = aClass->allocate(npp, aClass);
} else {
    npobj = (NPObject *)malloc(sizeof(NPObject));
}

if (npobj) {
    npobj->_class = aClass;
    npobj->referenceCount = 1;

    //TEST:
            NPError status;
    NPString url;
    NPVariant result;
    NPVariant variant;
    NPIdentifier property = "existing_property";
    NPIdentifier *arrayId;
    uint32_t count = 2;

    const char *str = "https://test_url.com";
    url.UTF8Characters = str;//;
    url.UTF8Length = 20;

    variant.type = NPVariantType_String;
    variant.value.stringValue = url;
    NPVariant args[] = { variant };

    status = 1; //GENERIC ERROR VALUE
    status = npobj->_class->structVersion; //OK
    status = npobj->_class->hasMethod(npobj,L"existing_set_function"); //STATUS OK
    status = npobj->_class->enumerate(npobj, &arrayId, &count); //Privileged instruction ERROR
    status = npobj->_class->hasProperty(npobj, property); //STATUS OK
    status = npobj->_class->getProperty(npobj, property, &result); //STATUS OK BUT NO RESULT
    status = npobj->_class->invoke(npobj,L"existing_set_function",args,1,&result); //STATUS OK
    status = npobj->_class->invoke(npobj,L"existing_get_function",args,0,&result); //STATUS OK BUT NO RESULT
    status = npobj->_class->invokeDefault(npobj,args,0,&result); //STATUS OK BUT NO RESULT
    //END TEST
}
return npobj;
}

最后,这里是声明 ndata 和 pdata 的 plugin_instance 方法:

nsNPAPIPluginInstance::nsNPAPIPluginInstance()
{
  mNPP.pdata = NULL;
  mNPP.ndata = this;
}

nsNPAPIPluginInstance::~nsNPAPIPluginInstance()
{

}

提前致谢。

4

1 回答 1

0

虽然不清楚“无结果”是什么意思,但特权指令错误表明函数指针已关闭。

我会开始:

  • 检查structVersion >= NP_CLASS_STRUCT_VERSION_ENUM(否则NPClass::enumerate 不可用
  • 使用正确的npapi-sdk 标头- 您的函数指针类型名称表明您没有这样做
  • 使用一个简单的测试插件来看看插件端发生了什么
  • 注意NPIdentifier主机应用程序和插件之间的 s 匹配,即对主机代码使用通用字符串->NPIdentifier 映射,并且NPN_GetStringIdentifier()- 正如发布的那样,这是行不通的
  • 不要在创建功能中测试权限 - 插件可能会在返回NPObject后才能正常工作NPN_CreateObject()
于 2013-09-17T11:07:44.133 回答