Lua Call Require From C

  1. Lua Call Require From Code
  2. Lua Call From C
  3. Lua Call Require From Chrome
  4. Lua Call Require From Command
  5. Lua Call Require From Computer
  6. Lua Call Require From Command

Lua 5.3 Reference Manual The reference manual is the official definition of the Lua language. For a complete introduction to Lua programming, see the book Programming in Lua.

wiki

On Windows, binary modules are dynamically loaded into Lua as [DLLs]. Windows almost always loads DLLs via the [LoadLibrary] or [LoadLibraryEx] Win32 API functions. These functions have Unicode (LoadLibraryW/LoadLibraryEx) and ANSI (LoadLibraryA/LoadLibraryExA) variants for the file name parameter. In Lua 5.1, loadlib.c calls LoadLibraryA. In Lua 5.2-beta, loadlib.c calls LoadLibraryExA, and its third parameter is taken from the LUA_LLE_FLAGS define (which defaults to 0).

Dll Path Rules

See [Dynamic-Link Library Search Order] for the rules governing the order of search paths that Windows looks for the DLL in. These rules are complicated and tend to change between versions of Windows. (Also, Windows Embedded (CE) [1]/Mobile [2] have special rules described later below.) In particular..

First, 'If the string specifies a fully qualified path, the function searches only that path for the module.' [3]. No further rules below are applied. Also, if you use [DLL redirection] or manifests (see manifest/WinSxS links at bottom), those techniques are used to find the DLL before the other techniques below.

Otherwise, the following two rules are applied next:

  • (1) 'If a DLL with the same module name is already loaded in memory, the system checks only for redirection and a manifest before resolving to the loaded DLL, no matter which directory it is in. The system does not search for the DLL.' [4] This rule seems to only govern DLLs that are not loaded with an absolute path. If a system DLL 'foo.dll' is loaded and you later try to load your own 'foo.dll' using an absolute path, both will indeed load.
  • (2) 'If the DLL is on the list of known DLLs for the version of Windows on which the application is running, the system uses its copy of the known DLL (and the known DLL's dependent DLLs, if any). The system does not search for the DLL.' [4] In Windows 7, there are a few dozen of these known DLLs, including user32, kernel32, gdi32, msvcrt, and others. This rule seems to only govern DLLs that are not loaded with an absolute path. If a system DLL 'kernel32.dll' is loaded and you later try to load your own 'kernel32.dll' using an absolute path, both will indeed load.

If a match is still not found, a search order (standard or alternate) is done as described below. Gta san andreas apk free download for android mobile.

  • In the 'Standard Search Order for Desktop Applications' with 'SafeDllSearchMode enabled' [4], which is normally the default behavior, Windows will search in the 'The directory from which the application loaded' (i.e. where the EXE is located), then various Windows system directories, then the current directory, and finally the directories in the PATH environment variable. The DLL's in the Windows system directories have the potential to interfere, and on Windows 7 there are close to 1500 of them.
  • Concerning LoadLibraryEx, 'If lpFileName specifies a relative path, the entire relative path is appended to every token in the DLL search path. To load a module from a relative path without searching any other path, use [GetFullPathName] to get a nonrelative path and call LoadLibraryEx with the nonrelative path. If the module is being loaded as a datafile and the relative path starts with . or ., the relative path is treated as an absolute path.' [3] (Is this also true of LoadLibrary?) Since Lua modules are loaded as images not as datafiles (LOAD_LIBRARY_AS_DATAFILE), a package.cpath of '.?.dll' will not be treated by LoadLibraryEx as an absolute path (as described below), and you will need to use something like GetFullPathName if you want to form an absolute path. [9][10]
  • Note: GetFullPathName has a worrying statement 'Multithreaded applications and shared library code should not use the GetFullPathName function and should avoid using relative path names [..] possible data corruption'. [5] Can the same corruption occur when LoadLibrary/LoadLibraryEx is passed a relative path?
  • Concerning LoadLibraryEx with LOAD_WITH_ALTERED_SEARCH_PATH, 'If this value is used and lpFileName specifies an absolute path, the system uses the alternate file search strategy discussed in the Remarks section to find associated executable modules that the specified module causes to be loaded.' [3] In other words, if your binary Lua module DLL in turn loads other DLLs via LoadLibrary without absolute paths, the LOAD_WITH_ALTERED_SEARCH_PATH will begin searching relative to the directory containing your DLL (which might be different from the directory containing lua.exe).
  • Concerning LoadLibraryEx with LOAD_WITH_ALTERED_SEARCH_PATH, 'If this value is used and lpFileName specifies a relative path, the behavior is undefined.' and 'LOAD_WITH_ALTERED_SEARCH_PATH does not work with relative paths.' [3] This suggests that Lua 5.2.0-beta LUA_LLE_FLAGS=LOAD_WITH_ALTERED_SEARCH_PATH will not work properly if the Lua binary module DLL is located via ./?.dll rather than via an absolute path.
  • Although it's undefined behavior, in practice LoadLibraryEx + LOAD_WITH_ALTERED_SEARCH_PATH with a 'relative' path of '.winmm.dll' seems to load preferentially from the current directory rather than from the system directory in Windows 7.

