Binded C++ Class In DLL

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Binded C++ Class In DLL

kuku super
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Binded C++ Class In DLL

kuku super
i found the reason.

because, luabind bind the .property member use the property_tag function pointer.

so when bind the member in a dll with the property_tag pointer in the dll
and when set the member in the .exe, the luabind::detail::class_rep::settable(lua_State* L) compare the pointer setted by dll and the &property_tag in .exe.  of course they were different. so the problem occured.

that's the bug.

i solved this problem by setting property_tag a const pointer temporarily.

would it be fixed?

On Thu, Jun 30, 2011 at 11:19 AM, kuku super <[hidden email]> wrote:
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Binded C++ Class In DLL

Daniel Wallin
On Thu, Jun 30, 2011 at 9:18 AM, kuku super <[hidden email]> wrote:

> i found the reason.
> because, luabind bind the .property member use the property_tag function
> pointer.
> so when bind the member in a dll with the property_tag pointer in the dll
> and when set the member in the .exe, the
> luabind::detail::class_rep::settable(lua_State* L) compare the pointer
> setted by dll and the &property_tag in .exe.  of course they were different.
> so the problem occured.
> that's the bug.
> i solved this problem by setting property_tag a const pointer temporarily.
> would it be fixed?

Are you linking dynamically to luabind? A very common mistake is
forgetting to define LUABIND_DYNAMIC_LINK when building both the
shared library and the application.

--
Daniel Wallin
BoostPro Computing
http://www.boostpro.com

------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Binded C++ Class In DLL

kuku super
if u forgot the LUABIND_DYNAMIC_LINK marco, dll can't be compiled successfully. 

whether using the luabind static lib or using the luabind dynamic link, the bug will occur .



On Thu, Jun 30, 2011 at 4:00 PM, Daniel Wallin <[hidden email]> wrote:
On Thu, Jun 30, 2011 at 9:18 AM, kuku super <[hidden email]> wrote:
> i found the reason.
> because, luabind bind the .property member use the property_tag function
> pointer.
> so when bind the member in a dll with the property_tag pointer in the dll
> and when set the member in the .exe, the
> luabind::detail::class_rep::settable(lua_State* L) compare the pointer
> setted by dll and the &property_tag in .exe.  of course they were different.
> so the problem occured.
> that's the bug.
> i solved this problem by setting property_tag a const pointer temporarily.
> would it be fixed?

Are you linking dynamically to luabind? A very common mistake is
forgetting to define LUABIND_DYNAMIC_LINK when building both the
shared library and the application.

--
Daniel Wallin
BoostPro Computing
http://www.boostpro.com

------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user


------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Binded C++ Class In DLL

Daniel Wallin
On Fri, Jul 1, 2011 at 9:16 AM, kuku super <[hidden email]> wrote:
> if u forgot the LUABIND_DYNAMIC_LINK marco, dll can't be compiled
> successfully.

Yes, but did you also define it when building the client application?

--
Daniel Wallin
BoostPro Computing
http://www.boostpro.com

------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Binded C++ Class In DLL

kuku super
only debug version, and now it's static lib version. and when build the dll version, just change the ConfigurationType of project luabind to dll and modify the preprocessor definitions " LUABIND_DYNAMIC_LINK_" to  "LUABIND_DYNAMIC_LINK"

On Mon, Jul 4, 2011 at 10:04 AM, kuku super <[hidden email]> wrote:
no, i didn't.

attach my project followed, have a look please.

luabind and lua code files didn't include in the rar.


On Fri, Jul 1, 2011 at 10:11 PM, Daniel Wallin <[hidden email]> wrote:
On Fri, Jul 1, 2011 at 9:16 AM, kuku super <[hidden email]> wrote:
> if u forgot the LUABIND_DYNAMIC_LINK marco, dll can't be compiled
> successfully.

Yes, but did you also define it when building the client application?

--
Daniel Wallin
BoostPro Computing
http://www.boostpro.com

------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user



------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user