Anonymous Method

c++ / delphi package - dll injection and api hooking
Post Reply
choochy2003
Posts: 88
Joined: Fri Mar 21, 2008 4:52 am
Location: Adelaide, South Australia
Contact:

Anonymous Method

Post by choochy2003 »

Is it possible to pass in an anonymous method for the callback procedure of CreateIpcQueue?

If not, is this something that could be included in the future?

Thanks
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Re: Anonymous Method

Post by madshi »

Currently this is not directly support, but I definitely see why you would want it. I'll add it on my to do list for a future version.

For now, you can use the function "MethodToProcedure()". However, I don't have this converted to C++, it's only available in Delphi source code. It should already be on your harddisk, check "madCollection\madTools\Sources\madTools.pas". If you convert the "MethodToProcedure()" code to C++, it should work for your needs. However, you should use stdcall/WINAPI for your method. Then you can call it like "LPVOID simpleProcWhichYouCanFeedIntoCreateIpcQueue = MethodToProcedure(this, YourMethodAddress)".

P.S: Found some comparable C++ code, but it's only for 32bit:

Code: Select all

LPVOID CreateThisStub(LPVOID this_, LPVOID func)
{
  LPVOID result = VirtualAlloc(NULL, 11, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  *(BYTE  *) ((ULONG_PTR) result +  0) = 0xb9;    // mov ecx, this_
  *(LPVOID*) ((ULONG_PTR) result +  1) = this_;
  *(BYTE  *) ((ULONG_PTR) result +  5) = 0x68;    // push func
  *(LPVOID*) ((ULONG_PTR) result +  6) = func;
  *(BYTE  *) ((ULONG_PTR) result + 10) = 0xc3;    // ret
  return result;
}

class CSomeClass
{
  public:
    [..]
    LPVOID GlobalWindowProc;
    LRESULT GlobalWindowMethod(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
};

static LRESULT (__thiscall CSomeClass::* GlobalWindowMethodAddr)(HWND,UINT,WPARAM,LPARAM) = &CSomeClass::GlobalWindowMethod;

CSomeClass::CSomeClass()
{
  GlobalWindowProc = CreateThisStub(this, (void*&) GlobalWindowMethodAddr);
}

CSomeClass::~CSomeClass()
{
  VirtualFree(GlobalWindowProc, 0, MEM_RELEASE);
}
It's a bit tricky, but works well - but only for stdcall/WINAPI methods.
choochy2003
Posts: 88
Joined: Fri Mar 21, 2008 4:52 am
Location: Adelaide, South Australia
Contact:

Re: Anonymous Method

Post by choochy2003 »

I want to make sure that all code surrounding the hook control and message processing is fully encapsulated within a class with no need for external references/globals. Something that I can now do with the MethodToProcedure function :)

Many Thanks
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Re: Anonymous Method

Post by madshi »

Yes, I've written MethodToProcedure for exactly that purpose, I'm using it myself quite often.

That said, it really would be nice if CreateIpcQueue(Ex) could support methods, too, in some way.
Post Reply