VB Method For Calling C program-Dynamic Link Library Method

Source: Internet
Author: User
Tags types of functions

Abstract
This article introduces a method for VB to call C language programs-the dynamic connection library method, and provides the dynamic link library system.
The general framework describes the implementation of the dynamic Connection Library, its manufacturing method, and programming steps through examples.
General significance.
Key words: VB dynamic Connection Library dll c program

I. Introduction

Visual Basic (VB) is a visual object-oriented programming language.
And other features, favored by the majority of Windows programmers. Using VB to develop user-friendly and easy-to-use software
Very short. However, VB runs slowly, which makes it difficult for a large number of scientific computing problems.
To endure. C language is a very popular intermediate programming language in the world. It is widely used for its flexibility and efficiency.
Extensive applications. Many applications are written in the DOS environment in C language. So how can we use both VB and C?
Advantages of different languages. We use VB to design a good for Windows interface to call C language programs.
What about language programs? The existing literature only mentions that the dynamic link library (DLL) can connect these two languages,
However, we will talk about how to create dynamic link library DLL and its programming methods. This topic describes how to create a dynamic link in Borland C environment.
The methods and steps for VB to call the dynamic link library of for DOS, and describes the methods and steps for VB to call the entire C program through an instance
.

2. Use Borland C to compile DLL programs

To implement VB to call C Programs, you must first use Borland C to write a dynamic Connection Library DLL Program (assuming that the DLL Program
Named example ). The dynamic link library contains four files: C language source code (. c) and module definition file.
(. Def), the prototype function file (. h), and the project file (. prj ). The following describes the four types of files in detail.
.
1. Framework for compiling C-language source code:
C language source program (example. C), which includes three types of functions: entry function, output function, and termination function. Its details
The structure is as follows:
/*************************************/
/* File name: example. C */
/*************************************/
# Include <windows. h>
/*********** Entry function ***********/
Int far Pascal libmain (handle hinstance,
Word wdataseg,
Word cbheapsize,
Lpstr lpsz1_line)
{
If (cbheapsize! = 0)
Unlockdata (0 );
Return (1 );
}

/************ Output function ***********/

