vb6的string

是unicode编码的,就是一个字母都是 两个字节的。这样,我一个英文的string 转换成byte() 就会出现0

其实本来想写个 string和byte数组 互转的函数:

'有问题函数,请勿使用
Public Function string2bytes(ByVal str As String) As Byte()
    Dim mulBits() As Byte
    mulBits = str
    myDebug UBound(mulBits)
    string2bytes = mulBits
End Function

Public Function bytes2string(ByRef bytes() As Byte) As String
    Dim str As String
    str = bytes()
    bytes2string = str
End Function

string len为 8 的字符串,编程 bytes数组 就是 ubound为 15 (从0开始)了。

所以 上面的函数要小改一下。

首先:Imports System.Runtime.InteropServices 然后: Private Declare Function EbExecuteLine Lib "VBA6.DLL" (ByVal pStringToExec As Long, ByVal Unknownn1 As Long, ByVal Unknownn2 As Long, ByVal fCheckOnly As Long) As Long ' <DllImport("VBA6.DLL", EntryPoint:="EbExecuteLine", SetlastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> Public Shared Function EbExecuteLine(ByVal pStringToExec As Long, ByVal Unknownn1 As Long, ByVal Unknownn2 As Long, ByVal fCheckOnly As Long) As Long '<DllImport("D:\vba6.dll", CharSet:=CharSet.Ansi, SetlastError:=True)> Public Shared Function EbExecuteLine(ByVal pStringToExec As Long, ByVal Unknownn1 As Long, ByVal Unknownn2 As Long, ByVal fCheckOnly As Long) As Long 'End Function '<DllImport("vba6.dll", CharSet:=CharSet.Ansi, SetlastError:=True)> 'Public Shared Function EbExecuteLine(ByVal pStringToExec As Long, ByVal Unknownn1 As Long, ByVal Unknownn2 As Long, ByVal fCheckOnly As Long) As Long 'End Function '定义StrPtr函数,实现获取字符串地址功能 Private Function StrPtr(ByVal e As Object) As Int32 Dim GC As System.Runtime.InteropServices.GCHandle = System.Runtime.InteropServices.GCHandle.Alloc(e, System.Runtime.InteropServices.GCHandleType.Pinned) Dim GC2 As Int32 = GC.AddrOfPinnedObject.ToInt32 GC.Free() Return GC2 End Function Function ExecuteLine(ByVal sCode As String, Optional ByVal fCheckOnly As Boolean = True) As Boolean ExecuteLine = EbExecuteLine(StrPtr(sCode), 0, 0, 0) End Function Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim dm() As String Dim i As Long dm = Split(TextBox.Text, ";") For i = 0 To UBound(dm) If Len(dm(i)) <> 0 Then Dim Result As Boolean Result = ExecuteLine(dm(i)) If Result = False Then Debug.Print(i + 1 & vbTab & "行执行错误") Else Debug.Print(i + 1 & vbTab & "行代码错误") End If Next End Sub
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值