编程语言
首页 > 编程语言> > c# 调用User32.dll

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

 

标签:IntPtr,GetForegroundWindow,hModule,c#,DllImport,dll,lpFileName,User32
来源: https://www.cnblogs.com/zutian/p/11235029.html