【问题标题】:npapi plugin_invoke function return string value garbage charactersnpapi plugin_invoke 函数返回字符串值垃圾字符
【发布时间】:2012-12-17 08:47:46
【问题描述】:

npapi 代码:

bool plugin_invoke(NPObject *obj, NPIdentifier methodName, const NPVariant *args, uint32_t argCount, NPVariant *result) {
NPUTF8 *name = browser->utf8fromidentifier(methodName);
if(strcmp(name, plugin_method_name_getAddress) == 0){
    NPString password;
    if(argCount > 0) {
        password = NPVARIANT_TO_STRING(args[0]);
    }
    const char * StringVariable = password.UTF8Characters;
    char* npOutString = (char *)malloc(strlen(StringVariable+1));
    if (!npOutString)
        return false;
    strcpy(npOutString, StringVariable);
    STRINGZ_TO_NPVARIANT(npOutString, *result);
    browser->memfree(name);
    return true;
}
return false;

}

html代码:

function run() {
    var plugin = document.getElementById("pluginId");
    var passwordBeforEncryption =  document.getElementById("passwordFeild");
    if(plugin){
        var value = plugin.getAddress("hello, just test it");
            alert(value);
    }else{
        alert("plugin is null");
    }

}

正确的结果应该是:“hello, just test it”,但有时会返回“hello, just test itÿÿÿÿ”。只是有时不是所有时间!

请帮忙。

【问题讨论】:

标签: plugins safari npapi firebreath


【解决方案1】:

错误不在您的 html 中,您应该看到 NPString 结构。

typedef struct _NPString {
    const NPUTF8 *UTF8Characters;
    uint32_t UTF8Length;
} NPString;

成员UTF8Length表示你的字符串的长度,所以你应该这样做:

const char * StringVariable = password.UTF8Characters;
char* npOutString  = (char*)browser->memalloc(password.UTF8Length+1);
if (!npOutString) {
    return false;
}
memcpy(npOutString  , password.UTF8Characters, password.UTF8Length);
npOutString[password.UTF8Length] = 0;

【讨论】:

  • 谢谢你告诉我真正的问题并给我演示代码!
【解决方案2】:

您的内存分配似乎不正确:

char* npOutString = (char *)malloc(strlen(StringVariable+1));

应该是:

char* npOutString = (char *)malloc(strlen(StringVariable)+1);

修正长度。

但是,为了让浏览器能够释放内存,您应该使用:

char* npOutString = (char *)NPN_MemAlloc(strlen(StringVariable)+1);

【讨论】:

    【解决方案3】:

    好的,喜欢答案! 在html代码中:

    var value = plugin.getAddress("hello, just test it");
    

    应该是这样的:

    var value = plugin.getAddress("hello, just test it\0");
    

    字符串结尾需要"\0"

    【讨论】:

    • 这行得通,但真正的问题是你的插件代码是错误的。您应该使用字符串长度(请参阅重复的问题),而不是假设它是一个以 null 结尾的字符串。
    • 谢谢你告诉我真正的问题!
    猜你喜欢
    • 2013-10-30
    • 2013-02-07
    • 1970-01-01
    • 2020-01-08
    • 1970-01-01
    • 2013-02-17
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    相关资源
    最近更新 更多