View previous topic :: View next topic |
Author |
Message |
dlpb Advanced Cheater Reputation: 0
Joined: 11 Dec 2013 Posts: 71
|
Posted: Sat Feb 04, 2017 7:09 pm Post subject: Calling a Function in a New Thread |
|
|
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 |
|
|
STN I post too much Reputation: 42
Joined: 09 Nov 2005 Posts: 2672
|
Posted: Sat Feb 04, 2017 7:15 pm Post subject: |
|
|
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
_________________
|
|
Back to top |
|
|
dlpb Advanced Cheater Reputation: 0
Joined: 11 Dec 2013 Posts: 71
|
Posted: Sat Feb 04, 2017 7:21 pm Post subject: |
|
|
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 |
|
|
atom0s Moderator Reputation: 199
Joined: 25 Jan 2006 Posts: 8518 Location: 127.0.0.1
|
Posted: Sat Feb 04, 2017 7:23 pm Post subject: |
|
|
Are you trying to fully take over the function or just add your call to it?
_________________
- Retired. |
|
Back to top |
|
|
dlpb Advanced Cheater Reputation: 0
Joined: 11 Dec 2013 Posts: 71
|
Posted: Sat Feb 04, 2017 7:27 pm Post subject: |
|
|
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 |
|
|
dlpb Advanced Cheater Reputation: 0
Joined: 11 Dec 2013 Posts: 71
|
Posted: Mon Feb 06, 2017 8:56 pm Post subject: |
|
|
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 |
|
|
dlpb Advanced Cheater Reputation: 0
Joined: 11 Dec 2013 Posts: 71
|
Posted: Tue Feb 07, 2017 2:59 pm Post subject: |
|
|
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
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 |
|
|
Dark Byte Site Admin Reputation: 458
Joined: 09 May 2003 Posts: 25296 Location: The netherlands
|
Posted: Tue Feb 07, 2017 5:57 pm Post subject: |
|
|
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 |
|
|
dlpb Advanced Cheater Reputation: 0
Joined: 11 Dec 2013 Posts: 71
|
Posted: Tue Feb 07, 2017 6:59 pm Post subject: |
|
|
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 |
|
|
Dark Byte Site Admin Reputation: 458
Joined: 09 May 2003 Posts: 25296 Location: The netherlands
|
Posted: Tue Feb 07, 2017 7:15 pm Post subject: |
|
|
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 |
|
|
dlpb Advanced Cheater Reputation: 0
Joined: 11 Dec 2013 Posts: 71
|
Posted: Tue Feb 07, 2017 7:38 pm Post subject: |
|
|
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 |
|
|
Dark Byte Site Admin Reputation: 458
Joined: 09 May 2003 Posts: 25296 Location: The netherlands
|
Posted: Tue Feb 07, 2017 7:54 pm Post subject: |
|
|
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 |
|
|
dlpb Advanced Cheater Reputation: 0
Joined: 11 Dec 2013 Posts: 71
|
Posted: Tue Feb 07, 2017 8:07 pm Post subject: |
|
|
This stuff really needs to be documented better by MS and so on. Thanks! I'll add you as a thank you credit.
|
|
Back to top |
|
|
atom0s Moderator Reputation: 199
Joined: 25 Jan 2006 Posts: 8518 Location: 127.0.0.1
|
|
Back to top |
|
|
dlpb Advanced Cheater Reputation: 0
Joined: 11 Dec 2013 Posts: 71
|
Posted: Wed Feb 08, 2017 11:28 am Post subject: |
|
|
Yeah, I already checked it. It calls the api ExitThread, though, I recall.
Edit. Yeah, also says so in your links.
|
|
Back to top |
|
|
|