Loadlib problems
Status: Alpha
Brought to you by:
cwalther
From: Andrea V. <and...@gm...> - 2008-05-19 13:05:36
|
Hi Chriss, I have tryed your example for luaplugin in the extras folder. I managed to build an addon.dll, but I have some problems. 1) I cannot understand how to link the dll, to access functions defined in pipmak.exe (like terminalPrintf in your example), so I comment out these. (but in this moment, access to these functions isn't fundamental for me) 2) I have successfully compiled you example after commented out call to terminalPrintf and after added definition #define LUA_API __declspec(dllexport) and postponing LUA_API to the init definition: LUA_API int init(lua_State *L) { .... } Without these steps I get an "procedure not found" during loadlib (Probably this is needed only with Windows and/or MSVC) Your example run correctly without any errors. 3) I have modified your example in this manner: ----------------------------------------------------- #define LUA_API __declspec(dllexport) #include <stdlib.h> #include <string.h> #include "lua.h" #include "lauxlib.h" static int greeting(lua_State *L) { lua_pushliteral(L, "Hello World!"); return 1; } typedef struct { char *greeting; } Greeter; static int newgreeter(lua_State *L) { const char *s; Greeter *greeter; if(lua_gettop(L)!=1) luaL_error(L, "wrong number of args"); s=luaL_checkstring(L, 1); greeter = lua_newuserdata(L, sizeof(Greeter)); luaL_getmetatable(L, "hello-greeter"); lua_setmetatable(L, -2); greeter->greeting = malloc(strlen(s) + 1); strcpy(greeter->greeting, s); return 1; } static int greeterCollect(lua_State *L) { Greeter *greeter = (Greeter*)luaL_checkudata(L, 1, "hello-greeter"); if (greeter == NULL) luaL_typerror(L, 1, "greeter"); free(greeter->greeting); return 0; } static int greeterGreeting(lua_State *L) { Greeter *greeter = (Greeter*)luaL_checkudata(L, 1, "hello-greeter"); if (greeter == NULL) luaL_typerror(L, 1, "greeter"); lua_pushstring(L, greeter->greeting); return 1; } static int luaGetProjectPath(lua_State *L) { if(lua_gettop(L)!=0) luaL_error(L, "no argument required"); lua_pushstring(L, "pipmak-projectpath"); lua_rawget(L, LUA_REGISTRYINDEX); return 1; } static const luaL_reg mylib[] = { {"getprojectpath", luaGetProjectPath}, {"newgreeter", greeterGreeting}, {"greeting", greeterGreeting}, {NULL, NULL} }; static const luaL_reg greeterMethods[] = { {"__gc", greeterCollect}, {"greeting", greeterGreeting}, {NULL, NULL} }; LUALIB_API int init(lua_State *L) { luaL_openlib(L,"tools",mylib, 0); luaL_newmetatable(L, "hello-greeter"); lua_pushliteral(L, "__index"); lua_pushvalue(L, -2); lua_rawset(L, -3); luaL_openlib(L, NULL, greeterMethods, 0); lua_pop(L, 1); /*greeter metatable*/ lua_pop(L,1); /*mylib */ return 0; } ----------------------------------------------------- As you can see, I have created a library "tools" so these functions are permitted: a) tools.greeting() b) tools.getprojectpath() c) tools.newgreeter("test") Well, (a) works fine (b) works fine if i cannot add any arguments, but if I write tools.getprojectpath(22) (I want to check luaL_error statement...) pipmak crash with this error: debug assertion failed dbgheap.c _CrtIsValidHeapPointer(pUserData) This may be due to a corruption of the heap, and indicates a bug in Pipmak.exe or any of the DLLs it has loaded. (c) always crash with the error above What is wrong? I think that my code is correct. Do you have any ideas? function init() is correct? Or i have to do something other to create library "tools"? Thank you for your hint. Andrea |