Beispiel #1
0
        public static int GetHook(HookTypes hookType, HookProc hookProc)
        {
            int hHook = WinApiMethods.SetWindowsHookEx((int)hookType, hookProc,
                                                       Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);

            return(hHook);
        }
        private void GenerateCallBackException(HookTypes type, SetCallBackResults result)
        {
            if (result == SetCallBackResults.Success)
            {
                return;
            }

            string msg;

            switch (result)
            {
            case SetCallBackResults.AlreadySet:
                msg  = "A hook of type " + type + " is already registered. You can only register ";
                msg += "a single instance of each type of hook class. This can also occur when you forget ";
                msg += "to unregister or dispose a previous instance of the class.";

                throw new ManagedHooksException(msg);

            case SetCallBackResults.ArgumentError:
                msg = "Failed to set hook callback due to an error in the arguments.";

                throw new ArgumentException(msg);

            case SetCallBackResults.NotImplemented:
                msg  = "The hook type of type " + type + " is not implemented in the C++ layer. ";
                msg += "You must implement this hook type before you can use it. See the C++ function ";
                msg += "SetUserHookCallback.";

                throw new HookTypeNotImplementedException(msg);
            }

            msg = "Unrecognized exception during hook callback setup. Error code " + result + ".";
            throw new ManagedHooksException(msg);
        }
Beispiel #3
0
 public static void Disable(HookTypes type)
 {
     GlobalHook hook = Create(type);
     hook.Proc = (HookInvoke) Delegate.Combine(hook.Proc, new HookInvoke(GlobalHook.IgnoreGlobalHook));
     DisabledHooks[type] = hook;
     EventLog.WriteLine("Global hooks of type: {0} have been disabled.", new object[] { type });
 }
Beispiel #4
0
 internal IntPtr SetHook(HookTypes typeOfHook, HookProc callBack)
 {
     using (Process currentProcess = Process.GetCurrentProcess())
         using (ProcessModule currentModule = currentProcess.MainModule)
         {
             return(SetWindowsHookEx((int)typeOfHook, callBack,
                                     GetModuleHandle(currentModule.ModuleName), 0));
         }
 }
Beispiel #5
0
        /// <include file='ManagedHooks.xml' path='Docs/SystemHook/FilterMessage/*'/>
        protected void FilterMessage(HookTypes hookType, int message)
        {
            FilterMessageResults result = InternalFilterMessage(hookType, message);

            if (result != FilterMessageResults.Success)
            {
                GenerateFilterMessageException(hookType, result);
            }
        }
Beispiel #6
0
 public static void Enable(HookTypes type)
 {
     if (DisabledHooks.ContainsKey(type))
     {
         DisabledHooks[type].Uninstall();
         DisabledHooks.Remove(type);
         EventLog.WriteLine("Global hooks of type: {0} have been enabled.", new object[] { type });
     }
 }
Beispiel #7
0
 public static GlobalHook Create(HookTypes type)
 {
     GlobalHook hook = new GlobalHook {
         mHookType = type
     };
     hook.Install();
     EventLog.WriteLine("Created global hook of type: {0}", new object[] { type });
     return hook;
 }
        public static string HookTypeToString(HookTypes hookType)
        {
            if (!s_stringByHookTypes.TryGetValue(hookType, out string result))
            {
                throw new ArgumentException($"Unknown hook type: {hookType}");
            }

            return(result);
        }
Beispiel #9
0
 public void SetWindowsHook(HookTypes idHook)
 {
     if (hHook == 0)
     {
         hHook = NativeMethods.SetWindowsHookEx((int)idHook, hProc, IntPtr.Zero, NativeMethods.GetCurrentThreadId());
         if (hHook == 0)
         {
             throw new Exception("Set Hook Failed!");
         }
     }
 }
Beispiel #10
0
 public void SetGlobalHook(HookTypes idHook)
 {
     if (hHook == 0 && hmod != IntPtr.Zero)
     {
         hHook = NativeMethods.SetWindowsHookEx((int)idHook, hProc, hmod, 0);
         if (hHook == 0)
         {
             throw new Exception("Set Hook Failed!");
         }
     }
 }
Beispiel #11
0
		/// <include file='ManagedHooks.xml' path='Docs/SystemHook/ctor/*'/>
		public SystemHook(HookTypes type)
		{
			_type = type;

			_processHandler = new HookProcessedHandler(InternalHookCallback);
			SetCallBackResults result = SetUserHookCallback(_processHandler, _type);
			if (result != SetCallBackResults.Success)
			{
				this.Dispose();
				GenerateCallBackException(type, result);
			}
		}
        public void ConstructionUnsupportedTypeTests(TestMethodRecord tmr)
        {
            HookTypes type = HookTypes.Hardware;

            tmr.WriteLine("If you implement the hook type HookTypes." + type +
                          ", change the type parameter to continue testing this senario.");

            tmr.RegisterException("An unimplemented hook type will cause an exception.",
                                  typeof(HookTypeNotImplementedException));

            SystemHookTestWrapper hook = new SystemHookTestWrapper(type);
        }
        /// <include file='Internal.xml' path='Docs/SystemHook/ctor/*'/>
        public SystemHook(HookTypes type)
        {
            this.type = type;

            processHandler = new HookProcessedHandler(InternalHookCallback);
            SetCallBackResults result = SetUserHookCallback(processHandler, type);

            if (result != SetCallBackResults.Success)
            {
                this.Dispose();
                GenerateCallBackException(type, result);
            }
        }
