 |
Cheat Engine The Official Site of Cheat Engine
|
| View previous topic :: View next topic |
| Author |
Message |
hyphen Advanced Cheater
Reputation: 0
Joined: 12 Sep 2008 Posts: 84 Location: Not Having Fun
|
Posted: Sat Jun 27, 2009 9:50 pm Post subject: [Uhm]Parallel Code? |
|
|
It seems to me that all these coding languages read top to bottom in order. That means you can only have one thing going on at once.
Well, I'm wondering, what if say, you made some basic console app. And you wanted to implement a check for the hotkey "ESC" at all times. When ESC is pressed, the application closes down (if... goto or w/e).
Assuming there are plenty of pauses and user prompts in the application, if you wanted the check for ESC to always be there, you would have to place code for it after every single instruction....
And if you wanted the application to constantly check for 10 other key combinations as well, you would have to add 10 more checks after every single instruction.
What do you do then? There has to be a way, considering how most programs these days are loaded with hotkeys/shortcut keys.
And I hope your solution doesn't involve running an application that monitors keypresses and acts on another application X.X
Feel free anyone to share your opinion. If there is actually a very simple solution............
|
|
| Back to top |
|
 |
Dark Byte Site Admin
Reputation: 471
Joined: 09 May 2003 Posts: 25820 Location: The netherlands
|
Posted: Sat Jun 27, 2009 10:07 pm Post subject: |
|
|
That's called threading
In windows (and most unix systems) applications can have 1 or more threads running at the same time.
You can spawn a new thread that runs in the same application but with it's own registers, but with direct access to the same memory as the original thread (main thread). I know, it might sound like a different app that has memory control over the original app, but it's a bit more complex than that.
Now in that other thread you can do a constant loop that checks the keystate, and if a key is pressed act on it.
Acting on something can be complex to implement though. You could just handle it directly from inside the thread that does the constant polling(if no gui interaction or notification is required), or you could dispatch the event to the mainthread so it handles it next time it checks the message que, or spawn of a new thread that does what you want.
Just modifying memory that is used by another thread is a risk, since the other thread might not expect you're changing it. Especially memory allocation, or more precisely freeing, can cause nice crashes when another thread is reading a buffer and the other one decides to free it. To fix this, you can make use of locks (mutex, critical section, multi read exclusive write locks, etc...) so that when your thread is going to write, you first acquire the lock for the specified block, write, and release, so other threads can make use of it.
Keep in mind that even though threads share the same memory, you might want to tell your compiler that certain memory might be modified by other threads. It depends on the language, bit with C you can usually handle this by adding the word volatile in front of it.
e.g:
| Code: |
int changeablememory; //global
...
changeablememory=0;
while (changeablememory==0)
{
dosomething;
}
|
if the compiler would be so stupid to compile it to:
| Code: |
mov eax,[changeablememory] //optimizes to put it in a register
mov eax,0
dosomething:
...
...
cmp eax,0
je dosomething
mov [changeablememory],eax
|
Sure, it's a lot faster to make use of a register instead of memory access, especially eax, but anyone with basic asm knowledge will know this will mess up bigtime in multithreaded code
by declaring as "volatile int changeablememory" you can at least tell the compiler to not optimize using registers for that variable
And there are lots of other things to keep a lookout for, so easy, no, really really useful, yes
Also, your main thread will still need to do some checking to know it has to do something, like displaying to the screen, or checking the state of threads (Unless you do the most horrible thing anyone could ever think of, and that is controlling the context of the main thread directly from another thread by changing it's registers and eip register at runtime)
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.
Like my help? Join me on Patreon so i can keep helping |
|
| Back to top |
|
 |
NoManchesPuto I post too much
Reputation: 0
Joined: 24 Jan 2009 Posts: 2820
|
Posted: Sat Jun 27, 2009 10:53 pm Post subject: |
|
|
| Dark Byte wrote: | That's called threading
In windows (and most unix systems) applications can have 1 or more threads running at the same time.
You can spawn a new thread that runs in the same application but with it's own registers, but with direct access to the same memory as the original thread (main thread). I know, it might sound like a different app that has memory control over the original app, but it's a bit more complex than that.
Now in that other thread you can do a constant loop that checks the keystate, and if a key is pressed act on it.
Acting on something can be complex to implement though. You could just handle it directly from inside the thread that does the constant polling(if no gui interaction or notification is required), or you could dispatch the event to the mainthread so it handles it next time it checks the message que, or spawn of a new thread that does what you want.
Just modifying memory that is used by another thread is a risk, since the other thread might not expect you're changing it. Especially memory allocation, or more precisely freeing, can cause nice crashes when another thread is reading a buffer and the other one decides to free it. To fix this, you can make use of locks (mutex, critical section, multi read exclusive write locks, etc...) so that when your thread is going to write, you first acquire the lock for the specified block, write, and release, so other threads can make use of it.
Keep in mind that even though threads share the same memory, you might want to tell your compiler that certain memory might be modified by other threads. It depends on the language, bit with C you can usually handle this by adding the word volatile in front of it.
e.g:
| Code: |
int changeablememory; //global
...
changeablememory=0;
while (changeablememory==0)
{
dosomething;
}
|
if the compiler would be so stupid to compile it to:
| Code: |
mov eax,[changeablememory] //optimizes to put it in a register
mov eax,0
dosomething:
...
...
cmp eax,0
je dosomething
mov [changeablememory],eax
|
Sure, it's a lot faster to make use of a register instead of memory access, especially eax, but anyone with basic asm knowledge will know this will mess up bigtime in multithreaded code
by declaring as "volatile int changeablememory" you can at least tell the compiler to not optimize using registers for that variable
And there are lots of other things to keep a lookout for, so easy, no, really really useful, yes
Also, your main thread will still need to do some checking to know it has to do something, like displaying to the screen, or checking the state of threads (Unless you do the most horrible thing anyone could ever think of, and that is controlling the context of the main thread directly from another thread by changing it's registers and eip register at runtime) |
WOW! Your a genius man ^^
_________________
|
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Sun Jun 28, 2009 9:16 am Post subject: |
|
|
| Also you might want to read about event driven programming. You shouldn't use N threads to do N stuffs(checks or sth) concurrent, instead you should be using events.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|