سؤال

i have writing NPAPI plugin to access DOM of the current page. i am able to build plugin. now i want to call javascript function console.debug("hello from c++"); from NPAPI plugin. i have taken following code i am using helloworld sample code from google to build npapi plugin: code:

bool ScriptablePluginObject::Invoke(NPObject* obj, NPIdentifier methodName, const NPVariant* args,uint32_t argCount, NPVariant* result) 
{   
   // The message i want to send.
   char* message = "Hello from C++";

   // Get window object.
   NPObject* window = NULL;
   NPN_GetValue(npp_, NPNVWindowNPObject, &window);

   // Get console object.
   NPVariant consoleVar;
   NPIdentifier id = NPN_GetStringIdentifier("console");
   NPN_GetProperty(npp_, window, id, &consoleVar);
   NPObject* console = NPVARIANT_TO_OBJECT(consoleVar);

   // Get the debug object.
   id = NPN_GetStringIdentifier("debug");

   // Invoke the call with the message!
   NPVariant type;
   STRINGZ_TO_NPVARIANT(message, type);
   NPVariant args[] = { type };
   NPVariant voidResponse;
   NPN_Invoke(npp_, console, id, args,sizeof(args) / sizeof(args[0]),&voidResponse);

   // Cleanup all allocated objects, otherwise, reference count and
   // memory leaks will happen.
   NPN_ReleaseObject(window);
   NPN_ReleaseVariantValue(&consoleVar);
   NPN_ReleaseVariantValue(&voidResponse);
}

but after loading when i am calling test.html it is getting crashed. please let me know "am i calling this code at right place" and "how can i test this code".

thanks...

هل كانت مفيدة؟

المحلول 2

thanks taxilian...

your suggestion helped. i have debugged chrome.exe and identified issue with npnfuncs structure. empty structure was causing the issue. so few code changes helped in resolving this issue. chrome can be debugged using (for others who might wanna try)

C:\Program Files\Google\Chrome\Application>chrome.exe --plugin-startup-dialog --wait-for-debugger

نصائح أخرى

First thing is first: whenever I read "it crashed" with no more information I have to facepalm a little bit. If that's all you know, you stopped troubleshooting way too soon. The FireBreath project has a page on debugging plugins that may help, but if you let it crash while a debugger is attached you should find out where it crashed and what the crash was.

I also want to warn you that I've had some significant performance problems with trying to do what you're doing in a production environment; don't use this as your main logging method! it's not fast enough for that.

That said, I don't see anything inherently wrong with your approach for the most part. It could be that your (presumably global) npp_ isn't valid, or it's possible that your string needs to be made up of memory allocated by NPN_MemAlloc; I know that if you were returning it then it would need to be, but I don't think it needs to be when you're just calling a new function; it's something to try, though.

If I were you, my next step would be to attach a debugger and see where the crash is. The easiest way to do that is probably to turn off plugin ipc in firefox so you can easily just attach to the process, but you could also just find the correct process and connect to it (see the debugging page I linked earlier for details)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top