1. Load the extended dll specially written for lua
Use the require or package. loadlib method and do not explain it too much here.
2. Load the extension dll not specifically written for lua
"Not an extended dll for lua" refers to an interface that is not exported in the form of _ declspec (dllexport) but is not registered as a function in the dll. In this way, the "package. loadlib" method cannot be used, but the "alien. load ()" method encapsulated by lua5.1 must be used. (Lua/5.1/lua/alien. lua)
Basic usage:
1,Load alien:
Require ("alien ")
2,Load dynamic link library:(Here we use "msvcrt. dll" as an example)
Libc = alien. load ("msvcrt. dll ")
3,Description parameter type:(The first parameter indicates the return type, and the following parameter indicates the input parameter type)
Libc. puts: types ("void", "string ")
Alien converts Lua numbers to the numeric type of C, converts nil to NULL, strings to const char *, and userdata to the void * pointer. The conversion of function return values is the opposite (pointer type is converted to userdata ).
After loading the dll, you can call the functions in the dll to perform the operation. For example:
Libc. puts ("test ")
When a reference type parameter is passed in, alien needs to allocate space in the stack. lua variables pass the value to function parameters, for example:
Scanf = libc. scanf
Scanf: types ("int", "string", "ref int", "ref double ")
_, X, y = scanf ("% I % lf", 1, 1)-the following two parameters do not actually make sense, just to describe the number of parameters
Enter 23 and 24.5 for the call. The two parameters are the parameters that need to be passed to the function. ref int and ref double indicate that the alien instance needs to be allocated space, call the C function to obtain its parameters from the stack. After the call is completed, the returned results are placed in the stack (to distinguish the returned results from other values in the stack, the number of results returned by each C function), and then the lua function returns the result value.