PDA

View Full Version : c(++) to delphi?



noeska
12-07-2004, 09:34 AM
I encountered the following c syntax how do i convert it to delphi?



static const struct lual_reg arraylib [] = {
{"new", newarray},
{"set", setarray},
{"get", getarray},
{"size", getsize],
{NULL,NULL}
};


This structure has to be passed to a c function in a dll. I am out of ideas...

Useless Hacker
12-07-2004, 01:07 PM
It would be helpful if you posted the structure definition too...

noeska
13-07-2004, 09:06 PM
here is the lua_reg structure as it appears in lua.pas:


type
luaL_reg = record
name: PChar;
func: lua_CFunction;
end;
PluaL_reg = ^luaL_reg;

var
luaL_openlib: procedure(L: Plua_State; const lr: PluaL_reg; nup: Integer); cdecl;


and this as in lauxlib.h:


typedef struct luaL_reg {
const char *name;
lua_CFunction func;
} luaL_reg;


LUALIB_API void luaL_openlib (lua_State *L, const char *libname,
const luaL_reg *l, int nup);


I hope this helps explaining the problem....

Also:


typedef struct lua_State lua_State;

typedef int (*lua_CFunction) (lua_State *L);

is in delphi:


type
Plua_State = Pointer;

lua_CFunction = function(L: Plua_State): Integer; cdecl;

This last thing is known to work ok.

Sly
14-07-2004, 03:37 AM
This should do it.


implementation

const
arraylib = array [0..4] of lual_reg =
(
(name: 'new'; func: newarray),
(name: 'set'; func: setarray),
(name: 'get'; func: getarray),
(name: 'size'; func: getsize),
(name: nil; func: nil)
);

begin
// Note that PAS declaration is missing the libname parameter
luaL_openlib(@luaState, 'libname', @arraylib[0], High(arraylib));
end;

Ultra
14-07-2004, 04:43 PM
A bit off topic, but where did you find the delphi headers? I've search for it but haven't found any.

noeska
14-07-2004, 09:05 PM
ultra look here: http://assoc.wanadoo.fr/thallium-software/thlua.en.html

sly your solution does not seem to work. It looks ok though...

Ultra
17-07-2004, 12:21 AM
Thanks for the tip. I'm sorry I can't seem to find a solution to your problem.

noeska
23-07-2004, 11:34 AM
It turned out the procedure definition for luaL_openlib in the lua.pas file was wrong. I uploaded the final working example here: http://www.noeska.com/dlua/downloads.aspx

Are there more people here interested in lua? As it seems that the thlua is not maintained anymore. Meaning the lua.pas is not up to date anymore.