Beispiel #14
0
 public virtual void InstallHooks(params HookTypes[] types)
 {
     if (types == null)
         types = new HookTypes[0];
     var selectedHookTypes = types.Select(t => (int)t + 1).ToArray();
     foreach (var i in selectedHookTypes.Where(i => _hooks[i] != null))
     {
         _hooks[i].Enabled = true;
     }
     foreach (var i in _hooks.Select((h, i) => i).Where(i => _hooks[i] != null).Except(selectedHookTypes))
     {
         _hooks[i].Enabled = false;
     }
 }
        public void UnsupportedFilterTypesTest1(TestMethodRecord tmr)
        {
            HookTypes type = HookTypes.Hardware;

            tmr.WriteLine("If you implement the hook type HookTypes." + type +
                          ", change the type parameter to continue testing this senario.");

            using (SystemHookTestWrapper hook = new SystemHookTestWrapper(HookTypes.MouseLL))
            {
                tmr.RegisterException("An unimplemented hook type will cause an exception (filter message).",
                                      typeof(ManagedHooksException));

                hook.FilterMessageWrapper(type, 12345);
            }
        }
        private void GenerateFilterMessageException(HookTypes type, FilterMessageResults result)
        {
            if (result == FilterMessageResults.Success)
            {
                return;
            }

            string msg;

            if (result == FilterMessageResults.NotImplemented)
            {
                msg  = "The hook type of type " + type + " is not implemented in the C++ layer. ";
                msg += "You must implement this hook type before you can use it. See the C++ function ";
                msg += "FilterMessage.";

                throw new HookTypeNotImplementedException(msg);
            }

            //
            // All other errors are general errors.
            //
            msg = "Unrecognized exception during hook FilterMessage call. Error code " + result + ".";
            throw new ManagedHooksException(msg);
        }
Beispiel #17
0
 private static extern bool InitializeHook(HookTypes hookType, UInt32 threadID);
Beispiel #18
0
        public void RegisterLuaFunctions()
        {
            _lua["HookTypes"] = new HookTypes();
            _lua["Hooks"] = _hooks;
            _lua["Game"] = _game;
            _lua["Color"] = new Color();

            _lua["Bans"] = TShock.Bans;
            _lua["Backups"] = TShock.Backups;
            _lua["Groups"] = TShock.Groups;
            _lua["Players"] = TShock.Players;
            _lua["Regions"] = TShock.Regions;
            _lua["Users"] = TShock.Users;
            _lua["Utils"] = TShock.Utils;
            _lua["Warps"] = TShock.Warps;
            _lua["ConfigType"] = new ConfigType();

            //More Lua Functions
            var luaFuncs = new LuaFunctions(this);
            var luaFuncMethods = luaFuncs.GetType().GetMethods();
            foreach (var method in luaFuncMethods)
            {
                _lua.RegisterFunction(method.Name, luaFuncs, method);
            }
        }
Beispiel #19
0
 protected HookBase(HookTypes type)
 {
     Type = type;
 }
Beispiel #20
0
 private static extern IntPtr SetWindowsHookEx(HookTypes code, HookProc func, IntPtr hInstance, int threadID);
 private static extern void DisposeCppLayer(HookTypes hookType);
 private static extern int InitializeHook(HookTypes hookType, int threadID);
Beispiel #23
0
		private void GenerateFilterMessageException(HookTypes type, FilterMessageResults result)
		{
			if (result == FilterMessageResults.Success)
			{
				return;
			}

			string msg;

			if (result == FilterMessageResults.NotImplemented)
			{
					msg = "The hook type of type " + type + " is not implemented in the C++ layer. ";
					msg += "You must implement this hook type before you can use it. See the C++ function ";
					msg += "FilterMessage.";

					throw new HookTypeNotImplementedException(msg);
			}

			//
			// All other errors are general errors.
			//
			msg = "Unrecognized exception during hook FilterMessage call. Error code " + result + ".";
			throw new ManagedHooksException(msg);
		}
Beispiel #24
0
 public virtual HookBase this[HookTypes type]
 {
     get { return _hooks[(int)type + 1]; }
 }
Beispiel #25
0
 private static extern IntPtr SetWindowsHookEx(HookTypes hookType, LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);
