Share how to call VC written in VB Dll_vb

Source: Internet
Author: User
Tags function definition

In general, VB and VC common programming there are 3 ways: One is the VC generation DLL, in VB to call the DLL; one is the VC generates ActiveX control (. ocx), inserted in VB; There is also a VC in the generation of ActiveX Automation server, in VB call. Relatively speaking, the first method of VC Programmer's request is lowest, but ask your partner to cooperate, I recommend this method.
Let's talk about VC + + programming. First, in VC + + to generate Win32 DLL project. In this project, several functions are added for VB users to call. A function in a DLL to be called by VB, must meet two conditions: one is called stdcall, the other must be export. To do the first article, you only need to add the __stdcall keyword before the function declaration. Such as:
Short __stdcall sample (short Nlen, short *buffer)
To do the second article, 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, each function is different. Note that the function name here is case-sensitive. As you say you need to pass a lot of data, you can do this, in VB with an array of data, and then the size and address of the array to VC (as for how to program in VB I will be introduced below). Just like the example above, Nlen is the size of the array, buffer is the array address, with these two, you can do the same as using the VC array. As for output graphics, you can generate WMF or BMP format, let VB call. However, I think you can also directly output to the window, as long as VB will be the windows of the handle HWND and HDC, as well as the drawing position of the Windows (VB and VC use of the coordinate system must be consistent only line) to the VC on the line. and VB AutoRedraw property must be false, in the Paint event to invoke VC drawing program.
Again to talk about VB programming. VB calls the DLL's method and calls the Windows API method is the same, generally in the VB book has the introduction. 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 declarations of two parameters are different, the first parameter plus the ByVal. The rule is this: if a parameter in VC is declared as a pointer and an array, it is not ByVal, otherwise it will be added ByVal. This function is invoked in VB using this syntax:
Sample, a (0)
The A () array here is used to hold the data, 10 for the length of the array, where the second argument cannot be a (), but must be the first in the data to be passed. This is the key to VB programming.
Here are a few questions that you might encounter. A problem is that VB may report that the DLL could not be found, you can put the DLL in the system directory, and make sure that VB Declare statement correct. Another problem is that the VB report cannot find the required function, which is usually because the *.def file is not set in VC. The third situation is that VB can not be converted, this may be in the VC did not add __stdcall keyword, but also may be VB and VC parameter types are inconsistent, note that in VC int is 4 bytes (the equivalent of VB long), and vb integer only 2 bytes. We must ensure that VB and VC are the same number of parameters, the number of bytes is also consistent. The last thing to pay attention to is that the VC must not appear in the array of the situation, otherwise it will cause the VB program crashes

1. Advantages of invoking DLLs

dynamic-link libraries (DLLs) are the foundation of the Windows operating system and have superior application performance:
DLLs extend the attributes of an application. Because DLLs can dynamically load the address space of a process, an application can determine at run time what action needs to be performed and then load the appropriate code to perform these actions as needed.
DLLs can be written in multiple languages. For example, using VB to write the interface of the application, and use C + + to write such as algorithms, communications and other low-level operations.
DLLs simplify the management of software projects. This project is easier to manage if different teams work on different modules during the software development process.
DLLs help save memory. If two or more applications use the same DLL, the page of the DLL is placed in RAM only once, and all applications can share its pages.
DLLs facilitate 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.
DLLs Help in localizing your application. For example, an application that contains only code and does not contain user interface components can load DLLs that contain localized user interface components.
DLLs help resolve platform differences. Different versions of Windows are equipped with different functions, and developers often want to invoke new functions. However, if the source code contains a call to a new function, and the application will run on a version of Windows that cannot provide the function, the operating system loader will refuse to run the process. If these new functions are saved in a DLL, the application can load them onto the old version of Windows, and the function will be successfully invoked.

2. Find entry point for DLL

First contact with the DLL users often encounter a problem: in the VC environment to create the DLL, in the VC run well, can be called in the VB application is always "call Convention Error", "No entry point" error. This is mainly caused by the following omissions.
First of all, to note that the functions in the DLL and VB in the function declaration in the name, return type, parameter type, number of parameters must be exactly the same, especially to note the case of the problem.
Second, you must add the entry function in the. def file of the DLL.
Finally, you must add extern "C" to the _stdcall keyword before the function definition.
The specific format can refer to the application example.


3. The passing of array parameters in a DLL

Because DLLs are often used for low-level operational operations, applications often need to pass large amounts of data to DLLs. In C + +, pointers are the best choice for array operations, but there is no concept of pointers in VB. This can usually be resolved 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, the array is declared as a Variant (variant), and the array is passed directly to the DLL.


4. Application examples

The following is a concrete example to illustrate the process of invoking the DLL created in VC environment in VB.
Create a DLL for signal processing, "SigPro.dll", which has a function "Fourier" for the Fourier calculation.

VC in the statement:

Add the following code to "SigPro.h".

Copy 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

The invocation declaration in VB:
Public Declare Function Fourier Lib "Sigpro" (ByRef Sample () as Long, ByVal Numsam as Integer, ByVal Overtoneorder as inte GER, ByVal Sinorcos as Boolean) as Double

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.