Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

win32 hook - easy

Posted on 2003-12-05
12
747 Views
Last Modified: 2008-02-01
here is my code for a local windows hook, the important bit is maked with commentary:


                      [DllImport("user32.dll")]
            public static extern long SetWindowsHookEx(long idHook,long lpfn,long hmod,long dwThreadId);

            [DllImport("user32.dll")]
            public static extern long CallNextHookEx(long idHook,long ncode,long wParam,long lParam);

            [DllImport("user32.dll")]
            public static extern long UnhookWindowsHookEx(long hHook);            
                        
            public const int WH_KEYBOARD=2;      
            public long hHook;
      
            public long Keypress(long idHook,long wParam,long lParam)
            {
                  MessageBox.Show("D");
                  return CallNextHookEx(hHook,idHook,wParam,lParam);
            }

            private void button1_Click(object sender, System.EventArgs e)
            {
                  hHook=SetWindowsHookEx(WH_KEYBOARD,0,0,0);//ERROR in last 3 values should be: address, wParam, lParam in that order
            }

            private void button2_Click(object sender, System.EventArgs e)
            {
                  UnhookWindowsHookEx(hHook);
            }

my question:

1) how do i say that the 'address' is the function "Keypress"?
2) what do wParam and lParam do?
2) what values are required for wParam and lParam?

note: ive already posted this in the programming section but i wanted to move it here and to put more points on it... if u answer this correctly i will also give u the 55 points i have on the other question in programming (its asked from my other account suma_ds)

thankz 4 readin... suma

0
Comment
Question by:dear_jimmy
12 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 9887417
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 75 total points
ID: 9887445
hHook=SetWindowsHookEx(WH_KEYBOARD, this.Keypress, IntPtr.Zero, AppDomain.GetCurrentThreadId());

Last two parameters of SetWindowsHookEx are not wParam and lParam. They are application handle and thread ID.
You can find description of wParam and lParam for Keypress function here:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/Windowing/Hooks/HookReference/HookFunctions/KeyboardProc.asp

I suggest you also to use Trace instead of message box inside of hook function. Hopok function should perform very fast, otherwise you can get unpredictable results.
0
 

Author Comment

by:dear_jimmy
ID: 9887923
so thats how its done... all good except i get this obvious error which i do not know what to do about:

Method 'Local_Keyboard_Hook.Form1.Keypress(long, long, long)' referenced without parentheses

surely i am not expected to assign values into the Keypress function??

and about the messagebox thing... its only there as a way for me to instantly see that the hook is working - i will be removing it when i get the code working

thanks again, suma
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 48

Expert Comment

by:AlexFM
ID: 9888462
I think this should be done using delegate, something like:

public delegate long HookDelegate(long idHook,long wParam,long lParam);

hHook=SetWindowsHookEx(WH_KEYBOARD, new HookDelegate(this.Keypress), IntPtr.Zero, AppDomain.GetCurrentThreadId());

I just guess since I am not in VS .NET now. I can extract the exact code from MSDN sample, if necessary. Try this meanwhile.
0
 

Author Comment

by:dear_jimmy
ID: 9889995
well this should to work but i cant try it out as im having serous trouble with the type of varibles i use... clearly they cant all be longs
they r supposed to be:

HHOOK SetWindowsHookEx(
  int idHook,                
  HOOKPROC lpfn,        
  HINSTANCE hMod,      
  DWORD dwThreadId
);

but HOOKPROC, HINSTANCE, and DWORD do not exist in c# :(
0
 

Author Comment

by:dear_jimmy
ID: 9890051
also no matter what type of varible lfpn is, it cannot convert type Local_Keyboard_Hook.Form1.HookDelegate to the same type as lfpn...
0
 

Author Comment

by:dear_jimmy
ID: 9890967
the last post was screwed up:

public static extern int SetWindowsHookEx(int idHook,IntPtr lpfn,IntPtr hMod,int dwThreadId);

hHook=SetWindowsHookEx(WH_KEYBOARD,(IntPtr)new HookDelegate(this.Keypress),IntPtr.Zero,(int)AppDomain.GetCurrentThreadId());

here the value being assigned to lfpn is (IntPtr)new HookDelegate(this.Keypress) and this gives the error.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 9891445
public delegate int HookProc(int code, IntPtr wParam, IntPtr lParam);
IntPtr m_hhook;

int Keypress(int code, IntPtr wParam, IntPtr lParam)
{
   // ...
}

private void button1_Click(object sender, System.EventArgs e)
{
  // install hook
  m_hhook = SetWindowsHookEx(
      2,     // WH_KEYBOARD  see enum HookType from demo project
      new HookProc(this.KeyPress),
      IntPtr.Zero,
      (int) AppDomain.GetCurrentThreadId());
}

This code is taken from the demo project from the article pointed by my first post.
0
 

Author Comment

by:dear_jimmy
ID: 9893855
but can u compile this code because i cannot.

i am importing SetWindowsHookEx with the parameters: (int idHook,IntPtr lpfn,IntPtr hmod,int dwThreadId);

and i get the error message: Cannot convert type HookProc to IntPtr

if i change the lpfn parameter to a integer i get the message: Cannot convert type HookProc to int

i've heard  that every method has a signature but i dont know how to get it... maybe the lpfn needs the method's signature i dunno
0
 
LVL 20

Assisted Solution

by:TheAvenger
TheAvenger earned 60 total points
ID: 9895079
Change your Win32 function declarations and types to what is given in the article provided by AlexFM. You can find them all here:

http://msdn.microsoft.com/msdnmag/issues/02/10/CuttingEdge/figures.asp#fig2

Then you will be able to compile.
0
 
LVL 1

Expert Comment

by:suma_ds
ID: 9899837
thanks man... i discovered what it was: the address varible lfpn needed to be declared as a delegate not an int

this article helps a lot (with the other varibles) so ima split the points

laterz... suma
0
 

Author Comment

by:dear_jimmy
ID: 9899846
opps i used wrong account ^
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Simple Injector with Web Service 4 54
What can cause the styling on a .NET site to not be found? 17 28
C# Gridview 1 45
VB.NET 2008 Winforms Signing 13 30
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question