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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM