I've written a previous tutorial on memory hacking with CopyMemory, but it wasn't entirely accurate or thorough. This crash course will give you the totally accurate and thorough situation on VB strings and string arrays.
A reminder:
StrPtr function returns the pointer to the string associated with a string variable.
VarPtr function returns the pointer to the actual variable.
Strings
Memory Commentary:
A string variable stores a 4 byte pointer to the first byte of the actual string. However, before the string is a 4 byte length descripter OF the string in number of bytes. In VB strings are UNICODE and are terminated by a 2-byte null character. However, make sure to only pass strings as ByVal VarPtr for the source - cause VB has a tendancy to pass the UNICODE strings as ASCII which totally screws things up.
'The returned address is identical to StrPtr(MyString)
CopyMemory Address, ByVal VarPtr(MyString), 4
'Store the length of MyString
CopyMemory Length, ByVal StrPtr(MyString) - 4, 4
'Copy the string from MyString to the Buffer
CopyMemory Buffer, ByVal StrPtr(MyString), Length
String Arrays
Memory Commentary:
A array variable stores a 4 byte pointer to the first element of the actual array. String arrays are actually an array of 4 byte pointers which point to the actual string in each element. Before the actual array is a 4 byte length descripter of the array. So like, if an array has 3 elements, the length will be 12. Each individual element being a pointer to the string associated with that element. That string holds true to strings pointed to by normal string vars. It is preceded by a 4 byte length of the actual string.
'Store the memory location of the array
CopyMemory Address, ByVal VarPtr(MyStringArray(0)), 4
'The 4 bytes before the array is the length of the array in bytes
CopyMemory Length, ByVal Address - 4, 4
'Retrieve the address to the string in the first element
CopyMemory Address, ByVal StrPtr(MyStringArray(0)), 4
'Retrieve the length of the string in the first element
CopyMemory Length, ByVal StrPtr(MyStringArray(0)) - 4, 4
'Copy the string in the first element to a string variable
CopyMemory MyString, ByVal StrPtr(MyStringArray(0)), Length