Question

I would like to access a function of a program from which is attached a DLL.

In DLL I've tried:

Assembly assembly = Assembly.GetExecutingAssembly();
Type type = assembly.GetType("Uranium");
MethodInfo methodInfo = type.GetMethod("Util");

methodInfo.Invoke("SendClient", new object[] { Packet.GetData()});

But not works I get a null exception but not say the line. The running program is called and namespace is 'Uranium', the class is 'Util' and function is 'SendClient'.

Was it helpful?

Solution

I have been able solve it on my own.

Code:

Assembly assembly = Assembly.LoadFrom("Uranium.exe");
Type type = assembly.GetType("Uranium.Util");
MethodInfo methodInfo = type.GetMethod("SendClient");

methodInfo.Invoke(null, new object[] { Packet.GetData() });

OTHER TIPS

From what I can read from the code you've posted you try to invoke the function Util of class Uranium. And you're passing a string as the instance of the class.

This should be more like what you're trying to do:

Assembly assembly = Assembly.GetExecutingAssembly();
Type type = assembly.GetType("Util");
MethodInfo methodInfo = type.GetMethod("SendClient");

methodInfo.Invoke(Activator.CreateInstance(type), new object[] { Packet.GetData()});

If SendClient is a static member function then Activator.CreateInstance(type) can be replaced with null. And of course you should add checks that the GetType and GetMethod return values are not null

You first need to find the assembly that contains the type. Also, you need to pass the class name to GetType(), not the namespace, and method name to GetMethod(), not class name.

foreach (Assembly currentassembly in AppDomain.CurrentDomain.GetAssemblies()) 
{
    Type t = currentassembly.GetType("Util", false, true);
    if (t != null) 
    {
        MethodInfo methodInfo = type.GetMethod("SendClient");
        methodInfo.Invoke(Activator.CreateInstance(t),new object[] { Packet.GetData()});
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top