提供 .Net 下调用非托管Dll内部函数的快捷方法
2009-01-06 22:42
218 查看
提供 .Net 下调用非托管Dll内部函数 的快捷方法2008年12月12日 星期五 下午 10:241,用C#实现一个DllImported 类,内部利用DllImport 访问kernel32.dll的LoadLibrary和GetProcAddress
[DllImport("kernel32.dll", EntryPoint = "LoadLibrary")]
public static extern int LoadLibrary( string lpLibFileName );
[DllImport("kernel32.dll", EntryPoint = "GetProcAddress")]
public static extern IntPtr GetProcAddress(int hModule, string lpProcName);
2,利用Marshal.GetDelegateForFunctionPointer 将非托管指针转化为委托
类代码如下:
using System;
using System.Collections.Generic;
using System.Text;
///////////////添加命名空间/////////////////////////
using System.Runtime.InteropServices;
namespace DllImported
{
public class CDllImported
{
[DllImport("kernel32.dll", EntryPoint = "LoadLibrary")]
public static extern int LoadLibrary( string lpLibFileName );
[DllImport("kernel32.dll", EntryPoint = "GetProcAddress")]
public static extern IntPtr GetProcAddress(int hModule, string lpProcName);
public CDllImported()
{
}
/// <summary>
/// 获取非托管DLL内部函数
/// </summary>
/// <param name="delegateName">托管函数定义类型,用typeof(your delegate typeof)传入 </param>
/// <param name="dllName">Dll路径 </param>
/// <param name="procName">要获取的函数名称</param>
/// <returns></returns>
public object GetApi (Type delegateName, string dllName,string procName)
{
IntPtr api = GetProcAddress(LoadLibrary(dllName), procName);
return (object)Marshal.GetDelegateForFunctionPointer(api, delegateName);
}
}
}
编译产生一个DllImported的 dll组件。
在Vb里调用Api
1 先添加上面的组件
2 定义委托 Delegate Function msgDelegate(ByVal hwnd As IntPtr, ByVal strText As String, ByVal strCaption As String, ByVal type As Int32) As Int
3 实现调用
Dim api As New DllImported.CDllImported
Dim msg As msgDelegate= api.GetApi(Gettype(msgDelegate) , "user32.dll", "MessageBoxW")
msg(IntPtr.Zero, "Done", "Change", 0)
类似的可以在C#内调用,由于类代码是C#写的,调用起来很容易,此略去。
[DllImport("kernel32.dll", EntryPoint = "LoadLibrary")]
public static extern int LoadLibrary( string lpLibFileName );
[DllImport("kernel32.dll", EntryPoint = "GetProcAddress")]
public static extern IntPtr GetProcAddress(int hModule, string lpProcName);
2,利用Marshal.GetDelegateForFunctionPointer 将非托管指针转化为委托
类代码如下:
using System;
using System.Collections.Generic;
using System.Text;
///////////////添加命名空间/////////////////////////
using System.Runtime.InteropServices;
namespace DllImported
{
public class CDllImported
{
[DllImport("kernel32.dll", EntryPoint = "LoadLibrary")]
public static extern int LoadLibrary( string lpLibFileName );
[DllImport("kernel32.dll", EntryPoint = "GetProcAddress")]
public static extern IntPtr GetProcAddress(int hModule, string lpProcName);
public CDllImported()
{
}
/// <summary>
/// 获取非托管DLL内部函数
/// </summary>
/// <param name="delegateName">托管函数定义类型,用typeof(your delegate typeof)传入 </param>
/// <param name="dllName">Dll路径 </param>
/// <param name="procName">要获取的函数名称</param>
/// <returns></returns>
public object GetApi (Type delegateName, string dllName,string procName)
{
IntPtr api = GetProcAddress(LoadLibrary(dllName), procName);
return (object)Marshal.GetDelegateForFunctionPointer(api, delegateName);
}
}
}
编译产生一个DllImported的 dll组件。
在Vb里调用Api
1 先添加上面的组件
2 定义委托 Delegate Function msgDelegate(ByVal hwnd As IntPtr, ByVal strText As String, ByVal strCaption As String, ByVal type As Int32) As Int
3 实现调用
Dim api As New DllImported.CDllImported
Dim msg As msgDelegate= api.GetApi(Gettype(msgDelegate) , "user32.dll", "MessageBoxW")
msg(IntPtr.Zero, "Done", "Change", 0)
类似的可以在C#内调用,由于类代码是C#写的,调用起来很容易,此略去。
相关文章推荐
- 提供 .Net 下调用非托管Dll内部函数 的快捷方法
- JavaScript—在嵌套的内部函数中调用外部this的方法
- 在Angular外部使用js调用Angular控制器中提供的函数方法或变量
- js 函数闭包内部返回函数体调用方法难点解答
- c#调用硬件提供的二次开发函数方法
- C# 函数方法内部实现循环调用自身
- array_map函数在PHP类中调用内部方法简介
- array_map函数在PHP类中调用内部方法简介
- SQL存储过程(CLR存储过程)或函数中调用.NET程序集中的方法
- .net 下动态加载自定义控件后传值及函数调用方法
- TGraphicControl(自绘就2步,直接自绘自己,不需要调用VCL框架提供的函数重绘所有子控件,也不需要自己来提供PaintWindow函数让管理框架来调用)与TControl关键属性方法速记(Repaint要求父控件执行详细代码来重绘自己,还是直接要求Invalidate无效后Update刷新父控件,就看透明不透明这个属性,因为计算显示的区域有所不同)
- 在Angular外部使用js调用Angular控制器中提供的函数方法或变量
- array_map函数在PHP类中调用内部方法简介
- javaScript 自执行函数—保留异步方法调用的内部变量
- C#: 调用存储过程或函数显示"需要"参数,但未提供该参数的解决方法
- 将对象转为数组方法:延伸array_map函数在PHP类中调用内部方法
- 在Angular外部使用js调用Angular控制器中提供的函数方法或变量
- 关于C语言调用Unity3d C#函数的方法
- C#调用VC DLL接口函数参数类型转换方法介绍
- 通过IE提供的window.external来再javascript中调用winform中的函数