VB calls C procedures - Dynamic Link Library

zhaozj2021-02-11  191

Abstract, a method of implementing VB call C language programs - dynamic connection library, gives a general framework for dynamic link library production, and the implementation of dynamic connection libraries and its manufacturing methods, programming steps, this The method has a general meaning. Key words VB Dynamic Library DLL C Pro Program 1 Using the VB development interface friendly, easy to operate only a short time. However, the VB runs slower, for huge scientific calculations, this slow speed of VB will make people feel unbearable. The C language is an internationally popular intermediate programming language that has been widely used in its flexible, efficient advantages. Many applications are written in a DOS environment with C language. So how do you play the advantages of both languages ​​of VB and C, use VB to design a good for Windows interface to call C language programs, that is, implement VB call C language program? Existing documents are only mentioned to dynamic link libraries (hereinafter referred to as DLL) can connect both languages, but there is a modified and programming method for dynamic link library DLL. This article describes the method, steps of making dynamic link libraries and VB calls for DOS dynamic link libraries in the Borland C environment. Second, the method of writing a DLL program with Borland C To implement the VB call C program, first need to write a dynamic connection library DLL program with Borland C (assuming that the DLL program is named). The dynamic link library contains four files: C language source program (.c), module definition file (.def), prototype function file (.h), and engineering file (.prj). The specific structure of these four files is described in detail below. 1. Write the framework of the C language source program: C language source program (Example.c), which includes three functions: portal functions, output functions, and termination functions. The specific structure is as follows: / ************************************ / / * file name: Example .C * // ********************************************************** / # include / ********** Entrance function ********************* * Handle Hinstance, Word WDataseg, Word Cbheapsize, LPSTR LPSZCMDLINE) {if (CBHEAPSIZE! = 0 ) UNLOCKDATA (0); return (1);} / ********** Output function ************* / INT FAR PASCAL EXAMPLE (int Param1, ..., Char param n) {... / * C language application * /} / *********** Term function *********** / int FAR Pascal WEP (int / * systemExit * /) {return (1);} The meaning of the above components: Windows.h header file, which contains the definition of the data type, the API entry point definition, and other useful parameter information. The PASCAL specifier defines the transfer parameters of the program and the protocol of the purification stack (Note: The plug-in directive of the DLL must be a far pointer FAR). Libmain with four parameters: Hinstance, WDataseg, CBHEAPSIZE, and LPSZCMDLINE.

The first parameter hinstance is a DLL case handle, and the wdataseg parameter is a data segment (DS) register value, and the CBHEAPSIZE parameter is the size of the heap defined in the module definition file, and libmain uses this value to initialize local heap. The LPSZCMDLINE parameter includes command line information, but it is rarely used by DLL. In general, these four parameters are parameters for making DLL universal. If the DLL data is not allowed to be blocked, you must call UNLOCKDATA to restore normal non-lock status. If the DLL initialization has been successful, the DLL returns 1, and the 0 value is returned if it is not successful, and the DLL exits the system. The output function of the DLL implements the task to be completed by the user, which is the core of the DLL. It differs from the general C language program that there is no Scanf function. All external pointers are far pointer FAR. Painted line functions To use the LINETO function. The DLL includes a termination function, and the termination function is sometimes referred to as an exit function, and its name must be a WEP. And it can be included in the exports segment of the DLL module definition file. 2, the module definition file (.def) structure and the meaning module definition file of each segment is as follows: / ************************ *********** // * file name: example.def * // ********************************** ********** / lIBRARY example / * dLLname * / DESCRIPTION 'example.DLL'EXETYPE WINDOWSCODE PRELOAD MOVEABLE DISCARDABLEDATA PRELOAD MOVEABLE SINGLEHEAPSIZE 1024EXPORTSexample @ 1WEP @ 2 keyword lIBRARY ran this module as a DLL, library The name EXAMPLE followed and must be consistent with the name of the library, the file name of the DLL in DEF. The Description statement uses a string with a length of 128 characters, which usually uses it to save the information described by the module. EXTYPE Windows statements are needed for each Windows application and DLL. DATA statements Define the memory properties of the library data segment, the keyword Moveable allows the memory management program to move the memory segment if necessary, the keyword SINGLE is necessary, because the DLL always has a single data segment, regardless of accessing its application Quantity. The Heapsize statement is used to define an initial size of a DLL local heap. The DLL that performs local memory allocation must initialize the heap when the library starts, the size of the stack is transmitted to the DLL of the Lientry. The Locallnit is used to initialize the local heap of the DLL after the size of the stack. The Exeports statement definition will be used as a program from the application or from other DLL entry points. Windows uses this information to establish a sequence number of portions, the sequence number inlet value is an optimized value, allowing the dynamic connection mechanism to operate more quickly and use less Memory. In general, the structure of the module definition file (.def) is except that the names of the dynamic library are different, and other structures are fixed.

3. The function of establishing the original formal function file (.h) The function of the original form is to further declare the function name and passing parameters of the call function. The form is: / ******************** **************** // * file name: example :.h * // ********************** *************** / EXTERN "C" INT _EXPORT FAR PASCAL EXAMPLE (int Param1, ..., char param n); 4. Establishing a project file (.prj) project file contains two files with example.c, example.def, and then compile the connection to generate a dynamic link library. The above is a framework for making a dynamic connection library, and now introduces a method of making dynamic link libraries through an example. If the program completion function is required to: Open a data file to read the first two data, the two numbers are added to the two parameters transmitted, returning them. The name of the DLL program is Add. Step 1: Edit the .c file, .def file, and .h file in the Borland C environment, and establish a .prj file.

Such as (1) Write C language source program list: / ******************************************* / / * File name: add.c * // *********************************** / # Include #include #include #include #include #include #include #include #include "c: /add.h" / * Entrance function * / int FAR Pascal libmain (Handle Hinstance, Word Word, Word Cbheapsize, LPSTR LPSZCMDLINE) {IF (cbHeapsize! = 0) unlockdata (0) Return 1;} / * Output function * / int 4, int y, char * filein {INT A, B; fp = fopen (filein, "r"); if (fp == null) {EXIT (0);} int TEMP1 = 0, TEMP2 = 0; FSCANF (FP, "% D", & A); FSCANF (FP, "% D", & B); TEMP1 = X A; TEMP2 = Y B; TEMP1 = Temp1 Temp2; Fclose (fp); Return (TEMP1);} / * Exit function * / int FAR Pascal WEP (INT / * SystemExit * /) {return (1);} 2) add.def file Program list: / ************************************ / / * file name: add. H * // ********************************************** / library adddscription 'add.dll'Exetype Windowscode Preload Moveable DiscardableData Prelioad Moveable SingleHeapsize 1024ExportsAdd @ 1WEP @ 2 (3) Add.h Program List: / ********************************************* * ******** // * file name: add.h * // **************************************** ******* / EXTERN "C" INT _EXPORT FAR PASCAL ADD (INT X, INT Y, CHAR * Filein); (4) Establish an engineering file: Open the Open project file in the Project item in the Borland C environment And establish add.prj, add the add.c file and add.def file in the Add ITEM to establish an engineering file. In the second step, select Windows DLL in the Applation of the Options item of Borland C, then select the build all in Compile, which can generate the dynamic link library add.dll for VB calls. Third, the VB3.0 calls the Dynamic Link Library DLL method to make a DLL, you can call it with VB to implement the VB call C program.

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

New Post(0)