Windows Embedded (CE) [1] / Mobile [2] have these special behaviors:

  • '[..] all path information is ignored when determining if the DLL is already loaded. This means that if a DLL with the same name is currently loaded by any process on the system the search order is ignored, even if the path is absolute. [..] Two different modules cannot have the same file name, even if the extensions are different. These effectively have the same module name. For example, if LoadLibrary is made on Sample.cpl, the OS does not load Sample.cpl, but instead loads Sample.dll again. A similar limitation exists for modules with the same name but residing in different directories. For example, if LoadLibrary is called on WindowsSample.dll, and then LoadLibrary is called on MyDirSample.dll, WindowsSample.dll will simply be reloaded.'
  • 'Unless the full path to the module is specified, Windows Embedded Compact searches the following path for the module: The absolute path specified by the lpLibFileName parameter; The .exe launch directory; The Windows directory; ROM DLL files; An OEM-specified search path.'
  • LoadLibrary/LoadLibraryEx does not support manifests, LOAD_WITH_ALTERED_SEARCH_PATH, and other such more advanced features.

Placement of DLLs

A typical use case involves an application process (e.g. lua.exe) that loads binary Lua modules (e.g. foo.dll). lua.exe and foo.dll may in turn also depend directly or indirectly on other (non-Lua) library/system DLLs (e.g. bar.dll). The simplest file organization is often to put all these files in the same directory, which 'largely' works under the above search rules. (There is, however, possible confusion if the binary Lua module DLL and library/system DLL names happen to collide, as is discussed later below.) Many people (e.g. LuaForWindows?) feel that the DLLs clutter up the directory containing the EXE, and they prefer to place the DLLs in a separate subdirectory that they don't typically need to look at. You can place both types of DLLs in the same subdirectory or you can further split these into two subdirectories. (Linux distributions of many scripting languages do the latter to avoid the scripting language libraries littering the system libraries.) In any case, putting these files in different directories can complicate the situation because you then need to have a mechanism for the files to find each other. Some solutions include

  • Use .bat file wrappers that set PATH, LUA_CPATH, and LUA_PATH environment variables. This is ugly, and the .bat files don't entirely behave identically to EXEs either (.exe wrappers are cleaner but more work). LuaDist abandoned this.
  • Use some combination of Win32 functions like [SetDllDirectory], LoadLibraryEx options like LOAD_WITH_ALTERED_SEARCH_PATH, [GetModuleFileName], and [GetFullPathName] to force absolute or more specific paths.

Caveats

Conflicts with system DLL names using relative paths: As an example, if you want to name your Lua module 'winmm', which you compile to a DLL named winmm.dll, this will happen to have the same name as winmm.dll in the Windows system directory. If your place your winmm.dll in the current directory (which we'll assume differs from the EXE's directory), then the following:

will both fail under 'Standard Search Order for Desktop Applications' with 'SafeDllSearchMode enabled'. If the path provided to LoadLibraryEx is relative, as both are considered here, Windows first looks in the EXE directory (where lua.exe is located) and then in the Windows system directories before looking in the current directory. (Moreover, if your application calls [SetDllDirectory], then it will never look in the current directory.)

