Convering a C# low level mouse hook to C++
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Convering a C# low level mouse hook to C++

  1. #1
    Join Date
    Jun 2009
    Posts
    5

    Convering a C# low level mouse hook to C++

    Hi,

    I am trying to convert over a MSDN C# code example to C++ and am struggling.

    I would like to eventually monitor the mouse and keyboard commands so I can block some. Should be fairly simple but I am very much a beginner.

    I am only interested in capturing commands in my form app and the controls within it, not system wide so I should not need to use a dll.

    There seems to be many problems. please help.

    Many Thanks

    Matt.

    Code:
    	private: System::Void Form1_Load(Object^  sender, EventArgs^  e) {
    				 hHook = SetWindowsHookEx(WH_MOUSE_LL, hookProceedure, GetModuleHandle(NULL), 0);
    			 }
     
    HHOOK hHook;
    
    HOOKPROC hookProceedure;
    
    private int hookProceedure(int nCode, IntPtr wParam, IntPtr lParam)
    {
    
    		return CallNextHookEx(hHook, nCode, wParam, lParam); 
    }
    
    //This is the Import for the SetWindowsHookEx function.
    //Use this function to install a thread-specific hook.
    public:
    	[DllImport("user32.dll")]
    	static int SetWindowsHookEx(int idHook, HOOKPROC lpfn, IntPtr hInstance, int threadId);
    
    //This is the Import for the UnhookWindowsHookEx function.
    //Call this function to uninstall the hook.
    public:
    	[DllImport("user32.dll")]
    	static bool UnhookWindowsHookEx(int idHook);
    		
    //This is the Import for the CallNextHookEx function.
    //Use this function to pass the hook information to the next hook procedure in chain.
    public:
    	[DllImport("user32.dll")]
    	static int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam);
    Last edited by cilu; June 3rd, 2009 at 02:22 AM. Reason: code tags

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Convering a C# low level mouse hook to C++

    You want to convert to C++ or C++/CLI? They are not the same. Anyway, from C++/CLI you don't need to import these windows APIs like in C#. You can call them directly, just include <windows.h> or whatever other header is necessary.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Jun 2009
    Posts
    5

    Re: Convering a C# low level mouse hook to C++

    Thanks for the heads up.

    It's clear I am still a long way off understanding what is going on here. I am trying to transition from VB to C++ and the differences are huge.
    I understand the differences between managed and un managed code. My project is a C++/CLI windows form application.
    There are so many different examples of hooking on the web its mind boggling. The problem is the are all different and very few to none for C++/CLI.
    If someone has some working code to give me a head start I would be greatly appreciative. I am after a hook that is just filtering mouse messages for the current form/app it does not have to system wide but I suppose could be.

    Thanks in Advance

    Matt.

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Convering a C# low level mouse hook to C++

    [ redirected ]
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Convering a C# low level mouse hook to C++

    I am trying to transition from VB to C++
    Are you interested in learning C++ (native) or C++/CLI (.NET) ? Do you know the difference ?

    If you're trying to learn C++/CLI I'd recommend learning C# instead. Do you have a specific reason for learning C++/CLI ?

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  6. #6
    Join Date
    Jun 2009
    Posts
    5

    Re: Convering a C# low level mouse hook to C++

    I do a little ANSI C programming for microcontrollers. Which is more like C++ native. I have done a little VB and Flash Action Script which is more like C#. I guess if I can get my head around it all C++ would give me the best of both worlds. Unfortunately knowing a little makes you very impatient I then to start on something hard and learn my way into it. Its a little more interesting that way.
    Any pointer or advice would be great.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center