473,499 Members | 1,648 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing values between Lua and C

42 New Member
Hi!
I'm trying to learn Lua scripting from within C and I'm now stuck on this (simple?) problem. I want to pass a value from C to Lua, and then get a value from Lua back to C.

Here's an example of a piece of C-code which gets a value from Lua and which works fine for me:
Expand|Select|Wrap|Line Numbers
  1. int test_Lua()
  2. {
  3.   lua_State *L=lua_open();
  4.   char myArg[] = "result = \"Lua-string\"";
  5.   luaL_dostring(L,myArg);
  6.   lua_pushstring(L,"result");
  7.   lua_gettable(L,LUA_GLOBALSINDEX);
  8.   char *result = lua_tostring(L,-1);
  9.   printf("\nValue of result fetched from Lua: %s\n",result);
  10.   lua_close(L);
  11.   return 0; 
  12. }
I get the expected result:
Value of result fetched from Lua: Lua-string

However, I want to pass a string from C to Lua and in Lua concatenate it with another string and then pass the result back to C again.
This is how I try to do it:
Expand|Select|Wrap|Line Numbers
  1. int test_Lua2()
  2. {
  3.   lua_State *L=lua_open();
  4.   char myArg[] = "result = \"Lua-string and\" .. cstr";
  5.   char cstr[] = "C-string";
  6.  
  7.   lua_pushstring(L,cstr); /*This is how I try to pass the c-string to Lua*/
  8.  
  9.   /*I now 'assume' the c-string is at the top of the Lua stack, so Lua should know it (?)*/
  10.   luaL_dostring(L,myArg);
  11.   lua_pushstring(L,"result");
  12.   lua_gettable(L,LUA_GLOBALSINDEX);
  13.   char *result = lua_tostring(L,-1);
  14.   printf("\nValue of result fetched from Lua: %s\n",result);
  15.   lua_close(L);
  16.   return 0; 
  17. }
But, the thing is, Lua doesn't know cstr, so result is NULL.
(I get the printout "Value of result fetched from Lua: (null)")
Does anyone know what I'm doing wrong and how to do it right?
Any help greatly appreciated!

Cheers
Mar 19 '08 #1
0 1416

Sign in to post your reply or Sign up for a free account.

Similar topics

3
14902
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
2
4287
by: Morgan | last post by:
Thanks to all of you because I solved the problem related with my previous post. I simply made confusion with pointers to pointers and then succeeded passing the reference to the first element...
2
1915
by: craigkenisston | last post by:
Hi, I created an array of objects like this : object Values = {myObject.myprop, otherobject.otherprop, thirdobject.xprop}; Then I pass it to a method. and I get the values filled in that...
1
2688
by: olduncleamos | last post by:
Hello all. With a background in ASP, I am finding the work required for passing values between pages mystifying. For various obvious reasons, I have eliminated using cookies and session to store...
11
8091
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
2
3619
by: o1j2m3 | last post by:
I created 2 forms, A and B using VB.NET 2003 in A, the codes are : ========================== public AsName as string public AsPrice as double public sub setValue(ByVal sname as string,...
1
2092
by: vncntj | last post by:
I have a C#.NET that simply passes 6 values to a Stored Procedure. But I'm trying to get the (Default.aspx.cs page) to handle passing the values to the sp. The goal is to pass the values and see...
3
2110
by: ishwarbg | last post by:
Hi Everyone, I have a .Net Application, through which I am invoking a function from a legacy DLL developed in C++. My structure in C# contains some data of type double which I need to pass to to...
2
1938
by: csmith8933 | last post by:
How do I write a function where the number of parameters it takes varies? This is what I have but it doesnt work. // function prototype void functionThree(int num1=1, int num2=2, int num3=3);...
5
3190
by: jmartmem | last post by:
Greetings, I have built an Update Record Form in an ASP page. This form contains a number of fields, such as text boxes and menus, to name a few. Upon clicking the 'submit' button, I want the...
0
7132
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7009
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7178
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4602
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
302
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.