Note: [DLL Preloading Attacks] recommends, for security reasons, that 'If you never have a need to load a DLL from the current directory, just call SetDllDirectory with an argument of '.'

If, however, you place your winmm.dll in the same directory as lua.exe, then the above two statements usually will both work. However, if your process (or even a DLL indirectly loaded into your process by the operating system, outside of your control) tries to load the system 'winmm.dll' (which it typically will try to load without an absolute path), it will instead receive your winmm.dll, which won't work and could crash. (Maybe we can avoid that problem with manifests?)

We could alternately pass an absolute path:

Now, Windows will only try to load the module from the specified location, which it will begin to do. However, if your process later (rather than before) tries to also load the system 'winmm.dll' (typically without an absolute path), it will fail because it sees a DLL with the same name already loaded and uses that, which is incorrect.

Lua

That above problems largely do not seem to affect the case if your module were named 'foo.winmm', located in .foowinmm.dll, and loaded with a relative path. When .foowinmm.dll is passed to LoadLibraryEx, Windows appends .foowinmm.dll to all its search paths, and of course none of these likely exists. However, we still have problems if your process later tries to load the system 'winmm.dll' without a path.

These problems could be compounded if scripting languages other than Lua also try to use a similar module naming scheme.

The above problems could be avoided by ensuring Lua module DLL names never conflict with system DLL names. You might do this by giving Lua module DLL names a globally unique suffix and/or extension--e.g. rather than '.dll' maybe '.luad' (analogous to Python .pyd and following similar conventions to .luac), '.luadll', '.lua.dll' (might not be a good idea since 'Many things break when you have an extra dot in DLL names' -- MikePall in [11]), or '-lua.dll'. LuaList:2011-09/msg00398.html . You will need to adjust package.cpath appropriately to handle this. On Linux, system shared libraries are prefixed by lib[12].

The above solutions, however, are insufficient on Windows CE/Mobile/Embedded, which have the additional restriction that 'all path [and extension] information is ignored when determining if the DLL is already loaded'. This has caused LuaSocket mime/core.dll and socket/core.dll to conflict [6]. It would also prevent the unique extension solution above, with the exception of things like -lua.dll (where the unique extension is actually part of the file name). One overall solution here, at least for embedded versions of Windows, is to have DLL names like socket_core-lua.dll, which will never conflict with mime_core-lua.dll nor any possible core.dll. A change may be made to findfile() in loadlib.c so that dots in the module name are replaced with '_' rather than '/' [6]. Lua 5.2 provides a LUA_CSUBSEP variable for this.

Other Comments

  • 'For those who do use non-path filenames in their LUA_CPATH e.g. 'abc.dll' there is a significant performance overhead (>2 orders of magnitude) in allowing windows to scan the serach path.' (David Burgess in LuaList:2006-12/msg00049.html )
  • 'even a seemingly innocent file open dialog may change the current directory ..' (LuaList:2006-12/msg00064.html)

Comparisons

Lua Call Require From Code

  • In Cygwin, all Cygwin DLL's (as opposed to native ones) are given the prefix 'cyg' (e.g. cyglua-5.1.dll), similar to the 'lib' prefix on Linux shared libraries. [13][7]
  • In Windows Python (2.7 and 3.2), binary Python module DLL's are given the extension .pyd rather than .dll See [Is a *.pyd file the same as a DLL?]. 'The output file should be called spam.pyd (in Release mode) or spam_d.pyd (in Debug mode). The extension .pyd was chosen to avoid confusion with a system library spam.dll to which your module could be a Python interface.' [8]. Python module DLLs (e.g. _sqlite3.pyd) and Python C library DLLs (e.g. sqlite3.dll) are both stored in a 'DLLs' directory (which only has about two dozen files), Python source libraries (e.g. sqlite3dump.py) are stored in a 'Lib' directory, and python.exe (and pythonw.exe) are in the parent directory. However, Cygwin Python 2.6 doesn't quite follow that (e.g. _sqlite3.dll and cygsqlite3-0.dll). Python [dynload_win.c] uses LoadLibraryEx with LOAD_WITH_ALTERED_SEARCH_PATH, and the path is forced to be absolute with GetFullPathName.
  • Windows Strawberry Perl uses names like TIFF.dll (binary Perl module) and libtiff-3_.dll (C library), and these are stored in separate directories. Perl [win32.c] uses LoadLibraryEx with LOAD_WITH_ALTERED_SEARCH_PATH, and the path appears forced to be absolute (PerlDir_mapA).
  • Windows Ruby uses binary module DLL names like zlib.so and system DLL names like zlib1.dll. The latter are stored in their own directories, whereas the former as stored in the same directory as the ruby.exe and rubyw.exe. Ruby dl.h/dln.c seems to just use LoadLibrary (not LoadLibraryEx).

