Re: copymemory basic question



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
.



Relevant Pages

  • Re: "Mastering C Pointers"....
    ... A pointer is a kind of variable that can "point to" some object. ... has a type (pointer to int), and a value of some kind. ... You may know that you can access these integers by using array notation ... The function will take one argument, a string, and will return the length ...
    (comp.lang.c)
  • Re: pesky Pointers !!
    ... > and the function takes it as a reference instead of a copy. ... function may access the string passed directly, ... > *px dereferences the pointer to get the value ... If pTest is a pointer-to-string, *pTest is the string it points to ...
    (alt.comp.lang.learn.c-cpp)
  • Re: strtok ( ) help
    ... > splitCommandssomehow modifying the pointer, but I HAVE to call that ... Here's an idea of how to use the strtok() function. ... don't mind trashing the contents of a string s, ... will give you a loop that extracts the tokens one at a time from s. ...
    (comp.lang.c)
  • Re: new IL: C (sort of...).
    ... C doesn't need a string type... ... variant of PL/1 which was very Pascal-ish. ... - C does implement an array declaration. ... effectively converted into a pointer that can be used with the offset ...
    (comp.lang.misc)
  • Re: copy a string into a 2d array of chars
    ... This split function should allocate a 2D array of chars ... >focus the program the string is not actually split. ... later) is an array of char containing the original contents of the ... The i-th pointer will contain the starting address of the ...
    (comp.lang.c)

Loading