First, load an extension DLL written specifically for LUA
Using the Require or Package.loadlib method, this does not explain too much.
Second, loading an extension DLL that is not written specifically for Lua
"An extension DLL that is not written for Lua" means that the DLL does not export interfaces in the form of LUA registration functions, but instead interfaces that are exported as __declspec (dllexport). Instead of using the "Package.loadlib" method call, use the lua5.1 encapsulated "alien.load ()" method. (Lua\5.1\lua\alien.lua)
Basic usage:
1, loading Alien:
Copy Code code as follows:
2, load dynamic link library: (Here to "Msvcrt.dll" as an example)
Copy Code code as follows:
libc = Alien.load ("Msvcrt.dll")
3. Description parameter type: (the first parameter represents the return type, and the following argument represents the incoming parameter type)
Copy Code code as follows:
Libc.puts:types ("Void", "string")
Alien convert Lua numbers to c numeric type, convert nil to null,strings as const char*, UserData to void* pointer. The conversion of the function return value is exactly the opposite (pointer type is converted to UserData).
The above three steps complete the loading of the DLL, and then you can invoke the functions in the DLL to implement the operation, for example:
Copy Code code as follows:
When you pass in a reference type parameter, you need to alien the space in the stack, and the LUA variable passes the value to the function parameter, such as:
Copy Code code as follows:
scanf = libc.scanf
Scanf:types ("int", "string", "ref int", "ref double")
_, x, y = scanf ("%i%lf", 1, 1)-The following two parameters have no practical meaning, just to illustrate the number of parameters
When called, enter 23 and 24.5, and these 2 parameters are the ones that really need to be passed to the function, ref int, and ref double is to tell the alien to allocate space and call the C function to get its arguments from the stack. The return result is placed on the stack after the call is completed (in order to distinguish between the returned results and other values in the stack, each C function returns the number of results), and the LUA function returns the resulting value.