Int far Pascal example (INT param1,..., char Param N)
{
.../* C language application */
}
***********/
Int far Pascal WEP (INT/* systemexit */)
{
Return (1 );
}
Description of the preceding parameters: Windows. h header file, which includes data type definition and API entry
Point definition and other useful parameter information. Pascal specifier defines the Transfer Parameters of the program and the protocol for purifying the stack
(Note: The pointer transmitted externally by dll must be a far pointer far ). Libmain has four parameters:
Hinstance, wdataseg, cbheapsize, and lpsz1_line. The first hinstance parameter is a DLL.
Handle. The wdataseg parameter is the DS register value, and the cbheapsize parameter is defined in the module definition file.
The size of the local heap. libmain uses this value to initialize the local heap. The lpszcmdline parameter includes command line information,
It is rarely used by DLL. In general, these four parameters are common DLL parameters.
If you do not want to block the DLL data, you must call unlockdata to restore the normal non-lock status.
If the conversion is successful, 1 is returned for the DLL. If the conversion is unsuccessful, 0 is returned, and the DLL exits the system.
DLL output functions implement the tasks to be completed by the user, which is the core of the DLL. It is different from general C language programs.
The difference is that there is no scanf function. All external pointers are far pointers far. Lineto is used to draw a linear function.
DLL includes an ending function, which is sometimes called an exit function. Its name must be WEP. And it can be
Included in the exports section of the DLL module definition file.
2. Structure of the module definition file (. Def) and its meaning
The structure of the module definition file is as follows:
/*************************************/
/* File name: example. Def */
/*************************************/
Library example/* dllname */
Description 'example. dll'
Exetype windows
Code preload moveable discardable
Data preload moveable single
Heapsize 1024
Exports
Example @ 1
WEP @ 2
The keyword library is to regard this module as a DLL. The Library name example follows and must be
. The description statement uses a string with the maximum length.
It is a string of 128 characters and is usually used to save the information described by the module. Exetype windows statement
Sequence and DLL are required. The data statement defines the Memory attribute of the Data Segment of the database. The keyword moveable allows the memory Tube
When necessary, the program moves the memory segment. The keyword single is required for DLL, because DLL always has a single data segment,
Regardless of the number of applications that access it. The heapsize statement is used to define the initial size of a local dll Heap,
The DLL for executing local memory allocation must initialize the heap when the library starts. The heap size is transferred to the lientry of the DLL.
Program. Then, call locallnit with the heap size to initialize the local heap of the DLL.
Exeports statement definitions will be used as programs from applications or from other DLL entry points.
This information creates an ordinal entry value. The ordinal entry value is an optimized value, allowing dynamic connection to perform block operations faster.
Use less memory.
In general, the structure of the module definition file (. Def) is fixed except for the name of the dynamic library.
.
3. Create the original function file (. h)
The function of the original function is to further declare the name of the function called and the passed parameter, in the form:
/*************************************/
/* File name: example. H */
/*************************************/
Extern "C" int _ export far Pascal example (INT param1,..., char Param N );
4. Create a project file (. prj)
The project file contains two files: example. C and example. Def. Then, compile the connection to generate a dynamic link.
Library.
The above is the framework for creating a dynamic connection library. Now we use an instance to introduce how to create a dynamic Connection Library. To
The program completion function is to open a data file and read the first two data files.
Returns the sum of them. Set the DLL program name to add.
Step 1: edit the. c file,. Def file, and. h file in the Borland C ++ environment, and create a. prj file. For example
(1) Compile the C language source program list:
/*************************************/
/* File name: Add. C */
/*************************************/
# Include <iostream. h>
# Include <conio. h>
# Include <Io. h>
# Include <alloc. h>
# Include <stdlib. h>
# Include <windows. h>
# Include <math. h>
# Include <stdio. h>
# Include "C:/Add. H"
/* Entry function */
Int far Pascal libmain (handle hinstance,
Word wdataseg,
Word cbheapsize,
Lpstr lpsz1_line)
{
If (cbheapsize! = 0)
Unlockdata (0 );
Return 1;
}
/* Output function */
Int far Pascal add (int x, int y, char * filein)
{Int A, B;
Fp = fopen (filein, "R ");
If (FP = NULL) {exit (0 );}
Int temp1 = 0, temp2 = 0;
Fscanf (FP, "% d", & );
Fscanf (FP, "% d", & B );
Temp1 = x +;
Temp2 = Y + B;
Temp1 = temp1 + temp2;
Fclose (FP );
Return (temp1 );
}
/* Exit the function */
Int far Pascal WEP (INT/* systemexit */)
{
Return (1 );
}
2) Add. Def file program list:
/*************************************/
/* File name: Add. H */
/*************************************/
Library add
Description 'add. dll'
Exetype windows
Code preload moveable discardable
Data preload moveable single
Heapsize 1024
Exports
Add @ 1
WEP @ 2
(3) Add. H program list:
/*************************************/
/* File name: Add. H */
/*************************************/
Extern "C" int _ export far Pascal add (int x, int y, char * filein );
(4) create a project file:
Open the open project file item in the project item in the Borland C ++ environment and create Add. prj.
Add the Add. c file and Add. Def file to item to create a project file.
Step 2: Select windows DLL in applation of options in Borland C, and then select
Build all to generate the dynamic link library Add. DLL for VB to call.

Iii. vb3.0 call the dynamic link library DLL Method

After creating the DLL, you can use VB to call it and implement VB to call the C program. The VB program uses the function in DLL.
First, you must have a special declaration. Use declare to declare the code of a statement at the form level, module level, or global module level.
The Declaration section declares the function in the dynamic link library to VB for the VB program to call.
Statement format: declare sub process name lib has been created _ n [alias "alias] ([byval parameter as type]),
Or the declare function name lib has been used as _ has alias "alias] ([byval parameter as type])
In the declaration, the type first uses the declare keyword to declare the function in the DLL. In C, some function types are
Void, which indicates that no return value exists, it must be declared as a process using the keyword sub. Some functions have returned
Value, it must be declared as a function with the keyword function, and it must be specified with the as keyword at the end of the declaration statement.
Type of the function return value.
For example, the above add. dll can be declared in VB:
Declare function add lib using C:/Add. DLL using-byval X as integer, byval y as integer, byval filein as string) as integer
You can use this statement to declare the Add function to VB.

Iv. Conclusion

The above details how to create a dynamic link library in Borland C environment and how VB calls the for DOS dynamic link library.
The following describes how to call the C language program using VB. The example program add is compiled by vb3.0 and is in the 486
Debugging on the microcomputer passed. The method described in this article has a wide range of meanings and is fully applicable to C ++ language programs.

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.