LuaBridge assertion error - c++

I've been going through some tutorials around luabridge for accessing c++ from lua but I've hit a problem I can't seem to find an answer to by searching google.
I've setup a sample program to run the script and access lua from c++, which works fine. But when I try to register a function in the global namespace it complains during runtime - compiling just fine.
#include <iostream>
#include <string>
#include "../LuaBridge/LuaBridge.h"
using namespace luabridge;
void printMessage(const std::string& s) {
std::cout << s << std::endl;
}
int main() {
lua_State* L = luaL_newstate();
getGlobalNamespace(L).addFunction("printMessage", printMessage);
luaL_dofile(L, "script.lua");
luaL_openlibs(L);
lua_pcall(L, 0, 0, 0);
LuaRef s = getGlobal(L, "testString");
LuaRef n = getGlobal(L, "number");
std::string luaString = s.cast<std::string>();
int answer = n.cast<int>();
std::cout << luaString << std::endl;
std::cout << "And here's our number:" << answer << std::endl;
}
So, this code with the addFunction call gives me this error
Lua: /home/linuxxon/Programming/kingdoms-online/Script/Lua/../LuaBridge/detail/Namespace.h:1080: luabridge::Namespace& luabridge::Namespace::addFunction(const char*, FP) [with FP = void (*)(const std::basic_string<char>&)]: Assertion `(lua_type(L, (-1)) == 5)' failed.
Withouth the addFunction call I get what is expected of the script.
Is there perhaps something obvious that I have missed, since I haven't found anything like it?
I would greatly appreciate all help!

The problem was that the
luaL_openlibs(L);
was after the script had been run. The problem was in the tutorial I was following, I encountered the same thing before in the beginning of my tries with lua.
It all works perfectly by calling it after creating the new lua state.

Related

ShellExecuteA cannot find file

I'm trying to open a file with a c++ program using ShellExecuteA in Windows10. (I'm also using VisualStudio2019 in case that's relevant.)
ShellExecute itself is working (I can use "explore" and "find" as it is intended), however it seems to be unable to find the file even though it exists in the directory. I have tried both an absolute as well as a relative path and neither work.
My code is this:
#include <iostream>
#include <windows.h>
#include <shellapi.h>
#include <string>
#include <limits.h>
using namespace std;
string getCurrentDir() {
char buff[MAX_PATH];
GetModuleFileName(NULL, buff, MAX_PATH);
string::size_type position = string(buff).find_last_of("\\/");
return string(buff).substr(0, position);
}
int main()
{
cout << "path: " << getCurrentDir() << endl;
int ret1 = (int)ShellExecuteA(NULL, "open", "C:\\Users\\Sasha\\source\\repos\\shellopen\\Debug\\MyTextFile.txt", NULL, NULL, SW_SHOWNORMAL);
cout << ret1 << endl;
int ret2 = (int)ShellExecuteA(NULL, "open", "MyTextFile.txt", NULL, NULL, SW_SHOWNORMAL);
cout << ret2 << endl;
return 0;
}
The result is
path: C:\Users\Sasha\source\repos\shellopen\Debug
2
2
"2" apparently means that the file couldn't be found, however "MyTextFile.txt" definitely exists in the directory and there is no spelling mistake.
I've tried googling the problem but it seems to be uncommon enough that I haven't found anything that works for me. I'd be very grateful for any help.
Ok, it turned out it was a spelling mistake, causing my file to be named "MyTextFile.txt.txt"
Thank you to everyone who tried to help me find the answer.

Binding LuaJIT to C++ with LuaBridge results in "PANIC: unprotected error"

Windows 10 x64, MSVC 2017, LuaJIT 2.0.5.
I searched the web, but answers didn't help.
Basically I'm trying to follow this manual, except that I had to place #include <LuaBridge.h> after Lua includes, because otherwise it doesn't work saying that the LuaBridge should go after Lua includes.
Hovewher, I get following error: PANIC: unprotected error in call to Lua API (attempt to call a nil value).
I have no idea why. If you need more info - just say what.
#include "stdafx.h"
#include <iostream>
#include <lua.hpp>
#include <LuaBridge/LuaBridge.h>
using namespace luabridge;
using namespace std;
int main()
{
lua_State* L = luaL_newstate();
luaL_dofile(L, "script.lua");
luaL_openlibs(L);
lua_pcall(L, 0, 0, 0);
LuaRef s = getGlobal(L, "testString");
LuaRef n = getGlobal(L, "number");
string luaString = s.cast<string>();
int answer = n.cast<int>();
cout << luaString << endl;
cout << "And here's our number:" << answer << endl;
system("pause");
return 0;
}
script.lua:
testString = "LuaBridge works!"
number = 42
The code in the tutorial is faulty. lua_pcall has nothing to call because luaL_dofile and luaL_openlibs don't push a function onto the stack, so it tries to call nil and returns 2 (the value of the macro LUA_ERRRUN).
I verified this by changing the code from the tutorial thus and compiling with g++. I didn't get a PANIC error, for whatever reason; maybe because it was using Lua 5.3:
#include <iostream>
extern "C" {
# include "lua.h"
# include "lauxlib.h"
# include "lualib.h"
}
#include <LuaBridge/LuaBridge.h>
using namespace luabridge;
int main() {
lua_State* L = luaL_newstate();
luaL_dofile(L, "script.lua");
std::cout << "type of value at top of stack: " << luaL_typename(L, -1) << std::endl;
luaL_openlibs(L);
std::cout << "type of value at top of stack: " << luaL_typename(L, -1) << std::endl;
std::cout << "result of pcall: " << lua_pcall(L, 0, 0, 0) << std::endl; // Print return value of lua_pcall. This prints 2.
LuaRef s = getGlobal(L, "testString");
LuaRef n = getGlobal(L, "number");
std::string luaString = s.cast<std::string>();
int answer = n.cast<int>();
std::cout << luaString << std::endl;
std::cout << "And here's our number: " << answer << std::endl;
}
As you noticed, the code is also faulty because the Lua headers have to be included before the LuaBridge header!

C++ call lua_dostring to load lua scrip which has 'require('cjson')' raise error:cjson.so: undefined symbol: lua_getfield

I define a function In a Lua Script and call it from my C++ program. The Lua Script use cjson module. I can executes the Lua script by Lua bin, but it can't run in my C++ program.
Error Message:
error loading module 'cjson' from file '/usr/local/app/cswuyg/test_lua/install/cjson.so':
/usr/local/app/cswuyg/test_lua/install/cjson.so: undefined symbol: lua_getfield
cpp code:
extern "C"
{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
void test_dostring(lua_State* L, const std::string& file_path) {
std::ifstream ifs;
ifs.open(file_path.c_str());
if (!ifs.is_open()) {
return ;
}
std::stringstream buffer;
buffer << ifs.rdbuf();
std::string file_info(buffer.str());
// test luaL_dostring
std::cout << luaL_dostring(L, file_info.c_str()) << std::endl;
std::cout << "error msg:" << lua_tostring(L, -1) << std::endl;
lua_getglobal(L, "comment2");
lua_pushstring(L, "xxx");
lua_call(L, 1, 0);
std::string lua_ret = lua_tostring(L, -1);
std::cout << "ret:" << lua_ret << std::endl;
}
int main(int argc, char* argv[]) {
lua_State* L = luaL_newstate();
luaL_openlibs(L);
test_dostring(L, "test.lua");
lua_close(L);
return 0;
}
Lua code:
local Json = require('cjson')
function comment2(test)
print(test)
end
comment2("xx")
How to fix it ? Any help would be appreciated.
If you're using Linux and the Lua core library is linked statically into your program, you need to expose the Lua C API by using -Wl,-E when you build your program. That's the incantation used to build the Lua command line interpreter from lua.org.

C++: Can not read the value inside a spesific memory address of my program using a different program

I'm currently learning c++ and i decided that i want to give it a shot and try to write a simple program that can read the values of other programs that are stored in memory (and later on be able to modify these values).
To test it i wrote a program that does a simple addition, i then run my other program and I'm trying to read in real time the value of the first program using its address in the memory.
The code will help you to understand better what i mean.
The "target" program that i want to read the value from:
#include <iostream>
#include <windows.h>
#include <conio.h>
using namespace std;
int main()
{
int userValue=0;
int total=0;
int* t;
SetWindowText(NULL, "Memory");
while(1)
{
cin >> userValue;
total += userValue;
t = &total;
cout << *t << endl;
cout <<"Pointer: " << &t <<" Total: " << &total;
}
getch();
return 0;
}
And the program that reads the value from the first one:
#include <windows.h>
#include <iostream>
using namespace std;
int main()
{
DWORD address = 0x28FEF0;
int value = 0;
DWORD pid;
HWND hwnd;
hwnd = FindWindow(NULL,"Memory");
if(!hwnd)
{
cout <<"Window not found!\n";
cin.get();
}
else
{
GetWindowThreadProcessId(hwnd,&pid);
HANDLE phandle = OpenProcess(PROCESS_VM_READ,0,pid);
if(!phandle)
{
cout <<"Could not get handle!\n";
cin.get();
}
else
{
while(1)
{
ReadProcessMemory(phandle,(void*)address,&value,sizeof(value),0);
cout << value << "\n";
Sleep(1000);
}
return 0;
}
}
}
The code from the second one was taken from a forum (after googling) and it works fine if i use it with any other program, but it doesn't want to work at all with my program.
I tried changing the address to both the pointer t and the actual integer total, but nothing worked.
I feel like I've done something wrong in the first program rather than the second one.
I'm using Code:Blocks with GCC compiler.
Thanks in advance for any help!
Oh, and Merry Christmas!
&t = the address of the pointer that points to total
&total = the address of the actual "total value"
In your first program you should output "&total"
In your second program you should use ReadProcessMemory to read the address which is output from the first program.
If it doesn't work, you must run the second program as administrator to get permissions. If you still have trouble, call GetLastError() after each Windows API call, check the return value of ReadProcessMemory() and utilize the last argument of ReadProcessMemory() for error checking.

Lua 5.3 undefined references

I am trying to learn how to embed lua in a C program, but I am not great at reading technical documents, and I haven't found any current tutorials. This is my program:
#include <iostream>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
void report_errors(lua_State*, int);
int main(int argc, char** argv) {
for (int n = 1; n < argc; ++n) {
const char* file = argv[n];
lua_State *L = luaL_newstate();
luaL_openlibs(L);
std::cerr << "-- Loading File: " << file << std::endl;
int s = luaL_loadfile(L, file);
if (s == 0) {
s = lua_pcall(L, 0, LUA_MULTRET, 0);
}
report_errors(L, s);
lua_close(L);
std::cerr << std::endl;
}
return 0;
}
void report_errors(lua_State *L, int status) {
if (status) {
std::cerr << "-- " << lua_tostring(L, -1) << std::endl;
lua_pop(L, 1);
}
}
The compiler gives undefined reference errors for luaL_newstate, luaL_openlibs, luaL_loadfilex, lua_pcallk, and lua_close. I am using Code::Blocks one a Windows computer and I have added the lua include directory to all of the search paths and liblua53.a to the link libraries. The IDE autocompleted the header names and the parser displays most of the lua functions, but with a brief search I found that the parser could not find either lua_newstate or luaL_newstate. Why does it find some of the functions and not others?
In c++ you should include lua.hpp not lua.h. lua.h does not define the extern "C" block to stop the name mangling of the c++ compiler.
The arguments for g++ had -llua before the input file. I put -llua at the end, and everything works fine now.
undefined reference to `luaL_newstate'
Need extern "C" wrapping and also as recommended above put "-llua" at the end.
extern "C" {
#include <lua5.3/lualib.h>
#include <lua5.3/lauxlib.h>
#include <lua5.3/lua.h>
}
gcc -o l -ldl l.cpp -llua5.3