The DLL written in C ++, which is used to transmit the structure to VB.

Source: Internet
Author: User

In some cases, we need to use CB or VC to write a DLL and call VB.

Here we particularly emphasize the use of standard data types, that is to say, to write in C ++, the best way to export a function is to use standard int, Char, long, double... standard Type and Their pointers.

Many people have stressed this issue. However, I recently encountered a DLL that requires VB to pass the structure to CB, and then the data is returned to the structure.

Typedef struct xtest

{

Char A [100];

Char B [100];

} * PX;

_ Declspec (dllexport) void _ stdcall test (* PX, char * X, char * y );

Void _ stdcall test (* PX, char * X, char * Y)

{

If (strlen (x)> 100) * (x + 99) = '/0 ';

If (strlen (y)> 100) * (Y + 99) = '/0 ';

Strcpy (PX-> A, X );

Strcpy (PX-> B, y );

Return;

}

In VB, you need to write the same structure.

Public type xtest

A As string * 100

B As string * 100

End type

Public declare funciton test lib "Your DLL name" (byref PX as xtest, byval X, byval y)

In this way, you can succeed. If you use a variable-length string, your system may crash. I am here to do this. If you have any questions, please correct them.

In addition, VB declares the functions in the DLL and requires the DLL name to be placed behind the Lib, but during the runtime, you must write the full path or put it in the System32 directory, the full path is required even if it is stored in the directory where the vbprogram is located. However, the compiled vbprogram can identify the DLL that is not fully written in the Declaration in the same directory.

This structure looks good, but for CB/Delphi, I think it is good to use the string type. When we need to transfer out non-standard data, encapsulate the data that needs to be passed in/out with a structure, so that we can avoid writing functions with more than N parameters, you only need to define an organization. Then in the structure, we use a type flag to determine the parameters we need to extract.

Public type xtest

Para as string

A As string

B As string

...

End type

Function Test (V as xtest)

If parta = "100" then

Your operations

End if

If parta = "200" then

...

End if

End Function

In this way, we can process more than N tasks in a function. You only need to identify the operations based on different labels.

Obviously, this is what we usually call protocol implementation. Both 8583 of UnionPay and rs232c constitute an agreement and specification. When programming, it is very good for us to develop some simple specifications. And the final wind effect is also very good.

Using classes to do such a thing is certainly a good thing, but we generally do not recommend using export classes directly. Instead, you can use interfaces to avoid data security issues. Once you export a class, you can easily obtain data from the instance of the class through simple pointer operations. Therefore, interface programming is more important.

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.