1

我有一个使用 Lua 5.2.1 的 Visual Studio 2008 C++03 项目,我希望允许 Lua 脚本使用 Win32 迭代函数(FindFirst*、FindNext* 等...)

例如,我希望能够列出正在运行的进程:

#include <tlhelp32.h>
#pragma comment( lib, "toolhelp.lib" )

static int process_iter_next( lua_State* L )
{
    HANDLE h = *( HANDLE* )lua_touserdata( L, lua_upvalueindex( 1 ) );
    PROCESSENTRY32 pe;
    pe.dwSize = sizeof( PROCESSENTRY32 );
    if( ::Process32Next( h, &pe ) )
    {
        lua_pushstring( L, pe.szExeFile );
        return 1;
    }
    return 0;
}

static int process_iter_first( lua_State* L )
{
    HANDLE h = *( HANDLE* )lua_touserdata( L, lua_upvalueindex( 1 ) );
    PROCESSENTRY32 pe;
    pe.dwSize = sizeof( PROCESSENTRY32 );
    if( ::Process32First( h, &pe ) )
    {
        lua_pushstring( L, pe.szExeFile );

        // How do I replace the closure with process_iter_next?

        return 1;
    }
    return 0;
}

static int l_list( lua_State *L )
{
    HANDLE* h = ( HANDLE* )lua_newuserdata( L, sizeof( HANDLE ) );
    luaL_getmetatable( L, "process.list" );
    lua_setmetatable( L, -2 );

    *h = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS | TH32CS_SNAPNOHEAPS, 0 );
    if( INVALID_HANDLE_VALUE == *h )
        luaL_error( L, "Failed to list processes. EC: %d", ::GetLastError() );

    lua_pushcclosure( L, process_iter_first, 1 );
    return 1;
}

static int process_gc( lua_State* L )
{
    HANDLE h = *( HANDLE* )lua_touserdata( L, 1 );
    if( INVALID_HANDLE_VALUE != h )
        ::CloseToolhelp32Snapshot( h );
    return 0;
}

extern "C" int luaopen_process( lua_State *L ) 
{
    static const luaL_Reg process[] = {
        { "list", l_list },
        { NULL, NULL }
    };

    luaL_newmetatable( L, "process.list" );
    lua_pushstring( L, "__gc" );
    lua_pushcfunction( L, process_gc );
    lua_settable( L, -3 );

    luaL_newlib( L, process );

    return 1;
}

我的 Lua 脚本:

for p in process.list() do
    print(p)
end

当我运行它时,只会process_iter_first调用 。如何将创建的 c-closure 替换为l_listwhenprocess_iter_next成功Process32First


根据@Mud 的建议进行编辑*

static int process_iter( lua_State* L )
{
    HANDLE h = *( HANDLE* )lua_touserdata( L, lua_upvalueindex( 1 ) );
    typedef BOOL ( *PFN_ProcessIter )( HANDLE, LPPROCESSENTRY32 );
    PFN_ProcessIter next = ( PFN_ProcessIter )lua_touserdata( L, lua_upvalueindex( 2 ) );

    PROCESSENTRY32 pe;
    pe.dwSize = sizeof( PROCESSENTRY32 );
    if( next( h, &pe ) )
    {
        lua_pushstring( L, pe.szExeFile );

        lua_pushlightuserdata( L, Process32Next );

        // not sure how to use this function. The docs are not enlightening.
        // lua_setupvalue( L, ???, ??? );

        // This looked like a good idea from the docs, but it causes an access violation
        // lua_setuservalue( L, lua_upvalueindex( 2 ) );

        return 1;
    }
    return 0;
}

static int l_list( lua_State *L )
{
    HANDLE* h = ( HANDLE* )lua_newuserdata( L, sizeof( HANDLE ) );
    luaL_getmetatable( L, "process.list" );
    lua_setmetatable( L, -2 );

    *h = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS | TH32CS_SNAPNOHEAPS, 0 );
    if( INVALID_HANDLE_VALUE == *h )
        luaL_error( L, "Failed to list processes. EC: %d", ::GetLastError() );

    lua_pushlightuserdata( L, Process32First );
    lua_pushcclosure( L, process_iter, 2 );
    return 1;
}
4

1 回答 1

1

只需将要调用的函数地址作为闭包 ( lua_pushlightuserdata) 的另一个上值推送,然后在第一次调用时更新它。

或者将您的用户数据更改为包含句柄和函数指针的结构,并在第一次调用时更新函数指针。


对 PaulH 编辑的回应:

static int process_iter( lua_State* L )
{
   HANDLE h = *( HANDLE* )lua_touserdata( L, lua_upvalueindex( 1 ) );
   typedef BOOL (WINAPI *PFN_ProcessIter )( HANDLE, LPPROCESSENTRY32 );
   PFN_ProcessIter next = ( PFN_ProcessIter )lua_touserdata( L, lua_upvalueindex( 2 ) );

   PROCESSENTRY32 pe;
   pe.dwSize = sizeof( PROCESSENTRY32 );
   if( next( h, &pe ) )
   {
      lua_pushstring( L, pe.szExeFile );

      if (next == Process32First)
      {
         lua_pushlightuserdata(L, Process32Next);
         lua_replace(L, lua_upvalueindex(2));
      }

      return 1;
   }
   return 0;
}
于 2012-08-22T19:09:18.813 回答