I want to override part of a functionality of certain winapi function, SetWindowPos. I'm only trying to prevent any Z axis operations - putting windows behind or on top. My basic pseudocode idea was:
bool NewSetWindowPos(zaxis_argument, ...) {
return OldSetWindowPos(NULL, ...);
}
I use Mhook library and it works perfectly.
The implementation is a little bit clumsy, as I'm casting variables to functions which always looks weird:
//Get the function refference for overriding with mhook
PNT_QUERY_SYSTEM_INFORMATION OriginalSetWindowPos =
(PNT_QUERY_SYSTEM_INFORMATION)::GetProcAddress(
::GetModuleHandle( L"user32" ), "SetWindowPos" );
I define a type that follows the original function argument count:
//Define type for the original SetWindowPos function
typedef BOOL(*SetWindowPos_type)(
_In_ HWND hWnd,
_In_opt_ HWND hWndInsertAfter,
_In_ int X,
_In_ int Y,
_In_ int cx,
_In_ int cy,
_In_ UINT uFlags
);
And here's my new function:
BOOL WINAPI
HookedSetWindowPos(
_In_ HWND hWnd,
_In_opt_ HWND hWndInsertAfter,
_In_ int X,
_In_ int Y,
_In_ int cx,
_In_ int cy,
_In_ UINT uFlags
) {
//The |SWP_NOZORDER adds flag that instructs the function to ignore any Z-order operations.
//Ther than Z-order functionality is still available, I just pass the original arguments
return ((SetWindowPos_type)OriginalSetWindowPos)(hWnd, 0, X, Y, cx, cy, uFlags|SWP_NOZORDER);
}
I hook that function on DLL init:
case DLL_PROCESS_ATTACH:
//Override function
Mhook_SetHook( (PVOID*)&OriginalSetWindowPos, HookedSetWindowPos );
//Force create console window
createConsole();
break;
The problem is that OriginalSetWindowPos somehow maps to HookedSetWindowPos. Once SetWindowPos is called, HookedSetWindowPos calls OriginalSetWindowPos which is actually HookedSetWindowPos too - and that loops forever, or at least until the process crashes due to stack overflow.
Q: How do I call original function after I have hooked a function in DLL?
Aucun commentaire:
Enregistrer un commentaire