Home All Groups Group Topic Archive Search About
Author
31 May 2005 4:04 PM
PC
i have the following code:
--------------------------
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As
Any, pSrc As Any, ByVal ByteLen As Long)

Private Sub Command1_Click()
    Dim a As String
    Dim b As String
    a = "abcdefghijklm"
    b = "123456"
    CopyMemory ByVal VarPtr(b), ByVal VarPtr(a), 4
    'now both variables refer to the same string
    'as can be seen by:
    Print a, StrPtr(a), b, StrPtr(b)
    'what happens to the string "123456" ?
    'does this cause a memory leak ?
End Sub
--------------------------

Author
31 May 2005 4:13 PM
Bob Butler
Show quote Hide quote
"PC" <On***@pandora.be> wrote in message
news:OqOmvofZFHA.3648@TK2MSFTNGP14.phx.gbl
> i have the following code:
> --------------------------
> Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory"
> (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
>
> Private Sub Command1_Click()
>     Dim a As String
>     Dim b As String
>     a = "abcdefghijklm"
>     b = "123456"
>     CopyMemory ByVal VarPtr(b), ByVal VarPtr(a), 4
>     'now both variables refer to the same string
>     'as can be seen by:
>     Print a, StrPtr(a), b, StrPtr(b)
>     'what happens to the string "123456" ?
>     'does this cause a memory leak ?
> End Sub
> --------------------------

Yes

--
Reply to the group so all can participate
VB.Net: "Fool me once..."
Author
31 May 2005 8:09 PM
PC
ok thanks Bob
now i have this:
-------------------
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As
Any, pSrc As Any, ByVal ByteLen As Long)

Private Sub Command1_Click()
    Dim a As String
    Dim b As String
    Dim L As Long
    a = "abcdefghijklm"
    b = "123456"
    Print a, StrPtr(a), b, StrPtr(b)
    CopyMemory L, ByVal VarPtr(a), 4
    CopyMemory ByVal VarPtr(a), ByVal VarPtr(b), 4
    CopyMemory ByVal VarPtr(b), L, 4
    'a and b are now swapped as seen by:
    Print a, StrPtr(a), b, StrPtr(b)
    'would that cause a memory leak ?
End Sub
------------------
"
Author
31 May 2005 9:15 PM
Bob Butler
Show quote Hide quote
"PC" <On***@pandora.be> wrote in message
news:u0vavxhZFHA.612@TK2MSFTNGP12.phx.gbl
> ok thanks Bob
> now i have this:
> -------------------
> Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory"
> (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
>
> Private Sub Command1_Click()
>     Dim a As String
>     Dim b As String
>     Dim L As Long
>     a = "abcdefghijklm"
>     b = "123456"
>     Print a, StrPtr(a), b, StrPtr(b)
>     CopyMemory L, ByVal VarPtr(a), 4
>     CopyMemory ByVal VarPtr(a), ByVal VarPtr(b), 4
>     CopyMemory ByVal VarPtr(b), L, 4
>     'a and b are now swapped as seen by:
>     Print a, StrPtr(a), b, StrPtr(b)
>     'would that cause a memory leak ?
> End Sub

I don't think so... what are you really trying to accomplish?

--
Reply to the group so all can participate
VB.Net: "Fool me once..."
Author
1 Jun 2005 8:18 AM
PC
>>what are you really trying to accomplish?<<
nothing special really...just playing
(right now i am trying to come to grips with savearray's)
but it comes to my mind that it could be used as a fast swap routine
and i just think of something:
suppose i do something like:
-----------------
Swap2Strs a, b

Sub Swap2Strs(s1 As String, s2 As String)
'do something
End Sub
-----------------
then i suppose varptr(a) and varptr(b) get put on the stack
now if there were a way to swap them on the stack
would a and b then not be swapped just by leaving Sub Swap2Strs ?
any idea's ?
Author
1 Jun 2005 9:22 AM
Donald Lessau
"PC" <On***@pandora.be> schrieb im Newsbeitrag
news:%237bxTJoZFHA.580@TK2MSFTNGP15.phx.gbl...
> Swap2Strs a, b
> ...

See http://www.devx.com/vb2themax/Tip/18645

Don
Author
1 Jun 2005 9:57 AM
PC
thanks for the link Don
but the idea is just to not do that
the idea is getting my greedy hands directly on the stack
maybe something like putting some machine codes in a byte array
and somehow executing it (CallWindowProc it ?)
something like: (pseudo assembler)
put what is in stackpointer + somenumber in eax
put what is in stackpointer + somenumber+4 in stackpointer + somenumber
put what is in eax in stackpointer + somenumber
if only i knew how

Show quoteHide quote
"Donald Lessau" <don.s***@xbeat.snip.net> wrote in message
news:d7juvm$c2k$1@newsreader3.netcologne.de...
> "PC" <On***@pandora.be> schrieb im Newsbeitrag
> news:%237bxTJoZFHA.580@TK2MSFTNGP15.phx.gbl...
> > Swap2Strs a, b
> > ...
>
> See http://www.devx.com/vb2themax/Tip/18645
>
> Don
>
Author
1 Jun 2005 10:28 AM
Donald Lessau
"PC" <On***@pandora.be> schrieb im Newsbeitrag
news:ufZKZApZFHA.1404@TK2MSFTNGP09.phx.gbl...
> ...
> something like: (pseudo assembler)
> put what is in stackpointer + somenumber in eax
> put what is in stackpointer + somenumber+4 in stackpointer + somenumber
> put what is in eax in stackpointer + somenumber
> if only i knew how

You can inject Assembly Snippets into VB code, you might get some ideas
here:
http://www.xbeat.net/vbspeed/c_ShiftLeft.htm#ShiftLeft04