Click here to Skip to main content
15,897,518 members
Articles / Programming Languages / C#

Using Window Messages to Implement Global System Hooks in C#

Rate me:
Please Sign up or sign in to vote.
4.88/5 (68 votes)
3 May 2007GPL311 min read 376.2K   14.9K   255  
Explains how to implement any type of global system hook in C# by using an unmanaged C++ DLL and Windows messages.
<html>
<head>
<META HTTP-EQUIV="Content-Type" content="text/html; charset=Windows-1252">
</head>
<body>
<pre>
<table width=100% bgcolor=#CFCFE5><tr> <td> <font face=arial size=+3>
Build Log
</font></table><table width=* cellspacing=0 cellpadding=0><tr><td width=0 bgcolor=#EDEDF5>&nbsp;</td><td width=0 bgcolor=#FFFFFF>&nbsp;</td><td width=*><pre>
<h3>------- Build started: Project: GlobalCbtHook, Configuration: Release|Win32 -------
</h3>
</pre></table><table width=100% bgcolor=#DFDFE5><tr><td><font face=arial size=+2>
Command Lines
</font></table><table width=* cellspacing=0 cellpadding=0><tr><td width=0 bgcolor=#EDEDF5>&nbsp;</td><td width=0 bgcolor=#FFFFFF>&nbsp;</td><td width=*><pre>Creating temporary file "e:\Visual Studio Projects\GlobalHooks\GlobalCbtHook\Release\RSP00000A.rsp" with contents
[
/O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_USRDLL" /D "GLOBALCBTHOOK_EXPORTS" /D "_WINDLL" /D "_MBCS" /FD /EHsc /MT /GS /Yu"stdafx.h" /Fp"Release/GlobalCbtHook.pch" /Fo"Release/" /Fd"Release/vc70.pdb" /W3 /c /Wp64 /Zi /TP
".\GlobalCbtHook.cpp"
".\DllMain.cpp"
]
Creating command line "cl.exe @"e:\Visual Studio Projects\GlobalHooks\GlobalCbtHook\Release\RSP00000A.rsp" /nologo"
Creating temporary file "e:\Visual Studio Projects\GlobalHooks\GlobalCbtHook\Release\RSP00000B.rsp" with contents
[
/O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_USRDLL" /D "GLOBALCBTHOOK_EXPORTS" /D "_WINDLL" /D "_MBCS" /FD /EHsc /MT /GS /Yc"stdafx.h" /Fp"Release/GlobalCbtHook.pch" /Fo"Release/" /Fd"Release/vc70.pdb" /W3 /c /Wp64 /Zi /TP
".\stdafx.cpp"
]
Creating command line "cl.exe @"e:\Visual Studio Projects\GlobalHooks\GlobalCbtHook\Release\RSP00000B.rsp" /nologo"
Creating command line "rc.exe /fo"Release/GlobalCbtHook.res" ".\GlobalCbtHook.rc""
Creating temporary file "e:\Visual Studio Projects\GlobalHooks\GlobalCbtHook\Release\RSP00000C.rsp" with contents
[
/OUT:"Release/GlobalCbtHook.dll" /INCREMENTAL:NO /NOLOGO /DLL /DEF:"GlobalCbtHook.def" /DEBUG /PDB:"Release/GlobalCbtHook.pdb" /SUBSYSTEM:WINDOWS /OPT:REF /OPT:ICF /IMPLIB:"Release/GlobalCbtHook.lib" /MACHINE:X86  kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib
".\Release\DllMain.obj"
".\Release\GlobalCbtHook.obj"
".\Release\stdafx.obj"
".\Release\GlobalCbtHook.res"
]
Creating command line "link.exe @"e:\Visual Studio Projects\GlobalHooks\GlobalCbtHook\Release\RSP00000C.rsp""
</pre></table><table width=100% bgcolor=#DFDFE5><tr><td><font face=arial size=+2>
Output Window
</font></table><table width=* cellspacing=0 cellpadding=0><tr><td width=0 bgcolor=#EDEDF5>&nbsp;</td><td width=0 bgcolor=#FFFFFF>&nbsp;</td><td width=*><pre>Compiling...
stdafx.cpp
Compiling...
GlobalCbtHook.cpp
DllMain.cpp
Generating Code...
Compiling resources...
Linking...
   Creating library Release/GlobalCbtHook.lib and object Release/GlobalCbtHook.exp
GlobalCbtHook.exp : warning LNK4070: /OUT:SystemHookCore.dll directive in .EXP differs from output filename 'Release/GlobalCbtHook.dll'; ignoring directive
</pre></table><table width=100% bgcolor=#DFDFE5><tr><td><font face=arial size=+2>
Results
</font></table><table width=* cellspacing=0 cellpadding=0><tr><td width=0 bgcolor=#EDEDF5>&nbsp;</td><td width=0 bgcolor=#FFFFFF>&nbsp;</td><td width=*><pre>
Build log was saved at "file://e:\Visual Studio Projects\GlobalHooks\GlobalCbtHook\Release\BuildLog.htm"
GlobalCbtHook - 0 error(s), 1 warning(s)</pre></table><table   width=100% height=20 bgcolor=#CFCFE5><tr><td><font face=arial size=+2>
</font></table></body></html>

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 GNU General Public License (GPLv3)


Written By
Burkina Faso Burkina Faso
I currently live in a small town in Burkina Faso, where I spend some of my free time (during the hours when there's electricity) playing around with C#. For a bit more about my less nerdy activities, check out http://chrisburkina.blogspot.com or http://www.youtube.com/watch?v=Rw1Pb7hlIWw -- neither of them, however, has anything to do with C#.

Comments and Discussions