Sunday, December 31, 2017

Invoke functions by pointer

Previously, my P/Invoke command-line tool (which is in desperate need of an actual name) was unable to work with COM objects because the tool requires a library and name for functions it calls. COM objects appear from CoCreateInstance and their methods are accessible in their VTable, which is a member of each object/struct. To use them, I implemented the ability to call methods by a function pointer held in a slot. Currently, doing that requires some seriously ugly script, but at least it's now possible at all.

As a not-super-useful but concept-proving demonstration, this creates an IFhConfigMgr and calls AddRef:
newslot native fhPtr
call ole32.dll!CoCreateInstance /return uint (blockptr(guid {ED43BB3C-09E9-498a-9DF6-2177244C6DB4}), nullptr, int 1, blockptr(guid {6A5FEA5B-BF8F-4EE5-B8C3-44D8A0D7331C}), slotptr fhPtr)
newslot native fh
copyslot fh = fhPtr dereferenced
newslot block vtbl = nullptr, nullptr, nullptr, nullptr
copyslot vtbl = fh dereferenced
newslot native addref
copyslot addref = vtbl field 1
call funcat addref /call thiscall /return uint (slotdata fhPtr)

The funcat (that's "function at", not "fun cat", even though cats are pretty great) keyword on the call command is the new addition that does all the heavy lifting.

No comments:

Post a Comment