LuaRef Call crash in WNDPROC CALLBACK

265 views Asked by At


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.

0

There are 0 answers