![]() |
Calculating the size in bytes of a C++ function
Hello,
I'm writing a little proggie that injects code inside a running process without using the DLL loading approach and I got to the point of code injection itself and here an issue arises ... calculating how big my piece of code is. Obviously how the compiler lays out the code inside your binary may play a role depending on how you try to calculate the size ... Anyhow, I seem to be able to calculate a size that is always greater than the real size ... so this would be enough for it to work but I was wondering if there is any nice trick C/C++ gurus may suggest. Obviously I could check how many bytes this function gets compiled into using a disassembler or a debugger ... but doing everything directly from the IDE editor and using C/C++ code would be so much better!! :D yaa |
exports, is a quick and dirty way...
make an export above your proc 'proc start' make an export below your proc 'proc end' end - start = size tons of ways to do it really |
Above and below have very relative meanings ...
Above and below where? In source code? And what are you suggesting, to export a function before and one after and calculate the difference to get the size? How about sharing someone else of those *tons of ways*? yaa |
void main()
{ do whatever main function does } void dummy() { } #define mainsize ((DWORD)dummy-(DWORD)main) This will calculate size of main function |
But this is not true. It depends entirely on how your compiler and linker lay out your code in the binary. Without doing anything special and without touching optimization flags, taking your example, I even got to the point that dummy's code was placed BEFORE main's code in the compiled binary!
yaa |
Code:
int main(){ |
Agreed Deroko, I think that is about the only way of getting close to the the answer. It still won't be exact because of any epilogue the compiler issues, but I can't think of a way of getting any closer.
Git |
Quote:
Code:
void funct1() |
Your best bet, in my opinion, would be emitting different recognizeable byte sequences using
__asm { emit BYTE0; emit BYTE1; ...} at the beginning and at the end of the function This will however turn out to be a bit complicated for non-voids, as if you'll insert the sequence below the return instruction it'll get ignored, and if you'll insert it above the return you won't account for that... Alternatively, if you don't need to dynamically get the value from time to time but you need it for processing after compilation of an executable, you can consider generating a mapfile for your compiled code and parse that. Regards |
| All times are GMT +8. The time now is 14:38. |
Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2026, vBulletin Solutions, Inc.
Always Your Best Friend: Aaron, JMI, ahmadmansoor, ZeNiX