lua-users home
lua-l archive

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


2009/9/10 Anurag Sharma <anurag.sharma.101@gmail.com>:
> I wrote an extension of Lua. Now I am writing a Lua script which makes a
> call to a function in that extension.
>
> signature of the function is as follows
> get_version(char* a, int b)
>
>
> My Lua script is as follows
>
> SUCCESS = 1
> Version = '          '  --  # Create a 10 character buffer to store the version string
> status = extLua:get_version(Version,10)
> if (status != SUCCESS) then die ("Returned bad status: $status") end
> print("\nCurrent Version: " Version "\n")
> print("\nEnd of example\n")

Strings in Lua are immutable. You probably noticed that lua_tostring
returns a "const char*", not a "char*". It's wrong to overwrite the
content of an existing Lua string. So either your get_version function
don't work (ie. Version don't get changed), or you're overwriting a
Lua string.

A common way to return a value and signal success at the same time is
to return nil on error (with an error/status message as second return
value), and the value otherwise. You would then write your Lua code as
follows:

local Version = assert(extLua.get_version())
print("Current Version:", Version)