Scriptable plugin: Error calling method on NPObjec

2019-09-18 14:01发布

问题:

I am getting a JavaScript error: Error calling method on NPObject when calling a method in my NPAPI plugin in Chrome & Firefox on XP. Running the same code on Windows 7 with the same browsers was successful.

I have built a Scriptable plugin using the NPAPI, so far I can debug into the Invoke method of my scriptable object. But I don't believe I have any control after it's finished.
Does anyone have any ideas? Is this an issue only in Windows XP?

bool MY_ScriptableObject::Invoke(NPObject*        npobj,   
                                 NPIdentifier     name,    
                                 const NPVariant* args,    
                                 uint32_t         argCount,
                                 NPVariant*       result)  
{ 
bool                    rc   = true;
char*                   wptr = NULL;

    rc = false;
    wptr = NULL;

    if  (name == NPN_GetStringIdentifier("getVersion"))
        {
        wptr = (NPUTF8*)NPN_MemAlloc(strlen("version:1.0.1") + 1); //Should be freed by browser
        if  (wptr != NULL)                                                      
            {
            rc = true;
            memset(wptr,
                   0x00,
                   strlen("version:1.0.1")+1);
            memcpy(wptr,
                   "version:1.0.1",
                   strlen("version:1.0.1"));
            STRINGZ_TO_NPVARIANT(wptr,
                                 *result);
            }
        }
    return (rc);
}

Here is the HTML function that I am executing:

function Version()
{
var plugin = document.getElementById("plugin");
if (plugin == undefined)
    {
    alert("plugin failed");
    return;
    }
var text = plugin.getVersion();  //Error happens at this line
alert(text);
}

回答1:

The (sarcasm)awesome(/sarcasm) thing about NPAPI in current versions of the browsers is that if anything goes wrong with the call you automatically get that error message, even if the plugin has otherwise tried to set an exception with NPN_SetException.

My first guess would be that you compiled your code targeting a later version of windows than windows XP; I'm not sure if that would produce this issue or not. I have never seen the issue you're describing, and I've got plugins running on xp, vista, and 7 with no trouble. You might also try playing with a FireBreath plugin and see if the issue occurs there or not.

I would recommend that you attach with a debugger and set some breakpoints. Start in NPN_GetValue and make sure that it's instantiating your NPObject, then set breakpoints in your NPObject's HasMethod and Invoke methods and see what is hit. Likely something in there will show you what is actually happening, or at least tell you what code is or isn't getting hit.