copymemory拷贝为何不行

hotheartlewis 2006-01-23 09:52:09
目的是想把"12345"传给字符串str1,但为何不行?
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)

Private Sub Command1_Click()
'Debug.Print "1" & Chr(192) & Chr(192)
Dim bt(5)
Dim str1 As String
For i = 0 To UBound(bt)
bt(i) = 48 + i
Next
Call CopyMemory(ByVal StrPtr(str1), ByVal bt(0), 5)
Debug.Print str1
End Sub
...全文
110 点赞 收藏 6
写回复
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Tiger_Zhao 2006-01-23
复制需要有足够的空间,StrPtr(str1) = NULL,怎么可能赋值成功!
其实不需要 API,直接 str1 = bt 或 str1 = StrConv(bt, vbUnicode)
回复
hotheartlewis 2006-01-23
返回DLL调用约定错误
回复
of123 2006-01-23
Dim bt(5)
Dim str1 As String
For i = 0 To UBound(bt) - 1
bt(i) = 48 + i
Next
bt(5) = 0
Call CopyMemory(ByVal StrPtr(str1), ByVal bt(0), 6)
Debug.Print str1
回复
zou19820704 2006-01-23
Call CopyMemory(ByVal StrPtr(str1), ByVal bt(0), 6)这样呢?
回复
zyl910 2006-01-23
刚才忘了分配空间


方法1:

Private Sub Command1_Click()
'Debug.Print "1" & Chr(192) & Chr(192)
Dim bt(0 to 4) as Integer
Dim str1 As String
For i = 0 To UBound(bt)
bt(i) = 48 + i
Next
str1 = String(5, 0)
Call CopyMemory(ByVal StrPtr(str1), bt(0), 5)
Debug.Print str1
End Sub
回复
zyl910 2006-01-23
bt的数据类型不对

方法1:

Private Sub Command1_Click()
'Debug.Print "1" & Chr(192) & Chr(192)
Dim bt(0 to 4) as Integer
Dim str1 As String
For i = 0 To UBound(bt)
bt(i) = 48 + i
Next
Call CopyMemory(ByVal StrPtr(str1), bt(0), 5)
Debug.Print str1
End Sub



方法2:

Private Sub Command1_Click()
'Debug.Print "1" & Chr(192) & Chr(192)
Dim bt(0 to 4) as Byte
Dim str1 As String
For i = 0 To UBound(bt)
bt(i) = 48 + i
Next
str1 = StrConv(bt, vbUnicode)
Debug.Print str1
End Sub
回复
发动态
发帖子
VB基础类
创建于2007-09-28

7451

社区成员

VB 基础类
申请成为版主
社区公告
暂无公告