private static void EnsureSubscribedToGlobalMouseEvents()
        {
            // install Mouse hook only if it is not installed and must be installed
            // 필요한 경우에만 마우스 훅을 설치
            if (s_MouseHookHandle == 0)
            {
                //See comment of this field. To avoid GC to clean it up.
                s_MouseDelegate = MouseHookProc; // 마우스훅 proc에서 받아온 값을 대리자에 전달하고
                //install hook

                //s_MouseHookHandle = SetWindowsHookEx(
                //    WH_MOUSE_LL,
                //    s_MouseDelegate,
                //    Marshal.GetHINSTANCE(
                //        Assembly.GetExecutingAssembly().GetModules()[0]),
                //    0);

                var mar = ImportFunctions.LoadLibrary("user32.dll");    //FrameWork 4.0 and up, Using this is alternative
                s_MouseHookHandle = ImportFunctions.SetWindowsHookEx(
                    Constants.WH_MOUSE_LL,
                    s_MouseDelegate,
                    mar,
                    0);

                //If SetWindowsHookEx fails.
                if (s_MouseHookHandle == 0)
                {
                    //Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set.
                    int errorCode = Marshal.GetLastWin32Error();
                    //do cleanup

                    //Initializes and throws a new instance of the Win32Exception class with the specified error.
                    throw new Win32Exception(errorCode);
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Installs the global hook
        /// </summary>
        public void hook()
        {
            IntPtr hInstance = ImportFunctions.LoadLibrary("User32");

            hhook = ImportFunctions.SetWindowsHookEx(Constants.WH_KEYBOARD_LL, khp, hInstance, 0);
        }