CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  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

  7. #7
    Join Date
    Oct 2009
    Posts
    7

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

    When you use SetWindowPos you cannot make the window bigger than the screen resolution. It is limited by the WM_GETMINMAXINFO structure.

    For example, Spy++ catches all messages from any external window, too. So I can't imagine that the only solution is to use a memory hack.

    I've read that global hooks require an unmanaged DLL because the got to load it into their memory when you set the hook.

    Therefore I think the solution is to set a hook like Spy++ does. (I think it does so..)
    Do you have another idea?

  8. #8
    Join Date
    Jun 2008
    Posts
    592

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

    Unless c# provides a way... not really. I believe there are a few more methods, but this is the most common way to do such a thing. Really I actually found an article here on codeguru that does it this way.

    http://www.codeguru.com/Cpp/W-P/dll/article.php/c105/
    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

  9. #9
    Join Date
    Jun 2008
    Posts
    592

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

    The method your using is suppose to work.. I haven't done it this way, but I can review it some more.
    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

  10. #10
    Join Date
    Oct 2009
    Posts
    7

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

    Thank you, it would be very nice.

  11. #11
    Join Date
    Jun 2008
    Posts
    592

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

    I have a working example written in c++ that uses setwindowhookex. I learned you can't modify the messages at all Do you need to modify the messages?
    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

  12. #12
    Join Date
    Oct 2009
    Posts
    7

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

    Hi Joeman,

    thank you very much for your help. I've got the solution.

    First of all, it was wrong to use the file pointer in a shared DLL. When I replaced it with PostMessage, my main application received a notification when the Callback was called.

    The solution to manipulate the WM_GETMINMAXINFO is to subclass the window in the callback function like this:
    Code:
    if ( (cw->message == WM_GETMINMAXINFO))
               {
                  LONG_PTR proc = SetWindowLongPtr(cw->hwnd, GWL_WNDPROC,
                                             (LONG_PTR)WndProc);
                  SetProp(cw->hwnd,
                    "_old_proc_",
                    (HANDLE) (proc));
    Then a new function is called where I can manipulate the message:
    Code:
    LRESULT
    CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
     HANDLE hdl = GetProp(hwnd, "_old_proc_");
     RemoveProp(hwnd, "_old_proc_");
     SetWindowLongPtr(hwnd, GWL_WNDPROC, (LONG_PTR) (hdl));
     
     switch (uMsg)
        {
            case WM_GETMINMAXINFO:
            {
                MINMAXINFO* minmax = (MINMAXINFO*) (lParam);
                minmax->ptMinTrackSize.x = 2500;
            }
            break;
            case WM_WINDOWPOSCHANGING:
            {
                WINDOWPOS* wpos    = (WINDOWPOS*) (lParam);
                wpos->cx = 2500;
            }
            break;
            case WM_WINDOWPOSCHANGED:
            break;
    
            default:
                return CallWindowProc(
                    (WNDPROC) (hdl),
                    hwnd,
                    uMsg,
                    wParam,
                    lParam);
                break;
    
        }        
    }
    Thank you very much for you effort

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