Really Not An Easy Ques. !!!!!!!!
-
what if i want to copy the function code from the memory to a file?? !! thanks
Why would you need/want this? Even if it were possible, the "code" you'd end up with would be machine code.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
what if i want to copy the function code from the memory to a file?? !! thanks
IMHO (I'm not expert about), you need to hack DLL (or executable) internals, i.e. you need to know PE file format, have a look at http://msdn.microsoft.com/msdnmag/issues/02/02/PE/default.aspx[^] :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
[my articles] -
Dear All how can i get the size of a function (its code size in the memory in bytes) from its pointer (its function pointer) function like double add (double , double) i tried sizeof by many ways and no result:~ any tips:confused::confused: thanks all bye
The only way would be a loop that reads through the assembly code looking for the next
ret
. This would only work with simple functions having only oneret
. Once you put the code in a file it would be useless anyway because code withjump
instructions in it (most code) is usually address dependent. ie. if you don't load the code back at the same address it won't work anymore. As you can never gaurentee to do this in any future instance of the process it would be of no use to have the unbased machine code in a file, except possibly for comparison to detect a code modifying virus attack or something. There are better ways to do that anyway like security cookie checks which are already built into every function for you by the MS Compiler. See the /GS compiler switch. If you want to know more have a look at some disassemblers which turn compiled code back into something almost readable.Nothing is exactly what it seems but everything with seems can be unpicked.
-
Dear All how can i get the size of a function (its code size in the memory in bytes) from its pointer (its function pointer) function like double add (double , double) i tried sizeof by many ways and no result:~ any tips:confused::confused: thanks all bye
Caveat: This is dubious practice. The memory layout is not guaranteed and should not be relied upon. Pointer arithmatic is not good. It is mostly just an "irresistible" question. Given the above: an old technique is to use two function pointers
void functionA()
{
// whatever
}
void functionB()
{
// whatever
}void dontdothis()
{
void * p1 = functionA;
void * p2 = functionB;
char * p3 = static_cast<char *>(p1);
char * p4 = static_cast<char *>(p2);
size_t sizeFn = p4 - p3;
}There is little, if anything, that can be done with this.
-
Caveat: This is dubious practice. The memory layout is not guaranteed and should not be relied upon. Pointer arithmatic is not good. It is mostly just an "irresistible" question. Given the above: an old technique is to use two function pointers
void functionA()
{
// whatever
}
void functionB()
{
// whatever
}void dontdothis()
{
void * p1 = functionA;
void * p2 = functionB;
char * p3 = static_cast<char *>(p1);
char * p4 = static_cast<char *>(p2);
size_t sizeFn = p4 - p3;
}There is little, if anything, that can be done with this.
can u kindly explain more what the meaning by ur code ?? you just sub. two addr of two pointer of two function !! how this be the size and size of which one .. i really misunderstand thx
-
can u kindly explain more what the meaning by ur code ?? you just sub. two addr of two pointer of two function !! how this be the size and size of which one .. i really misunderstand thx
Assuming the two functions are back-to-back in memory, you can simply take the difference of their addresses to obtain the size. In other words, if
functionA()
is at 0x1234 andfunctionB()
is at 0x2345, then the size offunctionA()
would be 4,369 bytes."Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Assuming the two functions are back-to-back in memory, you can simply take the difference of their addresses to obtain the size. In other words, if
functionA()
is at 0x1234 andfunctionB()
is at 0x2345, then the size offunctionA()
would be 4,369 bytes."Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
Nicely said. :) This is precisely what is happening. Once more, don't do this.
-
Assuming the two functions are back-to-back in memory, you can simply take the difference of their addresses to obtain the size. In other words, if
functionA()
is at 0x1234 andfunctionB()
is at 0x2345, then the size offunctionA()
would be 4,369 bytes."Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
ooh sure should not be used !! nothing can guaranty that the two functions are above each other in the memory !!! see no way here .. indeed its a client request for what i don't know ! thx all
-
Nicely said. :) This is precisely what is happening. Once more, don't do this.
Member 754960 wrote:
Once more, don't do this.
I wouldn't. I was just explaining to Adore what was happening.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
ooh sure should not be used !! nothing can guaranty that the two functions are above each other in the memory !!! see no way here .. indeed its a client request for what i don't know ! thx all
Since that won't do, try this: Load the program in a debugger and read the starting and ending address of the function and do the math. Otherwise you need to do a lot more research before you can ask your question.