Excel VBA : 문자열을 프로 시저로 포인터로 전달하여 문자열을 추가하는 방법은 무엇입니까?

StackOverflow https://stackoverflow.com/questions/6045592

  •  15-11-2019
  •  | 
  •  

문제

절차에 문자열을 in / out 매개 변수로 전달하고 싶습니다.나는 이것이 VBA가 보통 작동하지 않지만 이것은 특별한 경우 때문입니다.이미 파일 형식 파서에 대한 기존 코드 생성기 도구 - 코드 생성 코드가 있습니다.그리고 나는이 코드를 해킹하고 싶지 않습니다.생성 된 구문은 텍스트를 사용하여 VBA로 쉽게 변환 할 수 있습니다. 큰 거래를 교체하십시오 (이미이 작업을 수행했습니다).그러나 기능에 대한 prodedures를 변경하는 것은 어렵습니다.

내가 알아 낸 것은 포인터를 전달하여 문자열을 확장 할 수있는 방법입니다.그러나 문자열에 문자를 어떻게 추가 할 수 있습니까?

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top