关于从 .net dll 导出函数方法 (C#)
math.dll
namespace math
{
public class MyClass {
public static int Add(int x, int y)
{
return x+y;
}
}
在我的 exe 项目中,我想使用 Add() 函数,因此,
示例 1 - 这是工作
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());
}
}
示例 2 - 这是不工作
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());
}
}
示例 3 - 这不起作用
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());
}
你能告诉我不起作用的示例 (2,3) 的错误在哪里吗? 谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在示例 2 和 3 中,您使用的是
Marshal.GetDelegateForFunctionPointer
这是使用非托管代码时使用的函数,以便将非托管函数指针转换为托管委托。math
程序集包含托管 .NET 代码,因此您应该像示例 1 中那样使用反射。因此,除非您尝试重用非托管 C、C++ 等库中的功能,否则不要使用此函数。您确实应该区分非托管代码(C、C++...)和托管代码(.NET)。
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. Themath
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).