Solved

Self-modifying code in Win32

Posted on 1997-05-29
4
813 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
  • 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 20 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now