Using Win32 API function in VB enhance operation function
1 INTRODUCTION
The author found in the practice of programming, VB-operation to be limited to the support of AND, OR, XOR several-bit computing, as well as other comprehensive development tools (such as Visual C + +, C + + Builder, Delphi, and other development tools provided plastic variable displacement, split, and merge computing), VB in the use of encryption, such as the preparation of definitive data processing procedures are often difficult. In order to enable the development of the future will not come to a deadlock, I began to seek to strengthen the operation of the VB-general method to achieve permanent results.
VB rich enough data types, including plastic few Byte, Integer, Long three types, corresponding C + + in the unsigned char, short and long types, and we used two-byte unsigned plastic unsigned short (also called "word "Word), the four-byte unsigned plastic unsigned long (also called" double word "DWord) in VB is not supported. Fortunately, a few but no symbols and the symbols of the binary level, there are no differences, the only difference is that the compiler of the variables and understanding. In bit operations, we only concern bit binary variables, in the VB Integer type can be used as a Word type, the corresponding DWord Long type. (Hereinafter referred to in the text are means Integer type VB Integer type, Long types mean VB Long type, Word, DWord type is not dependent on the specific compiler of the two bytes, four-byte value of the common plastic called) and then see-bit computing, we can see that VB integer variables do not support the left, shifted to right, split, and merge operations such as.
After the above analysis, have been identified and the feasibility of objectives, so I decided to develop a common module to enhance the VB-operation, this module is reusable, as long as the accession of this module works, we can like use the built-in functions like VB transparent in the use of the module function, very convenient. If you use a large number of reusable modules to the development process, short development cycles, code readability, and easy to maintain, prone to error.
The design idea
1. Achieve plastic variables split, and merge
Integer variables split, and merge often use to the operation, such as the IP address is a four-byte dual characters, and sometimes to the decimal point to show the way IP addresses, it is necessary to separate out the value of each byte , and sometimes to the point of the metric conversion for computer IP address internal dual characters, but it also needs to form a four-byte one pair of words. VB does not offer such a feature, integer variables split, and merge this is our function to be achieved. In addition integer variables split, and merge the achievement of Integer, Long variable displacement type the precondition for the (behind "divide and rule strategy" will be mentioned), as long as the realization of the Resolution of integer variables merger displacement problem completely resolved.
Method 1: Use API function to achieve Copymemory
Here I use Win32 API function CopyMemory realized shaping variables split, and merge operations. Use in VB API function must statement, the statement CopyMemory function code is as follows:
Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(Destination As Any, Source As Any, ByVal Length As Long)
The Destination is the goal of the first byte of memory address, is being copied Source parameters of the memory address of the first byte, is the need to copy the parameters Length number of bytes.
The principle is simple: to achieve resolution on the use of a plastic CopyMemory function variables copy to another part of a small plastic variable in the address space while achieving the merger, to the use CopyMemory function of the two pending merger of the small variables Another big plastic copy to the address space of variables. See sample code:
Public Function Hi (ByVal Word As Integer) As Byte
'From a word (Word)-byte (Byte)
'INPUT -------------------------------------------
'Word word (Word)
'OUTPUT ------------------------------------------
'Go back to the Word of the high-value bytes
'Last updated by Liu Qi 2004-3-20.
Dim bytRet As Byte
CopyMemory bytRet, ByVal VarPtr (Word) + 1, 1 'to the high byte Word baked into the contents of the address of the bytRet
Hi = bytRet 'return results
End Function according to the type of data needed, the authors designed a total of six functions, HI () function to obtain a high-byte words, LO () function access to the low-byte words, HIWORD () function was two-word high characters, LOWORD () function of the low double-word word. CON () function combination of the two byte characters, CONWORD () function to the word combinations dual characters. As long as these six combinations of functions can be arbitrary separation combination of integer variables. For example, the aforementioned IP addresses, IP addresses are variable with a DWORD type storage, while counterparts in VB Long types of variables, assuming an IP address stored in the long integer variables, we can extract such an IP address of the highest character Festival: HI (HIWORD (lngIP)).
Method 2: Using an array of security borrow memory method
Although the method used to a simple, but to implement the API function calls, function calls, to preserve the scene, site restoration, spending considerable time, inefficient and therefore not suitable for large data-intensive computing occasions. The author had in the development of encryption software to deal with the use of a document data, the effect is not optimal, speed Aiman. In fact, there is a method can be clever fool VB, direct access to an array of memory space other variables, so as to achieve split, and merge the purpose of plastic variables. As a result of this method eliminates API function calls, it is highly efficient. Now let VB awareness about the safety of the array. VB array of security in C language and an array of the great difference, although VB and C language in the array variable is the guideline, but in the C language array variables directly at the array elements, and in the VB at the array variable is a SafeArray structure, this pvData SafeArray structure in the domain pointing to the array elements.
Well, this is what SafeArray structure used? It stored in the array on the sector, the lower bound, dimension, size and other elements of a series of information is SafeArray structures exist, making VB program to be able to do an array of cross-border checks, and that is why the array in VB array called security reasons, and in the C language does not have the array is the ability to cross-border checks. Of course, the shortcomings of an array of security is not an array of flexible C language, but nevertheless, we still have to manipulate it through an array of security manipulation, it can visit arbitrary memory locations, including other variables memory space . For one-dimensional array, it's SafeArray structured as follows: Type SafeArray1d'1-dimensional array SafeArray definition
CDims As Integer 'dimension
FFeatures As Integer 'signs
CbElements As Long 'individual elements bytes
Clocks As Long 'lock count
PvData As Long 'at the array element pointer
CElements As Long 'peacekeeping definition, the number of peacekeeping elements
Lbound As Long 'the lower bound of the peacekeeping
End Type If the explicit assignment to an array of variables, it at the SafeArray create our own structure, can be set up through the structure of pvData domain SafeArray to visit arbitrary memory locations. See sample code: Public Declare Function VarPtrArray Lib "msvbvm60.dll" _Alias "VarPtr" (ptr () As Any) As Long
Private Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Sub Command2_Click ()
Dim pBytesInLong () As Byte
Dim SA1D As SafeArray1d
Dim i As Long
With SA1D
. CDims = 1
. FFeatures = 17
. CbElements = 1
. Clocks = 0
. PvData = VarPtr (i) 'so that the data pointer at the array of plastic variable i
. CElements = 4
. Lbound = 0
End With
'So that the array variable (in fact, is a pointer) at our own structure created SafeArray1d
CopyMemory ByVal VarPtrArray (pBytesInLong), VarPtr (SA1D), 4
I = & HFFFFFFFF
MsgBox pBytesInLong (1) 'visit to the variable length plastic two bytes (from the low start number)
PBytesInLong (3) = 0 'the full array element is set to 0
PBytesInLong (2) = 0
PBytesInLong (1) = 0
PBytesInLong (0) = 0
MsgBox i 'you will find that i have become 0
'The array variable (in fact, is a pointer) at 0, not only in the C language NULL
CopyMemory ByVal VarPtrArray (pBytesInLong), 0 & 4
End Sub can be seen from the code we use a byte array using a long plastic variable i address space, so that we can access through the array elements of the various variables i bytes. This has also realized the split, the purpose of variable composition of plastic, and a method converge, but it apparently does not need two methods function calls, do not require data replication, it is highly efficient. This way, I create a specialized modules: FastBitEx module, a method of achieving the six mentioned in the function of the Fast version of the code is very long, is not here, please refer to the code readers.
2. Design and Implementation of the shift in computing in the information and many VB code is multiplied by 2 methods used to achieve the left, divided by 2 method shifted to right. It is feasible, as well as the theoretical basis. The chart is a BYTE types of weights table:
- No. 76543210
Weight
2 7
2 6
2 5
2 4
2 3
2
2 1
2 0
Can be seen every one of the weights are lower than the one it that the right to a value of 2 times the left of a BYTE a variable binary equivalent of a high-Mobile to all, each one Weight becomes twice the original (except highest), as BYTE variables in metric equivalent to the value of each binary values and the right of the total value of the product, a BYTE variables to the left and it The decimal value multiplied by two is equivalent to, the only difference is that if the highest BYTE variable 1, multiplied by 2 to overflow, we have to use a little skill to prevent overflow: first highest shielding is 0, then take the 2 will not overflow the. Accordingly we can write to the left BYTE type of a variable function: Private Function ShLB_By1Bit (ByVal Byt As Byte) As Byte
'BYTE types of variables to the left of a function, parameter Byt is to wait until the transfer of bytes, functions return translocation results
'(Byt And & H7F) is the role of shielding highest. * 2: the left one
ShLB_By1Bit = (Byt And & H7F) * 2
End Function BYTE similar to a type variable shifted to right used divided by 2, then we should pay attention to decimals Charities, according to VB to avoid rounding ways to deal with decimal places is cause incorrect results. Accordingly we can write to BYTE type variable shifted to right a function: Private Function ShRB_By1Bit (ByVal Byt As Byte) As Byte
'BYTE type variable shifted to right to a function, parameter Byt is to wait until the transfer of bytes, functions return translocation results
'/ 2: 1 shifted to right
ShRB_By1Bit = Fix (Byt / 2)
End Function
With a function of a shift, then shift arbitrary function of the median is not hard to write: as long as the repeated calls ShLB_By1Bit () or ShRB_By1Bit () can be, and see the code in the function ShLB () and ShRB ().
Thus byte variable displacement problem has been resolved, now let's look at the words and word pairs shift, they were in the corresponding VB and Long Integer type. Multiplied by 2 and with the approach also divided by the two firms? Test with a few to a few will be found, this is a failure. See the results of a variety of comparison:
A = 1001'0111'1110'1100
Shifted to right one: 0100'1011'1111'0110
(A / 2): 1100'1011'1111'0110
The problem seems to change things a bit more complicated, but in fact this approach led to the malfunctioning of the most fundamental reason is that the VB type Integer and Long understood as a symbol of the symbol of a divided by the number multiplied by 2 or 2, the highest (that is, symbols bit) simply did not take part in operations, which result from the above calculation can be seen on contrast: A divided by the highest after 2 or 1, there is no change, and shifted to right after a maximum of recruits is 0 , the results of the two operations is far from natural. Symbol not only the question of, if the data used to compare the other will find more problems will not repeat them here on the. Is really no other way? Approach is certainly there, since it has been achieved byte shift operation, then can be used "divide and rule" strategy, the Integer variables divided into two, and to open into two bytes, these two bytes to ShLB () or ShRB (), the shift to a Talia, the shift after the last two bytes regrouped into a Integer variable is the result of the shift, which is not on the realization of the Integer type of the variable displacement ?. The method is completely bypassed a number of symbols to bring us a symbol of the many troubles, the smooth realization of its purpose. Use this method NOTE: If it is the left, it is necessary to ensure that the maximum displacement of the low byte to byte the lowest high, if the contrary is shifted to right, to the high-byte minimum displacement of low-byte maximum bit. From the following code can be seen in the process of realization: Private Function ShLW_By1Bit (ByVal Word As Integer) As Integer
'A word to the left of a function, parameter is the Word of the pending shift characters, the function returns translocation results
'INPUT -------------------------------
'Word source operand
'OUTPUT ------------------------------
'Back to the results of displacement
'Last updated by Liu Qi 2004-3-24
Dim HiByte As Byte, LoByte As Byte
'Resolution for byte characters
HiByte = Hi (Word): LoByte = Lo (Word)
'To the left a high-bytes, and guarantee that the maximum displacement of the low byte to byte the lowest high
HiByte = ShLB_By1Bit (HiByte) Or IIf ((LoByte And & H80) = & H80, & H1, & H0)
LoByte = ShLB_By1Bit (LoByte) 'a low-byte backspace
'Shift to the re-byte characters combination
ShLW_By1Bit = Con (HiByte, LoByte)
End Function
As Long types, and Integer type, there are few symbols, nor can multiplied by 2 and 2 divided by the displacement method. And we have to deal with the same type Integer replicated using divide-and-rule approach to shift. Specific process will not repeat them, please refer to the code.
3. Shift Operational Performance Optimization
In this paper, the shift in emphasis on methods to achieve readability of the code, not the performance of optimized code, it does not apply to the performance of demanding occasions. In order to optimize the performance, look-up table method can be used to optimize the speed of execution, it is a space-time with the programme, the shift results can be calculated in advance are stored in shift in the table, with the time table look-up, compared with 2 * , / 2 more quickly. For example, the shift type byte array table defined as follows:
Dim aSHLB (0 to 255,1 to 7) as byte 'byte backspace Table
Dim aSHRB (0 to 255,1 to 7) as byte 'byte shifted to right Table
Is also very simple to use, for example, would like to ask byte variable x backspace a result of simply aSHLB (x, 1) can be, and the function call is very similar. Of course, different from the function call, the use of transposition tables must be initialized prior to the transfer table all the elements, otherwise they will get the wrong results.
Integer type of displacement can also use the look-up table method, transposition tables occupied 65535 * 15 * 2 * 2 bytes of memory space.
Table shift array defined as follows:
ASHLW (0 to Hffff & & 1 to 15) as integer 'words of the left table
ASHRW (0 to Hffff & & 1 to 15) as integer 'words shifted to right the Table
NOTE: Integer type is a symbol, the table when making use of its symbolic value without making table, the same time to look-up table with its no-value symbol look-up table. (Subscript because of the array is not negative.)
Integer type for unsigned value of the method are: (Int and hFFFF & &), the attention, not the same CLng (Int)
Unfortunately, the Long Table type not made because of the value of the Long types of 4 GB, if it made table, then the table will exceed the total size of the room in Xian刂of evolution?
Tabular shift codes, please see the accompanying this code is not presented here.
3 Conclusion
In order to realize those described in this paper-manipulation functions fact, there are many methods used in this paper may not be the best way, mainly to provide a solution to the problem thinking: encountered in the process of programming in difficult problems, think the big problem can be decomposed into or has been resolved to solve the problems, this is the "divide and rule" strategy. Because of the limited level of the author, this article will be inevitable omissions and weaknesses of the welcome correction criticism, comments or suggestions to me by e-mail liuqi5521@sina.com.
This procedure VB6.0 in Win2000 + debugging through.








0 Comments to “Using Win32 API function in VB enhance operation function”
No Comments. Send your comment.
Leave a Reply
You must be logged in to post a comment.