Click here to Skip to main content
16,013,516 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Reading a USB device Pin
koumodaki27-Mar-08 9:41
koumodaki27-Mar-08 9:41 
QuestionRe: Reading a USB device Pin
David Crow27-Mar-08 10:14
David Crow27-Mar-08 10:14 
GeneralRe: Reading a USB device Pin
JudyL_MD27-Mar-08 10:25
JudyL_MD27-Mar-08 10:25 
GeneralRe: Reading a USB device Pin
David Crow27-Mar-08 10:30
David Crow27-Mar-08 10:30 
QuestionAutomatically generate serialization code? Pin
Adrian Idar (Ln)27-Mar-08 5:53
Adrian Idar (Ln)27-Mar-08 5:53 
QuestionRe: Automatically generate serialization code? Pin
CPallini27-Mar-08 6:00
mveCPallini27-Mar-08 6:00 
QuestionGdiplus::Bitmap will using a file in read-exclusive mode? Pin
followait27-Mar-08 5:11
followait27-Mar-08 5:11 
Questionhow to hook the window API such as messagebox? thanks Pin
King Tran27-Mar-08 5:10
King Tran27-Mar-08 5:10 
i want to hook the windows API such as messagebox, i find the code in the net, but it dosn't work, why? the code as follows:

typedef struct tag_HOOKAPI
{
LPCSTR szFunc;
PROC pNewProc;
PROC pOldProc;
}HOOKAPI, *LPHOOKAPI;

HOOKAPI api[1];
api[0].szFunc ="MessageBoxA";
api[0].pNewProc = (PROC)MessageBoxA1;
HookAPIByName(GetModuleHandle(NULL),"User32.dll",&api[0]);

extern "C" __declspec(dllexport)PIMAGE_IMPORT_DESCRIPTOR
LocationIAT(HMODULE hModule, LPCSTR szImportMod)

{

PIMAGE_DOS_HEADER pDOSHeader = (PIMAGE_DOS_HEADER) hModule;
if(pDOSHeader->e_magic != IMAGE_DOS_SIGNATURE) return NULL;
PIMAGE_NT_HEADERS pNTHeader = (PIMAGE_NT_HEADERS)((DWORD)pDOSHeader+
(DWORD)(pDOSHeader->e_lfanew));
if(pNTHeader->Signature != IMAGE_NT_SIGNATURE) return NULL;
if(pNTHeader->OptionalHeader.DataDirectory
[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress == 0)
return NULL;

PIMAGE_IMPORT_DESCRIPTOR pImportDesc =
(PIMAGE_IMPORT_DESCRIPTOR)((DWORD)pDOSHeader + (DWORD)
(pNTHeader->OptionalHeader.DataDirectory
[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress));

while (pImportDesc->Name)
{
PSTR szCurrMod = (PSTR)((DWORD)pDOSHeader +
(DWORD)(pImportDesc->Name));
if (stricmp(szCurrMod, szImportMod) == 0) break;
pImportDesc++;
}
if(pImportDesc->Name == NULL) return NULL;
return pImportDesc;
}

extern "C" __declspec(dllexport) HookAPIByName( HMODULE hModule, LPCSTR szImportMod, LPHOOKAPI pHookApi)

{
PIMAGE_IMPORT_DESCRIPTOR pImportDesc = LocationIAT(hModule, szImportMod);
if (pImportDesc == NULL) return FALSE;

PIMAGE_THUNK_DATA pOrigThunk = (PIMAGE_THUNK_DATA)((DWORD)hModule + (DWORD)(pImportDesc->OriginalFirstThunk));

PIMAGE_THUNK_DATA pRealThunk = (PIMAGE_THUNK_DATA)((DWORD)hModule + (DWORD)(pImportDesc->FirstThunk));

while(pOrigThunk->u1.Function)
{
if((pOrigThunk->u1.Ordinal & IMAGE_ORDINAL_FLAG) != IMAGE_ORDINAL_FLAG)
{
PIMAGE_IMPORT_BY_NAME pByName =(PIMAGE_IMPORT_BY_NAME)((DWORD)hModule+(DWORD)(pOrigThunk->u1.AddressOfData));
if(pByName->Name[0] == '\0') return FALSE;
if(strcmpi(pHookApi->szFunc, (char*)pByName->Name) == 0)
{
MEMORY_BASIC_INFORMATION mbi_thunk;
VirtualQuery(pRealThunk, &mbi_thunk, sizeof(MEMORY_BASIC_INFORMATION));
VirtualProtect(mbi_thunk.BaseAddress,mbi_thunk.RegionSize, PAGE_READWRITE, &mbi_thunk.Protect);

if(pHookApi->pOldProc == NULL)
pHookApi->pOldProc = (PROC)pRealThunk->u1.Function;

pRealThunk->u1.Function = (PDWORD)pHookApi->pNewProc;
DWORD dwOldProtect;
VirtualProtect(mbi_thunk.BaseAddress, mbi_thunk.RegionSize, mbi_thunk.Protect, &dwOldProtect);
}
}
pOrigThunk++;
pRealThunk++;
}
SetLastError(ERROR_SUCCESS);
return TRUE;
}

static int WINAPI MessageBoxA1 (HWND hWnd , LPCTSTR lpText, LPCTSTR lpCaption, UINT uType)
{

return MessageBox(hWnd, "Hook API OK!", "Hook API", uType);
}
AnswerRe: how to hook the window API such as messagebox? thanks Pin
James R. Twine27-Mar-08 7:32
James R. Twine27-Mar-08 7:32 
AnswerRe: how to hook the window API such as messagebox? thanks Pin
Stephen Hewitt27-Mar-08 14:35
Stephen Hewitt27-Mar-08 14:35 
GeneralRe: how to hook the window API such as messagebox? thanks Pin
King Tran29-Mar-08 2:45
King Tran29-Mar-08 2:45 
GeneralHelp me please Pin
Member 417463927-Mar-08 4:31
Member 417463927-Mar-08 4:31 
GeneralRe: Help me please PinPopular
led mike27-Mar-08 4:42
led mike27-Mar-08 4:42 
GeneralRe: Help me please Pin
David Crow27-Mar-08 4:57
David Crow27-Mar-08 4:57 
GeneralRe: Help me please Pin
led mike27-Mar-08 5:05
led mike27-Mar-08 5:05 
GeneralRe: Help me please Pin
Member 417463927-Mar-08 5:26
Member 417463927-Mar-08 5:26 
QuestionRe: Help me please Pin
CPallini27-Mar-08 5:34
mveCPallini27-Mar-08 5:34 
GeneralRe: Help me please Pin
Member 417463927-Mar-08 5:43
Member 417463927-Mar-08 5:43 
GeneralRe: Help me please Pin
CPallini27-Mar-08 5:58
mveCPallini27-Mar-08 5:58 
GeneralRe: Help me please Pin
toxcct27-Mar-08 6:19
toxcct27-Mar-08 6:19 
GeneralRe: Help me please Pin
CPallini27-Mar-08 6:33
mveCPallini27-Mar-08 6:33 
GeneralException-Safe Copy Assignment Pin
George_George27-Mar-08 4:14
George_George27-Mar-08 4:14 
GeneralRe: Exception-Safe Copy Assignment Pin
led mike27-Mar-08 4:23
led mike27-Mar-08 4:23 
GeneralRe: Exception-Safe Copy Assignment Pin
George_George27-Mar-08 4:42
George_George27-Mar-08 4:42 
GeneralRe: Exception-Safe Copy Assignment Pin
CPallini27-Mar-08 4:32
mveCPallini27-Mar-08 4:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.