Let the script support the dynamic link library.

xiaoxiao2021-03-06  66

Don't always be depressed, to access the database in the script, you have to recompile the scripting engine, write a bunch of interfaces to let the script can access the database, or touch the network to access the network in the script, Compile a script engine to write a bunch of interfaces. Every time you increase a new thing, you need to re-modify the script engine. Is it very troublesome?

In fact, there is a simple way to let your scripting engine support the dynamic link library.

First, we know that there are two ways to access the dynamic link library, one is specified when the connection is connected, and the other is specified in the run. In order to allow the script engine to access any dynamic link library, we can use the methods specified in the run. Linux is fine, Windows can only get a function pointer through the function name, but for the script, but not know the definition of the function, it is impossible to call it.

Then we use the following method (as an example under Linux, the Windows platform is similar, only the API is different):

For example, there is a script engine to receive the script.

Dynamic Link Library Name: xxx.so

To call the function: Test_func

Parameters to be passed in: Var1, Var2, Var3

Void * DL_Handle = DLOPEN ("xxx.so", RTLD_LAZY); // WINDOWS under.dll file

Void * f = DLSYM (DL_Handle, "Test_func");

__ASM __ ("PUSH% 0": "R" (var1));

__ASM __ ("PUSH% 0": "R" (var2));

__ASM __ ("PUSH% 0" :: "r" (var3));

// Windows is:

// __asm ​​push var1;

// __asm ​​push var2;

// __ASM Push Var3;

__ASM __ ("Call% 0" :: "r" (f));

__ASM __ ("Add %% ESP, $ 0xc"); // Because there are three parameters, it is 4 * 3 = 12 (0xc)

/ / Then if there is a return value, get the return value from Eax.

With this thing, the script can easily access any dynamic link library without making modifications and recompilation of the scripting engine.

转载请注明原文地址:https://www.9cbs.com/read-118855.html

New Post(0)