The common theme is that there is an effort to keep distinct DLL naming conventions for scripting language binary C modules and system libraries.

Possible Conclusions

  • Lua binary modules should have a unique DLL naming convention, like 'foo.luad' or 'foo-lua.dll' rather than 'foo.dll', to avoid conflict with non-Lua DLLs. LUA_CPATH can be updated accordingly.
  • Something maybe should be done about Lua 5.2.0-beta LoadLibraryEx triggering undefined behavior with LUA_LLE_FLAGS=LOAD_WITH_ALTERED_SEARCH_PATH and relative paths like package.cpath='.?.dll'. A recommended approach is to make paths absolute. This may be done via GetFullPathName, though it possibly leads to corruption in multithreaded and shared library situations (why shared libraries???). [5] Alternately, '.?.dll' could could be removed from package.cpath, but the user might still try to add it.
  • Embedded versions of Windows have unique requirements, and loadlib.cfindfile() may need a small patch.

(Footnote: Perhaps LUA_PATH should match .luac files too. On the other hand, renaming .luac files to .lua works without complicating the path.)

DLL Test code

The following test DLL in conjunction with package.loadlib may be useful in understanding this behavior.

Lua Call From C

Links

  • LuaList:2010-01/msg00572.html - 'LoadLibrary and 5.2' - Scuri suggest LOAD_WITH_ALTERED_SEARCH_PATH
    • LuaList:2010-01/msg00836.html - addition of LUA_LLE_FLAGS in 5.2work2
    • LuaList:2010-01/msg00757.html - Burgess suggesting luaconf.h option for LOAD_WITH_ALTERED_SEARCH_PATH
  • LuaList:2008-05/msg00397.html - 'Module loading on Windows' - suggest LOAD_WITH_ALTERED_SEARCH_PATH
  • LuaList:2006-12/msg00047.html - 'Suggestion for LoadLibrary in windows' - suggest LOAD_WITH_ALTERED_SEARCH_PATH
    • LuaList:2006-12/msg00064.html - Pall notes
  • LuaList:2007-03/msg00778.html - Pall on *A v.s. *W calls
  • LuaList:2005-09/msg00135.html - Burgess suggesting LUA_CDIR'?.dll;' prior to '?.dll;'
  • Manifests/WinSxS links: [WinSxS blog post 1][WinSxS blog post 2][WinSxS blog post 3][MSDN: Manifests][MSDN: Assembly search sequence]
  • [LoadLibrary in Windows] / [LoadLibraryEx in Windows]
  • [LoadLibrary] and [LoadLibraryEx] in Windows Mobile 6.5]
  • [LoadLibrary] and [LoadLibraryEx] in Windows Embedded Compact 7]

Related Topics

RecentChanges · preferences
edit · history
Last edited December 15, 2011 1:40 am GMT (diff)

The reference manual is the official definition of the Lua language.
For a complete introduction to Lua programming, see the bookProgramming in Lua.

Copyright © 2015–2020 Lua.org, PUC-Rio.Freely available under the terms of theLua license.

Lua Call Require From Chrome

Contents

Lua Call Require From Command

  • 1 – Introduction
  • 2 – Basic Concepts
    • 2.5 – Garbage Collection
  • 3 – The Language
    • 3.3 – Statements
    • 3.4 – Expressions
  • 4 – The Application Program Interface
  • 5 – The Auxiliary Library
  • 6 – Standard Libraries
    • 6.4 – String Manipulation
  • 7 – Lua Standalone
  • 8 – Incompatibilities with the Previous Version

