c# 调用User32.dll

获取当前窗口句柄:GetForegroundWindow()
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetForegroundWindow();

使用方法   IntPtr myPtr=GetForegroundWindow();
  [DllImport("kernel32.dll")]
   static extern IntPtr LoadLibrary(string lpFileName);

   [DllImport("kernel32.dll", SetLastError = true)]
   static extern bool FreeLibrary(IntPtr hModule);
// <summary>
        /// Loadlibrary 返回的函数库模块的句柄
        /// </summary>
        private IntPtr hModule = IntPtr.Zero;
     /// <summary>
        /// 装载 Dll
        /// </summary>
        /// <param name="lpFileName">DLL 文件名 </param>
        public void LoadDll(string lpFileName)
        {
            hModule = LoadLibrary(lpFileName);
            if (hModule == IntPtr.Zero)
            {
                throw (new Exception(" 没有找到 :" + lpFileName + "."));
            }
        }
        /// <summary>
        /// 卸载 Dll
        /// </summary>
        public void UnLoadDll()
        {
            FreeLibrary(hModule);
            hModule = IntPtr.Zero;
        }
    

 

c# 调用User32.dll

上一篇:如何使用Senparc.Weixin SDK 底层的Redis缓存并设置过期时间


下一篇:如何在Mac或Windows上下载和安装R,RStudio,Anaconda