In general, there are three methods for VB and VC to program together: one is VC to generate DLL, and the other is calling DLL in VB; the other is VC to generate ActiveX Control (. ocx), which is inserted in VB; another is to generate ActiveX Automation server in VC and call it in VB. Relatively speaking, the first method has the lowest requirements for VC programmers, but requires your partners to cooperate. I recommend this method.
Let's talk about the programming of VC ++. First, generate the Win32 DLL project in VC ++. Add several functions in this project for VB users to call. To be called by VB, a function in a DLL must meet two conditions: stdcall and export. To achieve the first entry, you only need to add the _ stdcall keyword before the function declaration. For example:
Short _ stdcall sample (short nLen, short * buffer)
To do the second, add the following lines to the *. def file:
EXPORTS
Sample @ 1
The sample here is the name of the function you want to call in VB. @ 1 indicates the number of the function in the DLL. Each function is different. Note that the function names are case sensitive. As for what you said, you need to transmit a large amount of data. In VB, you can use an array to store data, then pass the size and address of the array to VC (I will introduce how to program in VB below ). As in the preceding example, nLen is the array size and buffer is the array address. With these two parameters, You can process them like using the VC array. As for the output image, you can generate WMF or BMP format for VB to call. However, I think it can also be directly output to the window, as long as VB transfers the Windows handle hWnd and hDC as well as the drawing position of the window (the coordinate system used by VB and VC must be consistent) pass it to VC. The AutoRedraw attribute of VB must be set to False. In the Paint event, the VC plotting program is called.
Let's talk about VB programming. The method for VB To Call DLL is the same as that for calling Windows API. It is generally introduced in the VB book. In the preceding example, the VC function must be declared first:
Declare Function sample Lib "mydll. dll" (ByVal nLen As Integer, buffer As Integer) As Integer
Here mydll. dll is your dll name. You may have noticed that the declaration of the two parameters is different. The first parameter is added with ByVal. The rule is as follows: if a parameter in VC is declared as pointer and array, ByVal is not added; otherwise, ByVal is added. The following syntax is used to call this function in VB:
Sample 10, a (0)
Here, the () array is used to store data. 10 is the length of the array. The second parameter here cannot be a (), but must be the first of the data to be passed. This is the key to vbprogramming.
The following describes several possible problems. One problem is that VB may report that dll cannot be found. You can put the dll under the system directory and ensure that the Declare Statement of VB is correct. Another problem is that the required function cannot be found in the VB report, usually because the *. def file is not set in the VC. The third case is that VB cannot tell the conversion, which may be because the _ stdcall keyword is not added to VC, or the parameter types of VB and VC are inconsistent, note that in VC, int Is 4 bytes (equivalent to Long of VB), while Integer of VB is only 2 bytes. Ensure that the number of parameters of VB and VC is the same, and the number of bytes is the same. The last problem to be noted is that the VC absolutely cannot see arrays out of bounds. Otherwise, the vbprogram will crash.
1. Advantages of calling DLL
As the basis of the Windows operating system, the dynamic link library (DLL) has superior application performance:
DLL extends the features of applications. Because the DLL can dynamically load the address space of the process, the application can determine what operations need to be performed at runtime, and then load the corresponding code to perform these operations as needed.
DLL can be written in multiple languages. For example, you can use VB to compile the application interface, and C ++ to compile underlying operations such as algorithms and communications.
DLL simplifies the management of software projects. If different teams work on different modules during software development, it is easier to manage this project.
DLL helps to save memory. If two or more applications use the same DLL, all applications can share its pages as long as the DLL page is put into RAM once.
DLL facilitates resource sharing. DLL can contain resources such as dialog template, String, icon, and bitmap. Multiple applications can use DLL to share these resources.
DLL facilitates application localization. For example, an application that only contains code but does not contain user interface components can load a DLL containing localized user interface components.
DLL helps resolve platform differences. Different versions of Windows have different functions. Developers often want to call new functions. However, if the source code contains a call to a new function and the application is about to run on a Windows version that does not provide the function, the operating system loader rejects the process. If you save these new functions in the DLL, the application can load them to the old version of Windows and successfully call the function.
2. Find the DLL entry point
Users who contact the DLL for the first time often encounter a problem: the DLL created in the VC environment runs well in the VC environment, when calling in a VB Application, errors such as "Call Convention errors" and "entry points not found" always occur. This is mainly caused by the following omissions.
First, you must note that the DLL functions and the function declaration in VB must be identical in terms of name, return type, parameter type, and number of parameters, especially case sensitivity.
Secondly, the entry function must be added to the. def file of the DLL.
Finally, the extern "c", _ stdcall keyword must be added before the function definition.
For specific formats, see application instances.
3. Passing array parameters in DLL
Because the DLL is often used for some underlying operations, applications often need to transmit a large amount of data to the DLL. In C ++, pointers are the best choice for Array Operations, but there is no pointer concept in VB. This can be solved in two ways.
First, when declaring a DLL in VB, use byref instead of byval to pass the array pointer to the DLL.
In addition, declare the array as variant to directly pass the array to the DLL.
4. Application Instance
The following describes how to call the DLL created in the VC environment in VB through a specific example.
Create a DLL for signal processing, "SigPro. dll", which has a function "Fourier" for Fushi calculation ".
The declaration in VC:
Add the following code to "SigPro. h,
Copy codeThe Code is as follows: extern "C"
{
Double EXPORT _ stdcall Fourier (long int * Sample, int NumSam, int OvertoneOrder, bool SinOrCos );
}
Add the following code to "SigPro. cpp,
Extern "C"
Double EXPORT _ stdcall Fourier (long int * Sample, int NumSam, int OvertoneOrder, bool SinOrCos)
{
Int I;
Double result = 0.0;
If (SinOrCos = true)
{
For (I = 0; I <NumSam; I ++)
{
Result = result + * (Sample + I) * cos (OvertoneOrder * I * 2*3.1415926/NumSam );
}
}
Else
{
For (I = 0; I <NumSam; I ++)
{
Result = result + * (Sample + I) * sin (OvertoneOrder * I * 2*3.1415926/NumSam );
}
}
Result = result * 2/NumSam;
Return result;
}
Add the following code to "SigPro. def,
EXPORTS
Fourier
Call declaration in VB:
Public Declare Function Fourier Lib "SigPro" (ByRef Sample () As Long, ByVal NumSam As Integer, ByVal OvertoneOrder As Integer, ByVal SinOrCos As Boolean) As Double