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);
}
这个是我在网上找到的,可是无奈看不懂不知道怎么改,不管用什么办法,只要能动态调用这个API就行

解决方案 »

  1.   

    不用绝对路径不可以吗>
    把DLL放到你程序的目录下,它会自动搜索到的
      

  2.   

    不用绝对路径的话,我用的是VS2005,放在BIN目录下直接用
    [DllImport("CipherParse.dll")]
    这样的方法他提示我找不到这个文件
      

  3.   

    using   System.Runtime.InteropServices; 
      
    internal   delegate   bool   Delegate_g(string   str1,   string   str2); 
    [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("E:\\Work\\WebSiteVB\\Bin\\CipherParse.dll"); 
            IntPtr   vProcAddress   =   GetProcAddress(vLibraryHandle,   "G_EncryptStr"); 
            Delegate_g   g   =   Marshal.GetDelegateForFunctionPointer( 
                  vProcAddress,   typeof(Delegate_g))   as   Delegate_g; 
            g("str1",   "str2"); 
            FreeLibrary(vLibraryHandle); 
    }   
    *****************************************************************************
    欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) http://feiyun0112.cnblogs.com/
      

  4.   

    feiyun0112 改造过的我试了下没有问题,但是方法的提供是有返回值的,而我这里返回的之后true他正确的返回值应该是我的1#那里的返回的string一样的也就是说我用这个方法的话
    [DllImport("E:\\Work\\WebSiteVB\\Bin\\CipherParse.dll")] 
    static   extern   string   G_EncryptStr(string   str1,   string   str2);直接用G_EncryptStr这个方法是有返回的值的
      

  5.   

    internal delegate string Delegate_g(string str1, string str2);
    [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);IntPtr vLibraryHandle = LoadLibrary(Server.MapPath("bin/CipherParse.dll"));
    IntPtr vProcAddress = GetProcAddress(vLibraryHandle, "G_EncryptStr");
    Delegate_g g = Marshal.GetDelegateForFunctionPointer(vProcAddress, typeof(Delegate_g)) as Delegate_g;
    string str = g("123", "");
    FreeLibrary(vLibraryHandle); 这样写就有返回值了,谢谢大家帮忙了,问题解决,散分了,str是我最后需要的值