• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 335
  • Last Modified:

MCustom MessageBox

Hello,
here : http://www.catch22.net/tuts/msgbox.asp I've red an extremely interesting article about hooking MessageBox'es.
However:
"It is important to highlight the call to GetCurrentThreadId. It is possible to install a system-wide hook by specifying the module-handle of a DLL in which the hook procedure resides. Whenever an hooked event occurs in another process, Windows automatically loads this DLL into that process, which then enables the hook procedure to be executed in that process's address space. This is obviously quite an overhead, and is a feature that we do not require. To avoid this happening, we can specify a NULL module handle, and just install the hook for the current thread."  was a part of the text.

What I want to do now, is adapt the  code so that the hook is system-wide (I know it's "heavy" for a system to have a system-wide hook installed, but I'm just interested in getting this to work, because I'm interested in it.)

So what I want is that EVERY MessageBox from EVERY program is hooked , so that I can replace the YES/NO by a Klingon version (HIja'/ghobe')  (yes, I know it's childish)

Can anyone help me to adapt that code?
0
pimpernel
Asked:
pimpernel
  • 6
  • 3
1 Solution
 
jkrCommented:
>>What I want to do now, is adapt the  code so that the hook is system-wide

Just use

HINSTANCE g_hThisDll

int APIENTRY    DllMain (   HINSTANCE   hInstance,
                            DWORD       dwReason,
                            LPVOID      lpReserved
                        )
{
    if  (   dwReason    ==  DLL_PROCESS_ATTACH)
        {
            g_hThisDll  =   hInstance;

        }

    return( TRUE);
}

//...

HHOOK hMsgBoxHook = SetWindowsHookEx(
        WH_CBT,                // Type of hook
        CBTProc,               // Hook procedure (see below)
        NULL,                  // Module handle. Must be NOT be NULL for system wide hooks
        0
        );
0
 
jkrCommented:
BTW, see also http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwui/html/msdn_hooks32.asp ("Win32 Hooks"). For a system-wide hook, the key is to specify 0 for the thread and provide teh DLL's instance handle. To do so, grab that value in DLL main and export a

HINSTANCE g_hThisDll;

extern "C"
__declspec(dllexport)
void InstallHook () {

HHOOK hMsgBoxHook = SetWindowsHookEx(
       WH_CBT,                // Type of hook
        CBTProc,               // Hook procedure (see below)
       NULL,                  // Module handle. Must be NOT be NULL for system wide hooks
       g_hThisDll
       );
}

from your DLL.
0
 
jkrCommented:
Oh, and that's how to use it:

typedef void (*FPTR_InstallHook) ();

FPTR_InstallHook InstallHook;
HINSTANCE hDll = LoadLibrary("myhook.dll");
InstallHook = (FPTR_InstallHook) GetProcAddress(hDll,"InstallHook");

InstallHook();
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
jkrCommented:
Argh, correction - the hook installer should be

HINSTANCE g_hThisDll;

extern "C"
__declspec(dllexport)
void InstallHook () {

HHOOK hMsgBoxHook = SetWindowsHookEx(
      WH_CBT,                // Type of hook
       CBTProc,               // Hook procedure (see below)
      g_hThisDll,                  // Module handle. Must be NOT be NULL for system wide hooks
      0
      );
}
0
 
pimpernelAuthor Commented:
Thanks for your help, but I think I did something somewhere wrong.
Here is what I did: I made these 2 little program's and the dll:


FIRST : DLL
------------------------------------------------------------------------------------------------------------------------
// test.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "test.h"

HINSTANCE g_hThisDll;
HHOOK hMsgBoxHook;

LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam)
{
      HWND hwnd;
      HWND hwndButton;

      if(nCode < 0)
            return CallNextHookEx(hMsgBoxHook, nCode, wParam, lParam);

      switch(nCode)
      {
      case HCBT_ACTIVATE:

            // Get handle to the message box!
            hwnd = (HWND)wParam;

/*            SetWindowText(hwnd, _T("Message from Bill"));*/

            hwndButton = GetDlgItem(hwnd, IDOK);
            SetWindowText(hwndButton, "HIja'");
            
            return 0;

      }

      return CallNextHookEx(hMsgBoxHook, nCode, wParam, lParam);
}

BOOL APIENTRY DllMain( HINSTANCE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                               )
{
 if  (ul_reason_for_call == DLL_PROCESS_ATTACH)
       {g_hThisDll  =   hModule;}
    return TRUE;
}

extern "C"
__declspec(dllexport)
void InstallHook () {

HHOOK hMsgBoxHook = SetWindowsHookEx(
     WH_CBT,                // Type of hook
      CBTProc,               // Hook procedure (see below)
     g_hThisDll,                  // Module handle. Must be NOT be NULL for system wide hooks
     0
     );
}
------------------------------------------------------------------------------------------------------------------------


SECOND: LOADER
------------------------------------------------------------------------------------------------------------------------
#include <windows.h>
#include <iostream>

using namespace std;

int main(){
typedef void (*FPTR_InstallHook) ();
FPTR_InstallHook InstallHook;
HINSTANCE hDll = LoadLibrary("myhook.dll");
InstallHook = (FPTR_InstallHook) GetProcAddress(hDll,"InstallHook");
Sleep(10000); /* I want it to "stay" 10 seconds */
return 0;}
------------------------------------------------------------------------------------------------------------------------

THIRD : TESTPROGRAM
------------------------------------------------------------------------------------------------------------------------
#include <windows.h>
#include <iostream>

using namespace std;

int main(){
MessageBox(0,"test","test",0);
return 0;}
------------------------------------------------------------------------------------------------------------------------

This combination doesn't works.

I guess it's caused by stupid mistakes, but windows and hooking is all new to me.
0
 
jkrCommented:
You forgot to call 'InstallHook', that should be

SECOND: LOADER
------------------------------------------------------------------------------------------------------------------------
#include <windows.h>
#include <iostream>

using namespace std;

int main(){
typedef void (*FPTR_InstallHook) ();
FPTR_InstallHook InstallHook;
HINSTANCE hDll = LoadLibrary("myhook.dll");
InstallHook = (FPTR_InstallHook) GetProcAddress(hDll,"InstallHook");
InstallHook(); // Install the hook
Sleep(10000); /* I want it to "stay" 10 seconds */
return 0;}
0
 
pimpernelAuthor Commented:
Thanks a lot.
That works indeed.
However, when I change it to
            hwndButton = GetDlgItem(hwnd, IDYES);
            SetWindowText(hwndButton, "HIja'");

            hwndButton = GetDlgItem(hwnd, IDNO);
            SetWindowText(hwndButton, "ghobe'");
And the call a  MessageBox(0,"test","test",MB_YESNO);
It doesn't works ?????

Thanks for all your help.
0
 
pimpernelAuthor Commented:
sorry, forgot to replace the dll. Works now. Thanks a lot.
0
 
jkrCommented:
You're welcome :o)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now