Thread: Need some help with "self modifying code" in C++

  1. #1
    The Autodidact Dante Wingates's Avatar
    Join Date
    Apr 2010

    Need some help with "self modifying code" in C++

    Hello... Maybe what Im asking isnt very hard, but I really dont know because I never did it before... Anyway heres what I want:

    Im learning Assembly right now and with Assembly its very easy to write machine code in some memory location while the application is actually running and then call it using a jmp instruction. So, as Im much more familiar with C++ and like it better, Im trying to think of some way to do something like that(if it is possible) using C or C++... Basically writing machine code to memory and then call it... Is there any way of doing this in C++, or will I have to use Assembly to do this?

    And of course I'll not try to write a whole program in machine code straight to memory and then call it.. I Just wanna know if it is possible or not...

  2. #2
    Registered User
    Join Date
    Jun 2005
    The only way to write self-modifying code in C or C++ is to do something outside scope of the standards. The obvious way is by embedding assembler, which is inherently non-portable.

    Keep in mind that self-modifying executables are, more often than not, considered symptomatic of a virus. Whatever it is you're trying to achieve, better to find an approach that is more likely to be considered legitimate.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Putting some executable somewhere in memory and jumping to it is self modifying?

    I don't think so. You can allocate some memory, put some code and jump to it with C/C++ anytime you like. Like when OS loads an executable.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    Yes it's possible. Read about "The Welder" in "Pixomatic 2".
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Modifying Struct Member Passed Into Function
    By soj0mq3 in forum C Programming
    Replies: 1
    Last Post: 04-27-2010, 01:07 AM
  2. Text file modifying
    By a0161 in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2009, 01:24 PM
  3. Combine two strings without modifying them
    By kezman in forum C Programming
    Replies: 1
    Last Post: 09-07-2009, 11:07 AM
  4. Modifying CDocument Member Objects :: MFC
    By kuphryn in forum C++ Programming
    Replies: 0
    Last Post: 04-12-2002, 05:52 PM
  5. modifying strings
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-13-2001, 12:36 PM