1


Hi guys.
I have some overlay with lua support.
I need to create some WndProc callbacks, so I hooked WndProc of target window using SetWindowLong and saving old one

LRESULT CALLBACK nProc(HWND hWnd, UINT _Msg, WPARAM wP, LPARAM lP) // its new wndProc
{
    switch (_Msg)
    {
    case WM_CUT: case WM_COPY: case WM_PASTE: case WM_CLEAR:
        return 1;
    case WM_DESTROY: case WM_NCDESTROY:
        UnhookWndP(); // switching back to original wndProc
        return 1;
    case WM_KEYDOWN: case WM_KEYUP: case WM_LBUTTONDBLCLK: case WM_LBUTTONDOWN: case WM_LBUTTONUP: case WM_RBUTTONDBLCLK: case WM_RBUTTONDOWN: case WM_RBUTTONUP: case WM_MOUSEWHEEL:
        try
        {
            if (_Msg == WM_KEYUP && wP == 120) // restart LuaState
            {
                if (LuaInit)
                    DelLua();
                else
                    CreLua();
            }
            for (luabridge::LuaRef f : wndCall) // call all our WndProc Lua callbacks
            {
                if (Msg != NULL && wP != NULL && f.isFunction() &&  f.isFunction() && lua_gettop(L) == 0)
                {
                    f((int)_Msg, wP); // always in callstack when error occurs
                }
            }
        }
        catch (luabridge::LuaException ex) { Msg(ex.what()); }
    default:
        return CallWindowProc(OriWndP, hWnd, _Msg, wP, lP);
    }
}

Created global function for adding callback, which is saving func to wndCall Vector(LuaRef)
Lua part is working fine. It shows messages of WM_KEY... events successfully.
The only problem is: When i hold some button or spamming commands I got Crash with Random error :(
I think its because CALLBACK got hit multiple times at one tick and something got broken in LuaState or idk.
Please help me find a solution or some extra checks for WndProc func.

Alxspb
  • 35
  • 6
  • You are mixing the messages. You call `UnhookWndP` once in `WM_DESTROY` and again in `WM_NCDESTROY` (remove the reference to `WM_DESTROY`). You should also add separate handlers for `WM_KEYUP`, `WM_KEYDOWN` etc. – Barmak Shemirani Jun 09 '16 at 22:05
  • User-provided callback functions on Windows are invoked in threads belonging to OS (not in your host application's threads), so all code in your callback functions must be thread-safe. Lua is not thread-safe, that's why you are getting random errors. – Egor Skriptunoff Jun 09 '16 at 22:13
  • separated handlers and got it working finally! Its strange bit thank you) – Alxspb Jun 09 '16 at 22:23
  • Anyway have no idea why it isn't working in previous way. Does C++ can't use case: ... case: ... multiple conditions? – Alxspb Jun 09 '16 at 22:24
  • Also, I thought Im using it threadsafe cheking stack for null before executing any code from lua – Alxspb Jun 09 '16 at 22:26

0 Answers0