Index

Lua Call Require From Computer

Lua functions

basic
_G
_VERSION
assert
collectgarbage
dofile
error
getmetatable
ipairs
load
loadfile
next
pairs
pcall
print
rawequal
rawget
rawlen
rawset
require
select
setmetatable
tonumber
tostring
type
xpcall

coroutine
coroutine.create
coroutine.isyieldable
coroutine.resume
coroutine.running
coroutine.status
coroutine.wrap
coroutine.yield

debug
debug.debug
debug.gethook
debug.getinfo
debug.getlocal
debug.getmetatable
debug.getregistry
debug.getupvalue
debug.getuservalue
debug.sethook
debug.setlocal
debug.setmetatable
debug.setupvalue
debug.setuservalue
debug.traceback
debug.upvalueid
debug.upvaluejoin

io
io.close
io.flush
io.input
io.lines
io.open
io.output
io.popen
io.read
io.stderr
io.stdin
io.stdout
io.tmpfile
io.type
io.write
file:close
file:flush
file:lines
file:read
file:seek
file:setvbuf
file:write

math
math.abs
math.acos
math.asin
math.atan
math.ceil
math.cos
math.deg
math.exp
math.floor
math.fmod
math.huge
math.log
math.max
math.maxinteger
math.min
math.mininteger
math.modf
math.pi
math.rad
math.random
math.randomseed
math.sin
math.sqrt
math.tan
math.tointeger
math.type
math.ult

os
os.clock
os.date
os.difftime
os.execute
os.exit
os.getenv
os.remove
os.rename
os.setlocale
os.time
os.tmpname

package
package.config
package.cpath
package.loaded
package.loadlib
package.path
package.preload
package.searchers
package.searchpath

string
string.byte
string.char
string.dump
string.find
string.format
string.gmatch
string.gsub
string.len
string.lower
string.match
string.pack
string.packsize
string.rep
string.reverse
string.sub
string.unpack
string.upper

table
table.concat
table.insert
table.move
table.pack
table.remove
table.sort
table.unpack

utf8
utf8.char
utf8.charpattern
utf8.codepoint
utf8.codes
utf8.len
utf8.offset

metamethods

__add
__band
__bnot
__bor
__bxor
__call
__concat
__div
__eq
__gc
__idiv
__index
__le
__len
__lt
__metatable
__mod
__mode
__mul
__name
__newindex
__pairs
__pow
__shl
__shr
__sub
__tostring
__unm

environment
variables

LUA_CPATH
LUA_CPATH_5_3
LUA_INIT
LUA_INIT_5_3
LUA_PATH
LUA_PATH_5_3

C API

lua_Alloc
lua_CFunction
lua_Debug
lua_Hook
lua_Integer
lua_KContext
lua_KFunction
lua_Number
lua_Reader
lua_State
lua_Unsigned
lua_Writer

lua_absindex
lua_arith
lua_atpanic
lua_call
lua_callk
lua_checkstack
lua_close
lua_compare
lua_concat
lua_copy
lua_createtable
lua_dump
lua_error
lua_gc
lua_getallocf
lua_getextraspace
lua_getfield
lua_getglobal
lua_gethook
lua_gethookcount
lua_gethookmask
lua_geti
lua_getinfo
lua_getlocal
lua_getmetatable
lua_getstack
lua_gettable
lua_gettop
lua_getupvalue
lua_getuservalue
lua_insert
lua_isboolean
lua_iscfunction
lua_isfunction
lua_isinteger
lua_islightuserdata
lua_isnil
lua_isnone
lua_isnoneornil
lua_isnumber
lua_isstring
lua_istable
lua_isthread
lua_isuserdata
lua_isyieldable
lua_len
lua_load
lua_newstate
lua_newtable
lua_newthread
lua_newuserdata
lua_next
lua_numbertointeger
lua_pcall
lua_pcallk
lua_pop
lua_pushboolean
lua_pushcclosure
lua_pushcfunction
lua_pushfstring
lua_pushglobaltable
lua_pushinteger
lua_pushlightuserdata
lua_pushliteral
lua_pushlstring
lua_pushnil
lua_pushnumber
lua_pushstring
lua_pushthread
lua_pushvalue
lua_pushvfstring
lua_rawequal
lua_rawget
lua_rawgeti
lua_rawgetp
lua_rawlen
lua_rawset
lua_rawseti
lua_rawsetp
lua_register
lua_remove
lua_replace
lua_resume
lua_rotate
lua_setallocf
lua_setfield
lua_setglobal
lua_sethook
lua_seti
lua_setlocal
lua_setmetatable
lua_settable
lua_settop
lua_setupvalue
lua_setuservalue
lua_status
lua_stringtonumber
lua_toboolean
lua_tocfunction
lua_tointeger
lua_tointegerx
lua_tolstring
lua_tonumber
lua_tonumberx
lua_topointer
lua_tostring
lua_tothread
lua_touserdata
lua_type
lua_typename
lua_upvalueid
lua_upvalueindex
lua_upvaluejoin
lua_version
lua_xmove
lua_yield
lua_yieldk

