Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

self modify code , this example don't work

Status
Not open for further replies.

Trope

Programmer
May 14, 2001
110
US
Anyone know why I am getting an error?

Code:
#include "iostream.h"
#include "windows.h"
char * function_addr;

LPVOID addr;
DWORD prev;
__declspec(naked) int selfmodify()
{
        __asm
        {
                [b]mov dword ptr [addr],offset [modify_this + 1][/b] //get the address of the code

        }

        VirtualProtect(addr,4,PAGE_READWRITE,&prev); //make the writable

        __asm
        {
                mov ecx, dword ptr [addr]
                mov dword ptr [ecx], 12 //set new function return value
        }

        VirtualProtect(addr,4,PAGE_EXECUTE,&prev); //restore it to executable

        __asm
        {

                modify_this:
                mov eax,255 //we are returning 255 but code gets modified here
                ret
        }
}

int main(int argc, char* argv[])
{
        int retval = selfmodify();
        cout << "Function returned: " << retval << endl;

        return 0;
}

compiler is telling me that modify_this is undefined.

Any help would be GREATLY appreciated!

Thanks ,
Trope
 
I've never seen modify_this. I've seen this. Did you get this from an example somewhere? Maybe they meant that modify_this was a place for you to change.

James P. Cottingham
-----------------------------------------
[sup]To determine how long it will take to write and debug a program, take your best estimate, multiply that by two, add one, and convert to the next higher units.[/sup]
 
it's because your label is in a different asm block. And I'm not sure taking it out of the block and making it a normal label would help.

You could compile to assembler and then cut&paste that function into its own .asm file assuming you have tasm or the pro or above version of builder.


A programmer is a device for converting Coke into software.
 
So this code cannot work as written without serious modifications?

damn...

Trope
 
Well, if you modify:
__asm
{
modify_this:
mov eax,255 //we are returning 255 but code gets modified here
ret
}

to:

modify_this:;
__asm
{
mov eax,255 //we are returning 255 but code gets modified here
ret
}

This would make "modify_this" a global label.

Totte
Keep making it perfect and it will end up broken.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top