Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Global System Hooks in .NET

, 9 Jan 2005 CPOL
A class library for using *global* system hooks in .NET.
systemhooks.zip
SystemHooks
Documentation
Managed Hooks.chm
ManagedHooks.ndoc
External References
HarnessIt - [Home].url
UnitedBinary.UnitTests.Framework.TestBasis.dll
ManagedHooks
ManagedHooks.csproj.user
ManagedHooksTests
ManagedHooks Tests.htp
ManagedHooksTests.csproj.user
SampleHookingApp
App.ico
SampleHookingApp.csproj.user
Stable Reference
Kennedy.ManagedHooks.dll
SampleHookingApp.exe
SystemHookCore.dll
SystemHookCore
SystemHookCore.def
systemhookscompiledonly.zip
SystemHooksCompiledOnly
Kennedy.ManagedHooks.dll
Managed Hooks.chm
SampleHookingApp.exe
SystemHookCore.dll
// /////////////////////////////////////////////////////////////
// File: SystemHookTests.cs	Class: Kennedy.ManagedHooks.Tests.SystemHookTests
// Date: 2/25/2004			Author: Michael Kennedy
// Language: C#				Framework: .NET
//
// Copyright: Copyright (c) Michael Kennedy, 2004-2005
// /////////////////////////////////////////////////////////////
// License: See License.txt file included with application.
// Description: See compiled documentation (Managed Hooks.chm)
// /////////////////////////////////////////////////////////////

//
// SOFTWARE REQUIREMENT: To run these tests you will need to download and install HarnessIt.
// You can do this free of charge at http://www.unittesting.com and I strongly encourage
// you to do so, especially if you plan to modify the library.
//

using System;

using Kennedy.ManagedHooks;

using UnitedBinary.UnitTests.Framework;

namespace Kennedy.ManagedHooks.Tests
{
	public class SystemHookTests
	{
		public SystemHookTests() {}

		[TestMethod]
		public void HookInstallTests(TestMethodRecord tmr)
		{
			using (SystemHookTestWrapper hook = new SystemHookTestWrapper(HookTypes.MouseLL))
			{
				//
				// Test that we can install the hook as a local hook.
				//
				hook.InstallHook();

				//
				// Failure is signalled with an exception, so there is nothing 
				// to actually test. That's why we're passing true.
				//
				tmr.RunTest(true, "Install the hook as a global mouse hook.");
			
				hook.UninstallHook();
			}
		}

		[TestMethod]
		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);
		}

		[TestMethod]
		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);
			}
		}

		[TestMethod]
		public void UnsupportedFilterTypesTest2(TestMethodRecord tmr)
		{
			using (SystemHookTestWrapper hook = new SystemHookTestWrapper(HookTypes.MouseLL))
			{
				hook.FilterMessageWrapper(12345);

				tmr.RunTest(true, "Filter message succeeded on HookTypes.MouseLL.");
			}
		}

		[TestMethod]
		public void SingletonErrorTest1(TestMethodRecord tmr)
		{
			using (SystemHookTestWrapper hook1 = new SystemHookTestWrapper(HookTypes.MouseLL))
			{
				tmr.RegisterException("Creating a second hook of the same type will fail.", typeof(ManagedHooksException));
				SystemHookTestWrapper hook2 = new SystemHookTestWrapper(HookTypes.MouseLL);
			}
		}

		[TestMethod]
		public void UtilityMethodTests1(TestMethodRecord tmr)
		{
			using (SystemHookTestWrapper hook = new SystemHookTestWrapper(HookTypes.MouseLL))
			{
				int vkCode = 0;

				tmr.RegisterException("Invalid LPARAM in GetKeyboardReading utility method is not allowed..",
					typeof(ManagedHooksException));

				hook.GetKeyboardReadingWrapper(new UIntPtr(512), new IntPtr(0), ref vkCode);
			}
		}

		[TestMethod]
		public void UtilityMethodTests2(TestMethodRecord tmr)
		{
			using (SystemHookTestWrapper hook = new SystemHookTestWrapper(HookTypes.MouseLL))
			{
				int x = 0, y = 0;

				tmr.RegisterException("Invalid LPARAM in GetMousePosition utility method is not allowed..",
					typeof(ManagedHooksException));

				hook.GetMousePositionWrapper(new UIntPtr(512), new IntPtr(0), ref x, ref y);
			}
		}
		
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Michael Kennedy
Instructor / Trainer DevelopMentor
United States United States
Michael Kennedy is a founding partner and software engineer at United Binary, LLC (http://www.unitedbinary.com [^]) and he is active in the agile software development community. Michael has been developing software for over 10 years. The last 4 of those years have been solidly focused on .NET development. For more information, please visit his website http://www.michaelckennedy.net [^]
 
In a previous life, Michael was pursuing a fairly successful career in mathematics before he saw the True Light and chose The Way of Programming.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 10 Jan 2005
Article Copyright 2004 by Michael Kennedy
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid