I created a C++ dll that can be injected into Garry's Mod. I get a Client lua state via CreateInterface. As soon as the dll loads and I get the lua_State, I try to call print("ruffles") and it randomly works, and randomly doesn't. Whenever it doesn't work it'll crash "client lua stack error" with a lua error or something like "<function>" I assume this means that whenever I inject it something else might be doing something on the stack the moment I inject it, and if so, how can I avoid this? I've tried using sleep(insert ms here) but that has failed aswell. If I'm completely wrong about the problem, what could be causing this?
Please Read
I'm not using this to hack. It's for learning and just enjoying something I like. If you are going to reply "You're just trying to hack, nobody help him." then just don't. I don't need 4 posts saying that I'm doing this no matter what I say. Thanks.
threading
Alright. Could you elaborate? Do you mean start a new thread inside of C++ (std::thread)?
I can already execute it fine, but random times it'll crash with an error.
Ex:
"client lua stack error
attempt to call a number value"
Whenever it does work, however, it does it just fine.
You CAN call them, but you are now chancing that the main thread is free to run the print call.
I see. Thank you.
What way would I get into the main thread? Would I have to detour a function so the main thread will call that, or is there another way?
depending on your injection method you could already be in the main thread so you don't have to call CreateThread, other than that no
I inject via C# and CreateRemoteThread. And wouldn't createthread run in a new thread, not the main thread?
Okay so after a bunch of testing with different things, I'm still unable to call lua functions without the game crashing (and sometimes not).
My C++ Lua is:
iface->PushSpecial(SPECIAL_GLOB);
iface->GetField(-1,"print");
iface->PushString("herro");
iface->Call(1, 1);
iface->Pop(iface->Top());
My DllMain DLL_PROCESS_ATTACH statement just calles Initialize() which is my function that calls the lua after getting he lua_State
All help is very much appreciated.
The problem is that Lua isn't thread safe. You cannot have 2 threads interacting with the same Lua state at the same time. Doing so will inevitably corrupt the internal structure of the Lua state and lead to a crash. You either need to find a way to pause the main thread while it isn't interacting with Lua, or find a way to execute your Lua calls in the main thread so you don't have to think about it.
The only reason it doesn't always crash is because sometimes you get lucky and your thread calls into Lua while the main thread is doing something not Lua related.
Now, I'm not all that familiar with the internal machinations of WIN32 API, but I think that DllMain is called by whatever thread called LoadLibrary, so if you are initiating the module load externally, it is most likely running in a new thread that only exists to call LoadLibrary, and thus, you have to be thinking about the concurrency issues that threading brings to the table.
Doing those Interface and detour functions things/hacks always seems quite advanced to me. A way more advanced (I think) then the concept of threads and the problems they could cause if not handled probably. This problem got explained multiple times, now. To be honest this guy seems not to understand or is ignoring these problems.
I would recommend you to get to know the basics of threading first before continuing to your project. You would get to learn what thread locking and thread safe programming is for instance. You might need that for your project. If you can't safeguard the game's main thread from getting corrupted by your project, you will never be able to use or finish it.
I firmly believe that any concept can be explained to anyone, so long as they are willing to learn, and you have the patience for it. Sometimes, you just need to explain it differently, or in a more familiar context. In this case, I think he may be deciding that his "personal experience" with this is more valuable than the advice being given here (humans are kind of wired to think like that), because he was told he couldn't do it this way, but he got it to work sometimes , so he thinks that he must be on the right track. You can't just tell someone that they can't do something without telling them why, or they may end up dismissing your advice.
OP, you have 2 paths forward for this particular project. Listen to Grocel's advice, and learn about thread synchronization, or listen to Handsome Matt's advice, and sidestep the threading issue altogether by accessing Lua only from the main thread. Personally, I think trying to synchronize with a thread that uses a "shared resource" with code that you didn't write is going to be a rough time, and you should just go with Handsome Matt's advice. The path of least resistance.
Thank you. This gave me my answer.
I was indeed trying to get lua_State for another game, but it was way too much for me, so I decided to take a step down and just use Garry's Mod based stuff so that I could learn how to properly use lua to c++ (one of my two objectives, first objective being getting the lua_State for another game)
Sorry, you need to Log In to post a reply to this thread.