?
Solved

Self-modifying code in Win32

Posted on 1997-05-29
4
Medium Priority
?
861 Views
Last Modified: 2013-12-03
In Win16, one used PrestoChangoSelector() to convert a DS segment into a CS segment and then run code that's constructed at runtime.  This was done by loader applications that: 1) allocated memory, 2) loaded a binary image into the memory, and 3) executed the contents of the memory.  How does one do something like this from a Win32 program.I'm trying to do the following.  I have an application which has a scripting subsystem.  I want to add the functionality of Visual Basic's "AddressOf" operator -- basically on-the-floy Callback's.  AddressOf is a function returns a "void*" to a newly defined callback.  I basically need to be able to create a "Thunk" the way MakeProcInstance worked in Win16.Anyone have a clue on how to do this from a straight Win32program?
0
Comment
Question by:Shrif
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 6

Expert Comment

by:jpk041897
ID: 1397713
Have you tried using a function referance to a memoty area that was new'd (malloc'ed) and in which you have constructed/loaded the binary code?

The pointer referance could be defined as a Callback function pointer to this memory area.
0
 
LVL 3

Accepted Solution

by:
byang earned 40 total points
ID: 1397714
Here's a way:
1. use VirtualAlloc to allocate memory
2. load your code image into this memory
3. patch your code
4. use VirtualProtect to change memory to executable
5. call FlushInstructionCache(). I think this step is optional
6. run your code

Sadly, Win95 doesn't support writable-and-runnable memory block at the same time. This means your code cannot modify
itself when it's running. You must exit from it, call VirtualProtect(), then repeat step 3-6.

0
 

Author Comment

by:Shrif
ID: 1397715
byang, if what you say works, then I will accept the answer.However, before I give you the A grade, could you explain something to me that you said in your answer that I do not understand.  "Sadly, Win95 doesn't support writable-and-runnable memory block at the same time. This means your code cannot modify
itself when it's running. You must exit from it, call VirtualProtect(), then repeat step 3-6.
"What do you mean by I must exit from "it".  What is "it"?  Exit Windows 95?  Exit my application?Can you give me the steps that I should do in Windows 95?  
0
 
LVL 3

Expert Comment

by:byang
ID: 1397716
By "it" I mean the block of code you want to modify. It cannot modify itself in Win95. In DOS, you can do something like this:

label0: mov ax,1234h ;will be self-modified here
           ;... more code
           mov word ptr cs:[label0+1],bx ;self-modify
           loop label0

This is not possible (at least not easily possible) in Win95. To do it, the cs segment must be readable, writable, and executable. Win95 does not support all three attribute at the same time. So the code above would cause an access violation.
0

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question