thongkorn โพสต์ 2018-5-2 13:48:50

[VB6] การอ่านค่า Serial Number ของ USB Flash Drive ด้วย WMI

http://www.g2gnet.com/webboard/images/vb6/USBFlashDriveSN.png

สำหรับกระบวนการขั้นตอนวิธีคิด แอดมินได้อธิบายไปแล้วในส่วนของโค้ด VB.Net ซึ่งก็ใช้หลักการเดียวกันนั่นแหละครับ (คลิ๊กที่นี่เพื่อไปดูโค้ด VB.Net) สำหรับโค้ด VB6 แอดมินได้แก้ไขเพิ่มเติม โดยแนะนำการใช้งานคำสั่งพื้นฐานบางอย่าง ซึ่งเราจำเป็นต้องใช้กันบ่อยมาก คือ คำสั่งภายใน (ฟังค์ชั่น) In String หรือ InStr และ InStrRev โดยที่
InStr คือ การค้นหาข้อความที่ต้องการจากทางซ้ายไปทางขวา หากเจอตัวแรกก็จะคืน ค่าตำแหน่งที่มันอยู่ กลับไป หากไม่เจอก็คืนค่ากลับ 0 (นั่นคือฟังค์ชั่นที่มีการคืนค่าเลขจำนวนเต็ม)

InstrRev ก็จะทำกลับด้านกัน คือไล่จากทางขวามาทางซ้ายแทน

เช่น ...
i = InStr("ABCD", "B") ... ค่า i = 2 เพราะ B อยู่ตำแหน่งที่ 2 จากทางซ้ายมือ
i = InStrRev("ABCD", "C") ... ค่า i = 3 เพราะ C อยู่ตำแหน่งที่ 3 จากทางซ้ายมือ (เอาตำแหน่งที่เจอ)
i = InStrRev("ABCDC", "C") ... ค่า i = 5 เพราะเจอ C ตัวแรก จากทางขวามาซ้าย ซึ่งอยู่ตำแหน่งที่ 5


มาดูโค้ดฉบับเต็ม ...
' / --------------------------------------------------------------------------------
' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
' / eMail : thongkorn@hotmail.com
' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
' / Facebook: http://www.facebook.com/g2gnet (For Thailand only)
' / Facebook: http://www.facebook.com/CommonIndy (Worldwide)
' / UsbFlashSerialNoWMI:Read Serial Number of USB Flash Drive with WMI
' / WMI = Windows Management Instrumentation
' / Microsoft Visual Basic 6.0 Service Pack 6
' /
' / This is open source code under @CopyLeft by Thongkorn Tubtimkrob.
' / You can modify and/or distribute without to inform the developer.
' / --------------------------------------------------------------------------------
Option Explicit

Private Sub Form_Load()
    Me.Move (Screen.Width - Width) \ 2, (Screen.Height - Height) \ 2
    txtUsbFlash.Text = ""
    txtUsbFlash.SelText = "USB FLASH SERIAL NUMBER" & vbCrLf & GetSerialWMI
End Sub

Public Function GetSerialWMI() As String
    Dim strComputer As String
    Dim objWMIService As Object
    Dim ColItems As Object
    Dim ObjItem As Object
    Dim strSerial As String
    Dim strTemp As String
    Dim strArr() As String
    Dim MaxLen As Byte '<-- หาค่าความยาวสูงสุดของชุดข้อมูล ซึ่งจะเป็น Serial Number
    Dim idx As Byte '<-- เป็น Index ของ Array ซึ่งจะเก็บค่าคำตอบ Serial Number
   
    '/ (dot) is Local machine
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
    '/ DiskDrive Class
    Set ColItems = objWMIService.ExecQuery("SELECT * FROM Win32_DiskDrive", , 48)
    '/ Detect USB Flash All Drive
    For Each ObjItem In ColItems
      If ObjItem.interfacetype = "USB" Then
            '// เช่น "USBSTOR\DISK&VEN_&PROD_&REV_1.00\7&15FF80F3&0&__&0"
            strTemp = ObjItem.pnpdeviceid
            '// InStrRev ค้นหาเครื่องหมาย \ จากทางขวามาซ้าย หากพบให้ +1 เพื่อเริ่มต้นการอ่านค่าจากฟังค์ชั่น Mid String
            strTemp = Mid(strTemp, InStrRev(strTemp, "\") + 1)
            '// ผลที่ได้ "7&15FF80F3&0&__&0"
            '// จึงต้องแยกเครื่องหมาย & ออกจากชุดข้อมูล
            strArr = Split(strTemp, "&")
            '// ผลที่ได้
            ' strArr(0) = "7"               '<-- LBound
            ' strArr(1) = "15FF80F3"
            ' strArr(2) = "0"
            ' strArr(3) = "__"
            ' strArr(4) = "0"               '<-- UBound
            '/ หาความยาวสูงสุดของชุดข้อมูลจาก Array
            '/ ลูปตามจำนวน Index ของ Array โดยเริ่มจาก LBound = 0 ไปจนสิ้นสุดที่ UBound
            Dim Count As Byte
            MaxLen = 0: idx = 0 '<-- เคลียร์ค่าเดิม กรณีหากมี USB Flash Drive หลายตัว มิเช่นนั้นจะเกิด Bug (Logical Error)
            For Count = LBound(strArr) To UBound(strArr)
                '// เปรียบเทียบค่าความยาวของข้อมูลในแต่ละ Array
                If MaxLen < Len(strArr(Count)) Then
                  '// เก็บค่า Index ไว้เป็นคำตอบ (ตัวอย่างคือ Index = 1)
                  idx = Count
                  '// นำค่าความยาวที่มากกว่ามาเก็บไว้ ก่อนที่วนกลับไปเปรียบเทียบค่าใหม่
                  MaxLen = Len(strArr(Count))
                End If
            Next
            '// เก็บค่าคำตอบ
            strSerial = strSerial & strArr(idx) & vbCrLf
      End If
    Next
    '/ Return value
    GetSerialWMI = strSerial
End Function

Private Sub Form_Resize()
    If WindowState <> vbMinimized Then
      txtUsbFlash.Move 0, 0, ScaleWidth, ScaleHeight
    End If
End Sub
ดาวน์โหลดโค้ดฉบับเต็ม VB6 (SP6) ได้ที่นี่ ...

MrDen โพสต์ 2018-5-3 09:37:38

ขอบคุณมากครับผมขอให้สุขภาพแข็งแรง เงินทองไหลมาเทมา ครับผม.

mozza โพสต์ 2018-5-3 22:00:50

wowww สุดยอดคับ
หน้า: [1]
ดูในรูปแบบกติ: [VB6] การอ่านค่า Serial Number ของ USB Flash Drive ด้วย WMI