以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  怎樣轉換這段VB.net代碼為FT代碼  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=50928)

--  作者:jaegea
--  发布时间:2014/5/15 16:02:00
--  怎樣轉換這段VB.net代碼為FT代碼

Option Explicit On
Imports Microsoft.Win32
Public Class SearchforUSBCom
    \'Search for the virtual serial port created by usbclient.
    Public Function SearchforCom(ByRef sCom As String) As Boolean
        Dim sComValue As String
        Dim sTmpara As String
        Dim myReg As RegistryKey
        myReg = Registry.LocalMachine.OpenSubKey("HARDWARE\\\\DEVICEMAP\\\\SERIALCOMM")

        Dim sComName() As String
        sComName = myReg.GetValueNames() \'strings array composed of the key name holded by the subkey "SERIALCOMM"
        Dim i As Integer
        For i = 0 To sComName.Length - 1
            sComValue = myReg.GetValue(sComName(i)).ToString() \'obtain the key value of the corresponding key name
            If sComValue = "" Then
                Continue For
            End If

            sCom = ""
            Dim j As Integer
            If sComName(i) = "\\Device\\USBSER000" Then \'find the virtual serial port created by usbclient
                For j = 0 To 10
                    sTmpara = ""
                    Dim myReg2 As RegistryKey
                    myReg2 = Registry.LocalMachine.OpenSubKey("SYSTEM\\\\CurrentControlSet\\\\Enum\\\\USB\\\\VID_1B55&PID_B400\\\\" & j.ToString() & "\\\\Device Parameters")

                    If myReg2 IsNot Nothing Then
                        sTmpara = myReg2.GetValue("PortName").ToString()

                        If sComValue = sTmpara Then
                            sCom = sTmpara
                            Return True
                        End If
                    End If
                Next
            End If
        Next
        Return False
    End Function
End Class


--  作者:有点甜
--  发布时间:2014/5/15 16:06:00
--  

 如下。全局代码那里输入

 

Public Class SearchforUSBCom
    \'Search for the virtual serial port created by usbclient.
    Public Function SearchforCom(ByRef sCom As String) As Boolean
        Dim sComValue As String
        Dim sTmpara As String
        Dim myReg As Microsoft.Win32.RegistryKey
        myReg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("HARDWARE\\\\DEVICEMAP\\\\SERIALCOMM")

        Dim sComName() As String
        sComName = myReg.GetValueNames() \'strings array composed of the key name holded by the subkey "SERIALCOMM"
        Dim i As Integer
        For i = 0 To sComName.Length - 1
            sComValue = myReg.GetValue(sComName(i)).ToString() \'obtain the key value of the corresponding key name
            If sComValue = "" Then
                Continue For
            End If

            sCom = ""
            Dim j As Integer
            If sComName(i) = "\\Device\\USBSER000" Then \'find the virtual serial port created by usbclient
                For j = 0 To 10
                    sTmpara = ""
                    Dim myReg2 As Microsoft.Win32.RegistryKey
                    myReg2 = Registry.LocalMachine.OpenSubKey("SYSTEM\\\\CurrentControlSet\\\\Enum\\\\USB\\\\VID_1B55&PID_B400\\\\" & j.ToString() & "\\\\Device Parameters")

                    If myReg2 IsNot Nothing Then
                        sTmpara = myReg2.GetValue("PortName").ToString()

                        If sComValue = sTmpara Then
                            sCom = sTmpara
                            Return True
                        End If
                    End If
                Next
            End If
        Next
        Return False
    End Function
End Class


--  作者:Bin
--  发布时间:2014/5/15 16:06:00
--  
直接复制到全局代码即可
--  作者:有点甜
--  发布时间:2014/5/15 16:07:00
--  

 全局代码 http://www.foxtable.com/help/topics/1842.htm

 


--  作者:jaegea
--  发布时间:2014/5/15 16:07:00
--  
謝謝
--  作者:jaegea
--  发布时间:2014/5/15 16:13:00
--  

把代碼改成這樣做成函數是不是一樣的效果?

 

Dim sCom As String = Args(0)
Dim sComValue As String
Dim sTmpara As String
Dim myReg As Microsoft.Win32.RegistryKey
myReg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("HARDWARE\\\\DEVICEMAP\\\\SERIALCOMM")

Dim sComName() As String
sComName = myReg.GetValueNames() \'strings array composed of the key name holded by the subkey "SERIALCOMM"
Dim i As Integer
For i = 0 To sComName.Length - 1
    sComValue = myReg.GetValue(sComName(i)).ToString() \'obtain the key value of the corresponding key name
    If sComValue = "" Then
        Continue For
    End If
   
    sCom = ""
    Dim j As Integer
    If sComName(i) = "\\Device\\USBSER000" Then \'find the virtual serial port created by usbclient
        For j = 0 To 10
            sTmpara = ""
            Dim myReg2 As Microsoft.Win32.RegistryKey
            myReg2 = Registry.LocalMachine.OpenSubKey("SYSTEM\\\\CurrentControlSet\\\\Enum\\\\USB\\\\VID_1B55&PID_B400\\\\" & j.ToString() & "\\\\Device Parameters")
           
            If myReg2 IsNot Nothing Then
                sTmpara = myReg2.GetValue("PortName").ToString()
               
                If sComValue = sTmpara Then
                    sCom = sTmpara
                    Return True
                End If
            End If
        Next
    End If
Next
Return False


--  作者:有点甜
--  发布时间:2014/5/15 16:14:00
--  
是的,亲
--  作者:Bin
--  发布时间:2014/5/15 16:14:00
--  
YOU一点点不一样.  ByRef sCom As String  这个变量在全局代码的这种方式下是可以带值出去的.
--  作者:jaegea
--  发布时间:2014/5/15 16:14:00
--  
非常感謝,又學到了一招,哈哈