首页  编辑  

动态调用API函数

Tags: /C#/API调用/   Date Created:

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);

}