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 1038453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:58:39+00:00 2026-05-16T14:58:39+00:00

I have a small app that uses Lua linked as dll (not static). I

  • 0

I have a small app that uses Lua linked as dll (not static). I want to load my own c++-written dll via Lua using package.loadlib (libname, funcname). For this purpose I need to export a function that follows the Lua’s lua_CFunction protocol. Clearly for that reason I have to incorporate lua.h into my project and use Lua’s functions to pass parameters and results. So my questions are:

  1. Will my DLL use Lua dll that is already loaded into small app’s process?
  2. Does package.loadlib load and unload my DLL immediately or my DLL once loaded remains until the end lua scrpit execution or application termination?
  • 1 1 Answer
  • 2 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-05-16T14:58:39+00:00Added an answer on May 16, 2026 at 2:58 pm

    Starting with your specific questions:

    1. Yes, but only if your DLL is implicitly linked to it. Be careful about this, because if you accidentally link two copies of the Lua VM into your application this can cause great confusion. For that matter, similar concerns apply to the C runtime as well. I would load the whole application under Dependency Walker to verify that it only refers to one instance of the Lua DLL and one single C runtime.

    2. My understanding is that package.loadlib() is only responsible for loading and linking to the named function in the named library. As long as the returned function object (representing the lua_CFunction you named) is alive, then the DLL is certainly loaded. If you loose the last reference to the function, then the library could be available for garbage collection, and if collected it will be unloaded. There has been talk on the Lua-L mailing list about exactly how to guarantee that a specific DLL got unloaded, if that is your concern. Otherwise, if you just assume the DLL is loaded as long as you can reach the function stored in it, you will be fine.

    Let me add that the module system built on top of this is a much better way to extend Lua with C or C++ code. Chapter 26 of Programming in Lua describes this in more detail, and the link is to that chapter in the online copy of the first edition (which describes Lua 5.0). Note that the module system did change a little in Lua 5.1, and again in Lua 5.2. Obtaining a copy of the second or third editions of PiL (available in both paper and ebook form through many booksellers) may be helpful.

    Here’s an executive summary: To create a module named foo in C, you create foo.dll that exports at minimum a function with the prototype int luaopen_foo(lua_State *L). That function should load your module (typically by using luaL_register() in Lua 5.1, or luaL_newlib() or luaL_setfuncs() in Lua 5.2 to register a table full of C functions) and return that table. On the Lua side, you put the DLL somewhere on the path described in package.cpath, and then it can be loaded by code like local foo = require "foo". There are other subtle differences between the various Lua 5.x versions, but it is relatively straightforward to create C code that can be compiled for any of them.

    Doing things this way, you have the advantage that the module can be loaded from a path, can be written in either C or Lua or a mix of both, and plays well with other modules. You also get to load as many or as few C functions as you need with a single invocation of require.

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

Sidebar

Related Questions

I have written a small chat app that uses mysql + php to facilitate
I have a small Windows app that makes uses of these DLLs: kernel32.dll user32.dll
I have a small Rails 3.2.1 app that uses CarrierWave 0.5.8 for file uploads
I have what I would consider a small sized iPhone app that uses SQLite.
I have written a small app that puts my bluetooth in discoverable mode for
I used to have a small chat app(which was almost working), that uses PHP,
I have a small app that a client uses to manually update some data.
I am writing a small app that uses the GAE. I have parts of
Using PHP.. I have a small app that I built that currently uses a
I have a small Java desktop app that uses Swing. There is a data

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.