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
You probably need to add a .def file (e.g., dllforlua.def) to your
project (this is what I do with C++ Builder in order to get rid of
leading underscores). Its contents:
EXPORTS
luaopen_dllforlua = _luaopen_dllforlua
--
Shmuel