Re: copymemory basic question
- From: mscir <mscir@xxxxxxxxx>
- Date: Fri, 26 Aug 2005 11:48:46 -0700
Tony Proctor wrote:
I hope not Sam. I'm implicitly saying that I've used CopyMemory to move strings (and string pointers), and it does work. Plus there is no character set translation involved. If it doesn't work for the OP then his declaration of CopyMemory must be wrong.
Tony Proctor
I was definitely doing it wrong... I originally thought it might be possible to reduce the number of steps by copying one string to another in one step, but nothing I tried worked (see code below).
I believe this explains why (two variables were pointing to the same string) :
http://www.developerfusion.co.uk/show/3367/5/
Although CopyMemory achieves great speeds in comparison to the good ol' For..Next loop, there are some datatypes you just can't include in the UDT (or the element type for the array) without special handling. These are variable-length strings, and COM object variables.
The String Datatype
The variable string datatype (... As String) occupies 4 bytes. These 4 bytes are filled with a pointer that points to the first character of the string in memory. Visual Basic handles automatically the allocation/deallocation of the memory occupied by the string characters. But, if you override the pointer by using CopyMemory, you will most likely get a Access Violation error when Visual Basic accesses the supposedly not-used pointer. In order to avoid this, you must get rid of any "residual" pointer as well as any memory allocated, if necessary (not necessary in the examples treated here). The following example shows how to override a string safely with CopyMemory. Note that you must do this cleanup for every single string variable you "free" for use with CopyMemory.
strString1 = "This string will get moved from strString1 to strString2." MsgBox "strString1:" & vbCrLf & vbCrLf & strString1, vbInformation CopyMemory strString2, strString1, 4 'Override with 0 the string pointer stored in strString1. 'If this is not done, when the variables strString1 and strString2 go 'out of scope, Visual Basic will attempt deallocation of the memory 'associated with the strings, assuming that no two variables can point 'to the same string, which is normally true if you don't hack the 'variable contents using CopyMemory. CopyMemory strString1, 0&, 4 MsgBox "From strString2:" & vbCrLf & vbCrLf & strString2, vbInformation
========================================= My original non-working code:
'module
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
'form
Private Sub Command1_Click()
Dim s1 As String, s2 As String
Dim pSrc As Long, pDest As Long, lpTmp As Long
s1 = "111111111"
s2 = "222222222222222222"
pSrc = StrPtr(s2)
pDest = StrPtr(s1)
Debug.Print "before:"
Debug.Print "s1 " & s1
Debug.Print "s2 " & s2
CopyMemory ByVal pDest, ByVal pSrc, 4
Debug.Print "after: "
Debug.Print "s1 " & s1
Debug.Print "s2 " & s2
End Sub'debug window before: s1 111111111 s2 222222222222222222 after: s1 221111111 s2 222222222222222222
Mike .
- References:
- copymemory basic question
- From: mscir
- Re: copymemory basic question
- From: Sam Hobbs
- Re: copymemory basic question
- From: Tony Proctor
- Re: copymemory basic question
- From: Sam Hobbs
- Re: copymemory basic question
- From: Tony Proctor
- copymemory basic question
- Prev by Date: Re: Connecting to the Internet
- Next by Date: CreateDirectory fails but GetLastError says succeeded
- Previous by thread: Re: copymemory basic question
- Next by thread: Re: copymemory basic question
- Index(es):
Relevant Pages
|
Loading