|
本帖最后由 leolee82 于 2024-4-30 16:37 编辑
可以以数组形式操作字符串,又不用复制字符串数据。
目前参数只能是string,试图支持Variant形式的字符串,StrPtr(s)是正确的,但得到的字符串长度都是3。
4月30日:
发现将Len(s)改Clng(Len(s))就可以将s改为Variant形式的字符串,可能是GetMem4将Len(s)视为了Variant,3正好是Variant中的vt的值(vbLong)
- Private Declare Function ArrPtr Lib "msvbvm60" Alias "VarPtr" (a() As Any) As Long
- Private Declare Function GetMem4 Lib "msvbvm60" (ByRef Source As Any, ByRef Dest As Any) As Long
- Private Declare Function SafeArrayCreateVector Lib "OleAut32" (Optional ByVal vt As VbVarType = vbInteger, Optional ByVal lLow As Long = 0, Optional ByVal lCount As Long = 0) As Integer()
- 's不能为CONST
- Function StringAsIntArray(s) As Integer()
- Dim pSa As Long
- StringAsIntArray = SafeArrayCreateVector
- GetMem4 ByVal ArrPtr(StringAsIntArray), pSa
- GetMem4 StrPtr(s), ByVal (pSa + 12&)
- GetMem4 Clng(Len(s)), ByVal (pSa + 16&)
- End Function
复制代码- Sub TestStringAsIntArray0()
- Dim s$
- s = "ABCD"
- Dim arr
- arr = StringAsIntArray(s)
- For i = LBound(arr) To UBound(arr)
- arr(i) = 65
- Next
- Debug.Print s
- End Sub
复制代码
|
评分
-
1
查看全部评分
-
|