VB VC Mixed Programming difficult problem solution

Source: Internet
Author: User
Tags character set mixed

When we write VisualBasic applications, we often need to write some library functions or ActiveX controls, and these functions or controls often use the VC language to write, and therefore often for VB and VC two languages between different parameter types, memory space use methods and so on, How do you do it? This paper introduces the method of solving these problems in the case of VB and VC mixed programming.

The delivery of custom type parameters to DLL library functions

When mixed programming with VB,VC, it is usually necessary to call the DLL library function written by VC in VB, at this time, the problem of passing parameters to the library function is generally encountered. For the standard type (such as Double,long, etc.) parameters, the transfer is relatively simple, as long as the library function in VB in the Declaration and the VC in the definition of the library function in the parameter type, order and transmission mode of consistency, the parameters will not be wrong to pass. However, if you need to pass the parameters of a custom type to a library function, the situation becomes complicated.

Case one: All member variables of a custom type are of the same type (for example, the following pens custom type, whose members are all long).

Typepens

Redpennumaslong

Greenpennumaslong

B1uepennumaslong

Endtype

At this time, as long as in VB and VC in the structure of the same definition, and fully aware of the VB and VC on some data types (such as 32, bit operating systems, VC in the Int and VB in the integer) storage on the difference, will not occur parameter delivery errors.

Case two: The type of the member variable in the custom type is not exactly the same. At this point, there are two things to distinguish:

Situation (1) does not have a double member variable.

In general, there is no parameter passing error.

The case (2) contains a double member variable.

Arguments are often misrepresented. For example, pass the following person type parameter to VC development DLL library function, the value of double member height will be lost in delivery:

Typeperson

Ageaslong

Heightasdouble

Endtype

The reason for the height value loss is that when you store the person type variable in vc, a few bytes of space between the long member age and the double member height are automatically inserted, while VB does not. Therefore, the VC store a person-type variable requires more than 12 bytes of memory, and VB only need 12. Therefore, the person-type variable passed into the DLL library function from VB cannot be received correctly.

There are a number of ways to solve this problem, and here is a simpler and more pervasive approach called "introducing a complement member": Introducing several memory complement members into a custom type such as person. The total number of bytes that are occupied by all members before any double member is the integer multiple of the byte number of a single double variable (an integer of 8).

Still, take the person type as an example, because the age member consumes 4 bytes of memory, so to introduce a complement member that occupies 4 bytes after it, you may want to introduce a string-type member Tempst:

Typeperson

Ageaslong

Tempstasstring*4

Heightasdouble

Endtype

As a result, the total amount of memory consumed by all members before the double member height becomes 8 bytes, and is an integer multiple of 8. At this point, the definition of person in the DLL library function is changed, and the person type parameter from VB can be received correctly.

Note: When you introduce a complement member, you should not only rationally allocate the number of bytes it occupies, but also properly arrange its position in the structure, both of which are indispensable. In the example above, if you put a complement member in height, all the members before the DOUBL variable height will still have a total of 4 bytes, not an integer multiple of 8.

When you write a DLL library function yourself, you often use a complex custom structure at the function interface. When calling this function in VB, it is possible to avoid the error of parameter passing by "introducing the complement member method" to modify the definition of the structure properly.

Second, the use of VC in the dynamic application of memory

Mixed language programming, sometimes need to use the VB code in the dynamic application through VC memory. At this point, you can implement the following:

1 DLL library function for dynamic memory in VC

Char*apientrycreatestringbuffer (Longlength)

{

CHAR*BUFV;//assumes that you need to request dynamic memory for storing word Fuschen

Buf= (char*):: malloc (Length);

returnbuf;//returns a string pointer, which is actually a long number

}

2 code to receive the dynamic memory pointer in VB

......

Declarefunctioncreatestringbufferlib "C:dlltesttest.dll" _

(Byvallengthaslong) Aslong

' Long variable t receives dynamic memory pointer

......

dimslbuffer&

Atbuffer=createstringbuffer (20)

' Apply a piece of memory that can hold 20 characters and get a pointer to that memory

......

' Use this dynamic memory

......

Note: VB in the use of dynamic memory, in order to avoid memory leakage two to return the pointer to VC for memory release work.

Third, the custom type parameter passes to the ActiveX control

