Note that there are some explanatory texts on larger screens.

plurals
  1. POabout exporting function methods from .net dll (C#)
    primarykey
    data
    text
    <p><strong>math.dll</strong></p> <pre><code>namespace math { public class MyClass { public static int Add(int x, int y) { return x+y; } } </code></pre> <p>And in my exe project I want to use Add() function so,</p> <p><b>Example 1 - This is working</b></p> <pre><code> 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()); } } </code></pre> <p><b>Example 2 - This is not working</b></p> <pre><code>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()); } } </code></pre> <p><b>Example 3 - This is not working</b></p> <pre><code>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()); } </code></pre> <p><strong>Can you tell me where is the mistakes of not working examples(2,3)? Thanks.</strong></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload