Invoking function from out of process DLL
-
Hi all, I am new in C# World and working on C# project which uses reflection. Problem: I am calling a function from a DLL say ServerDLL (C#)which is loaded by an C# application say MyApp. I want to invoke some funcition say (SomeFunction)of Class Class1 from another DLL say ClientDLL. For this I am using following.
//In ClientDLL object MyObj = Marshal.GetActiveObject("ServerDLL.Class1") // Object reference object[] args = new object[1]; args[0] = my_data_to_pass; // my data to pass MyObj.GetType().InvokeMember("SomeFunction", BindingFlags.InvokeMethod, null, MyObj, args);
This works fine, but I found these very expensive and taking too much time. Seems that MyObj.GetType() is loading everytime. I have googled and come to know that these is heavy operation (see reference) http://msdn.microsoft.com/msdnmag/issues/05/07/Reflection/[^] I have changed the code to work to load it once,//In ClientDLL MethodInfo g_MFinfo = MyObj.GetType().GetMethod("SomeFunction", BindingFlags.InvokeMethod);
But I am getting null value g_MFinfo? Does anyone know what i am doing wrong or probably a good approach? Thanks in Advance Abhi Lahare -
Hi all, I am new in C# World and working on C# project which uses reflection. Problem: I am calling a function from a DLL say ServerDLL (C#)which is loaded by an C# application say MyApp. I want to invoke some funcition say (SomeFunction)of Class Class1 from another DLL say ClientDLL. For this I am using following.
//In ClientDLL object MyObj = Marshal.GetActiveObject("ServerDLL.Class1") // Object reference object[] args = new object[1]; args[0] = my_data_to_pass; // my data to pass MyObj.GetType().InvokeMember("SomeFunction", BindingFlags.InvokeMethod, null, MyObj, args);
This works fine, but I found these very expensive and taking too much time. Seems that MyObj.GetType() is loading everytime. I have googled and come to know that these is heavy operation (see reference) http://msdn.microsoft.com/msdnmag/issues/05/07/Reflection/[^] I have changed the code to work to load it once,//In ClientDLL MethodInfo g_MFinfo = MyObj.GetType().GetMethod("SomeFunction", BindingFlags.InvokeMethod);
But I am getting null value g_MFinfo? Does anyone know what i am doing wrong or probably a good approach? Thanks in Advance Abhi LahareHuge question :) The point should be where you catch the Object Reference: may be MyObj isn't what you expect to. Try to verify which class you have referred to. Having a null result mean that signature cannot be detected in the class (you may have optionally an exception to be raised instead of null return using a BindingFlag if i'm not joked by my memory). As long as MyObj is ServerDLL.Class1 your code should work, it seems to me. If you do not come to a point, please write a little demo for us to be tested against. I'm probably not able to come forth with a solution without playing a little with the child. Sorry, it's all I can do
Parsiphal
-
Hi all, I am new in C# World and working on C# project which uses reflection. Problem: I am calling a function from a DLL say ServerDLL (C#)which is loaded by an C# application say MyApp. I want to invoke some funcition say (SomeFunction)of Class Class1 from another DLL say ClientDLL. For this I am using following.
//In ClientDLL object MyObj = Marshal.GetActiveObject("ServerDLL.Class1") // Object reference object[] args = new object[1]; args[0] = my_data_to_pass; // my data to pass MyObj.GetType().InvokeMember("SomeFunction", BindingFlags.InvokeMethod, null, MyObj, args);
This works fine, but I found these very expensive and taking too much time. Seems that MyObj.GetType() is loading everytime. I have googled and come to know that these is heavy operation (see reference) http://msdn.microsoft.com/msdnmag/issues/05/07/Reflection/[^] I have changed the code to work to load it once,//In ClientDLL MethodInfo g_MFinfo = MyObj.GetType().GetMethod("SomeFunction", BindingFlags.InvokeMethod);
But I am getting null value g_MFinfo? Does anyone know what i am doing wrong or probably a good approach? Thanks in Advance Abhi LahareIs SomeFunction a public function? If not, you'll need to specify an additional BindingFlag, something like
MyObj.GetType().GetMethod("SomeFunction", BindingFlags.InvokeMethod | BindingFlags.NonPublic);
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Check out this cutie The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
Is SomeFunction a public function? If not, you'll need to specify an additional BindingFlag, something like
MyObj.GetType().GetMethod("SomeFunction", BindingFlags.InvokeMethod | BindingFlags.NonPublic);
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Check out this cutie The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
Hi Judah Himango Thanks a lot for your reply. It is a private function I have added the BindingFlafs.NonPublic option. but no luck :(. Its still giving null for call. Regards Abhi Lahare
-
Huge question :) The point should be where you catch the Object Reference: may be MyObj isn't what you expect to. Try to verify which class you have referred to. Having a null result mean that signature cannot be detected in the class (you may have optionally an exception to be raised instead of null return using a BindingFlag if i'm not joked by my memory). As long as MyObj is ServerDLL.Class1 your code should work, it seems to me. If you do not come to a point, please write a little demo for us to be tested against. I'm probably not able to come forth with a solution without playing a little with the child. Sorry, it's all I can do
Parsiphal
Hi Parsiphal, Thanks for reply. I am pointing to correct object if i am not wrong.Is there any method to check the validity of the Object. I have tried simple check
if(MyObj is ServerDLL.Class1) System.Console.WriteLine("Success"); else System.Console.WriteLine("Fail");
Here it is giving me "Fail". Am doing validating it correctly? Regards Abhi Lahare -
Hi Judah Himango Thanks a lot for your reply. It is a private function I have added the BindingFlafs.NonPublic option. but no luck :(. Its still giving null for call. Regards Abhi Lahare
Is it static or an instance method? You'll need to specify that in the binding flags there as well.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Check out this cutie The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
Hi Parsiphal, Thanks for reply. I am pointing to correct object if i am not wrong.Is there any method to check the validity of the Object. I have tried simple check
if(MyObj is ServerDLL.Class1) System.Console.WriteLine("Success"); else System.Console.WriteLine("Fail");
Here it is giving me "Fail". Am doing validating it correctly? Regards Abhi LahareHi, sorry for get you answeret a bit late. I was heavily involved with a few thing here and there. Having you defined MyObj as object, it's not surprising Type is Sustem.Object. With that test we are also testing against any possible derived class instanced and handled by Object. So definitely we can say the object you are handling is not ServerDLL.Class1, and therefore the answer to your first question should be: "you get null because no such a method exist in the instance". I asked you to write a little demo for me to play with, just to be sure of running same condition you have (even if no logic is to be included in the demo: just a Console.Writeline "Method xxx called" would work: i'm not interested in spying what you are doing :)). In my opinion, may be the instance you are handling is still Class1 type, but masquerade by the object declaration. Try to verify this with your IDE, putting a break point on the first line of your test, than putting MyObj under watch and drilling the tree (expanding '+') looking for some that can tell you: there's a Class1 under the cover; otherwise to estabilish which instance have you taken from memory. I'm not sure what you are getting since I'm not aware of the complexity of your application. If you can say either you handle or not a Class1 instance, you can say some on the first line of your snippet, that is: obtaining the instance. From there you can see if it is a matter of member protection (public, private, protected), or some modifier than can prevent you from accessing that memeber. If still is plumbing, consider to split you code-line to examine each step and its result. If you are not able to have a MemberInfo Instance of the nethod you want to invoke, invocation will never be possibile: consider splitting the problem in prerequisite and action, so to say :) I can show you a couple of way to instancing a class by reflection, but no other to obtaining a reference to an istance: I think either is correct and the problem is elsewhere, or it is not fully correct. Hope this can help :) P.S. Feel free to send me an example that can reproduce the behavior for me to play around with: I will give you some closer advice perhaps ;)
Parsiphal
-
Is it static or an instance method? You'll need to specify that in the binding flags there as well.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Check out this cutie The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
Thanks Judah, Sorry for delay, my method is a simple private method. Thanks Abhishake Lahare
-
Thanks Judah, Sorry for delay, my method is a simple private method. Thanks Abhishake Lahare
Then you'll need to specify that it is an instance method in the BindingFlags as well.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Check out this cutie The apostle Paul, modernly speaking: Epistles of Paul Judah Himango