My article is visible: http://www.9cbs.net/author/adambear
Fancy six: useful and useless technology pointer Keywords: VB, SafeArray, value type pointer Difficulty: Intermediate Reference article: 1, July 2000 VBPJ Black Belt column "Modify a Varialbe's Pointer" Author: Bill McCarthy
Introduction: This is really the last article of the pointer (of course, it will definitely mention the pointer). Mainly to talk about Bill McCarthy's article "Modify a Varialbe's Pointer". About this article, I talked in my "VB pointer sunflower book SafeArray", but that article did not write SafeArray's essence, the most wonderful place with SafeArray lied to build a variable The specified memory is the same as the string class given by the previous article.
Text: BILL MCCARTHY In that "Modify A Varialbe's Pointer" with SafeArray to implement multi-process array shared memory, he considers the type of array variable, so it can be compatible with most arrays of most numerical types, is a very good thing. I don't talk about it here, just want to see what SafeArray can do. Modifying the PVData pointer of the SafeArray structure is a very useful technology. By modifying PVDATA, you can directly access the specified memory through the array. Like the previous article, we can also package a pointer class for normal numerical type variables by modifying PVDATA. I said in the first article of the pointer. To implement the variable value function of '*' in the C language, you must use CopyMemory. In fact, I am wrong, we can fully implement the same pointer as C, as follows: // C language long L; long * pl = & l; * PL = 12; Printf ("l =% D * PL =% D ", L, * PL);
'VB DIM PL AS NEW PLONG, L As Long Pl.attach L' can also PL.PTR = Varptr (L) PL = 12 Debug.print "L ="; "* pl ="; PL results can be The purpose of modifying the variable L is achieved by modifying the PL pointer. The Plong in the VB code above is a pointer class for a wrapped long-type variable, look at how to implement it:
Option explicit '****************************************************************************************************************************************************************************************************************************************************** ********* 'pLong.cls' packing a Long pointer to a class type' author: Xiong ID: AdamBear 2002 March 18 'http://www.9cbs.net/Author/AdamBear' You can freely use this class module, but please keep this statement '******************************************* ***************************** Private Declare Sub CopyMemory LIB "Kernel32" Alias "RTLMOVEMEMORY" (Destination As Any, Source As Any, BYVAL LENGTH AS Long Private M_ARR (0 AS Long 'Default Properties Public Property Get Data () As long Data = M_ARR (0) End Property Public Property Let Data (Byval Value As Long) M_arr (0) = Value End Property Public Sub Attach (Target As Long) PTR = VARPTR (TARGET) End Sub Public Property Let Ptr (Byval Target As Long) DIM PSA AS Long 'Get the Safearray Structure Pointer PSA CopyMemory PSA, BYVAL VARPTRARRAY (M_ARR), 4' This pointer offset 12 bytes later is the PVData pointer CopyMemory Byval (PSA 12), Target, 4 End Property Public Property PUBLIC PR OPERTY GET PTR () AS Long Ptr = m_sa.pvdata End Property Private Sub Class_Terminate () CopyMemory Byval VarptraRray (M_ARR), 0 &, 4 End Sub To change it into Byte's pointer, you only need to pop up the M_ARR array in the above code The type, the parameter type in the DATA property and the ATTACH method can be changed to the BYTE type.
When we make Plong, Pbyte, Pinteger, we can play the same tricks like C. Sub main () DIM PB AS New PBYTE, B AS BYTE DIM PI AS New Pinteger, I as "The type check will pass the type inspection with the attach method, and use the PTR attribute to bypass the type check. Pb.attach B Pi.attach I Pl.attach L 'Try Pointer B = 1 Debug.print "B ="; B; "* Pb ="; PB PB = 1 Debug.print "B ="; B; " * Pb = "; PB i = 1000 debug.print" i = "; I;" * pi = "; Pi Pi = 2000 debug.print" i = "; i;" * pi = "; pi L = 40000 debug .Print "l ="; L; "* pl ="; PL PL = 60000 debug.print "l ="; L; "* pl ="; PL 'Try the type conversion in c' to access long with Integer pointer Type variable pi.ptr = varptr (L) debug.print "* pi ="; What is the use of PI End Sub? There is basically no big purpose. However, it is proved to feasibility, and demo technology. What is the use of this technology, and it will talk about it when you need it. Postscript: This article, the "memory sharing and pointer" on the 9CBS sharing software, the topic of the pointer is here, the next article is ready to start writing a series of articles in the VB and COM, and I am ready to translate "VB Design" Patterns, this is a good book.