Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8858951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:56:34+00:00 2026-06-14T14:56:34+00:00

Now I want to pass a void* pointer to Lua, use userdata? How to

  • 0

Now I want to pass a void* pointer to Lua, use userdata?
How to do this?

BTW, I used luabind, but it cannot pass a void* pointer to Lua stack, this is annoying! Could you guys help me?

struct Event
{
     A* a;
     B* b;
     ...
};

Event *e;
void* instance = (void*)e;


// param is a parameter that is passed from Lua script. param is a Event object. And I cast it into a void* type
string Answer(void* param)
{
    WorkEvent *pWorkEvent = static_cast<WorkEvent*>(param);
    ASSERT_RET(pWorkEvent, NULL);

    string call_id = pWorkEvent->GetCallId();
    CCmThreadManager::TType thrd_id =  pWorkEvent->GetHandleThrdID();
    Coroutine *pco    = pWorkEvent->m_pco;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:56:35+00:00Added an answer on June 14, 2026 at 2:56 pm

    Let’s answer your actual question before getting into why you shouldn’t be doing this.

    LuaBind is a tool for binding C++ functions and objects to Lua. A void* is neither a function nor an object. It is, quite literally, a pointer to nothing. As such, it has no real meaning to LuaBind. So you cannot pass one directly.

    You can however return a luabind::object, which can represent any Lua value. For example, a Lua userdata. Which means you can create some light userdata from your void*, stick it into a luabind::object and return it from a function you register with LuaBind.

    luabind::object RegisteredFunction(..., lua_State *L)
    {
      void *return_value = ...;
    
      lua_pushlightuserdata(L, return_value);
      luabind::object ret(luabind::from_stack(L, -1));
      lua_pop(L, 1);
      return ret;
    }
    

    To allow Lua to pass it back so that you can retrieve it, simply create a function that takes a luabind::object as the void* parameter:

    void OtherRegisteredFunction(..., luabind::object obj, ...)
    {
      assert(luabind::type(obj) == LUA_TLIGHTUSERDATA);
      obj.push();
      void *param = lua_touserdata(obj.interpreter(), -1);
      lua_pop(obj.interpreter(), 1);
    }
    

    So that’s how you pass that kind of data. Now here’s why you shouldn’t do that.

    First, your code is broken:

    WorkEvent *pWorkEvent = static_cast<WorkEvent*>(param);
    

    Assuming that your param came from this line, void* instance = (void*)e, C++ doesn’t guarantee this to work. If you cast an object to a void*, C++ only provides a guarantee of getting something useful back if you cast it to the exact same object as it was before.

    You started with an Event*, then converted it to a void*. The only legal operation you can do is to cast it back to an Event*. Even if WorkEvent is a derived class of Event, you cannot cast it directly to that class. You have to turn it back into an Event* first. Also, you should use a dynamic_cast to do the down-cast once you have an Event*.

    Second, stop using void*s for this. If all of your events are derived from Event*, then just pass an Event* and use dynamic_cast where appropriate. If they aren’t, then you should be using boost::any (LuaBind requires Boost, so obviously you’re using it already). Not only will you be able to bind that to LuaBind (since it’s an actual type), it’s much more natural to work with.

    And it has built-in protection from exactly the problem your code had before; if you tried to cast it to a WorkEvent when it was given only an Event, it would throw an exception.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have this program below. Now I want to pass both the
This is my code i want to pass parameters to the flyout function but
I need to pass to function pointer to int. Now if I want to
I have created Web-Service in asp.net with c# . Now i want to pass
i want to reload ViewdidLoad in a method But now want to call again
I'm receiving an xml response and I now want to parse this. Currently what
i have wrote a script to produce an array of data but now want
I want to pass in an array of parameters to SQLBindParameters, and have this
I written a program to retrieve data from database in servlets.now I want pass
I have a problem(again)...now i want to pass a String from one Activity to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.