I don't think about it: high efficiency string pointer

zhaozj2021-02-17  44

My article is visible: http://www.9cbs.net/author/adambear

I can't think of five: high-efficiency string pointer keywords: VB, HCAK, string pointer, BSTR, efficiency, memory sharing difficulty: Intermediate or advanced reference article: 1, July 2000 VBPJ Black Belt column article "Modify a varialbe's Pointer "author: Bill McCarthy2, April 1998 VBPJ Black Belt column" Play VB's Strings "author: Francesco Balena

Introduction: This is the last article for the VB pointer with memory sharing. It should be discussed separately. On the problem of memory sharing, I am particularly concerned about the sharing of strings, because in the source code released more than a month, the CshareStr class provided by Bruce McKinney in "Hardcore VB" is used, which implements the memory of the string. shared. But Bruce has no breakthrough, the processing of the string is still a big movement of CopyMemory, especially the annoying ANSI / DBCS and Unicode conversion. I have said that it is very efficient in Readme, and should be implemented in a variant or byte array to avoid conversion. Later, I thought I can do with StrPtr and implemented a string memory sharing that can be converted with a DLL sharing section in the VC. However, in VC, I still need to use sysallocstring to establish a BSTR that VB can use. This is not what I want, I want something like the CString in the VC, as long as the strings are big enough, do not reassign the memory, but also like the CCOMBSTR class in the VC to ATTACH to a specific BSTR. I know what to do, after reading two articles on Bill McCarthy and Francesco Balena on VBPJ. Bill uses the modified SafeArray description structure to implement the memory sharing of the array, and FRANCESCO is discussed in depth of the string pointer. But Bill and Francesco's things have not achieved the string class I want. The method knows that the implementation is not difficult, so I decide myself to pack a such thing.

Text: Two shortcomings in the string type String in VB: First, its assignment is controlled by VB runtime, we cannot assign it in the specified memory; second, any of the assignment of the string To make memory reassignment. To achieve efficient and flexible string, we must overcome these two shortcomings. For the first question, you can implement the BSTR descriptor pointer in the String variable can be implemented; for the second problem, you can use the MID statement (note that statements instead of functions). Not detailed, directly look at the following:

Option explicit '****************************************************************************************************************************************************************************************************************************************************** ********* 'clsBSTR.cls' author: Xiong ID: AdamBear 2002 March 18 'http://www.9cbs.net/Author/AdamBear' you are free to use this class module, However, please keep this statement '***************************************************** ************ Private Declare Sub CopyMemory LIB "Kernel32" Alias ​​"RTLMOVEMEMORY" (Destination as any, source as any, byval length as long) Do not assign a value directly (can be used by MID statements) , Set it to the public only to improve efficiency. Public SSTRING AS STRING 'BSTR Descriptor Pointer Private PSTR AS Long' BSTR Address Private NMAXLEN As Long 'BSTR Maximum Byte Number' Let this string points to the specific address public sub attach (AddR as long, Optional Nlen as long) PSTR = AddR 'Modify the BSTR descriptor pointer to point to Addr CopyMemory Byval Varptr (SSTRING), ADDR, 4 if ismissing (nlen) THEN EXIT SUB' setting maximum string byte NMAXLEN = Nlen End Sub 'to restore this string original BSTR description Public Sub Detach () CopyMemory Byval Varptr (SSTRING), 0 &, 4 End Sub 'Let this string points to the source string public sub attachstr (sstr as string) Attach StrPtr (SSTR), lenb (sstr) end sub' data is Default Properties Public Property Let Data (Sval As String) DIM C as long c = lenb (sval) 'exceeds the maximum number of strings, throw errors.

IF c> nmaxlen dam 3000, _ "cstring :: let data", "overflow" 'write string length CopyMemory Byval (PSTR - 4), C, 4' Writing string MID (SString, 1) = SVAL End Property 'can read strings through public variables, efficiency, higher PUBLIC Property Get Data () AS String Data = SSTRING End Property

Private sub class_terminate () Call Detach End Sub Usage, assuming that we have received a 4K byte readable memory address baseaddr: Dim Sshare as new clsbstr: Dim Sshare as new clsbstr: Dim Sshare as new clsbstr: Dim Sshare as new clsbstr The first 4 bytes were used for BSTR saving string bytes SSHARE.ATTACH (BaseAddr 4, 4096-4) 'below "Test" will write directly to the baseaddr 4 byte sshare = "test" DIM Y as string 'read strings You can use the SSTRING attribute or default attribute y = sshare.sstring' to use AttachStr method ATTACH to a string. 'Must first debach sshare.detach sshare.attachstr (y) sshare = "hahaha" debug.print y' Once AttachStr to the string y, the modification of SSHARE is equivalent to the modification of Y. 'And the modifications to Y later can only use the MID statement MID (Y, 1) = "xxxxx", which cannot be directly assigned, so that VB will release the original Y (also referred to as SSHARE) memory,' Reassign Y. This will be wrong when accessing SSHARE. 'Y = "test" I don't talk about the detailed principle of this class here, you can refer to the two articles I said earlier. There are several places that need to be taken using this class. 1. You can use the sstring attribute to read and faster when reading the string. There are two ways to read SSHARE, one is to read with the default attribute DATA, one is to read directly with the sstring property. Use the sstring attribute to not reassign the memory, it is much better. 2, do not assign the SSTRING directly, you should use the default DATA property to assign a value. The reason why the SSTRING attribute is exposed is for efficiency and convenience. We can modify it with a MID statement, but don't use "=" directly. 3, pay attention to the second parameter of Attach, indicate the maximum number of bytes of the string, do not let it exceed the already assigned memory. 4. After attaching the string object attach to a string (such as the Y) above, the string Y cannot be re-assigned, and it cannot be delivered to the process of reassigning it. Wow, so many questions that need attention are not convenient to use it. Indeed, consider whether it is necessary before it. Because it is also expensive because it is established. So there is also a question that needs attention: 5, its main application is to place the string in the specified memory. Although it can also make several strings in the same process to reach the purpose of sharing, but if they are just two or three very strings, they are slower.

Afterwards: Array pointers and string pointers We have already talked, there is no need for a pointer to ordinary numerical type variables, but it is related to a useful technology, the next article talks. This article and the code of the next article, and the code that implemented shared memory with this class, I will release the 9CBS sharing software, the name is "memory sharing and pointer".

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

New Post(0)