CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Hybrid View

  1. #1
    Join Date
    Oct 2009
    Posts
    7

    Catching WM_GETMINMAXINFO from notepad.exe doesn't work

    Hello!

    I want to catch all WM_GETMINMAXINFO messages which are sent to the "nodepad.exe" application.
    My base app is written in C#, the DLL with the windows hook is written in C.

    Let me show you the hook inside the C DLL:
    file: dll.h
    Code:
    #ifndef _DLL_H_
    #define _DLL_H_
    
    #include <windows.h>
    
    #if BUILDING_DLL
    # define DLLIMPORT __declspec (dllexport)
    #else /* Not BUILDING_DLL */
    # define DLLIMPORT __declspec (dllimport)
    #endif /* Not BUILDING_DLL */
    
    #define SHARED __attribute__((section(".shr"), shared))
    
    DLLIMPORT BOOL InstallHook(int i);
    DLLIMPORT BOOL UninstallHook();
    static LRESULT CALLBACK MyWndProc(int nCode, WPARAM wParam, LPARAM lParam);
    
    #endif /* _DLL_H_ */
    file dll.c
    Code:
    /* Replace "dll.h" with the name of your header */
    #include "dll.h"
    #include <stdio.h>
    
    HHOOK hHook SHARED = NULL; // Handle des Hooks
    HINSTANCE hInstance SHARED = NULL; // Handle der DLL
    FILE *fp SHARED;
    
    BOOL APIENTRY DllMain (HINSTANCE hInst     /* Library instance handle. */ ,
                           DWORD reason        /* Reason this function is being called. */ ,
                           LPVOID reserved     /* Not used. */ )
    {
        hInstance = hInst;
    
        /* Returns TRUE on success, FALSE on failure */
        return TRUE;
    }
    
    static LRESULT CALLBACK MyWndProc(int nCode, WPARAM wParam, LPARAM lParam)
    {
            if (nCode < 0)
               CallNextHookEx(hHook, nCode, wParam, lParam);
             
            CWPSTRUCT* cw = (CWPSTRUCT*)(lParam);  
               if ( (cw->message == WM_GETMINMAXINFO))
               {
                  fprintf(fp, "GETMINMAX! \n"); // Never called!
               }
            return CallNextHookEx(hHook, nCode, wParam, lParam);
    }
    
    DLLIMPORT BOOL UninstallHook()
    {
     UnhookWindowsHookEx(hHook);
     hHook = NULL;
     return TRUE;         
    }
    
    DLLIMPORT BOOL InstallHook(int i)
    {
              fp = fopen("log.txt", "w");
    
              hHook = SetWindowsHookEx(WH_CALLWNDPROC, MyWndProc, hInstance, i);
              if (hHook == NULL)
                 return FALSE;   
                 
              return TRUE;          
    }
    In C# I call my DLL in this way:
    Code:
    [DllImport("Hook.dll")]
            public static extern bool InstallHook(int i);
    ...
    
    Process process = new Process();
                process.StartInfo.FileName = "notepad.exe";
                process.Start();
    
                bool b = InstallHook(process.Threads[0].Id);
                if (b == false)
                    MessageBox.Show("Error");
    What am I doing wrong?
    Thank you very much!

    Marlene

  2. #2
    Join Date
    Oct 2009
    Posts
    7

    Re: Catching WM_GETMINMAXINFO from notepad.exe doesn't work

    Hello friends,

    I noticed that even MyWndProc is never called.
    Perhaps it may help you to get an idea to make it work. I would be pleased to get it running.

  3. #3
    Join Date
    Jun 2008
    Posts
    592

    Re: Catching WM_GETMINMAXINFO from notepad.exe doesn't work

    This type of hooking can lead to malicous code. Alot of people here won't just hand out how to hook another app so you can manipulate its nature behaviour. I am not saying you're intentions are bad, but you cleary just joined here and ask such "questionable" question

    but to your question.. you didn't tell the exe to load it( the dll ) in "its" memory dynamically. I will let you figure out the rest
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

  4. #4
    Join Date
    Oct 2009
    Posts
    7

    Smile Re: Catching WM_GETMINMAXINFO from notepad.exe doesn't work

    Hello Joeman, yes you are right, I want to conquer the world

    I got to use it for an application that makes scrollable windows higher than the screen resolution in order to capture windows in their whole size.

    Thank you for the great hint. I will try to use the LoadLibrary and GetProcAddress methods. In a few minutes I will be back

  5. #5
    Join Date
    Oct 2009
    Posts
    7

    Re: Catching WM_GETMINMAXINFO from notepad.exe doesn't work

    Hello Joeman,

    I used the LoadLibrary and GetProcAddress methods in order to set the hook in my dll.
    Code:
    hModule = LoadLibrary(@"Hook.dll");
                if (hModule == 0)
                    MessageBox.Show("Error");
    
                IntPtr intPtr = GetProcAddress(hModule, "InstallHook");
                installHook = (InstallHook) Marshal.GetDelegateForFunctionPointer(intPtr, typeof (InstallHook));
    
                IntPtr uninstallPtr = GetProcAddress(hModule, "UninstallHook");
                uninstallHook = (UninstallHook) Marshal.GetDelegateForFunctionPointer(uninstallPtr, typeof (UninstallHook));
    
                bool b = installHook(process.Threads[0].Id);
    
                if (b == false)
                    MessageBox.Show("Error");
    My hook funtion MyWndProc is just never called. Your hint was to load the dll dynamically.
    Did I do it in the right way?

  6. #6
    Join Date
    Jun 2008
    Posts
    592

    Re: Catching WM_GETMINMAXINFO from notepad.exe doesn't work

    Can you just use SetWindowPos if you just need to make that window bigger?

    http://msdn.microsoft.com/en-us/libr...45(VS.85).aspx
    all you need is the hwnd of that window.

    I suppose I could have been more clear. When I say tell the exe, I mean tell the notepad's process to load the dll up in its memory and not your own process. You have to memory hack some of that memory first.
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured