VB really can't think of the second: VB "Sunflower Collection" - pointer technology

zhaozj2021-02-17  53

"VB really can't think of the series." Every time I see the master's things to the wonderful place, I will shoot the case: "Wow, I can't think of it!". After many this kind of emotion, I found that as long as we move your brains, we can have something that I can't think of others. So I thought that I would like to share these things and share it with everyone, I hope to throw bricks to introduce more people who can't think about it. VB really can't think of the second: VB "Sunflower Collection" - Pointer Technical Keywords: VB, Pointer, Dynamic Memory Allocation, Efficiency, Difficulty: Intermediate To Advanced Requirements: Familiar with VB, master basic C, understand compilation, understand memory The principle of allocation. I want to be unbeaten in the past, and the black wood cliff room is fighting, only a four major masters of a embroidered needle, and the ghost is not, it is the first master of the world martial arts. If you want to become an unbeaten in VB, familiarize VB "Sunflower Collection", master VB pointer technology, is the method of not bia. Want to practice the gods, lead the knife ..., in fact, master VB pointer technology does not need to suffer. Because I have been worn, I will do so a few tricks, add another practice, and finally go to the ghost. Less nonsense, let us first start from the definition of the pointer. First, what is the pointer? It is not necessary to find what standard definition, it is a 32-bit integer, in the C language and in VB can be represented by a long type. There is no difference in 32-bit Windows platforms and the ordinary 32-bit long integer, but its value is a memory address, it is because this integer quarrel point to a memory address, so there is a pointer concept. . There is a statistical indication that a large part of the program defects and memory error access. It is because the pointer is directly dealing with memory, so the pointer has always been seen as a dangerous thing. So many languages, such as the famous Java, do not provide support for pointer operations, all memory access is completed by the compiler. In the case of C and C , the use of pointers is basic skills, and pointers give programmers to handle memory access, many very clever things to complete. Regarding a senior programming language should be canceled by the pointer operation, there is no point to operate, I don't discuss it here, because there is no discussion on this area on the Internet, it has caused occupation. Several GB resources. No matter whether it is finally determined to repair the instrument "Sunflower Collection", it is always beneficial to understand this Kungfu. Note: In VB, the official is not encouraging what pointers, anything told in this article Don't expect to get the official technical support, everything depends on our own efforts, everything is more exciting! Let's start the magical VB pointer adventure!

Second, what can the pointer do? What is the use? First come and see two programs, the functionality of the program is swap two strings: [Program 1]: 'Standard Practical Swapstr SUB SWAPSTR (SA AS String, SB AS String) DIM Stmp AS STRING STMP = SA: SA = SB : SB = stmp end sub

[Procedure 2]: 'Use the Pointer Practice Swapptr Private Declare Sub CopyMemory LIB "Kernel32" Alias ​​"RTLMOVEMEMORY" _ (Destination As Any, Source As Any, BYVALLENGTH AS Long) SUBPTR (SA AS String, SB As String) DIM LTMP As Long CopyMemory LTMP, BYVAL VARPTR (SA), 4 CopyMemory Byval Varptr (SA), BYVAL VARPTR (SB), 4 CopyMemory Byval Varptr (SB), LTMP, 4 End Sub

Do you think the first program is fast, because it looks simple and does not call the API (call the API requires additional processing, the VB document clearly indicates that a large number of call API will reduce program performance). But in fact, running in the VB integration environment, the program is more than one-quarter than the program; and the compilation cost code or P-code, the program is basically more than the program is twice. Below is the comparison of two functions taken after compiling cost machine code: running 100,000, SwapStr requires 170 milliseconds, and Swapptr requires 90 milliseconds. Run 200,000 times, SWAPSTR takes 340 milliseconds, and Swapptr requires 170 milliseconds. Run 20,000,000 times, SWAPSTR takes 3300 milliseconds, and Swapptr takes 1500 milliseconds. Indeed, calling the API is a need for additional instructions, but since the pointer technology is used, it does not perform the assignment and copy of the temporary string, so the speed has increased a lot. How, I can't think of it! The C / C programmer relies on the pointer, not because the use of pointers can often deal with the root cause of the problem, and there is more pleasure. They don't know the danger of using pointers. They are not willing to open satellites to locate mutual speed-saving vehicles, just riding a motorcycle more pleasant, and some places have only a motorcycle to go. Similar to C, in VB, we use pointers in VB, but only three reasons: one is efficiency, this is an attitude of a pursuit, the same in VB; second, no need, because the operating system is written, It always reminds us that it needs a pointer; the third is a breakthrough limit, VB wants to take care of us, VB gives us a strong type of check, VB is like our mom, we can't stand it, think Occasionally, don't you listen to my mother? You need a pointer! But due to lack of official technical support, in VB, pointers become mysterious. Therefore, some basic technologies in C have become more difficult in VB. The purpose of this article is to provide a simple way to get the technology of the C handle pointer to VB, tell you what is feasible, what is feasible, but must be careful, what is possible but not feasible What is not possible at all. Third, the three axes of the bite gold are, the program II has given us the appearance of VB pointer technology. Summarize, in VB, we need to master three things in VB: CopyMemory, Varptr / Strptr / Objptr, AdRessof. Three ax, cross-biting gold three ax, in VB Hack tool. 1, CopyMemory About CopyMemory and Bruce McKinney Masters, MSDN's Knowledge Base has an article introduction, you can search for "ID: Q129947" articles. It is this master brings this API that can move the memory. It is also this API. We can use the pointer to complete some of our work, thank you Bruce McKinney to bring us. The pointer revolution comes from VB. Such as CopyMemory declaration, it is defined in the RTLMoveMemory this API in kernel32.dll, the memory in the 32-bit C library is the package of this API, such as the MSDN document, its function is to be indicated by the Source pointer The starting length is copied to the memory of the memory to the memory referred to as Destination.

It doesn't take care of our programs to read the permissions you should have in the memory, one, when it wants to read the memory protected by the system, we will get the famous Access Violation Fault (Memory Visual Visual Error), even It will cause more famous General Protection (GP) Fault (General Protection Error). So, when conducting the experiments in this series, please pay attention to your program files at any time, set the "Environment" tab in the "Tool" -> Options "in the VB integration environment. To "save changes", remember to save our work results before performing dangerous code in the Immediate window.

