Click here to Skip to main content
15,892,005 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionGetDesktopWindow() and Window Hooks Pin
Madhu_Rani5-Aug-09 21:22
Madhu_Rani5-Aug-09 21:22 
AnswerRe: GetDesktopWindow() and Window Hooks Pin
Adam Roderick J5-Aug-09 22:01
Adam Roderick J5-Aug-09 22:01 
GeneralRe: GetDesktopWindow() and Window Hooks Pin
Madhu_Rani5-Aug-09 23:05
Madhu_Rani5-Aug-09 23:05 
GeneralRe: GetDesktopWindow() and Window Hooks Pin
Madhu_Rani5-Aug-09 23:05
Madhu_Rani5-Aug-09 23:05 
Questionpatching operation code known as opcodes Pin
nah13375-Aug-09 20:29
nah13375-Aug-09 20:29 
AnswerRe: patching operation code known as opcodes Pin
Adam Roderick J5-Aug-09 20:57
Adam Roderick J5-Aug-09 20:57 
GeneralRe: patching operation code known as opcodes Pin
nah13376-Aug-09 2:30
nah13376-Aug-09 2:30 
AnswerRe: patching operation code known as opcodes Pin
Bacon Ultimate Cheeseburger6-Aug-09 16:27
Bacon Ultimate Cheeseburger6-Aug-09 16:27 
In order to properly perform the type of hook operation you are describing you will need to implement some type of code relocation functionality. This is typically done by relocating one opcode at a time until enough memory is available to insert the patched code (or hook). This requires that each opcode and any operands be decoded and then encoded at a new location. Simply copying the opcode may not be sufficient if it uses relative addressing. From a top level view you would typically do the following:

1. Allocate enough memory for the original code to be copied to. See VirtualAllocEx() in the Windows SDK.

2. Decode a single opcode.

3. Encode/assemble the opcode at it's new location.

4. Repeat steps 2 and 3 until there is enough memory available to insert the hook.

5. Insert a jmp operation at the end of the relocated opcodes. This typically points to the end of the decode stream (or pointer if you prefer).

6. Insert the hook code (typically a jmp to your code).

You end up with something like this:
Before code relocation
    user code   ->  | LoadLibrary (movable) | LoadLibrary main code

After code relocation
    user code   ->  * jmp to your code      * LoadLibrary main code
                        |                   |
                        |                   |
                      your code -> LoadLibrary (moved)



Before you begin you will probably want to familiarize yourself with the target CPU. Assuming that you are targeting Intel platforms visit "Intel® 64 and IA-32 Architectures Software Developer's Manuals[^]" for a list of references.

The reference manuals will tell you how each instruction is composed and what if any operands need to be processed. It will also tell you how each operand is composed, which addressing modes apply to each opcode and give you a list of all opcode modifiers (prefix bytes) and what they mean.

For an example of how to decode an opcode check out http://udis86.sourceforge.net/[^]

1300 calories of pure beef goodness can't be wrong!

GeneralRe: patching operation code known as opcodes Pin
nah13376-Aug-09 19:44
nah13376-Aug-09 19:44 
QuestionCTreeCtrl Pin
john56325-Aug-09 20:16
john56325-Aug-09 20:16 
AnswerRe: CTreeCtrl Pin
Adam Roderick J5-Aug-09 20:22
Adam Roderick J5-Aug-09 20:22 
GeneralRe: CTreeCtrl Pin
john56325-Aug-09 22:08
john56325-Aug-09 22:08 
GeneralRe: CTreeCtrl Pin
Adam Roderick J5-Aug-09 22:53
Adam Roderick J5-Aug-09 22:53 
GeneralRe: CTreeCtrl Pin
john56326-Aug-09 0:37
john56326-Aug-09 0:37 
GeneralRe: CTreeCtrl Pin
Adam Roderick J6-Aug-09 17:44
Adam Roderick J6-Aug-09 17:44 
GeneralRe: CTreeCtrl Pin
TinDude23-Sep-11 21:27
TinDude23-Sep-11 21:27 
QuestionTICKS PER MILLISECOND Pin
RKP7285-Aug-09 19:55
RKP7285-Aug-09 19:55 
AnswerRe: TICKS PER MILLISECOND Pin
Adam Roderick J5-Aug-09 20:16
Adam Roderick J5-Aug-09 20:16 
GeneralRe: TICKS PER MILLISECOND Pin
RKP7285-Aug-09 21:09
RKP7285-Aug-09 21:09 
GeneralRe: TICKS PER MILLISECOND Pin
Adam Roderick J5-Aug-09 22:14
Adam Roderick J5-Aug-09 22:14 
GeneralRe: TICKS PER MILLISECOND Pin
RKP7286-Aug-09 1:53
RKP7286-Aug-09 1:53 
Questionquery foxprodata base Pin
trioum5-Aug-09 19:49
trioum5-Aug-09 19:49 
QuestionRe: query foxprodata base Pin
David Crow6-Aug-09 4:20
David Crow6-Aug-09 4:20 
QuestionSendInput() and MOUSEINPUT Problems Pin
Madhu_Rani5-Aug-09 19:08
Madhu_Rani5-Aug-09 19:08 
AnswerRe: SendInput() and MOUSEINPUT Problems Pin
Adam Roderick J5-Aug-09 19:47
Adam Roderick J5-Aug-09 19:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.