VB array (non-string) VC

Source: Internet
Author: User
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: the call method is stdcall, And the other must be 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, WMF or BMP format can be generated 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) just upload 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 array length, the second parameter here NoIs a (), but must be the first 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 int in VC is 4 bytes (equivalent to long in VB), while integer in VB only has 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 arrays in VC cannot be out of bounds. Otherwise, the vbprogram will crash.

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.