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语言钩取函数 并奉上使用实例 2021-04-08 110


下一篇:在动态库里获取动态库的模块句柄