lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hello Cloud,

This is not so much a Lua question, more a Windows question, but anyway.

Your options are:
1. The Global Variable
2. GetWindowLong/SetWindowLong

Option 1 is just plain obvious, have a global lua_State* L somewhere. It works fine, provided there is only one lua_State in the application.

Option 2 is for when you need one lua_State per window.
You can read MSDN on GetWindowLong/SetWindowLong (I won't explain them).
But the following code will give you an idea:

// init code
hWnd=CreateWindow(....);
lua_State* L=lua_open();
SetWindowLong(hWnd,(LONG)L); // put the state into the window

static LRESULT CALLBACK
WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam) 
{
  lua_State* L=(lua_State*)GetWindowLong(hWnd,GWL_USERDATA);
  // etc
}

Hope that helps,
Mark

> Hello ,
> 
>       When I wrote a windows application embedding lua, I want to call
>       some lua code in WinProc . Like this :
> 
> -------------------------------
> static LRESULT CALLBACK
> WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam) 
> {
>         switch (iMessage) {
>         case WM_MOUSEMOVE:
>              /* call some lua function */
>              ...
> ---------------------------------
> 
>       But in WndProc , I can't get the running thread in lua .
>       I can store the main lua state in a global variable, but the
>       main lua state may not be the running thread. And I have no way
>       to pass the L into the WndProc .
> 
>       How to get the current thread from the main lua state in C ?
> 
> -- 
> Best regards,
>  Cloud                          mailto:cloudwu@163.com
>             http://blog.codingnow.com