Question

I have a VB 6.0 dll which has a method like below:

Public Function PrintDemo() As Integer
PrintDemo = 10
End Function

The dll is registered on my machine. I need to load this dll dynamically into C#. Below is the way I tried in C#:

Type obj = Type.GetTypeFromProgID("Project1.Class1");
object ins = Activator.CreateInstance(obj);

var method1 = obj.InvokeMember("PrintDemo", BindingFlags.InvokeMethod, null, obj, null);
Console.WriteLine(method1);

The dll is getting loaded successfully but I am unable to invoke it.

Could someone help me on this ?

Was it helpful?

Solution

Try to change

obj.InvokeMember("PrintDemo", BindingFlags.InvokeMethod, null, obj, null);

// To...
obj.InvokeMember("PrintDemo", BindingFlags.InvokeMethod, null, ins, null);

I believe you should be sending the instance 'ins' as target and not the Type. Otherwise there would be no reason to create an instance.

It's a really long time since I've worked with reflection...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top