EXETOOLS FORUM  

Go Back   EXETOOLS FORUM > General > Source Code

Notices

Reply
 
Thread Tools Display Modes
  #1  
Old 04-13-2018, 10:25
zeffy zeffy is offline
Friend
 
Join Date: Jul 2017
Posts: 14
Rept. Given: 0
Rept. Rcvd 3 Times in 3 Posts
Thanks Given: 76
Thanks Rcvd at 50 Times in 13 Posts
zeffy Reputation: 3
[C] Helper function to call arbitrary x86 Delphi functions

A project I was working on a while back required me to hook several functions in an application written in Delphi. I was writing my code in C, so I needed to figure out some way to inter-operate with the main application's code. I ended up deciding to write a normal __cdecl function that translated its parameters to Borland Register (aka Delphi "fastcall", different from Microsoft's fastcall, read more here).

This is what I came up with. The helper function takes a pointer to a delphi function, the number of arguments, and then the arguments you want to pass to the delphi function. The result of the invoked function is returned as a void *, but can be easily cast to whatever the return type actually is.

Code:
void *Delphi_InvokeMethod(void *pfn, size_t param_count, ...)
{
        va_list ap;
        void *tmp;
        void *registers[3];
        void *result;

        va_start(ap, param_count);
        for ( size_t i = 0; i < param_count; i++ ) {
                tmp = va_arg(ap, void *);

                if ( i < _countof(registers) )
                        registers[i] = tmp;
                else
                        __asm push tmp
        }
        va_end(ap);
        __asm {
                mov eax, registers[0]
                mov edx, registers[type registers * 1]
                mov ecx, registers[type registers * 2]
                call pfn
                mov result, eax
        }
        return result;
}
Keep in mind this is only for x86. As far as I know x64 delphi applications use the universal Microsoft x64 calling convention (someone feel free to correct me here, I haven't had the opportunity to reverse a x64 delphi application yet, so I could be wrong).

It might not be perfect, but it worked for what I needed it for. Feel free to suggest ways to improve it or any other kinds of criticism.

I hope somebody finds this useful, cheers!
Reply With Quote
The Following User Says Thank You to zeffy For This Useful Post:
niculaita (04-13-2018)
Reply

Tags
delphi

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On


Similar Threads
Thread Thread Starter Forum Replies Last Post
IDA Script Function rename for Delphi VCL (x32 - x64) Coldzer0 Community Tools 0 05-12-2018 21:51
[ASM] Helper function for type inline static messages dosprog Source Code 0 04-15-2018 12:14
Keygenning With Delphi: Useful Delphi Functions and Tips chessgod101 General Discussion 5 01-05-2015 23:02
How to call the original function when it's overridden? BlackWhite General Discussion 10 08-25-2014 20:45


All times are GMT +8. The time now is 08:16.


ICP05004977
Always Your Best Friend: Aaron, JMI, ahmadmansoor, ZeNiX