Method of loading DLL dynamic link library in Lua5.1

Source: Internet
Author: User
Tags lua require

This article mainly introduces the method of loading DLL dynamic link library in Lua5.1, this article explains the method to load the extension DLL written specifically for Lua and the method of loading extension DLLs that are not written specifically for Lua, you need friends to refer to the following

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. (Lua5.1luaalien.lua)

Basic usage:

1, loading Alien:

The code is as follows:

Require ("Alien")

2, load dynamic link library: (Here to "Msvcrt.dll" as an example)

The code is 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)

The code is 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:

The code is as follows:

Libc.puts ("Test")

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:

The code is 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 the 2 arguments you enter are the arguments that really need to be passed to the function, ref int, which tells Alien to allocate space, calls the C function to get its arguments from the stack, and then puts the results back on the stack after the call ends ( To differentiate between returning results and other values in the stack, each C function also returns the number of results), and the LUA function returns the resulting value.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.