Get USB serial number

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pentahari
    New Member
    • Dec 2007
    • 60

    #1

    Get USB serial number

    It is very URGENT.

    How to get the USB serial no using VB6.
  • pentahari
    New Member
    • Dec 2007
    • 60

    #2
    Solution :

    Code:
    Function GetUSBSerialNo(ByVal DriveLetter As String)
        Dim PnPID As String
        PnPID = USBSerialNo(DriveLetter)
        
        If Not Trim(PnPID) = "" Then
            GetUSBSerialNo = formatSerialNo(PnPID)
        Else
            GetUSBSerialNo = ""
       End If
       
    End Function
    
    
    Function USBSerialNo(ByVal DriveLetter As String)
    
    Dim objFSO
    Dim objFolder
    Dim Directory
    Const OverwriteExisting = True
    
    Dim SerialNo As String
    
    Dim ComputerName
    ComputerName = "."
    Dim wmiServices, wmiDiskDrives, wmiDiskDrive, query, wmiDiskPartitions, wmiDiskPartition, wmiLogicalDisks, wmiLogicalDisk
    
    Set wmiServices = GetObject( _
        "winmgmts:{impersonationLevel=Impersonate}!//" _
        & ComputerName)
    
    Set wmiDiskDrives = wmiServices.ExecQuery("SELECT Caption, DeviceID,PNPDeviceID FROM Win32_DiskDrive")
    
    For Each wmiDiskDrive In wmiDiskDrives
            
        SerialNo = wmiDiskDrive.PNPDeviceID '1
            
     query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" _
            & wmiDiskDrive.deviceid & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition"
        Set wmiDiskPartitions = wmiServices.ExecQuery(query)
    
        For Each wmiDiskPartition In wmiDiskPartitions
            Set wmiLogicalDisks = wmiServices.ExecQuery _
                ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" _
                 & wmiDiskPartition.deviceid & "'} WHERE AssocClass = Win32_LogicalDiskToPartition")
    
            For Each wmiLogicalDisk In wmiLogicalDisks
    
            If (wmiLogicalDisk.deviceid = DriveLetter) And (wmiLogicalDisk.DriveType = 2) Then '2
                USBSerialNo = SerialNo
                Exit Function
            End If
    
            Next
        Next
    Next
    End Function
    
    
    Function formatSerialNo(ByVal PnPID As String)
        Dim arrSerialNo
        Dim arrSerialNo1
        arrSerialNo = Split(PnPID, "\")
        Dim i
        arrSerialNo1 = Split(arrSerialNo(UBound(arrSerialNo)), "&")
    
        If UBound(arrSerialNo1) > 0 Then
            formatSerialNo = arrSerialNo1(UBound(arrSerialNo1) - 1)
        Else
            formatSerialNo = arrSerialNo1(UBound(arrSerialNo1))
        End If
    
    End Function

    Comment

    • 9815402440
      New Member
      • Oct 2007
      • 180

      #3
      hi
      i also tried the code but it is giving an automation error on following line

      Set wmiServices = GetObject("winm gmts:{impersona tionLevel=Imper sonate}!//" & ComputerName)

      can you please tell if there are any references that must be added to run this code.

      thanks
      regards
      manpreet singh dhillon hoshiarpur

      Comment

      • pentahari
        New Member
        • Dec 2007
        • 60

        #4
        Originally posted by 9815402440
        hi
        i also tried the code but it is giving an automation error on following line

        Set wmiServices = GetObject("winm gmts:{impersona tionLevel=Imper sonate}!//" & ComputerName)

        can you please tell if there are any references that must be added to run this code.

        thanks
        regards
        manpreet singh dhillon hoshiarpur
        what OS are you use?

        Easy to get the USB serial no in .Net
        Please follow the below URL.

        http://home.hot.rr.com/graye/Article...ialNumbers.htm

        Comment

        • 9815402440
          New Member
          • Oct 2007
          • 180

          #5
          hi
          thanx for the reply
          i am using vb6 and operating system is WinXP
          i tried the link suggested by you. but that is for .net.
          please help why the specified line gives run time error
          regards
          manpreet singh dhillon hoshiarpur

          Comment

          • pentahari
            New Member
            • Dec 2007
            • 60

            #6
            Originally posted by 9815402440
            hi
            thanx for the reply
            i am using vb6 and operating system is WinXP
            i tried the link suggested by you. but that is for .net.
            please help why the specified line gives run time error
            regards
            manpreet singh dhillon hoshiarpur
            I don't know, i have given references:
            Visual basic for applications
            Visual basic runtime objects and procedures
            Visual basic objects and procedures
            OLE Automation
            Microsoft ActiveX Data objects 2.0 Library

            my friend say:
            Have you checked whether or not WMI is permitted on the machine?

            Comment

            Working...