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 


Plugin - New Thread

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine
View previous topic :: View next topic  
Author Message
Icesythe7
How do I cheat?
Reputation: 0

Joined: 17 Jan 2016
Posts: 7

PostPosted: Tue Mar 01, 2022 10:51 pm    Post subject: Plugin - New Thread Reply with quote

I'm trying to create a plugin which works fine however if I create a new thread with a while loop it just closes CE, how might one create a new thread in a plugin? (Can't obviously use while loop in plugin thread or will lock up CE until while is done) (C++)
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 473

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

PostPosted: Wed Mar 02, 2022 6:19 am    Post subject: Reply with quote

it depends on what your thread does. You are aware that the dll runs inside the context of CE and not the target process?
_________________
Tools give you results. Knowledge gives you control.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
Icesythe7
How do I cheat?
Reputation: 0

Joined: 17 Jan 2016
Posts: 7

PostPosted: Wed Mar 02, 2022 2:28 pm    Post subject: Reply with quote

Dark Byte wrote:
it depends on what your thread does. You are aware that the dll runs inside the context of CE and not the target process?


Yes I'm aware so it's not possible to have a while loop in a plugin outside of CE's thread?

As far as what it did it didn't do anything just a naked while true for a test and it just closes cheat engine
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 473

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

PostPosted: Wed Mar 02, 2022 4:52 pm    Post subject: Reply with quote

if it is a thread then it should be fine. But what kind of thread are you talking about and how do you create it ? And are you waiting for the thread to finish before continuing ?
_________________
Tools give you results. Knowledge gives you control.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
Icesythe7
How do I cheat?
Reputation: 0

Joined: 17 Jan 2016
Posts: 7

PostPosted: Wed Mar 02, 2022 10:55 pm    Post subject: Reply with quote

Dark Byte wrote:
if it is a thread then it should be fine. But what kind of thread are you talking about and how do you create it ? And are you waiting for the thread to finish before continuing ?


just an std::thread and not waiting for it to finish I need it to just run forever basically because I plan on creating an overlay window with dxd9 but can't get a thread to stay alive yet xd.

Code:
BOOL CEPlugin_InitializePlugin(PExportedFunctions ef, int pluginid)
{
   self_plugin_id = pluginid;

   exported = *ef;

   if (exported.sizeofExportedFunctions != sizeof exported)
      return false;

   std::thread t1(NewThread);
...


Code:
[[noreturn]] void NewThread()
{
   while (true)
   {
      printf("test\n");
   }
}
this was the test I did
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 473

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

PostPosted: Wed Mar 02, 2022 11:07 pm    Post subject: Reply with quote

remove that printf and see how that goes (there is no console)
_________________
Tools give you results. Knowledge gives you control.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
Icesythe7
How do I cheat?
Reputation: 0

Joined: 17 Jan 2016
Posts: 7

PostPosted: Wed Mar 02, 2022 11:10 pm    Post subject: Reply with quote

Dark Byte wrote:
remove that printf and see how that goes (there is no console)


Still closes CE like 3 seconds after plugin loads, I have a message box at the end of the Init plugin function that is displayed saying the function returns successfully then CE just closes itself.
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 154

Joined: 06 Jul 2014
Posts: 4754

PostPosted: Wed Mar 02, 2022 11:10 pm    Post subject: Reply with quote

What comes after the ellipses is important. If you don't explicitly join or detach it, the destructor will call std::terminate, which is probably bad.
_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
Icesythe7
How do I cheat?
Reputation: 0

Joined: 17 Jan 2016
Posts: 7

PostPosted: Wed Mar 02, 2022 11:14 pm    Post subject: Reply with quote

ParkourPenguin wrote:
What comes after the ellipses is important. If you don't explicitly join or detach it, the destructor will call std::terminate, which is probably bad.


nothing thread related was under the impression if I call thread join then i freeze CE's thread again waiting for it, hence I need a new thread so the dll has its own thread to do stuff in without ce waiting for it

Code:
BOOL CEPlugin_InitializePlugin(PExportedFunctions ef, int pluginid)
{
   self_plugin_id = pluginid;

   exported = *ef;

   if (exported.sizeofExportedFunctions != sizeof exported)
      return false;

   std::thread t1(NewThread);

   lua_game_state = std::make_unique<sol::state_view>(ef->GetLuaState());
   auto& lua = *lua_game_state;

   lua["testFunc"] = [](const uint64_t num1, const uint64_t num2)
   {
      return (num1 + num2) / 2;
   };

   lua.new_usertype<Vector3>("Vector3",
      sol::constructors<Vector3(float, float, float)>(),
      "x", &Vector3::x,
      "y", &Vector3::y,
      "z", &Vector3::z);

   lua.new_usertype<Example>("ExampleClass", sol::no_constructor,
      "health", &Example::health,
      "maxHealth", &Example::max_health,
      "pos", &Example::pos,
      "getHealthPercent", &Example::getHealthPercent);

   lua.set("Player", Example());

   exported.ShowMessage(const_cast<char*>("Lua Test Plugin enabled!")); //this is printed

   return true; //returns fine
}


edit yep you were right destructor was being called automatically setting it globally fixed the issue xd
Code:

std::thread window_thread;
BOOL CEPlugin_InitializePlugin(PExportedFunctions ef, int pluginid)
{
   self_plugin_id = pluginid;

   exported = *ef;

   if (exported.sizeofExportedFunctions != sizeof exported)
      return false;

   window_thread = std::thread(NewThread);
...
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 154

Joined: 06 Jul 2014
Posts: 4754

PostPosted: Wed Mar 02, 2022 11:37 pm    Post subject: Reply with quote

You can also call detach.
https://en.cppreference.com/w/cpp/thread/thread/detach

_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
Icesythe7
How do I cheat?
Reputation: 0

Joined: 17 Jan 2016
Posts: 7

PostPosted: Wed Mar 02, 2022 11:49 pm    Post subject: Reply with quote

ParkourPenguin wrote:
You can also call detach.
snip cant post urls

Yes but I need to kill the thread in disable plugin it kinda works but my console locks up instead of closing even tho I join and wait for the thread to close.

edit: fixed console issue
Code:

void NewThread()
{
   if (AllocConsole())
   {
      console_window = GetConsoleWindow();
      SetConsoleTitle("CE Plugin Debug Console");
      FILE* stream;
      freopen_s(&stream, "CONOUT$", "w", stdout);
   }

   static uint64_t num = 0;
   while (!shutdown)
   {
      printf("Test %llu\n", num);
      num++;
      Sleep(1);
   }

   FreeConsole();
}

Code:

BOOL CEPlugin_DisablePlugin()
{
   shutdown = true;
   window_thread.join();
   if (console_window)
   {
      FreeConsole();
      PostMessage(console_window, WM_CLOSE, 0, 0);
   }
   MessageBoxA(nullptr, "Disabled plugin", "Lua Test Plugin", MB_OK);
   return true;
}

seems to be working as expected now I appreciate the help!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine 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