0

math.dll

namespace math
{
    public class MyClass {
        public static int Add(int x, int y)
        {
            return x+y;
        }
    }

And in my exe project I want to use Add() function so,

Example 1 - This is working

    public void Example_1()
    {
                SampleAssembly = Assembly.Load("math");
                Type type = SampleAssembly.GetType("math.MyClass");
                MethodInfo methodInfo  = type.GetMethod("Add");
                if(methodInfo != null)
                {
                    object result = null;
                    ParameterInfo[] parameters = methodInfo.GetParameters();
                    object classInstance = Activator.CreateInstance(type, null);
                    object[] parametersArray = new object[] { 3, 5 };
                    result = methodInfo.Invoke(classInstance, parametersArray);
                    MessageBox.Show(result.ToString());
                } 
  }

Example 2 - This is not working

public delegate int Add(int x,int y);                
public void Example_2()
                {
                    SampleAssembly = Assembly.Load("math");
                    Type type = SampleAssembly.GetType("math.MyClass");
                    MethodInfo methodInfo  = type.GetMethod("Add");
                    if(methodInfo != null)
                    {

                    Add add = (Add)Marshal.GetDelegateForFunctionPointer
                      (methodInfo.MethodHandle.GetFunctionPointer(),typeof(Add));
                      MessageBox.Show(add(3,2).ToString());
                    } 
              }

Example 3 - This is not working

public void Example_3() {

        IntPtr hdl = LoadLibrary("math.dll");
        IntPtr addr = GetProcAddress(hdl,"MyClass");
        IntPtr myfunction = GetProcAddress(addr,"Add");
        Add add= (Add)Marshal.GetDelegateForFunctionPointer(hdl,typeof(Add));
        MessageBox.Show(add(2,3).ToString());
}

Can you tell me where is the mistakes of not working examples(2,3)? Thanks.

Okan Kocyigit
  • 13,203
  • 18
  • 70
  • 129
  • Are you trying to understand the mechanics of assemblies and methods? Or are you simply trying to use a method in an external assembly? If the latter, simply add an assembly reference to your project and instantiate the class directly. You don't need to code any of these. – ScottTx Apr 19 '11 at 21:45
  • Yes I know that is the easiest way to add reference. as you say, I want to understand assemblies and exporting methods.thanks for advice. – Okan Kocyigit Apr 19 '11 at 21:51

1 Answers1

2

In example 2 and 3 you are using the Marshal.GetDelegateForFunctionPointer which is a function used when working with unmanaged code in order to convert an unmanaged function pointer to a managed delegate. The math assembly contains managed .NET code so you should use Reflection as in Example 1. So don't use this function unless you are trying to reuse functionality in an unmanaged C, C++, ... library.

You really should make the distinction between unmanaged (C, C++, ...) and managed code (.NET).

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Ahh Darin again you, thanks. So I understand that I can't create same function with unmanaged code,can I? – Okan Kocyigit Apr 19 '11 at 21:42
  • @ocanal, your math.dll is a .NET assembly => managed code. Not unmanaged C/C++ code. If you want to create unmanaged code and use Marshal.GetDelegateForFunctionPointer then go ahead and start writing C++, not C#. – Darin Dimitrov Apr 19 '11 at 21:43