Hook causing NullReferenceException

c++ / delphi package - dll injection and api hooking
Post Reply
Davita
Posts: 163
Joined: Tue Sep 13, 2005 7:31 pm

Hook causing NullReferenceException

Post 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
madshi
Site Admin
Posts: 10753
Joined: Sun Mar 21, 2004 5:25 pm

Re: Hook causing NullReferenceException

Post by madshi »

Your CreateCompatibleDCCallback function is missing the WINAPI.
Davita
Posts: 163
Joined: Tue Sep 13, 2005 7:31 pm

Re: Hook causing NullReferenceException

Post by Davita »

damn, I can't recall how many times I'm making such a stupid mistake :( Thanks god we have you :))
Post Reply