How to get the NPP Instance inside a NPAPI C plugin - c

I have written a NPAPI plugin in C which needs to call the NPN_Invoke function
(in order to call a JavaScript function).
But NPN_Invoke() takes the NPP instance as a parameter.
Only the NP_New() and NP_Destroy() functions get passed NPP instance. How do I get this NPP instance?
Thanks in advance.

The best way is actually to extend NPObject with a field to save the associated NPP instance, and provide the allocate/deallocate functions with your NPClass definition. You can then cast the NPObject to your subtype to access the actual NPP instance.
I would NOT recommend doing this at a global level (NP_GetEntryPoints, etc.) as suggested above, as there are potentially several instances of your plugin loaded - maybe even on the same page - and you want to be sure you're invoking the correct one. Unfortunately there seems to be a lot of sample code out there where some random instance is just kept in a global variable, and updated as much as possible.
As an example, assuming C++, you'll want to extend NPObject:
struct MyNPObject : public NPObject {
NPP npp_;
explicit MyNPObject(NPP npp) : npp_(npp) {}
};
Then your NPClass definition will need to have allocate and deallocate definitions:
static NPClass obj_Class = {
NP_CLASS_STRUCT_VERSION,
&obj_allocate,
&obj_deallocate,
NULL,
&obj_hasMethod,
&obj_invoke,
...
Which could be implemented like so:
static NPObject* obj_allocate(NPP npp, NPClass *aClass)
{
return new MyNPObject(npp);
}
static void obj_deallocate(NPObject *obj)
{
delete obj;
}
And when you need to call NP_Invoke, assuming you have the NPObject* (inside obj_invoke, for example) you just downcast:
MyNPObject* myObj = reinterpret_cast<MyNPObject*>(obj);
g_browser->invoke(myObj->npp, ...)

In the NP_GetEntryPoints define your own NP_yourNew Function, Now when the after the NP_New the framework calls your NP_yourNew with the instance. The instance could be saved when the your callback just gets invoked.

Related

Why non-static variables/methods are not accessible inside a static method?

Need the reason from JVM side. Explanation of the flow of the JVM is much appreciated(In simply words).
Static variables and methods do not belong to an object, but they exist on an outermost level.
Conversely, non-static variables and methods belong to an instantiated object.
This means that non-static variables and methods are not accessible to static methods because static methods would not have an object on which to operate.
Let me do an example:
Imagine the class Foo is defined as
public class Foo{
public static int a;
}
We can do operations like
public static int main(String[] args){
Foo.a = 10;
int b = Foo.a - 5;
}
without instantiating an object
Foo foo = new Foo();
Now think about the following class
public class Bar{
int a = 10;
public static void doSomething(){
this.a = 5;
}
}
We know the keyword this refers to the object in use, but doSomething() it is not linked to an object, so what object does this refer to?
This is why we can't access non-static variables and methods inside a static method.
So to understand this you first need no understand what a static method is. Basically, a static method is a method inside a class that can be called even when the class hasn't been initiated.
These static methods (or variables) exist independently of any initiation of the class.
In general anything static should be avoided as they are kinda an antipattern when it comes to javas object-oriented approach to things.

Is it bad to wrap singleton instance methods in static methods?

Is there a con to implementing a singleton that wraps instance methods with static ones?
For example:
public void DoStuff() { instance._DoStuff(); }
private void _DoStuff() {
...
}
And of course instance would be static. But it would be nicer to call:
Singleton.DoStuff();
Instead of:
Singleton.GetInstance().DoStuff();
I think it depends.
First the GetInstance() really should be used for getting an object back and then using that else where in your code. The Singleton should just help guarantee a single instance of that object exists.
Next if you want to make DoStuff static go ahead, though you have to know to call it that way everywhere else in your code.
So you really have this difference:
var instance = Singleton.GetInstance();
...
instance.DoStuff ()
Vs
Singleton.DoStuff ()
This means that you can pass a singleton object around and not have to know static calls.
Also, I have to mention that Singletons if not used properly can lead to a nightmare in unit testing: http://misko.hevery.com/2008/08/25/root-cause-of-singletons/

C# setting form.visible = false inside a method?

hi i have this lines of code that i cant make it work
the goal is simple setting the form1 to visible = false
public static void DoActions(string Cmd){
if(Cmd == true)
{
MainForm.Visible = false;
}
}
but i keep on having this error
An object reference is required for
the non-static field, method, or
property
usually i set the called methond to static.. so the error will go away
but on this case how do i do it?
thanks for any help guys
'System.Windows.Forms.Control.Invoke(System.Delegate)'
This is happening because DoActions is a static method rather than an instance method, however MainForm is an instance field / property. The distinction is that instance methods operate on an instance of the class on which they are defined, wheras static methods do not.
This means that wheras instance methods are able to access properties, fields and methods of their containing class through the this keyword, for example:
// Instance field
Form1 MainForm;
void InstanceMethod()
{
Form1 frm = this.MainForm;
}
You cannot do the same thing from inside a static method (think about it, what instance would it operate on?). Note that C# will implicitly assume the use of the this keyword in places where it makes sense (so the above example could have been written as Form1 frm = MainForm).
See C# Static Methods for an alternative explanation of static vs instance methods (this is an important concept in object oriented programming that you should take the time to understand properly).
In your example you probably want to change DoActions to an instance method (by removing the static declaration):
public void DoActions(string Cmd)
{
if(Cmd == true)
{
this.MainForm.Visible = false;
}
}
This will allow it to access the instance MainForm field / property, however this may cause problems elsewhere in your code in places where you attempt to call DoActions from another static method without supplying an object instance.
Set form opacity and showintaskbar property in property window:
this.Opacity = 0;
this.ShowInTaskbar = false;
Your method is static - and so cannot access MainForm.
Make your method non-static if it is not required to be so.
public void DoActions(string Cmd)
{
if(Cmd == true)
{
MainForm.Visible = false;
}
}
If it is required, then create a static field in your class and ensure it is set before this method runs.

In php we can access static member functions using class objects. Can someone please tell any practicle use of this feature

In php we can call static member functions using class objects. For example
class Human
{
public static function Speak()
{
echo "I am a human.";
}
}
$human = new Human();
$human->Speak();
What we would expect is that a static member function can only be called using the class name and not the class instance variable (object). But what i have seen while programming is that php allows calling a static member function using the class object also. Is there any practical use or some important reason that this feature has been provided in php ?
This feature exists in java and c++ also. Thanks Oli for pointing this out in your response.
This is the same as in other OO languages, such as C++ and Java. Why would you want the interpreter to prevent this?
UPDATE
My best guess for this (and this is only a guess) is "for convenience". In essence, why should the user of your class necessarily care whether a given member function is static or not? In some circumstances, this will certainly matter; in others, maybe not. I'm not saying this is a great justification, but it's all I can come up with!
it allows you to abstract from the particular definition of the method, so that for example if you had to turn it into a static one at some point, you don't have to rewrite all the method calls!
I can't answer for PHP, (or really for anything) but consider this hypothetical C++:
class base{
public:
static void speak(){cout<<"base\n";}
};
class sub :public base {
public:
static void speak(){cout<<"sub\n"; }
};
int _tmain(int argc, _TCHAR* argv[]){
base *base1 = new base();
base1->speak();
sub *sub1 = new sub();
sub1->speak();
base *sub2 = new sub();
sub2->speak();
((sub*)sub2)->speak();
}
The output would be:
base
sub
base
sub
I'm sure it could be useful... maybe helping you determine which class's static method you should call based on the object currently in hand.

passing (function) pointers between c and mono

Hi after refering to http://www.mono-project.com/Embedding_Mono
i can call methods from managed code by using mono_runtime_invoke.
Now i want to call a method in the managed code with a function pointer (or at least some pointer) as argument from native c code
managed code
public delegate void MyDelegate ();
//method i want to call from native code
public static MyDelegate mono_method(MyDelegate c_ptr)
{
//...do sth
return c_ptr;
}
native code
typedef void (*FUNC_PTR)();
FUNC_PTR my_fct_ptr = some_c_function;
//calling the managed method
MonoObject *result_of_mono_method =
mono_runtime_invoke(mono_method, NULL, my_fct_ptr, NULL);
edit: to point out the problem
how can i call
public static unsafe int* mono_method(int *c_ptr)
from native c code, without using dllImport.
You have several options.
One is to add an internal call that takes a IntPtr (the function pointer) and the arguments: you will then cast the pointer to the function pointer type and call it normally from C code.
Using something like libffi can help to overcome the limitation of having just one function pointer type, it depends how many you need, you didn't specify.
Another option is to use Reflection.Emit to build a dynamic method: in it you will use the calli IL instruction to invoke the function pointer directly.
I'm not exactly sure what you're trying to ask here, but this is the easiest way to do a call back.

Resources