www.pudn.com > CS_CALLAPI_20111018A.rar > CALLAPI.cs, change:2011-10-18,size:1236b


using System; 
using System.Collections.Generic; 
using System.Text; 
using System.IO; 
using System.Runtime.InteropServices; //必须引入包 
 
 
namespace CS_CALLAPI 
{ 
    class CALLAPI 
    { 
        [DllImport("kernel32.dll")] 
        private extern static IntPtr LoadLibrary(string path); 
        [DllImport("kernel32.dll")] 
        private extern static IntPtr GetProcAddress(IntPtr lib, string funcName); 
        [DllImport("kernel32.dll")] 
        private extern static bool FreeLibrary(IntPtr lib); 
 
        private IntPtr hInstance; 
 
        public int LoadDll(String DLLPath) 
        { 
            hInstance = LoadLibrary(DLLPath); 
            return 0; 
        } 
        public void FreeDll() 
        { 
            FreeLibrary(hInstance);    
        } 
        public CALLAPI() 
        { 
            //hInstance = LoadLibrary(DLLPath); 
        } 
        ~CALLAPI() 
        { 
            //FreeLibrary(hInstance);    
        } 
        //将要执行的函数转换为委托 
        public Delegate Invoke(string APIName, Type t) 
        { 
            IntPtr api = GetProcAddress(hInstance, APIName); 
            return (Delegate)Marshal.GetDelegateForFunctionPointer(api, t); 
        } 
         
    } 
}