Here's what I am trying to do from javascript:
var result;
result = document.myplugin.GetClientName();
document.write(result);
"GetClientName" should return a NPVariant to the browser with a string. I am getting my output with garbage appended to the end.
Does anyone have any suggestions on how to solve this issue?
Umm... do it correctly? You haven't provided any sample code, nor have you explained how you are allocating your NPVariant; that makes it really hard to help you, my friend.
Basically you just need to create an NPVariant, allocate enough memory for it using NPN_MemAlloc, and assign the pointer to the UTF8Characters field of the NPString inside the NPVariant union and the length to the UTF8Length.
Make sure that you allocate the memory and copy your string over; if you're trying to pass in memory from a std::string or something it's likely getting released before you use it, which could easily cause issues. Also remember to allocate an extra byte at the end of the string for a NULL character -- remember that C strings are NULL terminated, and while the NPAPI spec says that you just need to set the UTF8Length, I've still seen cases where Firefox in particular expects a NULL terminated string and behaves oddly if you don't give it one.
If you want to take this route, you may find this blog post helpful:
However, might I suggest a simpler route? Writing a NPAPI plugin is not a simple thing, and it takes a lot of experience and research to do it well and securely. If you use FireBreath you can write NPAPI plugins on a C++ framework that already does all of this for you and lets you use normal C++ STL datatypes, works on multiple platforms, and generally saves you a ton of time and hassle.
For a little information on the approach used by FireBreath with scripting, see my blog post on the subject.