VB writing dll and specific examples of calling

Table of contents

1. Introduction to dll

2. dll in VB6

1. Common calling methods:

2. Write dll through vb6:

3.dll call

3. Some problems that arise:


1. Introduction to dll

1. Dll is called a dynamic link library. It is a code and library that can be called by multiple programs or other DLLs. Using DLL can make the program more modular and avoid duplicate codes.

2. The dll of vb6 can include procedures and functions. Functions have return values, but procedures do not.

3. When the program calls a dll, dependencies will be generated. After the dll is deleted, the code that calls the dll function will report an error .


2. dll in VB6

1. Common calling methods:

(1).Static call:

Method:Private/public Declare Sub method name Lib "dll name" (method parameter)

Function: Private/public Declare function function name Lib "dll name" (method parameter)

To give an example:

方法:Private Declare Sub Sleep Lib "kernel32.DLL" (ByVal dwMilliseconds As Long)

函数:Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Long) 

 (2). Call through the reference window:

Click Project - Reference to open the reference window, find the dll you want to call, and click OK. This method can call all functions and procedures in the dll. The calling method is shown in the figure below

 mathv3.dll is a written dll.

2. Write dll through vb6:

(1). Create a new ActiveX DLL in the vb6 new window.

(2). Set the Instance attribute in the class to 6. This attribute determines whether the class can be made public.

(3). Write code and define procedures and functions as shown in the code:

Private Declare Sub Sleep Lib "kernel32.DLL" (ByVal dwMilliseconds As Long) '调用kernel32.dll的sleep的方法

Function Flashbacks(number As Long) As Long '返回倒序数
    For i = Len(number) To 1 Step -1
        Flashbacks = Flashbacks & Mid(number, i, 1)
    Next
End Function

Sub yc(sleeptime As Long) '调用系统api的延迟
    Sleep sleeptime
End Sub

In the dll project, click File---Generate dll to generate a new dll.

3.dll call

(1).Create a new project

(2). Call the dll through 1. Common calling methods: Note : the original project name cannot be consistent with the project

(3).Write code:

Private Sub Form_Click()
    yc 1000
    Debug.Print Flashbacks(321)
End Sub

Click Run, click the form and observe that the immediate window displays 123 after about one second, as shown in the figure:


3. Some problems that arise:

1. Problem: Generate the exe from the code you just wrote and find that it runs normally on this machine. However, when using a virtual machine to simulate a new environment, an error is reported. Runtime error does not have a registered class. Analysis: This situation occurs because the dll is not registered.

Solution: (1) Open the task manager--Details--Run new task--check Run in administrator mode, enter cmd to run in administrator mode, in the cmd command prompt, cd /d to switch to the dll directory, regsvr32 dll file name. (2). The dialog box prompts that dllregsvristrserver has been successfully registered. The process is as shown in the figure.

 Now run the exe just generated and find that it can run normally.

2. Problem: When running exe, an error occurs, Runtime error automation error. Analysis: This situation is caused by the loss of the dll file or the conflict of dll references. It is divided into two types, one is the dll name conflict, and the other is the old version. conflict

Solution: Check whether the dll is missing. If not, refer to method 1 to re-register the dll.

3. Problem: Open the VB project, run the program, and a Runtime error automation error occurs. Analysis: This situation may be that the referenced dll does not match the registered dll, or it may be that the file is missing. Similar to problem 2, the solution: check whether the dll is Lost, if not lost, refer to method 1 to re-register the dll or reference the correct dll.


Developed by Fuzhou Mechanical and Electrical Engineering Vocational and Technical School wh

Email contact information: [email protected]

QQ contact information: 2151335401, 3135144152

Guess you like

Origin blog.csdn.net/m0_60277871/article/details/128360626