1

npapi code:

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 code:

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");
    }

}

The right result should be: "hello, just test it", but sometimes return"hello, just test itÿÿÿÿ". It only sometimes not all time!

Please help.

1

3 Answers 3

2

The error is not in your html,you shuld see NPString structure.

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

The member UTF8Length indicate the length of your string,so you should do as below:

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;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks you tell me the real problem and give me the demo code!
1

It looks like you are allocating the memory incorrectly:

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

should be:

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

to correct the length.

However, in order for the browser to be able to free the memory, you should be using:

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

Comments

0

OK, fond the answer! In html code:

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

should be like this:

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

at string end need "\0"

2 Comments

This works, but the real problem is that your plugin code is wrong. You should be using the string length (see the duplicate question), not assuming that it's a null-terminated string.
Thanks you tell me the real problem!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.