Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to know, how I can call the methods of the class defined in my dll. The class defined in the dll uses this interface:

C#
interface class ILightControl{
            void Settings();
};


This is the part, where I need to call the method.

C#
System::Reflection::Assembly^ x = System::Reflection::Assembly::LoadFile("C:\\LightControl.dll");
             array<Type^>^t = x->GetTypes();
             Type^InterfaceType;
             Object^obj;
             for(int i = 0; i < t->Length; i++){
                 InterfaceType = t[i]->GetInterface("ILightControl");
                 if(InterfaceType){
                     obj = Activator::CreateInstance(t[i]);
                     //I need to call Settings(); here
                 }
}


How call I now call the method? Thank you very much for your help!
Posted

In that case I think you may have to call the method using reflection
System::Reflection::MethodInfo^ mi = interfaceType->GetMethod("Settings");
cli::array<System::Object> parameters = gcnew cli::array<System::Object>();
// add any parameter here
System::Object^ retVal = mi->Invoke(obj, parameters)
 
Share this answer
 
v2
Comments
egon_ll 25-Sep-10 5:24am    
Thank you! Now it works.
egon_ll 25-Sep-10 5:24am    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
((ILightControl)obj)->Settings();
 
Share this answer
 
Comments
egon_ll 22-Sep-10 11:59am    
I have already tried that, but i get the error:

The object of the type LightControl cannot be converted to the type ILightControl.
(LightControl is the name of the class that implements ILightControl in the dll.)
ARopo 23-Sep-10 4:47am    
Have you tried
LightControl^ lighcontrol = (LightControl^)obj;
((ILightControl^)lighcontrol)->Settings();
egon_ll 23-Sep-10 5:07am    
As LightControl is only defined in the dll, the software doesn't know LightControl at compilation time.

I defined the interface ILightControl in the dll and derived LightControl. I also defined ILightControl in my main program. Now I have to tell the compiler, that the object I created has the function Settings, and call it. This is the first time I'm working with interfaces, so I might overlook something.
egon_ll 23-Sep-10 5:20am    
I now tried this:

ILightControl^ilc = dynamic_cast<ilightcontrol^>(obj);
ilc->Settings();

The cast works fine, but when I call the method I get the error:
"Object reference not set to an instance of an object."

I think the problem is, that I'm trying to create an Object of an interface. That is probably not possible.
But as I already have the right object, there must be a possibility to tell the compiler, that the object implements the interface ILightControl, and can therefore call the method Settings().
Try this as a test
<br />
<br />
  LightControl^ lc = (LightControl^)Activator::CreateInstance(LightControl::typeid));<br />
((ILightControl^)lc)->Settings(); <br />
 
Share this answer
 
Comments
egon_ll 23-Sep-10 11:15am    
This can not work, as LightControl is only defined in the dll but not in the program calling the dll.
I tried it anyway and I get the error "LightControl not declared".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900