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
/* 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");
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
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.
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.
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:
Bookmarks