Excel VBA: Comment transmettez-vous une chaîne en tant que pointeur sur une procédure pour ajouter la chaîne?

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

  •  15-11-2019
  •  | 
  •  

Question

Je veux transmettre une chaîne comme paramètre d'entrée dans une procédure.Je sais que ce n'est pas comme VBA fonctionne habituellement mais c'est à cause d'un cas particulier.J'ai un outil de générateur de code déjà existant - générant du code pour un analyseur au format de fichier.Et je ne veux pas pirater ce code.La syntaxe générée peut être facilement converse en VBA à l'aide de texte remplacer ce problème sans grave (je l'ai déjà fait).Mais il est difficile de changer de production aux fonctions.

Qu'est-ce que j'ai compris est comment je peux prolonger une ficelle en passant son pointeur.Mais comment puis-je ajouter les caractères à la chaîne?

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

Était-ce utile?

La solution

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
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top