Re: Outdated help (feat. Access '97 and VB4)
- From: "Terry Kreft" <terry.kreft@xxxxxxxxx>
- Date: Sat, 17 Dec 2005 12:52:38 -0000
At y we find the block of memory which holds the string this is then
terminated by a null character (a pair of bytes holding 00)
With an empty string the y-4 to y-1 memory holds 00 00, indicating the that
the string has zero length, therefore when we look at y we find no data
terminated with a pair of byte holding 00.
We can modify some code I posted earlier to get what is in memory from y-4
right through to the terminating character
Option Explicit
Private Declare Sub CopyMemory _
Lib "kernel32.dll" Alias "RtlMoveMemory" ( _
ByRef Destination As Any, _
ByRef Source As Any, _
ByVal Length As Long _
)
Sub TestString3()
Dim b As String
Dim c() As Byte
Dim lngSize As Long
Dim lngStrLocn As Long
Dim lngSizeLocn As Long
Dim intCount As Integer
b = "a"
lngStrLocn = StrPtr(b)
lngSizeLocn = lngStrLocn - 4
Call CopyMemory(lngSize, ByVal lngSizeLocn, 4)
ReDim c(1 To lngSize + 6)
Call CopyMemory(c(1), ByVal lngSizeLocn, lngSize + 6)
Debug.Print "Variable b = '" & b & "'"
Debug.Print "================="
Debug.Print "String", "Size", "String"
Debug.Print "Location", "Location", "Length"
Debug.Print "---------", "---------", "-------"
Debug.Print lngStrLocn, lngSizeLocn, lngSize
For intCount = 1 To lngSize + 6
Debug.Print "y" & intCount - 5 & " ="; c(intCount)
Next
b = ""
lngStrLocn = StrPtr(b)
lngSizeLocn = lngStrLocn - 4
Call CopyMemory(lngSize, ByVal lngSizeLocn, 4)
ReDim c(1 To lngSize + 6)
Call CopyMemory(c(1), ByVal lngSizeLocn, lngSize + 6)
Debug.Print
Debug.Print "Variable b = '" & b & "'"
Debug.Print "================="
Debug.Print "String", "Size", "String"
Debug.Print "Location", "Location", "Length"
Debug.Print "---------", "---------", "-------"
Debug.Print lngStrLocn, lngSizeLocn, lngSize
For intCount = 1 To lngSize + 6
Debug.Print "y" & intCount - 5 & " ="; c(intCount)
Next
End Sub
This returns
Variable b = 'a'
=================
String Size String
Location Location Length
--------- --------- -------
2247068 2247064 2
y-4 = 2
y-3 = 0
y-2 = 0
y-1 = 0
y0 = 97
y1 = 0
y2 = 0
y3 = 0
Variable b = ''
=================
String Size String
Location Location Length
--------- --------- -------
2422604 2422600 0
y-4 = 0
y-3 = 0
y-2 = 0
y-1 = 0
y0 = 0
y1 = 0
>From the results we can see that for an empty string
1) the total of y-4 to y-1 is 0
2) that at y(0) we have just the two terminating bytes (which are set to
0 in both cases)
With the string containing the letter 'a' we can see that
1) the total for y-4 to y-1 is 2 (that is the string occupies 2 bytes of
memory)
2) in the 2 bytes of memory occupied by the string there is a 97 0 (97
is the ascii for a)
3) there are 2 terminating bytes both containing a 0
The following from "Chapter 6: Strings" from "Win32 API Programming with
Visual Basic" by Steven Roman may help:=
" ...
There are several important things to note about the BSTR data type.
The BSTR is the actual pointer variable. It has size 32 bits, like all
pointers, and points to a Unicode character array. Thus, a Unicode character
array and a BSTR are not the same thing. It is correct to refer to a BSTR as
a string (or VB string) but, unfortunately, the Unicode character array is
also often called a string! Hence, we will not refer to a BSTR simply as a
string--we will refer to it by its unequivocal name--BSTR.
The Unicode character array that is pointed to by a BSTR must be preceded by
a 4-byte length field and terminated by a single null 2-byte character (ANSI
= 0).
There may be additional null characters anywhere within the Unicode
character array, so we cannot rely on a null character to signal the end of
the character array. This is why the length field is vital.
Again, the pointer points to the beginning of the character array, not to
the 4-byte length field that precedes the array. As we will see, this is
critical to interpreting a BSTR as a VC++-style string.
The length field contains the number of bytes (not the number of characters)
in the character array, excluding the terminating null bytes. Since the
array is Unicode, the character count is one-half the byte count.
.... "
--
Terry Kreft
"Lyle Fairfield" <lylefairfield@xxxxxxx> wrote in message
news:1134818451.536031.183060@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> Stephen
>
> I was asking about this:
>
> "Essentially string variables in VB (post VB3) are what is called a
> basic
> string (BSTR). The variable actually contains a pointer to a memory
> location where the data for the string is held, this string is null
> terminated. The four bytes just before the memory location pointed to,
> holds data which tells us how big the block of memory used to hold the
> string is. So, in the extract from Bruce McKinney, when he talks about
> the
> empty BSTR and he refers to the single null character to the right of
> the
> address he is talking about the Null character which terminates the
> empty
> string, when he talks about the long integer containing zero he is
> talking
> about the memory location which tells us how long the string is (i.e.
> it has
> zero length).
>
> If we look in the MSDN we get confirmation that this is the case. Now
> I'm
> using the MSDN from October 2001 so I can't give
> online references, but in the article "Strings the OLE Way" by Bruce
> McKinney April 18, 1996 we read:-
>
> "Rule 8: A null pointer is the same as an empty string to a BSTR.
> Experienced C++ programmers will find this concept startling because it
> certainly isn't true of normal C++ strings. An empty BSTR is a pointer
> to a
> zero-length string. It has a single null character to the right of the
> address being pointed to, and a long integer containing zero to the
> left. A
> null BSTR is a null pointer pointing to nothing. There can't be any
> characters to the right of nothing, and there can't be any length to
> the
> left of nothing. Nevertheless, a null pointer is considered to have a
> length
> of zero ..."
> "
> And my question is about "" which is or has a varptr, say x. x points
> to the strptr of "", say y. At y - 4 we find 4 bytes which show the
> length of "", so we find 4 bytes that represent zero.
> I have always assumed we would find zip at y. If I am interpreting
> Terry and his quotes correctly, the accept interpretation is that we
> find a null character at y.
>
> I am asking, what would we find at y if there were not a null character
> at y, that is if "" were fully represented by the zero at y-4, and the
> contents of y were irrelevant.
>
.
- Follow-Ups:
- Re: Outdated help (feat. Access '97 and VB4)
- From: Lyle Fairfield
- Re: Outdated help (feat. Access '97 and VB4)
- References:
- Outdated help (feat. Access '97 and VB4)
- From: ZillionDollarSadist
- Re: Outdated help (feat. Access '97 and VB4)
- From: Danny J. Lesandrini
- Re: Outdated help (feat. Access '97 and VB4)
- From: David W. Fenton
- Re: Outdated help (feat. Access '97 and VB4)
- From: Danny J. Lesandrini
- Re: Outdated help (feat. Access '97 and VB4)
- From: David W. Fenton
- Re: Outdated help (feat. Access '97 and VB4)
- From: Lyle Fairfield
- Re: Outdated help (feat. Access '97 and VB4)
- From: david epsom dot com dot au
- Re: Outdated help (feat. Access '97 and VB4)
- From: Lyle Fairfield
- Re: Outdated help (feat. Access '97 and VB4)
- From: Terry Kreft
- Re: Outdated help (feat. Access '97 and VB4)
- From: Lyle Fairfield
- Re: Outdated help (feat. Access '97 and VB4)
- From: Terry Kreft
- Re: Outdated help (feat. Access '97 and VB4)
- From: Lyle Fairfield
- Re: Outdated help (feat. Access '97 and VB4)
- From: Stephen Lebans
- Re: Outdated help (feat. Access '97 and VB4)
- From: Lyle Fairfield
- Outdated help (feat. Access '97 and VB4)
- Prev by Date: Re: ftp from Access: problems
- Next by Date: mixing linked tables
- Previous by thread: Re: Outdated help (feat. Access '97 and VB4)
- Next by thread: Re: Outdated help (feat. Access '97 and VB4)
- Index(es):
Relevant Pages
|