Binded C++ Class In DLL
kuku super <[email protected]> Thu, 30 Jun 2011 11:19:56 +0800
| Newsgroups | gmane.comp.lang.lua.bind.user |
|---|---|
| Message-ID | <[email protected]> |
it's a simple example about this error, and i still have no idea.....
---------------------------BINDDLL.h--------------------------
#ifndef BINDDLL_H
#define BINDDLL_H
#ifdef BINDDLL_EXPORTS
#define DLLEXPORT __declspec(dllexport)
#define DLLIMPORT __declspec(dllimport)
#else
#define DLLEXPORT
#define DLLIMPORT
#endif
struct lua_State;
bool DLLEXPORT Bind(lua_State* pState);
#endif
-------------------------------BINDDLL.cpp-------------------------------
-------------------------------just bind the BindTestClass
class----------------------------
#include "BindDLL.h"
#include <luabind/luabind.hpp>
#include <luabind/lua_include.hpp>
#include <luabind/operator.hpp>
#include <luabind/detail/convert_to_lua.hpp>
#include <luabind/iterator_policy.hpp>
class BindTestClass
{
public:
int GetA()
{
return a;
}
void SetA(int i)
{
a = i;
}
int a;
char str[10];
};
static luabind::object Getstr(BindTestClass &bindTestClass, lua_State *L)
{
return luabind::object(L, bindTestClass.str);
}
static void Setstr(BindTestClass &bindTestClass, luabind::argument const&
table)
{
strcpy_s(bindTestClass.str, 10, luabind::object_cast<const char*>(table));
}
bool DLLEXPORT Bind(lua_State* pState)
{
luabind::object g = luabind::globals(pState);
luabind::module(pState)
[
luabind::class_<BindTestClass>("BindTestClass")
.def(luabind::constructor<>())
.def_readwrite("a", &BindTestClass::a)
.property("str", Getstr, Setstr, luabind::raw(_2))
];
return true;
}
---------------------------------------LuaBindTestMain.cpp----------------------------------------------------
----------------------------------------1--------------------------------------------------------------------------------
#include "BindDLL.h"
#include <luabind/luabind.hpp>
#include <luabind/lua_include.hpp>
#include <luabind/operator.hpp>
#include <luabind/detail/convert_to_lua.hpp>
#include <luabind/iterator_policy.hpp>
class BindTestClass
{
public:
int GetA()
{
return a;
}
void SetA(int i)
{
a = i;
}
int a;
char str[10];
};
static luabind::object Getstr(BindTestClass &bindTestClass, lua_State *L)
{
return luabind::object(L, bindTestClass.str);
}
static void Setstr(BindTestClass &bindTestClass, luabind::argument const&
table)
{
strcpy_s(bindTestClass.str, 10, luabind::object_cast<const char*>(table));
}
void main()
{
lua_State *pL = lua_open();
luaL_openlibs(pL);
luabind::open(pL);
luabind::object g = luabind::globals(pL);
luabind::module(pL)
[
luabind::class_<BindTestClass>("BindTestClass")
.def(luabind::constructor<>())
.def_readwrite("a", &BindTestClass::a)
.property("str", Getstr, Setstr, luabind::raw(_2))
];
//Bind(pL);
int iRet = luaL_dostring(pL,
"local obj = BindTestClass();"
"obj.str = \"aaa\";"
);
if (iRet != 0)
{
printf("luaL_dostring failed:%s", lua_tostring(pL, -1));
lua_pop(pL, 1);
}
}
---------------------------------------LuaBindTestMain.cpp----------------------------------------------------
----------------------------------------2--------------------------------------------------------------------------------
#include "BindDLL.h"
#include <luabind/luabind.hpp>
#include <luabind/lua_include.hpp>
#include <luabind/operator.hpp>
#include <luabind/detail/convert_to_lua.hpp>
#include <luabind/iterator_policy.hpp>
class BindTestClass
{
public:
int GetA()
{
return a;
}
void SetA(int i)
{
a = i;
}
int a;
char str[10];
};
void main()
{
lua_State *pL = lua_open();
luaL_openlibs(pL);
luabind::open(pL);
Bind(pL);
int iRet = luaL_dostring(pL,
"local obj = BindTestClass();"
"obj.str = \"aaa\";"
);
if (iRet != 0)
{
printf("luaL_dostring failed:%s", lua_tostring(pL, -1));
lua_pop(pL, 1);
}
}
------------------------------------------------------------------------------Code
end--------------------------------
BindDLL.h and BindDll.cpp is in the DLL project. and luaBindTestMain.cpp is
exe project's code file.
if i use the 1 LuaBindTestMain.cpp. "obj.str = "aaa;"" will call the
Setstr Function
and if i use the 2 LuaBindTestMain.cpp, "obj.str = "aaa;"" won't call the
Setstr Function
help please!
------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2d-c2
_______________________________________________
luabind-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/luabind-user