auxiliary library

luaL_Buffer
luaL_Reg
luaL_Stream

luaL_addchar
luaL_addlstring
luaL_addsize
luaL_addstring
luaL_addvalue
luaL_argcheck
luaL_argerror
luaL_buffinit
luaL_buffinitsize
luaL_callmeta
luaL_checkany
luaL_checkinteger
luaL_checklstring
luaL_checknumber
luaL_checkoption
luaL_checkstack
luaL_checkstring
luaL_checktype
luaL_checkudata
luaL_checkversion
luaL_dofile
luaL_dostring
luaL_error
luaL_execresult
luaL_fileresult
luaL_getmetafield
luaL_getmetatable
luaL_getsubtable
luaL_gsub
luaL_len
luaL_loadbuffer
luaL_loadbufferx
luaL_loadfile
luaL_loadfilex
luaL_loadstring
luaL_newlib
luaL_newlibtable
luaL_newmetatable
luaL_newstate
luaL_openlibs
luaL_opt
luaL_optinteger
luaL_optlstring
luaL_optnumber
luaL_optstring
luaL_prepbuffer
luaL_prepbuffsize
luaL_pushresult
luaL_pushresultsize
luaL_ref
luaL_requiref
luaL_setfuncs
luaL_setmetatable
luaL_testudata
luaL_tolstring
luaL_traceback
luaL_typename
luaL_unref
luaL_where

standard library

luaopen_base
luaopen_coroutine
luaopen_debug
luaopen_io
luaopen_math
luaopen_os
luaopen_package
luaopen_string
luaopen_table
luaopen_utf8

constants

LUA_ERRERR
LUA_ERRFILE
LUA_ERRGCMM
LUA_ERRMEM
LUA_ERRRUN
LUA_ERRSYNTAX
LUA_HOOKCALL
LUA_HOOKCOUNT
LUA_HOOKLINE
LUA_HOOKRET
LUA_HOOKTAILCALL
LUA_MASKCALL
LUA_MASKCOUNT
LUA_MASKLINE
LUA_MASKRET
LUA_MAXINTEGER
LUA_MININTEGER
LUA_MINSTACK
LUA_MULTRET
LUA_NOREF
LUA_OK
LUA_OPADD
LUA_OPBAND
LUA_OPBNOT
LUA_OPBOR
LUA_OPBXOR
LUA_OPDIV
LUA_OPEQ
LUA_OPIDIV
LUA_OPLE
LUA_OPLT
LUA_OPMOD
LUA_OPMUL
LUA_OPPOW
LUA_OPSHL
LUA_OPSHR
LUA_OPSUB
LUA_OPUNM
LUA_REFNIL
LUA_REGISTRYINDEX
LUA_RIDX_GLOBALS
LUA_RIDX_MAINTHREAD
LUA_TBOOLEAN
LUA_TFUNCTION
LUA_TLIGHTUSERDATA
LUA_TNIL
LUA_TNONE
LUA_TNUMBER
LUA_TSTRING
LUA_TTABLE
LUA_TTHREAD
LUA_TUSERDATA
LUA_USE_APICHECK
LUA_YIELD
LUAL_BUFFERSIZE

Lua Call Require From Command

Last update:Tue Aug 25 13:45:14 UTC 2020