Generally speaking, there are three ways to program together between VB and VC: one is to generate a DLL in VC and call the DLL in VB; one is to generate an ActiveX control (.ocx) in VC and insert it in VB; the other is to create a DLL in VC Generate ActiveX Automation server in VB and call it in VB. Relatively speaking, the first method has the lowest requirements for VC programmers, but requires the cooperation of your partners. I recommend this method.
Let’s talk about VC++ programming first. First generate the Win32 DLL project in VC++. Add several functions to this project for VB users to call. For a function in a DLL to be called by VB, it must meet two conditions: one is that the calling method is stdcall, and the other is that it must be exported. To do the first one, just add the __stdcall keyword before the function declaration. like:
short __stdcall sample(short nLen, short *buffer)
To do the second one, you need to add the following lines to the *.def file:
EXPORTS
sample @1
Sample here is the name of the function you want to call in VB, @1 represents the number of the function in the DLL, and each function is different. Note that the function names here are case-sensitive. As for the need to transfer a large amount of data, you can do this, use an array to store the data in VB, and then pass the size and address of the array to VC (as for how to program in VB, I will introduce it below). Just like the above example, nLen is the array size and buffer is the array address. With these two items, you can process it like a VC array. As for the output graphics, you can generate WMF or BMP format and let VB call it. However, I think it can also be output directly to the window, as long as VB passes the window handles hWnd and hDC and the drawing position of the window (the coordinate systems used by VB and VC must be consistent) to VC. The AutoRedraw property of VB must be False, and the VC drawing program is called in the Paint event.
Let’s talk about VB programming again. The method for VB to call DLL is the same as the method for calling Windows API, which is generally introduced in VB books. For the above example, first declare the VC function:
Declare Function sample Lib "mydll.dll" (ByVal nLen As Integer, buffer As Integer) As Integer
Here mydll.dll is the name of your dll. You may have noticed that the two parameters are declared differently, with ByVal added to the first parameter. The rule is this: If a parameter is declared as a pointer or array in VC, ByVal will not be added, otherwise ByVal will be added. Calling this function in VB uses the following syntax:
sample 10, a(0)
The a() array here is used to store data, and 10 is the array length. The second parameter here cannot be a(), but must be the first of the data to be passed. This is the key to VB programming.
Below are some possible problems you may encounter. One problem is that VB may report that the dll cannot be found. You can put the dll in the system directory and make sure that VB's Declare statement is correct. Another problem is that VB reports that it cannot find the required function. This is usually because the *.def file is not set in VC. The third situation is that VB tells you that the conversion cannot be performed. This may be because the __stdcall keyword is not added in VC, or it may be that the parameter types of VB and VC are inconsistent. Note that int in VC is 4 bytes (equivalent to VB Long), while VB's Integer is only 2 bytes. It must be ensured that the number of parameters in VB and VC is the same, and the number of bytes they occupy is also the same. The last thing to pay attention to is that the array must not exceed the bounds in VC, otherwise it will cause the VB program to crash.
1. Advantages of calling DLL
As the foundation of the Windows operating system, the dynamic link library (DLL) has superior application performance:
DLLs extend the features of an application. Because a DLL is dynamically loaded into a process's address space, an application can determine at runtime what operations need to be performed and then load the appropriate code to perform those operations as needed.
DLLs can be written in a variety of languages. For example, VB is used to write the interface of the application program, and C++ is used to write underlying operations such as algorithms and communications.
DLLs simplify the management of software projects. If different working groups work on different modules during software development, the project is easier to manage.
DLL helps save memory. If two or more applications use the same DLL, the pages of the DLL only need to be put into RAM once, and all applications can share its individual pages.
DLL facilitates the sharing of resources. DLLs can contain resources such as dialog templates, strings, icons, and bitmaps, and multiple applications can use DLLs to share these resources.
DLL helps in localization of applications. For example, an application that contains only code and no user interface components can load a DLL that contains localized user interface components.
DLLs help resolve platform differences. Different versions of Windows are equipped with different functions, and developers often want to call new functions. However, if the source code contains a call to a new function, and the application is to be run on a version of Windows that does not provide that function, the operating system's loader will refuse to run the process. If these new functions are saved in a DLL, the application can load them into older versions of Windows and successfully call the function.
2. Find the entry point of the DLL
Users who come into contact with DLLs for the first time often encounter a problem: a DLL created in a VC environment runs fine in VC, but when called in a VB application, "calling convention error" or "not found" always appears. entry point" errors. This is mainly caused by the following omissions.
First of all, it should be noted that the function declaration in the DLL and the function declaration in VB must be exactly the same in terms of name, return type, parameter type, number of parameters, etc. Pay special attention to the issue of capitalization.
Secondly, the entry function must be added to the .def file of the DLL.
Finally, the extern "c" and _stdcall keywords must be added before the function definition.
Please refer to the application example for the specific format.
3. Passing array parameters in DLL
Since DLL is often used to perform some low-level operations, applications often need to pass a large amount of data to the DLL. In C++, pointers are the best choice for array operations, but there is no concept of pointers in VB. This can usually 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, by declaring the array as a variant, you can directly pass the array to the DLL.
4. Application examples
The following uses a specific example to illustrate the process of calling a DLL created in a VC environment in VB.
Create a DLL for signal processing, "SigPro.dll", which contains a function "Fourier" for Fourier calculations.
Statement in VC:
Add the following code to "SigPro.h",
Copy the code code 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 statement 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