Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Calling a Function in a New Thread

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
dlpb
Advanced Cheater
Reputation: 0

Joined: 11 Dec 2013
Posts: 71

PostPosted: Sat Feb 04, 2017 7:09 pm    Post subject: Calling a Function in a New Thread Reply with quote

Assuming I have a function

Function MyFunction (A, B: Integer): Boolean;
Begin
End;


How would I call that so that it is used in a new thread. You see, I have a lot of things to be doing in my functions that affect the main thread if they aren't being used from a separate thread.

I use Delphi 7.
Back to top
View user's profile Send private message
STN
I post too much
Reputation: 42

Joined: 09 Nov 2005
Posts: 2672

PostPosted: Sat Feb 04, 2017 7:15 pm    Post subject: Reply with quote

MyFunction(1,2); ?

Just create a thread, loop it with a sleep (so it doesn't exit or eats all cpu) and call your function

_________________
Cheat Requests/Tables- Fearless Cheat Engine
https://fearlessrevolution.com
Back to top
View user's profile Send private message
dlpb
Advanced Cheater
Reputation: 0

Joined: 11 Dec 2013
Posts: 71

PostPosted: Sat Feb 04, 2017 7:21 pm    Post subject: Reply with quote

The problem is that this function will be called from a hijacked function. I use a DLL. So...

Code:
Procedure MenuEffect74580A;
Var
  Param1: Byte;
Begin

asm
mov al, [ebp + $8]
mov Param1, al
end;

SetEffect(5, 64, Param1);

End;


Where SetEffect must be in a separate thread.

MenuEffect74580A is being initiated from a hijacked game function.
Code:

HookProc($74580A, @MenuEffect74580A);


unless you mean I can simply set a loop - and then create a boolean value that will call the function I need when set to true, and immediately set to false after it. I guess that can work. But it does seem really sloppy.


Last edited by dlpb on Sat Feb 04, 2017 7:24 pm; edited 1 time in total
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 198

Joined: 25 Jan 2006
Posts: 8517
Location: 127.0.0.1

PostPosted: Sat Feb 04, 2017 7:23 pm    Post subject: Reply with quote

Are you trying to fully take over the function or just add your call to it?
_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
dlpb
Advanced Cheater
Reputation: 0

Joined: 11 Dec 2013
Posts: 71

PostPosted: Sat Feb 04, 2017 7:27 pm    Post subject: Reply with quote

atom0s wrote:
Are you trying to fully take over the function or just add your call to it?


I've completely taken over the game function (in Final Fantasy VII PC, which uses really crappy 4 bit adpcm, I am replacing with full OGG support using bass.dll). In the above example, one parameter is sent to the game function (the Sound effect ID).

I pass this data to my own function that uses bass.

SetEffect(5, 64, Param1);

Set an effect on channel 5, with a balance of 64, with the sound effect of param1.

The problem is that seteffect will slow the game down if a lot of calls are sent to it (such as when moving a cursor down the screen ). For this reason, Seteffect must use a separate thread.

Bass automatically uses a separate thread, but the seteffect does some nifty stuff I have added to include fades and what not. The fades require that the there are pauses. These pauses especially will hang the game because seteffect is in the main thread.
Back to top
View user's profile Send private message
dlpb
Advanced Cheater
Reputation: 0

Joined: 11 Dec 2013
Posts: 71

PostPosted: Mon Feb 06, 2017 8:56 pm    Post subject: Reply with quote

I overthought this.

It can be done v easily for the parts I need using

CreateThread(nil, 0, @CustomMusicFadeOut, nil, 0, dwtemp);
Back to top
View user's profile Send private message
dlpb
Advanced Cheater
Reputation: 0

Joined: 11 Dec 2013
Posts: 71

PostPosted: Tue Feb 07, 2017 2:59 pm    Post subject: Reply with quote

What happens when a thread ends - like above when I call that and it ends naturally after all operations are complete.

Is there a leak? Will the memory be freed?

I guess I have to save the handle and then free it.

----------

It's cool. I found the answer. Seems I do need to close the thread properly Razz Shocked

http://www.delphibasics.co.uk/RTL.asp?Name=BeginThread

That's the best way to do this in Delphi that I've seen.
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 458

Joined: 09 May 2003
Posts: 25287
Location: The netherlands

PostPosted: Tue Feb 07, 2017 5:57 pm    Post subject: Reply with quote

when a thread exists it calls terminatethread, so there's no need to do anything.
At most call closehandle on the result of createThread, but you can do that even before the thread has finished (e.g right after createthread)

_________________
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
View user's profile Send private message MSN Messenger
dlpb
Advanced Cheater
Reputation: 0

Joined: 11 Dec 2013
Posts: 71

PostPosted: Tue Feb 07, 2017 6:59 pm    Post subject: Reply with quote

Yeah, that is something I am sure a lot of people don't realize. Nor did I until you confirmed it.

It seems that "Endthread" is still required though (when using beginthread)? This calls api ExitThread.

What happens if this isn't called? Or does delphi clean up a beginthread / createthread regardless? Is that what you are saying? It seems that people using C / C++ disagree - so perhaps those languages require manual clean up?
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 458

Joined: 09 May 2003
Posts: 25287
Location: The netherlands

PostPosted: Tue Feb 07, 2017 7:15 pm    Post subject: Reply with quote

when the createthread api creates a new thread, it's stack is setup in such a way, that the last ret instruction will jump to exitthread, therefore, just exiting the function will cause the thread to self terminate

same in Delphi , the thread will eventually call the last 'ret' and then self destruct.

_________________
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
View user's profile Send private message MSN Messenger
dlpb
Advanced Cheater
Reputation: 0

Joined: 11 Dec 2013
Posts: 71

PostPosted: Tue Feb 07, 2017 7:38 pm    Post subject: Reply with quote

Dark Byte wrote:
when the createthread api creates a new thread, it's stack is setup in such a way, that the last ret instruction will jump to exitthread, therefore, just exiting the function will cause the thread to self terminate

same in Delphi , the thread will eventually call the last 'ret' and then self destruct.


Why does "endthread" exist then? What's the point?
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 458

Joined: 09 May 2003
Posts: 25287
Location: The netherlands

PostPosted: Tue Feb 07, 2017 7:54 pm    Post subject: Reply with quote

it's faster than having to fall back to the end of the function. (e.g inside a function called by another function called by another function , etc...)
_________________
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
View user's profile Send private message MSN Messenger
dlpb
Advanced Cheater
Reputation: 0

Joined: 11 Dec 2013
Posts: 71

PostPosted: Tue Feb 07, 2017 8:07 pm    Post subject: Reply with quote

This stuff really needs to be documented better by MS and so on. Thanks! I'll add you as a thank you credit.

Smile
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 198

Joined: 25 Jan 2006
Posts: 8517
Location: 127.0.0.1

PostPosted: Tue Feb 07, 2017 10:53 pm    Post subject: Reply with quote

dlpb wrote:
This stuff really needs to be documented better by MS and so on. Thanks! I'll add you as a thank you credit.

Smile


Keep in mind that 'EndThread' is not a standard Win32 API and instead is a library call from Delphi itself. Check out the documentation for Delphi. Some links that include some information regarding how EndThread functions would be:

http://docwiki.embarcadero.com/Libraries/Seattle/en/System.EndThread
http://www.delphibasics.co.uk/RTL.asp?Name=EndThread

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
dlpb
Advanced Cheater
Reputation: 0

Joined: 11 Dec 2013
Posts: 71

PostPosted: Wed Feb 08, 2017 11:28 am    Post subject: Reply with quote

Yeah, I already checked it. It calls the api ExitThread, though, I recall.

Edit. Yeah, also says so in your links.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites