[Today's programming diary, using a VC-compiled DLL instance in VB]

Source: Internet
Author: User
Tags integer numbers

2003-12-3
Today, like a book, I used VC to compile a dynamic link library (DLL) --. DLL, then copy it to the Windows/system/directory, and then create a vbprogram, and declare a in the program. the add () function in DLL implements the addition operation of two integer numbers.
The implementation process is as follows:
1. Create a DLL
Open VC and create an API-Based DLL.

---- 1. In file-> New-> projects, select "Win32 dynamic-Link Library", enter "A" in project name, and press OK.
---- 2. In file-> New-> files, select C ++ source file, enter a. cpp in file, and press OK.
---- 3. In file-> New-> files, select the text file, enter a. h In file, and press OK.
---- 4. In file-> New-> files, select the text file, enter a. Def in file, and press OK.

---- 5. source file:

Code
//---------------------------
// A. cpp
# Include <Windows>
Winapi int add (int A, int B)
{Return (A + B );
}
//---------------------------
// A. h
Winapi int add (int A, int B );
//---------------------------
// A. Def
Library "A"; specifies the DLL name
Description 'a Windows dynamic link library'
; Describes the usage of DLL (this sentence is optional)
Exports add; Name of the export Function

------ 6. compile it into a. dll
------ 7. Copy A. DLL to the Windows/system/directory

2. Create a vbprogram
In form1, enter

Code

Option explicit
Private declare function add lib "A. dll" (byval A as integer, byval B as integer) as integer

Private sub commandementclick ()
Text3.text = STR (add (Val (text1.text), Val (text2.text )))
End sub


The program interface is as follows:

Problems I encountered during programming:
After copying a. DLL to the program directory (I have not copied a. DLL to the Windows/system/directory), run the program and report an error! For example:

Later I copied a. DLL to E: disk

Code
Private declare function add lib ". DLL "(byval A as integer, byval B as integer) change as integer to private declare function add lib" E:/. DLL "(byval A as integer, byval B as integer) as integer
Run the program again!
I don't know why. The book says that the DLL files required for calling a DLL must be in one of the following three directories:

(1) Windows System directory:/Windows/system;

(2) Any directory indicated by path in DOS;

(3) Directory of the program.
I don't know why I encountered an error when I met the third condition? ========================================================== ==========

This evening, I went to the library to read a book and finally understood why the program went wrong in the afternoon.

The main reason is that when you press F5 to execute a program, the program is still in the debugging stage, so it cannot be found.

To a. dll, think of this, I make the program as an application, and finally everything is normal. I am very grateful.

Wei ~
========================================== What is the earliest http://www.thugx.com/main/index.php of this article on the assassin's light website? Showtopic = 3250 ===============================

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.