i am trying Hook API,are you help me???,source or code
Printable View
i am trying Hook API,are you help me???,source or code
Click on "Search" on Google or Google Groups.
Hundreds of complete samples... (C, C++, asm Win32, etc)
It's not very clear what exactly you want to accomplish.
However, I hope this article from Codeguru can be a good starting point: Fancy Custom MessageBox.
Here is a sample of hooking MessageBox using Microsoft's Microsoft Detours
Code:#pragma comment(lib, "detours.lib")
#pragma comment(lib, "detoured.lib")
#pragma comment(lib, "user32.lib")
#include <windows.h>
#include <stdio.h>
#include "detours.h"
#define ID_BUTTONMESSAGE 200
INT (WINAPI * trueMessageBox)(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType) = MessageBox;
INT WINAPI hookedMessageBox(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType)
{
LPCSTR lpNewCaption = "You've been hijacked";
int iReturn = trueMessageBox(hWnd, lpText, lpNewCaption, uType);
return iReturn;
}
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
VOID SetupDetour(void)
{
DWORD dwError;
DetourRestoreAfterWith();
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)trueMessageBox, hookedMessageBox);
dwError = DetourTransactionCommit();
if (dwError == NO_ERROR)
MessageBox(NULL, "MessageBox hook installed successfully", "HOOK", MB_OK);
else
MessageBox(NULL, "MessageBox hook installation failed", "Error", MB_OK);
}
INT WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("MessageBox Hook") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetSysColorBrush(COLOR_3DFACE) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName,
TEXT ("MessageBox Hook"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
700,
300,
NULL,
NULL,
hInstance,
NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
CreateWindow( "button", "Display MessageBox",
WS_VISIBLE | WS_CHILD ,
200, 50, 180, 25,
hwnd, (HMENU) ID_BUTTONMESSAGE, NULL, NULL);
SetupDetour();
return 0 ;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case ID_BUTTONMESSAGE:
MessageBox(hwnd, "This is a test of the hook, note caption", "Test", MB_OK);
break;
}
break;;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
Try this one:
http://www.codeguru.com/forum/showthread.php?t=469549
@BobS0327 Hi I managed to get the above code working, though I would like to know if there is a way to make this work across different processes e.g. notepad.exe?