Beispiel #26
0
		private void GenerateCallBackException(HookTypes type, SetCallBackResults result)
		{
			if (result == SetCallBackResults.Success)
			{
				return;
			}

			string msg;

			switch (result)
			{
				case SetCallBackResults.AlreadySet:
					msg = "A hook of type " + type + " is already registered. You can only register ";
					msg += "a single instance of each type of hook class. This can also occur when you forget ";
					msg += "to unregister or dispose a previous instance of the class.";

					throw new ManagedHooksException(msg);

				case SetCallBackResults.ArgumentError:
					msg = "Failed to set hook callback due to an error in the arguments.";

					throw new ArgumentException(msg);

				case SetCallBackResults.NotImplemented:
					msg = "The hook type of type " + type + " is not implemented in the C++ layer. ";
					msg += "You must implement this hook type before you can use it. See the C++ function ";
					msg += "SetUserHookCallback.";

					throw new HookTypeNotImplementedException(msg);
			}

			msg = "Unrecognized exception during hook callback setup. Error code " + result + ".";
			throw new ManagedHooksException(msg);
		}
Beispiel #27
0
 private static extern IntPtr SetWindowsHookEx(HookTypes hookType, LowLevelKeyboardProc lowLevelKeyboardProc, IntPtr hMod, uint dwThreadId);
Beispiel #28
0
 public void FilterMessageWrapper(HookTypes type, int message)
 {
     base.FilterMessage(type, message);
 }
Beispiel #29
0
 public HookBase(HookTypes type, HookProc proc)
     : this(type)
 {
     Procedure = new HookProc(proc);
 }
Beispiel #30
0
 private static extern bool InitializeHook(HookTypes hookType, int threadID, IntPtr hwnd);
Beispiel #31
0
		private static extern SetCallBackResults SetUserHookCallback(HookProcessedHandler hookCallback, HookTypes hookType);
Beispiel #32
0
 public static extern IntPtr SetWindowsHookEx(HookTypes hookType, HookProc lpfn, IntPtr hMod, int dwThreadId);
Beispiel #33
0
		private static extern bool InitializeHook(HookTypes hookType, int threadID);
 private static extern SetCallBackResults SetUserHookCallback(HookProcessedHandler hookCallback, HookTypes hookType);
Beispiel #35
0
		private static extern void UninitializeHook(HookTypes hookType);
 private static extern void UninitializeHook(HookTypes hookType);
Beispiel #37
0
		private static extern void DisposeCppLayer(HookTypes hookType);
 private static extern FilterMessageResults InternalFilterMessage(HookTypes hookType, int message);
Beispiel #39
0
		private static extern FilterMessageResults InternalFilterMessage(HookTypes hookType, int message);
Beispiel #40
0
 public static extern int SetWindowsHookEx(HookTypes hookType, HookProc lpfn, IntPtr hInstance, int threadId);
Beispiel #41
0
        private static void SetHook(ref IntPtr hookHandle, HookTypes type, HookProc proc, bool local)
        {
            if (hookHandle != IntPtr.Zero) return;

            hookHandle = Hook.SetWindowsHookEx(type, proc, IntPtr.Zero,
                local ? ProcessThread.GetCurrentThreadId() : 0);
            if (hookHandle == IntPtr.Zero)
                throw new Win32Exception(Marshal.GetLastWin32Error());
        }
Beispiel #42
0
 /// <summary>
 /// Creates a new Hook of the specified type
 /// </summary>
 /// <param name="hookType"></param>
 public GlobalHook(HookTypes hookType)
 {
     _hookType = hookType;
     InstallHook();
 }
Beispiel #43
0
		/// <include file='ManagedHooks.xml' path='Docs/SystemHook/FilterMessage/*'/>
		protected void FilterMessage(HookTypes hookType, int message)
		{
			FilterMessageResults result = InternalFilterMessage(hookType, message);
			if (result != FilterMessageResults.Success)
			{
				GenerateFilterMessageException(hookType, result);
			}
		}
Beispiel #44
0
 public static extern IntPtr SetWindowsHookEx(HookTypes hookType, HookProc hookProc, IntPtr hInstance, int nThreadId);
 /// <summary>
 /// Creates a new Hook of the specified type
 /// </summary>
 /// <param name="hookType"></param>
 public GlobalHook(HookTypes hookType)
 {
     _hookType = hookType;
     InstallHook();
 }
Beispiel #46
0
 public static extern IntPtr SetWindowsHookEx(HookTypes hookType, HookProc hookProc, IntPtr hInstance, int nThreadId);
Beispiel #47
-1
 protected GlobalHook(HookTypes hookType)
 {
     this.mHookType = HookTypes.NONE;
     this.mHandle = IntPtr.Zero;
     this.mProc = new HookProc(this.OnProc);
     this.mHookType = hookType;
 }