Windows Develop Bookmark and Share   
 index > Windows Forms General > Hooking mouse events of a program from my app
 

Hooking mouse events of a program from my app

Hi experts,

I should write an app it should hook the mouse events not from entire desktop,it should hook them from an app which I know its main window title(i.e media player)
this is my code but it doesn't work

Code:
  [DllImport("user32.dll")]
  public static extern IntPtr SetWindowsHookEx(int param,HookEventHandler hookHandler,IntPtr handle,int thread);
  public delegate void HookEventHandler();
  int WH_MOUSE=14;
  int c=0;//a counter
  private void hey()
  {
   c++;
   textBox1.Text=c.ToString();
  }
  //for avoiding being garbage collected
  [MarshalAsAttribute(UnmanagedType.FunctionPtr)]
  HookEventHandler hookDelegate;
  private void hook()
  {
   int a=0;
   foreach(Process p in Process.GetProcesses())
if(p.MainWindowTitle=="Windows Media Player") 
 a=p.Threads[0].Id;//????
hookDelegate=new HookEventHandler(hey);
   Form1.SetWindowsHookEx(WH_MOUSE,hookDelegate,
Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),a);
  }
  private void Form1_Load(object sender, System.EventArgs e)
  {
   this.hook();
  }
when I call Form1.SetWindowsHookEx with 0 it works but it hooks events from the entire desktop not from media player window.

any opinion?
thank you
MigrationUser 1  Wednesday, May 18, 2005 10:04 AM

I'm not entirely sure how to do this (details are fuzzy!) but you might want to look at your Process MainWindowHandle property to set the handle hook to your application. 

You'll also need to make a call to the CallNextHookEx method to make sure that any other hooks that are added are called, too.

Setting Winodws hooks like this is a bit tricky in general from what I remember and you might want to read up on it to make sure you're covering all the bases of doing it properly.

-Paul
MigrationUser 1  Wednesday, May 18, 2005 10:56 AM
thanks for the reply,
in MSDN in SetWindowsHookEx function its fourth parameter takes the Id of the thread
my problem is there

=============================================
qoute MSDN

 HHOOK SetWindowsHookEx(
  int idHook,        // hook type
  HOOKPROC lpfn,     // hook procedure
  HINSTANCE hMod,    // handle to application instance
  DWORD dwThreadId   // thread identifier
);
lpfn 
[in] Pointer to the hook procedure. If the dwThreadId parameter is zero or specifies the identifier of a thread created by a different process, the lpfn parameter must point to a hook procedure in a dynamic-link library (DLL). Otherwise, lpfn can point to a hook procedure in the code associated with the current process. 
hMod 
[in] Handle to the DLL containing the hook procedure pointed to by the lpfn parameter. The hMod parameter must be set to NULL if the dwThreadId parameter specifies a thread created by the current process and if the hook procedure is within the code associated with the current process. 
dwThreadId 

[in] Specifies the identifier of the thread with which the hook procedure is to be associated. If this parameter is zero, the hook procedure is associated with all existing threads running in the same desktop as the calling thread. 
=========================================
in this pice of my code
private void hook()
{
int a=0;
foreach(Process p in Process.GetProcesses())
if(p.MainWindowTitle=="Windows Media Player") 
a=p.Threads[0].Id;//????
            hookDelegate=new HookEventHandler(hey);
Form1.SetWindowsHookEx(WH_MOUSE,hookDelegate,
Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),a);
//here instead of *a* if I send 0 as the fourth parameter it hooks 
//mouse event from the entire desktop not just media player window
}
I think I should find the id of media player thread and send it as fourth parameter but it doesn't work
I try to find it when I find its process 
foreach(Process p in Process.GetProcesses())
if(p.MainWindowTitle=="Windows Media Player") 
a=p.Threads[0].Id;//????but I don't know where I am wrong?
MigrationUser 1  Wednesday, May 18, 2005 12:46 PM

You can use google to search for other answers

Custom Search

More Threads

• Locate file path
• Deselect treeNode in TreeView
• Accessing Resources with ResourceManager
• How to create a NotifyIcon with 2 ContextMenuStrips - LClick and RClick
• Stupid little "SplitterDistance" problem
• WinForms app not closing right
• Accessing Form Controls From Another Class
• ListViewItemCollection.Remove throwing NullReferenceException
• Load NotifyIcon with class
• Need Help Commiting DataSet to Database