从Javascript访问NPAPI插件的属性

时间:2012-12-11 00:44:12

标签: javascript npapi

尝试使用NPAPI插件通过Javascript获取属性值时出现问题; 在调试过程中,我看到所有函数链(HasProperty,HasMethod和GetProperty)都被调用。更重要的是,我在调用GetProperty时看到我将新值设置为result参数。但是在离开GetProperty后,我得到了一个例外,无法理解它的原因。

May FireFox会调用一些我忘记初始化的其他功能吗?

提前致谢

我的代码是:

// static function which calls Get_Property for the instance of CScriptableNPObject
bool CScriptableNPObject::NP_GetProperty(NPObject *npobj, NPIdentifier name, NPVariant *result)
{
    m_Logs.WriteLogs(10, _T("Enter the CScriptableNPObject::NP_GetProperty()"));

    return ((CScriptableNPObject *)npobj)->GetProperty(name, result);
}

// just converter name from NPIdentifier to char *
bool CScriptableNPObject::GetProperty(NPIdentifier name, NPVariant *result)
{
    NPUTF8 *pszProperty = m_pNPNFuncs->utf8fromidentifier(name);

    return GetProperty(pszProperty, result);
}

// checking the dictionary of properties, if property exists put its value into the result
bool CScriptableNPObject::GetProperty(NPUTF8 *pszProperty, NPVariant *result)
{
    VOID_TO_NPVARIANT(*result);

    JSPropertiesMap::iterator it = m_JSProperties.find(pszProperty);

    if (it == m_JSProperties.end())
        return false;

    NPUTF8 *pszNewPropertyValue = new NPUTF8[it->second->value.stringValue.UTF8Length + 1];
    sprintf(pszNewPropertyValue, it->second->value.stringValue.UTF8Characters);

    STRINGZ_TO_NPVARIANT(pszNewPropertyValue, *result);

    return true;
}

1 个答案:

答案 0 :(得分:1)

您需要使用NPN_MemAlloc()

NPUTF8* newValue = NPN_MemAlloc(length + 1);