views:

160

answers:

0

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

#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

/* 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:

[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