1

プロシージャに文字列を in/out パラメータとして渡したい。これは、VBA が通常動作するようなものではないことはわかっていますが、これは特殊なケースによるものです。ファイル形式パーサー用のコードを生成する、既存のコード生成ツールがあります。そして、私はこのコードをハッキングしたくありません。生成された構文は、テキストを使用して vba に簡単に変換できますが、これは大したことではありません (私は既にこれを行っています)。しかし、手順を関数に変更するのは困難です。

私が理解したのは、ポインターを渡すことで文字列を拡張する方法です。しかし、文字列に文字を追加するにはどうすればよいですか?

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
4

1 に答える 1

0

VBA はこれをネイティブに実行できます

Sub AppendString(byref str as string, i_what As String)
    str = str & i_what
end sub

テストする

Sub Test()
    Dim s As String

    s = "Hello"

    AppendString s, " World"
    Debug.Print s
End Sub
于 2011-05-18T20:10:13.677 に答える