When writing a VB program, if you are using a standard ActiveX control, you generally do not need to pass a custom type parameter to the control, because most of the control's properties are standard types such as Double,long. However, in mixed language programming, when we adopt the ATL3.0 template in VC (instead of VB) to develop the ActiveX control, we often want to be able to pass the parameters of the custom type to some properties or methods of the control, so as to improve the transfer efficiency of the parameters.

This is an easy way to pass a custom type parameter to a control. Suppose you want to develop an ActiveX control for the client in VB Ax, it has a student property, the type is custom struct person:

Typeperson

Ageaslong

Heightasdouble

Endtype

First, you write the interface function of the Student property correctly (to use the ATL3.0 template to develop the AX as an example). We write the interface parameter type of the Student property access function as a long pointer, not a BSTR. Because ActiveX internal communications are all based on Unicode, this processing avoids the misinformation caused by the character set mismatch. The relevant code is as follows:

1) The definition of the student attribute in Ax.idl

[Propget,id (0), helpstring ("Propertystudent")] HRESULT

Student ([Out,retval]long*pval);

[Propput,id (0), helpstring ("Propertystudent")] HRESULT

Student ([In]longnewval);

2) The definition of Student attribute access function in AX.h

STDMETHOD (get_student) (/*[out,retval]*/long*pval);

STDMETHOD (put_student) (/*[in]*/longnewval);

3) Realization of student attribute access function in Axcpp

Stdmethodimpcax::get_student (Long*pval)

{

Todd:addyourimplementationcodehere

The pointer to the member variable t of the storage student property is obtained, and assigned to *pval RETURNS_OK;

}

Stdmethodimpcax::p ut_student (Longnewval)

{

Todd:addyourimplementationcodehere

The pointer that stores the member of the student property to the memory space referred to by newval.

Then copy the Student property value of the space stored in the memory copy mode

RETURNS_OK;

}

Second, correctly compiles the VB to Ax's student property dynamically assigns the value the code. In VB, first declare a person type variable, assign a value to the variable, get the memory address of the variable and assign it to the student property. The code is as follows:

......

Dimstudentpropasperson

Dimstudentaddraslong

Studentprop.age=23

studentprop.heigth=1.78

' Gets the memory address of the Studentprop variable (method Conlio), assigned to STUDENTADDR

AX1. Student=studentaddr

......

The use of pointers to complete a custom type parameter's delivery to an ActiveX control is based on the fact that, regardless of whether the control is a. dll or. ocx, it is a server that is in the same process as its customers. So, as long as Ax is compiled to. dll or. ocx, the pointer is passed safely and reliably.

Determination of the output length of mixed-type strings in Chinese and English

The problem of determining the output length of mixed-type strings in Chinese and English is often encountered in VB programming, and can be effectively solved by VB,VC mixed programming.

VB programming, often need to get a string in the actual output of the required length. At this point, we usually consider the two functions of Len () and LenB ().

We know that Len () returns the number of characters in the string, and the return value for a string without a Chinese character is usually equal to the output length of the string; LenB () returns the number of bytes taken up by a double-byte character set (DBCS) computed string, and the return value of a string consisting of plain Chinese characters is usually equal to the output length of the string. However, when the string contains both Chinese and English (where the numbers are treated as English), the return value of both is not equal to the output length of the string. For example: "A Chinese" this string, with the Len () function will return 4; LenB () returns 8, while the actual output, such as outputting the string to a record file, consumes 7 printer characters (1 for each English character and 2 for each Chinese character).

In order to calculate the output length of the mixed string in Chinese and English, we can use VC to write a DLL library function to complete this calculation, and call the function directly in VB. The VC function and VB in the calling code is as follows:

1 DLL function in VC 6.0

Longapientrysizeof_vbstring (Char*at)

{

Return (Long) (:: Atrlen (ST));

:: Atrlen () returns an int value, but under a 32-bit operating system,

The int type in VC is equivalent to the range of long type in VB

}

2) Declaration and invocation of Sizeof_vbstring () in VB6.0

......

Declarefunctionsizeof_vbstringlib "C:dlltest.dll" _

(byvalstasstring) Aslong

......

......

dimstlen&

Stlen=sizeof_vbstring ("a Chinese")

Stlen=7

......

It should be noted that the above method can also calculate the input length of pure English or pure Chinese strings.

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.