Thread: how execute code residing in data segment??

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    5

    Question how execute code residing in data segment??

    I am facing a problem and need a solution....

    I am working on windows 2000 platform and using the DEV-C++ IDE. I have created a small executable file and copied that executable code in an char array which is declared as global in other program. I need to execute this array by using this new program. How to do it??

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Explore alternative solutions to the problem perhaps?

    In other words, describe the problem, not how to make your current solution work.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Your program cannot call executable code. It is not an operating system. However, you can request that the system should.

    If you have an executable you want called remotely, see the FAQ.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Save it to temp File
    Call CreateProcess or something on it
    After it is finished - delete the file
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I'm not sure why you would need to do this. Please expand.

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    5

    Thumbs down

    I need to do this because I am asked to do it that way.... I dont want to store the executable in a file and then execute it using
    1.system() function..
    2.using fork sys call.
    3.using spawn..

    I want to execute the executable code after storing it in array..( when it is stored in array it resides in data segment, and data segment does not have an executable permission..) Is there any way to execute a code in data segment in windows using C++..

  7. #7
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    What is the point of emulating the caller?
    I need to do this because I am asked to do it that way
    Then it looks like you'll be doing some fairly low-level, entirely platform-specific coding. Not to mention potentially malicious.
    As for how this might be achieved in Windows, I do not know. Google showed me no love.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  8. #8
    Registered User
    Join Date
    Jun 2007
    Posts
    5

    Lightbulb Hi.....

    I tried it using Function Pointers ... But it gave an Segment violation Fault as expected because data segment doesn't havean executable permission.... c
    Can I some how make a part of data segment executable???????

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    You can't just take the hex dump of one executable and stuff it inside the data segment of another exectuable, and then expect to be able to run it.

    .exe files are not simple images of what the program looks like in memory, they have lots of internal structure which gets resolved by the program loader (part of the OS) when the program is run.

    If you've got the source code for both, then simply include the source code of the smaller project into the larger project (or make the common components a library). The result is much easier to do, and the result is far more predictable.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Due to the nature of the question I'm not going to tell you where you can get this information until you explain what this is for.
    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"

  11. #11
    Registered User
    Join Date
    Jan 2006
    Location
    Sweden
    Posts
    92
    Quote Originally Posted by ravindragjoshi View Post
    I tried it using Function Pointers ... But it gave an Segment violation Fault as expected because data segment doesn't havean executable permission.... c
    Can I some how make a part of data segment executable???????
    http://msdn2.microsoft.com/en-us/library/aa366899.aspx

    I'll let you figure out how to use this lovely function yourself (and my code using it is gone anyway), good luck
    Last edited by The Wazaa; 06-13-2007 at 10:07 AM.

  12. #12
    Registered User
    Join Date
    Jun 2007
    Posts
    5
    Thank you for your help...... I 'll try it out.....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. execute a code in data segment...
    By ravindragjoshi in forum Windows Programming
    Replies: 1
    Last Post: 06-12-2007, 11:43 PM
  2. question about a working linked list
    By cold_dog in forum C++ Programming
    Replies: 23
    Last Post: 09-13-2006, 01:00 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM