Hook causing NullReferenceException

c++ / delphi package - dll injection and api hooking

Hook causing NullReferenceException

Postby Davita » Tue Jun 26, 2018 2:11 pm

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
Davita
 
Posts: 163
Joined: Tue Sep 13, 2005 7:31 pm

Re: Hook causing NullReferenceException

Postby madshi » Tue Jun 26, 2018 2:35 pm

Your CreateCompatibleDCCallback function is missing the WINAPI.
madshi
Site Admin
 
Posts: 9774
Joined: Sun Mar 21, 2004 5:25 pm

Re: Hook causing NullReferenceException

Postby Davita » Tue Jun 26, 2018 3:09 pm

damn, I can't recall how many times I'm making such a stupid mistake :( Thanks god we have you :))
Davita
 
Posts: 163
Joined: Tue Sep 13, 2005 7:31 pm


Return to madCodeHook

Who is online

Users browsing this forum: No registered users and 2 guests