You are currently viewing 如何使用TSMaster调用外部DLL/LIB程序

How to use TSMaster with external DLL/LIB program?


In the process of user-defined development, it is often necessary to call external DLL/LIB program files. These files may be written by the user or provided by other suppliers. TSMaster supports calling external binary library, but it must be encapsulated by a certain method. This section takes calling NI's TDMS file recording program as an example to demonstrate the method of encapsulating the DLL library. The use of the LIB library is similar to that of the DLL, and users can perform similar operations in the visual studio project.

1. Get an external library

The TDMS library of National Instruments can be downloaded through this link. For the use of external libraries, please pay attention to the following restrictions:

1. In the process of downloading the external program library, please pay attention to the use agreement of the released program library. Shanghai TOSUN Technology Ltd only provides the use environment of the external program library, and is not responsible for violations of the use agreement of the external library

2. After the external library is loaded by TSMaster, it becomes a part of the main program. If the external library crashes/memory overflows, etc., it will cause the TSMaster program to be unstable or crash. At this time, you need to reopen the software and uninstall the problematic one. external library;

3. TSMaster only supports the 32bit-msvc version of the external library, please use the appropriate version of the DLL/LIB file, otherwise it will cause errors in the compilation process.

(In the "tdms_example\TDM C DLL" directory of the attachment, you can find the decompressed TDMS external library related files.)

2. Prepare the external library template

This template can be copied from the TSMaster program, through the project named VC++ under the help->api routine->Mini Program SDK directory, copy the project to other user-defined locations for backup.

In the attachment "tdms_example\tdms_sdk", you can see the project prepared for the TDMS function.

3. Edit the template and generate the DL

No matter the external library is DLL or LIB, or both, it can be called in the template project. It should be noted that in order to enable TSMaster to recognize normally, users need to provide function comments, parameter descriptions and other information in addition to implementing their own logic in the process of preparing template projects. The specific method can be to open the default template file and search for the fun1 keyword globally. This function is an example function. The place where it appears is the place where the user needs to add custom content.

As shown in the figure above, it can be seen that in the MPLibCode.cpp file, the declaration and implementation of the fun1 function are realized. In the MPLibCodeExtern.cpp file, the existence of the function fun1 is registered with the dll management template. In the TSMasterBaseSource.cpp file, the function fun1 is registered to the The dll management template registers the relevant parameter information of the fun1 function.

In the process of integrating TDMS functions, firstly, copy the h file and lib file required by the compilation process to the project directory, and use the LIB file as input in the project linker. For external functions, such as the DDC_CreateFile function that comes with the TDMS library, use a new function to encapsulate in the template and name it tdms_CreateFile. Although the functions of the LIB file can be directly exported, it is usually recommended to create a new function to encapsulate it. First, the name of the function can be unified, which is convenient for users to distinguish, but the return value of all API functions must be int type. If the native external library is not For this return type, the return value must be obtained by passing a pointer, etc. In this case, it must be used in the form of encapsulation.

Please refer to the sample project for other TDMS function encapsulation process, please pay attention to the matching of encapsulation function code and function registration code to work correctly. Based on this template, the required "tdms_sdk.dll" can be generated in Debug/Release-x86 mode.

4. Call the template DLL in TSMaster

In order to call "tdms_sdk.dll" in TSMaster, you can directly drag the dll in, or load it through Simulation -> Mini Program Library -> Load, as shown in the figure below, the direct loading will fail because the template dll It depends on the TDMS runtime DLL file. The dependent file is in the "tdms_example\TDM C DLL\dev\bin\32-bit" directory, provided by NI. Copy all the files to the TSMaster project\Plugins\Dependencies directory (this directory It needs to be created manually, TSMaster will not create it by default), and then load the template dll, it can be loaded correctly.

Open the example project "tdms_example\tdms_example", and generate test code for the TDMS file in the first use case of the test system. The user can also create a new applet to call. There is no difference in operation between the two. You need to check the required external library in the property window first, and then call the required function in the script program. As shown in the figure below, after the script runs, a sample TDMS file will be created in the root directory of the D disk.

Run the script directly, and you can find that the program can run, but the TDMS file is not created as required. Check the error message through the running log file, and you can see that the storage device cannot be opened. This problem is specific to the TDMS library, because its dependent files include a folder named DataModels in addition to the dll. When TSMaster uses the dependent files in the "tdms_example\Plugins\Dependencies" directory, it will not folder, so you need to manually copy the folder to the TSMaster installer directory, for example "C:\Program Files(x86)\TOSUN\TSMaster\bin". Some external libraries only have dll dependencies, so manual copying is not required.

Once the dependent folders are resolved, the TDMS files are generated correctly. Users can refer to the above process to implement their own logic. The whole process requires a certain understanding of the visual studio environment. If you are unclear, please refer to the sample project.

5. Debug template DLL in TSMaster

n the process of using the template dll, there is inevitably a debugging process. Users can modify the project properties->General->Output directory in the template visual stuido, and output the dll directory to the "MPLibrary" directory in the project used for debugging. For example, generate the dll in the "tdms_example" example to "tdms_example\tdms_example\MPLibrary", then run the TSMaster project first, and then start the visual studio debugging function. The debugging process is consistent with the debugging method of the applet, and you can refer to the debugging process of the applet.