C#中动态调用API函数

来源:互联网 发布:php到底是做什么的 编辑:程序博客网 时间:2024/04/27 05:01
using System.Runtime.InteropServices;
 
internal delegate bool Delegate_Beep(uint dwFreq, uint dwDuration);
[DllImport("kernel32.dll")]
internal static extern IntPtr LoadLibrary(string lpLibFileName);
[DllImport("kernel32.dll")]
internal static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName);
[DllImport("kernel32.dll")]
internal static extern bool FreeLibrary(IntPtr hLibModule);
 
private void button1_Click(object sender, EventArgs e)
{
    IntPtr vLibraryHandle = LoadLibrary("Kernel32.dll");
    IntPtr vProcAddress = GetProcAddress(vLibraryHandle, "Beep");
    Delegate_Beep Beep = Marshal.GetDelegateForFunctionPointer(
       vProcAddress, typeof(Delegate_Beep)) as Delegate_Beep;
    Beep(100, 100);
    FreeLibrary(vLibraryHandle);
}
原创粉丝点击