Page 1 of 1

Hook causing NullReferenceException

Posted: Tue Jun 26, 2018 2:11 pm
by Davita
Hi

I'm hooking CreateCompatibleDC function into single .NET 32bit application.
Problem is that, when I hook the function, the method that depends on that API crashes with NullReferenceException, even when I don't do anything specific, just call original API from callback. Here's the code:

Code: Select all

HBITMAP (WINAPI *CreateCompatibleDCNext)(_In_ HDC hdc);

HBITMAP CreateCompatibleDCCallback(_In_ HDC hdc)
{
	return CreateCompatibleDCNext(hdc);
}

HookAPI("gdi32.dll", "CreateCompatibleDC", (PVOID)CreateCompatibleDCCallback, (PVOID*)&CreateCompatibleDCNext);
And of course I call InitializeMadCHook.

In the logs, I see this error:
System.Drawing - Object reference not set to an instance of an object.
at System.Drawing.SafeNativeMethods+Gdip.GdipGetDC
at System.Drawing.Graphics.GetHdc
at System.Drawing.Graphics.CopyFromScreen
at System.Drawing.Graphics.CopyFromScreen
I'm also hooking CoCreateInstance and it has same error. Any idea what am I doing wrong? I'm running latest version of MadCodeHook 3 on Windows 10 64bit.

Thanks

Re: Hook causing NullReferenceException

Posted: Tue Jun 26, 2018 2:35 pm
by madshi
Your CreateCompatibleDCCallback function is missing the WINAPI.

Re: Hook causing NullReferenceException

Posted: Tue Jun 26, 2018 3:09 pm
by Davita
damn, I can't recall how many times I'm making such a stupid mistake :( Thanks god we have you :))