Page 1 of 1

Calling C function from a dll, in lua

Posted: Wed Mar 19, 2014 6:21 pm
by soulaymenc
hello lovers, I have been strugglin' for two days to make a DLL in C then call its functions from a lua script.
The C functions are to deal with wiiuse library, so I can use this feature in Love games.
So here is the code:

Code: Select all

#include "wiiuse.h"
#include "lua/lua.h"
#include "lua/lauxlib.h"

static int wiiInit(lua_State *L)
{
  int x = lua_tointeger(L, 1);
  printf("x = %d\n", x);
  //wiimote ** wm = wiiuse_init(x);
  //lua_pushinteger(L, wm);
  return 1;
}

int luaopen_wiilib (lua_State *L) {
	struct luaL_Reg driver[] = {
		{"wiiInit", wiiInit},
		{NULL, NULL}
	};
	luaL_openlib(L, "wiilib", driver, 0);
	return 1;
}
and the Lua code

Code: Select all

wiilib = require("wiilib")
wiilib.wiiInit(1, 2, 3)
So the thing is the output is always

Code: Select all

x = 0
It looks like the C functions from the dll file are not able to read arguments from Lua :(

What am I missing here? Any ideas, suggestions?
By the way the dll is compiled along with lua 5.2 and the wiiuse libraries.

Thanks <3

Re: Calling C function from a dll, in lua

Posted: Thu Mar 20, 2014 12:29 pm
by bartbes
You need to compile against lua 5.1 or luajit, so maybe that will fix it?

Re: Calling C function from a dll, in lua

Posted: Thu Mar 20, 2014 1:09 pm
by soulaymenc
Thanks for your reply :)

I compiled it using the latest version of lua 5.2.3, is that the problem maybe?