From: NadCixelsyd on
I just learned a bit about VARPTR and STRPTR. So I wrote some code
that doesn't make sense:

Private Type SR_REC
SR_LONG As Long
SR_STRING As String * 16
End Type

Private Sub Form_Load()
Dim LLL As Long, SSS As String
Dim SR4(3) As SR_REC
Debug.Print "A>"; VarPtr(SSS); " "; StrPtr(SSS); " "; VarPtr(I); " ";
StrPtr(LLL)
Debug.Print "B>"; VarPtr(SR4(0)); " "; VarPtr(SR4(1)); " ";
VarPtr(SR4(2)); " "; VarPtr(SR4(3))
Debug.Print "C>"; VarPtr(SR4(1).SR_LONG); "
";VarPtr(SR4(1).SR_STRING); " "; VarPtr(SR4(2).SR_LONG)
Stop

What gets printed is:

A> 1308840 0 1308844 1977924
B> 1897768 1897804 1897840 1897876
C> 1897804 1308808 1897840

Here are my questions ...

A> OK, I understand the zero because SSS was never initialized. Why
does the VB6 compiler even allow me to do STRPTR(LLL), when LLL is not
a string? (LLL was not initialized either)

B> Each element of the array SR4 is 36 bytes long. This explains why
the VARPTR of each element is 36 bytes offset from the previous
element. Which leads to

C> If VarPtr(SR4(1).SR_LONG) is 1897804, it stands to reason that
VarPtr(SR4(1).SR_STRING) would be four bytes later at 1897808, but it
comes up with a totally different number for this fixed length string.
From: ralph on
On Thu, 12 Aug 2010 10:31:45 -0700 (PDT), NadCixelsyd
<nadcixelsyd(a)aol.com> wrote:

>I just learned a bit about VARPTR and STRPTR. So I wrote some code
>that doesn't make sense:
>
>Private Type SR_REC
> SR_LONG As Long
> SR_STRING As String * 16
>End Type
>
>Private Sub Form_Load()
>Dim LLL As Long, SSS As String
>Dim SR4(3) As SR_REC
>Debug.Print "A>"; VarPtr(SSS); " "; StrPtr(SSS); " "; VarPtr(I); " ";
>StrPtr(LLL)
>Debug.Print "B>"; VarPtr(SR4(0)); " "; VarPtr(SR4(1)); " ";
>VarPtr(SR4(2)); " "; VarPtr(SR4(3))
>Debug.Print "C>"; VarPtr(SR4(1).SR_LONG); "
>";VarPtr(SR4(1).SR_STRING); " "; VarPtr(SR4(2).SR_LONG)
>Stop
>
>What gets printed is:
>
>A> 1308840 0 1308844 1977924
>B> 1897768 1897804 1897840 1897876
>C> 1897804 1308808 1897840
>
>Here are my questions ...
>
>A> OK, I understand the zero because SSS was never initialized. Why
>does the VB6 compiler even allow me to do STRPTR(LLL), when LLL is not
>a string? (LLL was not initialized either)
>
>B> Each element of the array SR4 is 36 bytes long. This explains why
>the VARPTR of each element is 36 bytes offset from the previous
>element. Which leads to
>
>C> If VarPtr(SR4(1).SR_LONG) is 1897804, it stands to reason that
>VarPtr(SR4(1).SR_STRING) would be four bytes later at 1897808, but it
>comes up with a totally different number for this fixed length string.

For question A the result is because these functions are designed to
never fail - ie, you always get something. Exactly what that something
is in this case you'll have to go peek. (I can't test at the moment.)

For question C the result is because for 'fixed strings' VB is doing
you a favor. <g> You wanted a String so VB creates a temporary dynamic
string and returned a pointer to it.

This can cause additional confusion since multiple calls may actually
report a different location each time. However, you can sure 'that'
pointer is valid, for whatever you want to do with it.

-ralph
For a good overview:
http://vb.mvps.org/tips/varptr.asp
For all the nasty details:
Get a copy of "Dan Appleman's Win32 API Puzzle Book and Tutorial for
VB Programmers".
From: Larry Serflaten on

"NadCixelsyd" <nadcixelsyd(a)aol.com> wrote
> I just learned a bit about VARPTR and STRPTR. So I wrote some code
> that doesn't make sense:

That's not my first choice when celebrating some new knowledge, but to
each their own....

One has to wonder what you'll do after learning about line numbers and
Erl.

<g>
LFS


From: Kevin Provance on

"Larry Serflaten" <serflaten(a)gmail.com> wrote in message
news:i41k7f$222$1(a)news.eternal-september.org...
:
: "NadCixelsyd" <nadcixelsyd(a)aol.com> wrote
: > I just learned a bit about VARPTR and STRPTR. So I wrote some code
: > that doesn't make sense:
:
: That's not my first choice when celebrating some new knowledge, but to
: each their own....
:
: One has to wonder what you'll do after learning about line numbers and
: Erl.

LOL. That was a good laugh. ;-)

From: Henning on

"NadCixelsyd" <nadcixelsyd(a)aol.com> skrev i meddelandet
news:fe3acba8-9aa9-400d-bdb0-8bcd22ee2a2a(a)d8g2000yqf.googlegroups.com...
>I just learned a bit about VARPTR and STRPTR. So I wrote some code
> that doesn't make sense:
>
> Private Type SR_REC
> SR_LONG As Long
> SR_STRING As String * 16
> End Type
>
> Private Sub Form_Load()
> Dim LLL As Long, SSS As String
> Dim SR4(3) As SR_REC
> Debug.Print "A>"; VarPtr(SSS); " "; StrPtr(SSS); " "; VarPtr(I); " ";
> StrPtr(LLL)
> Debug.Print "B>"; VarPtr(SR4(0)); " "; VarPtr(SR4(1)); " ";
> VarPtr(SR4(2)); " "; VarPtr(SR4(3))
> Debug.Print "C>"; VarPtr(SR4(1).SR_LONG); "
> ";VarPtr(SR4(1).SR_STRING); " "; VarPtr(SR4(2).SR_LONG)
> Stop
>
> What gets printed is:
>
> A> 1308840 0 1308844 1977924
> B> 1897768 1897804 1897840 1897876
> C> 1897804 1308808 1897840
>
> Here are my questions ...
>
> A> OK, I understand the zero because SSS was never initialized. Why
> does the VB6 compiler even allow me to do STRPTR(LLL), when LLL is not
> a string? (LLL was not initialized either)
>
> B> Each element of the array SR4 is 36 bytes long. This explains why
> the VARPTR of each element is 36 bytes offset from the previous
> element. Which leads to
>
> C> If VarPtr(SR4(1).SR_LONG) is 1897804, it stands to reason that
> VarPtr(SR4(1).SR_STRING) would be four bytes later at 1897808, but it
> comes up with a totally different number for this fixed length string.

Simple answer, don't worry about them numbers. You will almost for sure
never use them directly, and VB knows what they are.

/Henning


 | 
Pages: 1
Prev: Array question
Next: Formating Hex Values