I want to pass a String as in/out parameter to a procedure. I know this is not like VBA usually works but this is because of a special case. I have an already existing code generator tool - generating code for a file-format parser. And I do not want to hack this code. The generated syntax can be easily convertet to vba using text replace this no big deal (I have done this already). But It is difficult to change prodedures to functions.
What I have figured out is how I can extend a String by passing its pointer. But How can I append the characters to the string?
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" (Destination As Any, Source As Any, _
ByVal length As Long)
Private Declare Function lstrlenA Lib "kernel32" _
(ByVal lpString As Long) As Long
Private Declare Function lstrlenW Lib _
"kernel32" (ByVal lpString As Long) As Long
Sub AppendString(i_pt As Long, i_what As String)
Dim strLentgh As Long ' variable to hold the length of string
Dim ptrLengthField As Long ' pointer to 4-byte length field
' get the length of the string
ptrLengthField = i_pt - 4 ' length field is 4 bytes behind
CopyMemory strLentgh, ByVal ptrLengthField, 4
' extend the String length
strLentgh = strLentgh + (Len(i_what) * 2)
CopyMemory ByVal ptrLengthField, strLentgh&, 4
' How to apped the string?
' CopyMemory ByVal i_pt, ????
End Sub
Sub test2()
Dim str As String
Dim sPtr As Long
str = "hello"
sPtr = strPtr(str)
Debug.Print Len(str)
Call AppendString(sPtr, " there")
Debug.Print Len(str)
Debug.Print str
End Sub
VBA can do this natively
Sub AppendString(byref str as string, i_what As String)
str = str & i_what
end sub
To test
Sub Test()
Dim s As String
s = "Hello"
AppendString s, " World"
Debug.Print s
End Sub