Call the DLL written in C/C ++ language in VB and pass the array parameters.

Source: Internet
Author: User

The benefits of the dynamic link library (DLL) will not be discussed much. Here we will introduce how to call the DLL function written by VC in VB for your reference, at the same time, you may not forget it later.

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.

_ Stdcall is a function call Convention. For more information about function call conventions, see the article.

Http://hi.baidu.com/atomxu/blog/item/01334717c378924621a4e955.html

For example, to evaluate the sum of all elements in an array, the complete code is as follows:

Code in dll_test_dll.h

Extern "C"
{
Int _ stdcall sum (INT Len, int * );
}

 

The code in dll_test_dll.cpp. The red part is the code written by the user, and the rest is automatically generated by the system.

// Dll_test_dll.cpp: defines the DLL application entry point.
//

# Include "dll_test_dll.h"
# Include <iostream>
Using namespace STD;

# Ifdef _ managed
# Pragma managed (push, off)
# Endif

Bool apientry dllmain (hmodule,
DWORD ul_reason_for_call,
Lpvoid lpreserved
)
{
Return true;
}

# Ifdef _ managed
# Pragma managed (POP)
# Endif

Extern "C"
Int _ stdcall sum (INT Len, int *)
{
Int result = 0;
For (INT I = 0; I <Len; I ++)
{
Result = Result + A [I];
}
Return result;
}

Add a Def file to the project to specify the exported function. You can create a new file in VC. The extension is. Def.

The content in the dll_test_dll.def file is

Library "dll_test_dll"
Exports
Sum @ 1

Compile the above Code to generate the. dll and. Lib files. The following describes how to call and pass array parameters in VB.

First, declare the function in the DLL in VB. The name must be the same as the function name in the DLL. As for the variable type, it cannot be matched. After all, it is two languages, however, it should be noted that the number of variables and the size of the variables should correspond. For example, in C language, int type is 4 bytes, and integer type is used in VB. The function declaration method is as follows:

Public declare function Sum lib "E: // study // VB // dll_test_dll // debug // dll_test_dll.dll" (byval A as integer, byref B as integer) as integer

The above code is written in one line.

The difference from the general function life is that there is a declare, and the sum after the function is the function name. To be consistent with the DLL, lib specifies the DLL address. We recommend that you use the full path address, however, if you only write one name and put the DLL and program in the same directory, you should also be able to. I have never tried this.

Note the red byref keyword defined in the preceding parameter. There are two methods for passing function parameters in VB: one is to pass the value, that is, the previous byval, and the other is to pass the address, here, byref is used. Because we want to pass array parameters, we need to use byref to pass them.

How to call a function after it is defined? See the following example.

Dim A as integer defines an integer as the length of the array.
Dim B (1) as integer 'defines an array with two numbers. 1 represents the upper bound of the array subscript, which is different from the C language.
Dim C as integer' stores the result at the top of an integer.

A = 2
B (0) = 17
B (1) = 11
C = sum (a, B (0 ))

 

It may be a bit confusing here. I should clearly pass the array address. How did I pass a B (0?

In C/C ++, the array address is the same as the address of the first element. In VB, The byref method directly transmits the parameter address, based on the above two items, we can know that the statement actually passes the address of the first element B (0) in array B, which completes the transmission from VB to the DLL array parameter.

References:

Http://hi.baidu.com/hfutgiser/blog/item/63418858a4f09f87810a187e.html

Http://www.360doc.com/content/06/0804/14/6002_173346.shtml

 

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.