2, VatPtr / Strptr / Objptr They are good babies for VB, they are hidden functions in the VBA library. Why is it hidden? Because of the VB development group, we do not encourage us to use pointers. In fact, these three functions are the same function Varptr in VB Runtime Library MSVBVM60.DLL (or MSVBVM50.dll) (see Method I introduced in this series). Its library is defined as follows: ["Varptr", hidden] long _stdcall var); [Entry ("Varptr", hidden] long _stdcall strptr ([in] BSTR PTR); [Entry ("Varptr"), hidden] long _stdcall objptr ([in] iUnknown * ptr); it is the same function in the VB runtime library, we can also re-declare these in VB in VB Function, as follows: Private Declare function objptr lib "msvbvm60" alias "var AS Object) AS longprivate declare function varptr lib" msvbvm60 "_ (VAR AS ANY) AS Long (no strptr is because VB pair string processing There is a bit different from this way. There are too many questions in this series, and there is another "VB string full Raiders". By the way, I heard that there is no such function in VB.NET, but as long as you can Call the API, we can try the above declarations so that we can make a pointer operation in VB.NET. However, please note that if you call to use Varptr through the API call, the entire program 2 SWApptr will use the built-in Varptr function than the original use. It is 6 times slower.) If you like the root of the roots, then the Varptr function looks like a Varptr function in the C and assembly language: LONG VARPTR (VOID * PV) {Return (long) PV;} The assembly code is two lines: MOV EAX, DWORD PTR [ESP 4] RET 4 'pops up the value of the argument in the stack and returns. The reason why everyone knows Varptr's specific implementation is to tell you that its overhead is not big because they have two instructions, even if they add parameter assignment, stack, and call instructions, the entire acquisition pointer will have six instructions. Of course, the same function is in the C language, only one instruction is required due to the direct support of the language. But in VB, it is already the fastest function, so we don't have to worry about using Varptr to make us lose efficiency! Speed ​​is the fundamental requirement using pointer technology. In one sentence, Varptr returns the memory address where the variable is located, or it is also necessary to return a pointer to the variable memory location, which is one of our most important weapons in VB.

3, ByVal and Byref ByVal pass the parameter value, and the address of the parameter passed by ByRef. Here, we don't have to distinguish between password / biographical address / biography, in VB, they are all three different statements, even if there is also a VB document, there are also these terms (but in C indeed To distinguish between a pointer and reference) First contact the program of the program two Swapptr, be sure to make it in the CopyMemory call in it, where to add byval, where you are not adding (do not add byval, use VB default byref) The accurate understanding of the difference between the pass value and the address (pointer) is the basis of the correct use of the pointer in VB. Now one of the simplest experiments to see this problem, such as the following program 3: [Procedure 3]: 'Experience Byval and Byref Sub TestCopyMemory () Dim K As Long K = 5Note: CopyMemory Byval Varptr (K), 40000, 4 Debug .Print K End Sub's purpose of the statement at Note on the Note Note, is to assign K to 40000, equivalent to the statement k = 40000, you can test it in the "Immediate" window, will find that K's value is indeed 40000. In fact, the above statement is translated into vernacular, which is to copy 4 bytes to the memory of the variable k from the time of the temporary variable of the saving constant 40000. Now let's change the statement at Note. If you change it to the following statement: Note2: CopyMemory Byval Varptr (k), byval 40000, 4 This sentence means, from address 40000 4 bytes to variable K The memory is located. Due to the memory of the address 40000 we have no right to access, the operating system will give us an Access Viocation memory access error, tell us "Attempt to read the location 0x00009C40 error, the memory cannot be 'read'." We changed to the following statement to see. Note3: CopyMemory Varptr (k), 40000, 4 This sentence means that 4 bytes are copied from the provisional variable of the save constant 40000 to the provisional variable of the memory address value where the variable k is located. This will not have memory access error, but the value of K has not changed. We can change the program to a more clear rescue, such as the following program four: [Program 4]: 'Look at our things to copy to SUB TestCopyMemory () Dim i as long, k as Long K = 5 i = Varptr (k) Note4: CopyMemory I, 40000, 4 Debug.print K Debug.print i i = varptr (k) Note5: CopyMemory Byval i, 40000, 4 Debug.print K End Sub

Program Output: 54000040000 Due to the default ByVal by Note 4, the address of i is transmitted (that is, pointing to i's pointer), the constant 40000 is copied to the variable I, so the value of I is 40000, and the value of K is no change. However, there is: i = varptr (k) before Note4, is intended to use I itself as a pointer to use. At this time, we must use BYVAL as Note5 to deliver the pointer I, since i is a pointer to the variable k, so the last constant 40000 is copied in the variable k. I hope that you have understood this difference. In the discussion of the following questions, I will talk about it again. 4, addressof It is used to get a pointer to the VB function entry address, but this pointer can only be delivered to the API to make the API call to the VB function. This article is not prepared to discuss the function pointer in detail, please refer to the VB documentation on its use.

5. Take the right. In fact, there are CopyMemory, Varptr, Addressof three ax, we can already take the basic pointer operation in c. The following C program includes most basic pointer pointer operations: struct point {int x; int y;}; int compare (void * elem1, void * elem2) {} void ptrdemo () {// pointer declaration: char C = 'x'; // Declare a CHAR type variable char * pc; long * pl; // declared normal pointer Point * ppt; // declared structural pointer VOID * PV; // Declared Non-Type Pointer INT (* PfncastToint) (void *, void *); // Declaration function pointer: // Pointer assignment: PC = & C; // assigns the address value of the variable C to the pointer PC PFNCompare = compare; // function refers to the needle. / / The value of the pointer: c = * pc; // assigns the memory value indicated by the pointer PC to the variable C // assigns the value of the pointer: * pc = 'y' // refers to the pointer PC. Memory variables. / / Pointer movement: PC ; PL--;}

These have an equivalent thing in VB in VB. When I discuss byval and byref, I have said that the password and biography address are one thing. In fact, when we use the default byref declaration function parameters in VB, we have State the pointer. As a C declaration function: long func (char * pc) Its corresponding VB declaration is: Function Func (PC AS Byte) AS Long This time the parameter PC uses the default Byref transmission address to pass, this and C The pointer is the same as the parameter. So how can I clearly declare a pointer like C? Very simple, as before, use a 32-bit long integer to express the pointer. In VB, it is clearly declared with a long shape. We don't have to distinguish between a normal pointer, and there is no type of pointer or a function pointer, which can be declared using long. To give a needle, it is the address of another variable that is given to it with Varpar. See the program five. [Procedure 5]: Like C, various pointers. Type POINT X As Integer Y As Integer End Type Public Function Compare (elem1 As Long, elem2 As Long) As Long 'End Function Function FnPtrToLong (ByVal lngFnPtr As Long) As Long FnPtrToLong = lngFnPtr End Function Sub PtrDemo () Dim l As Long , C as Byte, CA () AS BYTE, PT AS POINT DIM PL AS Long, PC As Long, PV As Long, PPT AS Long, PFNCompare As LONG C = ASCB ("X") PL = Varptr (L) ' For the long, INT pointer PC = Varptr (C) 'corresponds to Char, SHORT type pointer PPT = VARPTR (PT)' structure pointer PV = Varptr (CA (0)) 'byte array pointer, can correspond to any type, That is, void * pfncompare = FnPTRTOLONG (AddressOf Compare) 'function pointer CopyMemory C, ByVal PC, lenb (c)' uses pointers CopyMemory ByVal PC, ASCB ("Y"), lenb (c) 'uses the pointer PC = PC LENB (C): PL = PL - LENB (L) 'Pointer Mobile End Sub We see that since VB does not directly support pointer operation, use the pointer to value and use the pointer to assign a CopyMemory this API in VB. The price of calling the API is relatively high, which determines that we use pointers in VB cannot be free and frequent in C., we must consider the price of pointer operations, in the "pointer application" we will change again. Talk about this problem. Problem 5 About function pointer, please refer to VB documentation, non-type pointer void * will say in the following "About Any". The program fifth basically has included all the pointer operations we can perform in VB, which is only.

There is a small test question below. If you now understand the three axes of the top bite, you should be able to do it. The above mentioned that there is no Varptr in VB.NET, we can use the declaration API to introduce Varptr in MSVBVM60.DLL. Now the problem doesn't have a VB's runtime DLL file, you can implement an ObjPTR yourself. The answer is given after the next section.

Fourth, the problem should be paid attention to in the use of pointers 1. If anything about Any If you talk with a teacher, I will say: It is best not to use any! Yes, I didn't say anything wrong, it is forever! So I didn't put it in the three axes of Cheng Jin. Of course, this problem should be the same as a result of the question, and I will tell you that it is just a point of view, because sometimes we will improve it in efficiency or to steal a little lazy. ANY, but doing this need to be borne. Any is not a real type, it just tells the VB compiler to abandon the inspection of the parameter type, in theory, we can pass any type to the API. Where is any? Let's take a look, how to say in the VB document, please open MSDN (Visual Studio 6 combo version), turn it back to "Visual Basic Document" -> "use Visual Basic" -> "tool tool Guide "->" Access DLL and Windows API "section, then look at" Convert C language to Visual Basic Declaration ". Tell us in the document, we use any when C is declared as lpvoid and null. In fact, if you are willing to take risks, all types You can use any. Of course, you can also use any anything as I said. Why is this? What is the official offer of VB? Is it a letter to me, or the official of VB? What is the reason not? As mentioned earlier, VB official does not encourage us to use pointers. Because one of the advantages advertised by the VB, there is no dangerous pointer operation, and the memory access is controlled by the VB runtime library. At this point, Java language has the same advertisement. However, like Java, VB must avoid using a pointer to get higher security, and must overcome problems that do not have pointers. VB has done the most effort to keep us away from the security of the pointer. However, the operating system is written, and the pointer is needed everywhere, and some pointers are not type, which is the terrible VOID * in which the C programmer is often said. It doesn't have a type, so it can represent all types. If CopyMemory is a C language Memcpy, its declaration is as follows: void * Memcpy (void * dest, const void * src, size_t count); because the first two parameters of Memcpy are Void *, therefore any type of parameters You can pass it to him. A programmer with C, you should know that Void * in the C function library is not rare, you should also know how dangerous it is. No matter what type of variable pointer to the above Memcpy's void *, the C compiler will not report an error or give any warnings. Most of the VB, we use any to use void *, and in C, VB does not check the ANY, we can also pass any type to any, the VB compiler will not report error or give any caveat. But if the program is running, it will not be wrong when the program is used. Because many errors in C are related to void *, C encourages us to use SatIC_CAST to clearly indicate this unsafe type conversion, which has gained error.

Said so many C / C , in fact, I want to tell all the VB programmers. When using any, we must use Void * as a C / C programmer to be highly careful. There is no SATIC_CAST in VB, but we can use the long type when passing the pointer, and use Varptr to get the pointer of the parameters, which has at least clearly indicates the dangerous pointer. If the program has elapsed this process, it will become the following procedure: [Procedure 5]: 'Using the safer copyMemory, the clear usage pointer! Private Declare Sub CopyMemory Lib "kernel32" Alias ​​"RtlMoveMemory" (ByVal Destination As Long, ByVal Source As Long, ByVal Length As Long) Sub SwapStrPtr2 (sA As String, sB As String) Dim lTmp As Long Dim pTmp As Long, psA As Long, PSB as long ptmp = varptr (LTMP): PSA = Varptr (SA): PSB = Varptr (SB) CopyMemory PTMP, PSA, 4 CopyMemory PSA, PSB, 4 CopyMemory PSB, PTMP, 4 End Sub Note, top calledMemory Declaration, use BYVAL and LONG, requiring 32-bit address values, when we pass an other type to this API, the compiler will report an error, such as now we use the following statement: [Program 6]: 'It's a bit like [program four], but converts a constant 40000 to a variable having a value of 1. Private declare sub copymemory lib "kernel32" Alias ​​"RTLMoveMemory" (Byval Source As Long, Length As long) Sub TestCopyMemory () DIM I as long, k as long, z as interger k = 5: z = 1 i = Varptr (k) 'The following statement will cause compilation errors that are inconsistent, this is a good thing! 'CopyMemory I, Z, 4' should use the following CopyMemory I, ByVal Varptr (Z), 2 Debug.print K End Sub Completed! Is a good thing! This is always better than running! Like the program four, use the Any type to declare the parameters of CopyMemory, although VB will not report, but the runtime results are wrong. Do not believe that you try to change the 40000 in the program 4 to 1, the value of I is not what we want, but 327681. Why is the result in the program four, the result will be wrong, and the result is not bad when the constant is 40000? The reason is that the VB processes the constants in the function parameters.

At 1, because 1 less than the Integer type maximum 32767 32767, VB generates an Integer type temporary variable that stores the value 1, that is, when we want to copy 1 CopyMemroy to the LONG type variable i, this constant 1 is actually an Integer type temporary variable! The INTEGER type in VB has only two bytes, and we actually copy four bytes. Know how dangerous! Nothing to protect the mistake is just our luck! If you must explain why i is finally become 327681 because we will copy the low of the K low 16-bit value 5, and there is 5 * 65536 1 = 327681. Tusatance talks about this problem involves the sequence of VB local variable declarations, the stack order of the CopyMemory parameter, the LONG type low in the previous high level. If you are interested in these issues, you can use the method provided by this series (DebugBreak API and VC Debugger) to track, you can deepen your understanding of VB internal processing, because this is discussed in this article. The problem has nothing to do, so it will not talk more. Here, everyone should understand that there is a mistake in the three and procedures! ! ! I use a constant 40000 above, not for 1, not to make a word number in the article, but because 40000 constant is greater than 32767, it will be interpreted as a long-type temporary variable we need, only this program three and program four can be normal. the work. Sorry, I am intentionally hidden mistake just want to deepen your understanding of Any hazards. In short, we must realize that it is very important to find a mistake when compiling, because you know the wrong place. So we should express the pointers of the LONG type byval as the programs five and programs, instead of the pointer of the ANY's BYREF. But with anything that is so popular, many masters use it. The only charm is not like using a long-type pointer, we need to call Varptr to get a pointer, and all processing pointers are completed by the VB compiler. Therefore, in the processing of the parameters, only one assembly instruction: PUSH [I], and use Varptr, because the function call is required, therefore uses five assembly instructions. Five extra assembly instructions can sometimes use our risk to use any. The VB Development Team provides any, just want to express Void * xxx with byref xxx as any. We can also use the Varptr and Long type pointers to handle. I think the VB development team has also hesitated to announce Varptr, or provide any, and finally they decided to provide any, but continue to conceal Varptr. Indeed, this is a dilemma decision. But after I analyzed above, we should know that this decision does not meet the "safer" original intentions pursued by VB. Because it may hide an incorrect error, debugging and finding this error will cost more time and effort. So I have "best never use any" this "amazing" conclusion. Another benefit of any any, simplifies the way we convert the API of the C declaration into a VB declaration. Now it has become a sentence: In addition to the type of VB built-in, other types should be Declare a long.

2, there are many articles about NULL. There are many articles that have been speaking, must be remembered: vbnullchar is equivalent to the '/ 0' in C, which is used to construct a C string in the byhenon to do the last element. . VBnullString This is the real NULL, which is 0, which can be used directly in VB6. Only the two of the above are used in the API call. There is also EMPTY, NULL is Variant, and Nothing is only related to class objects, and they will not be used in general API calls. Another: The third section of this article has proposed a small test question, is it? Now publish the correct answer: [Test questions answer] Function Objptr (Obj As Object) AS Long Dim Lpobj As Long CopyMemory Lpobj, Obj, 4 Objectptr = LPOBJ End Function

V. VB pointer application As mentioned in the VB, using the pointer is not like c, uses the pointer to handle data, you need to copy the data between the pointer and VB can process the data in the pointer and VB, which requires a lot of extra Overhead. Therefore, it is not a pointer operation in all C to VB, and we should only use pointers in VB when needed. 1. Dynamic Memory Allocation: It is impossible, possibly, but not feasible, VB standards frequently use the pointer in C and C , it is necessary to use dynamic memory allocation, use malloc or new to dynamically allocate memory from the stack, and get Pointing the pointer to this memory. In VB, we can also use the API to dynamically allocate memory, and implement the pointer chain table in the C. However, we cannot use the pointer to access the dynamically assigned memory, and we must use CopyMemory to copy data to VB, and a large number of uses this technique will inevitably reduce efficiency, so that it is like C, It is not feasible to use dynamic memory with a pointer. If C, Pascal implements a dynamic data structure, in VB, it should be actually implemented. There is a linked list in the LinkedList in this paper. It is a linked list that is implemented using HeapAlloc from the stack, and one calls FindFirsturlcachentry this API to operate IE Cache's small program IECHE, which uses Virtualalloc to dynamically assign RAM. But in fact, this is not necessary, VB has provided us with standard dynamic memory allocation methods, that is: objects, strings, and byte arrays limited to space, about object technology, LinkedList's source code Useful objects implemented, you can refer to. Strings can be dynamically allocated with the Space $ function, and VB's documentation has a detailed description. About the byte array, here is talking, it is very useful. We can use RedIM to dynamically change its size and pass pointers to its first element to pass to the API that requires pointers, as follows: DIM AB () AS BYTE, RET As long 'Pass NULL Value API will return it The length of the buffer. RET = Someapineedsbuffer (vbnullstring) 'Dynamically assigned a sufficiently size memory buffer redim ab (RET) AS BYTE' to pass the pointer to the API, at which point the first element of the first element. Someapineedsbuffer (byval Varptr (ab (1))) In the IECACHE in this supporting program, I also provides versions of the byte array to realize the version of the dynamic allocation buffer, which is more simpler than using VirtualAlloc. 2. Breakthrough restrictions below is a classic application that breaks through VB type checks to achieve special functions, from Bruce McKinney's "Hardcore Visual Basic" book. The low 16-bit of a long integer is extracted as an interger type, [program seven] standard method is also an efficient method, but it is not easy to understand.

Function Loword (BYVAL DW As Long) AS Integer IF DW AND H8000 & Then Loword = DW OR & HFFFFFF 0000 Else Loword = DW AND HFFFFFFFE END FUNCTION [Program Eight] 'Use a pointer to do efficiency, but the idea is clear. Function Loword (BYVAL DW As Long) AS Integer CopyMemory Byval Varptr (Loword), BYVAL VARPTR (DW), 2 End Function3 Look at the following two programs, they are functioning to move the first half of the array to the last half: [Program Nine]: 'Standard Mobile Array Practical Private Sub Shitarray (Ab () AS Mytype) DIM I AS Long, n as long n = clng (Ubound (ab) / 2) for i = 1 to n value (n i) = value (i) value (i) .DATA = 0 Next End Sub [program ten]: ' pointer approach Private Declare Sub CopyMemory Lib "kernel32" Alias ​​"RtlMoveMemory" _ (ByVal dest As Long, ByVal source As Long, ByVal bytes As Long) Private Declare Sub ZeroMemory Lib "kernel32" Alias ​​"RtlZeroMemory" _ (ByVal dest As Long, Byval Numbytes As Long) Private Declare Sub Fillmemory Lib "kernel32" Alias ​​"RTLFILLMEMORY" _ (Byval Dest As Long, Byval Fill AS Byte)

Private Sub ShitrrayByptr (Abound (AB) / 2) Dim Nlenth As long Nlenth = LEN (Value (1)) 'DebugBreak CopyMemory Byval Varptr (Value (1 N) ), _ Byval varptr (value (1)), N * Nlent ZeromeMory Byval Varptr (Value (1)), N * Nlenth End Sub When the array is large, the mobile operation is more (such as using an array to implement a HashTable) The program is much better. The program 10 introduces two APIs used in pointer operations: ZeromeMory is used to clear the memory; FillMemory uses the same byte to populate memory. Of course, the functions of these two APIs can be done using CopyMemory. As a good habit, in VB we can use ZeromeMory to initialize the array in VB, with FillMemory to fill in the blame in the memory that is not immediately used, which is conducive to debugging. 4. Of course, the last point of course, the application of VB pointers is more than this, and what the application will be explored by himself. For the application of the object pointer and string pointer, I will write another article to talk about the end of this article and the beginning of the article "VB string full Raiders", I will give the fastest to the two strings here. Method: [Procedure 11] 'Exchange two strings The fastest way private declare sub copymemory lib "kernel32" Alias ​​"RTLMOVEMEMORY" _ (Destination as any, source as any, byval length as long) Sub SwapstrPtr3 (SA AS String, SB AS STRING) DIM LTMP AS Long Dim PTMP As Long, PSA As Long, PSB As Long PTMP = StrPtr (SA): PSA = Varptr (SA): PSB = Varptr (SB) CopyMemory Byval PSA, BYVAL PSB, 4 CopyMemory Byval PSB, PTMP, 4 End Sub Sorry, for a little efficiency, use any! About Strptr, I will talk about it next article. Try yourself! Want to practice the gods, hurry!

转载请注明原文地址:https://www.9cbs.com/read-28394.html

New Post(0)