Excel VBA:如何将字符串作为指针传递给过程以附加字符串?

发布于 2024-11-08 12:16:03 字数 1345 浏览 0 评论 0原文

我想将字符串作为输入/输出参数传递给过程。我知道这不像 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

漫漫岁月 2024-11-15 12:16:03

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文