lua-users home
lua-l archive

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


suluyu wrote:
> My project  needs to call c function from a dll in lua script. I try 
> to write a sample with C++ builder and Lua5.1.3, but I come up against 
> some problems.
>  
> The c library's name is "dllforlua.dll", the codes are:
>  
> ---c library begin--------
>   static int lua_msgbox(lua_State* L)
>  {
>     const char* message = luaL_checkstring(L, 1);
>     const char* caption = luaL_optstring(L, 2, "");
>     int result = MessageBox(NULL, message, caption, MB_YESNO);
>     lua_pushnumber(L, result);
>  
>     return 1;
>  }
>  
>  static const  luaL_Reg mylib[] =
>  {
>      {"msgbox", lua_msgbox},
>      {NULL, NULL}
>  };
>  
>  int __declspec(dllexport) luaopen_dllforlua(lua_State* L)
> {
>      //lua_register(L, "msgbox",  lua_msgbox);
>      luaL_register(L, "dllforlua", mylib);
>      return 1;
>  }
> ---c library end----------------
>  
>  
> The lua script is:
>   dllforlua = package.loadlib("dllforlua.dll", "_luaopen_dllforlua")
>   dllforlua()
>   msgbox("Hey, it worked!", "Lua Message Box") 
>  
> If use the function "lua_register", the lua script executed successfully.
> But if use the function "luaL_register", exectued unsuccessfully,the 
> error message is: "name conflict for module 'dllforlua' ".
> If I use require in lua script, the lua script fail to execute also.
>  
> Could someone give me any advices?
> Thanks very much!
>  
>  
> ------------------------------------------------------------------------
> suluyu
> 2008-06-23

The script and c module must agree on how the module is loaded. In the
attached zip file are two possible ways for them to agree. In version1,
the luaL_register call is used and the script has been changed to use
the dllforlua table which is created. In version2, the lua_register call
is used and the script has been changed to correctly call the
luaopen_dllforlua function.

In both cases, a batch file is supplied which will build the dll using
Visual Studio. Sorry, but I don't have C++ Builder.

Attachment: dllforlua.zip
Description: Binary data