谁能帮我把这段VB6程序转成VB.NET程序,高分求!我通过VS自动转换后程序运行不下去

danky438 2007-06-29 05:32:36
原VB6代码:
1.这是执行部分的按钮事件
Private Sub eppWrite1_Click()
Dim mLen As Long
Dim buffer As arrRBuffer

mLen = HexToBcd(eppLen1.Text)

If (mLen <= 0 Or Len(eppData1.Text) = 0) Then
MsgBox "请输入写数据和长度!", vbExclamation, "CH341"
Exit Sub
End If

If (mLen > Len(eppData1.Text) \ 2) Then
mLen = Len(eppData1.Text) \ 2
End If

Call mStrtoVal(eppData1.Text, buffer, mLen) '将输入的十六进制格式字符数据转成数值数据
If (mOpen = True) Then
If (CH341EppWriteAddr(mIndex, buffer, mLen) = False) Then
MsgBox "EPP写数据块1失败!", vbExclamation, "CH341"
End If
eppLen1.Text = Hex(mLen)
Else
MsgBox "设备未打开!", vbExclamation, "CH341"
End If
End Sub
**********
这里面用到的APICH341EppWriteAddr(mIndex, buffer, mLen)的定义如下
Declare Function CH341EppReadAddr Lib "CH341DLL.DLL" (ByVal iIndex As Long, ByRef oBuffer As Any, ByRef ioLength As Long) As Boolean
2.程序的公共模块(注意,问题主要出现在下面定义的结构类型上)
Option Explicit

Type arrRBuffer
buf(mMAX_BUFFER_LENGTH - 1) As Byte
End Type

Public Function mCharToBcd(ByVal iChar As String) As Byte ' 输入的ASCII字符
Dim mBCD As Byte
If iChar >= "0" And iChar <= "9" Then
mBCD = iChar - "0"
ElseIf iChar >= "A" And iChar <= "F" Then
mBCD = Asc(iChar) - Asc("A") + &HA
ElseIf iChar >= "a" And iChar <= "f" Then
mBCD = Asc(iChar) - Asc("a") + &HA
Else
mBCD = &HFF
End If
mCharToBcd = mBCD
End Function

Sub mStrtoVal(str As String, ByRef strOut As arrRBuffer, strleng As Long)
Dim i, j As Long
Dim mLen As Long
Dim strRev(mMAX_BUFFER_LENGTH - 1) As Byte
mLen = strleng * 2
j = 0
For i = 0 To mLen - 1 Step 2
If (mCharToBcd(Mid(str, i + 1, 1)) = &HFF Or mCharToBcd(Mid(str, i + 2, 1)) = &HFF) Then
GoTo con
End If
' strRev(j) = mCharToBcd(Mid(str, i + 1, 1)) * 16 + mCharToBcd(Mid(str, i + 2, 1))
strRev(j) = mCharToBcd(Mid(str, i + 1, 1)) * 16 + mCharToBcd(Mid(str, i + 2, 1))
Debug.Print Hex(strRev(j))
j = j + 1
con: Next
j = 0
While (j < strleng)
strOut.buf(j) = strRev(j)
j = j + 1
Wend
End Sub

...全文
324 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
danky438 2007-07-13
  • 打赏
  • 举报
回复
偶最后把他给去掉了....
请问 wzd24(牧野)(衣带渐宽终不悔,为伊消得人憔悴) 兄:

我这还有个问题,是关于委托的,能不能再帮我解决下啊?因为我看到你回复过一个相似的帖子,但是梅看懂....
问题描述:
对“USB2ParallelPort!USB2ParallelPort.no+mPCH341_NOTIFY_ROUTINEDelegate::Invoke”类型的已垃圾回收委托进行了回调。这可能会导致应用程序崩溃、损坏和数据丢失。向非托管代码传递委托时,托管应用程序必须让这些委托保持活动状态,直到确信不会再次调用它们。


程序部分:
(1),公共模块变量里面定义的几个函数
Declare Function CH341SetDeviceNotify Lib "CH341DLL.DLL" (ByVal iIndex As Integer, ByRef iDeviceID As String, ByVal iNotifyRoutine As no.mPCH341_NOTIFY_ROUTINEDelegate) As Boolean

' '' 设备事件通知回调程序' 设备事件和当前状态(在下行定义): 0=设备拔出事件, 3=设备插入事件
Delegate Function mPCH341_NOTIFY_ROUTINEDelegate(ByVal iEventStatus As Integer, ByVal msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Declare Function PostMessage Lib "user32.dll" Alias "PostMessageA" (ByVal iEventStatus As Integer, ByVal msg As Integer, ByVal wParam As Integer, ByVal lVal As Integer) As Integer
Public Function mPCH341_NOTIFY_ROUTINE(ByVal iEventStatus As Integer, ByVal msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer

PostMessage(frmMain.CH341_NOTIFY_ROUTINE.Handle.ToInt32, WM_KEYUP, iEventStatus, 0) '将接收到的插拔事件值发到插拔处理程序中


End Function

(2)窗体程序里面一个按钮来模仿设备的插拔事件,然后调用公共模块里面的委托,通过委托来控制窗体的显示.
'》》》》》》》》》--这个按钮的事件用来模拟设备插拔的事件
Private Sub CH341_NOTIFY_ROUTINE_KeyUp(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyEventArgs) Handles CH341_NOTIFY_ROUTINE.KeyUp
Dim KeyCode As Short = eventArgs.KeyCode
Dim Shift As Short = eventArgs.KeyData \ &H10000 '设备插拔通知处理程序
Dim iEventStatus As Integer
iEventStatus = KeyCode '插拔事件
If (iEventStatus = CH341_DEVICE_ARRIVAL) Then ' 设备插入事件,已经插入
If (CH341OpenDevice(mIndex) = INVALID_HANDLE_VALUE) Then
MsgBox("打开设备失败!", MsgBoxResult.Ok, "CH341PAR")
mOpen = False
Else
mOpen = True '打开成功
End If
ElseIf (iEventStatus = CH341_DEVICE_REMOVE) Then ' 设备拔出事件,已经拔出
CH341CloseDevice((mIndex))
mOpen = False
End If
enablebtn((mOpen)) '设备打开,按钮可用,设备没打开,按钮禁用
End Sub
weipt 2007-06-30
  • 打赏
  • 举报
回复
转换器用不成的,手动转吧
wzd24 2007-06-30
  • 打赏
  • 举报
回复
Type arrRBuffer
buf(mMAX_BUFFER_LENGTH - 1) As Byte
End Type

------------------------------------------
VB.NET 下面:

Structure arrRBuffer
<MarshalAs(UnmanagedType.ByValArray,SizeConst=mMAX_BUFFER_LENGTH - 1)>_
Dim buf() as Byte;
End Structure
danky438 2007-06-29
  • 打赏
  • 举报
回复
我转换后的程序运行报错。。。。。高分求

16,555

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