LuaC Library: Access violation with certain function(s)

161 Views Asked by At

I've written a relatively basic C++ lua module using Visual Studio 2017. It exports functions from an application which can then be called from a lua script running within said application. It appeared to work fine for the most part, except for the fact that functions with more than two parameters result in an access violation. Below is a simplified variant of the code.

#include "lua.hpp"
#include "lauxlib.h"
#include <Windows.h>
#include <string>
#define LUA_LIB extern "C" __declspec(dllexport) int

typedef void(__fastcall* mod_Object)(void*, std::string*);
  static int lua_modObject(lua_State *L) {
  DWORD64 object = 0x155E941;
  static mod_Object newModObject = (mod_Object)(base + 0x2DF677);
  std::string mod = luaL_checkstring(L, 1);
  newModObject((LPVOID)object, &mod);
  return 0;
}
//The above function works as intended.
typedef void(__fastcall* mod_Multi_Object)(void*, std::string*,std::string*);
static int lua_mod_Multi_Object(lua_State *L) {
  DWORD64 object = 0x155E941;
  static mod_Multi_Object newModMultiObject = (mod_Multi_Object)(base +0x2DFC45);
  std::string moda = luaL_checkstring(L, 1);
  std::string modb = luaL_checkstring(L, 2);
  newModMultiObject((LPVOID)object, &moda, &modb);
  return 0;
}
//This time the above function, and any other function similar to it, results in a generic access violation or freeze.
LUA_LIB _init(lua_State *L) {
base = (DWORD64)GetModuleHandle(NULL);

lua_newtable(L);

lua_pushcfunction(L, &lua_modObject);
lua_setfield(L, -2, "objectMod");

lua_pushcfunction(L, &lua_mod_Multi_Object);
lua_setfield(L, -2, "objectMultiMod");

return 1;
}

The first function works as intended without crashing, whilst those which accept more than two parameters, such as the second, result in either a freeze or an access violation. Is there any particular reason as to why this might happen?

Thanks.

0

There are 0 best solutions below