serial number extracting ?-) 
Author Message
 serial number extracting ?-)

I would like to 'clip' an programm to an serialnumber of a (eg. cdrom) .
how do you get the 'number' in MSVB 5.0????

I know it's possible in DOS, but how to make a link OrSomethingLikeThat.

Thank you
Joris Janssen a starting programmer (15, the netherlands) (Hans is my

Sat, 04 Aug 2001 03:00:00 GMT  
 serial number extracting ?-)
Use this call:

Private Sub Form_Click()
  MsgBox DriveSerialNumber(1)
End Sub

Paste the following code into a module.  There's some extra stuff in here so
you could slim this code down some to just get the serial number.

Option Explicit

Private Enum DriveInfoTypes
  ditDriveLabel = 0
  ditDriveSerialNbr = 1
  ditDriveMaxComponentLength = 2
  ditDriveFlags = 3
  ditDriveFileSystemType = 4
  ditDriveType = 5
End Enum

Private Declare Function GetLogicalDriveStrings _
                Lib "kernel32" _
                Alias "GetLogicalDriveStringsA" _
                (ByVal nBufferLength As Long, _
                  ByVal lpBuffer As String) As Long

Private Declare Function GetVolumeInformation _
                Lib "kernel32" _
                Alias "GetVolumeInformationA" _
                (ByVal lpRootPathName As String, _
                  ByVal lpVolumeNameBuffer As String, _
                  ByVal nVolumeNameSize As Long, _
                  lpVolumeSerialNumber As Long, _
                  lpMaximumComponentLength As Long, _
                  lpFileSystemFlags As Long, _
                  ByVal lpFileSystemNameBuffer As String, _
                  ByVal nFileSystemNameSize As Long) As Long

Private Declare Function GetDriveType _
                Lib "kernel32" _
                Alias "GetDriveTypeA" _
                (ByVal nDrive As String) As Long

Public Function DriveSerialNumber(ByVal LogicalDriveNbr As Integer) As
  ' return the drive serial number of the given logical
  ' drive (0-based, 0 is probably the floppy)
  Dim varTmp As Variant

  varTmp = GetDriveInfo(LogicalDriveNbr, ditDriveSerialNbr)

  If Not IsNull(varTmp) And varTmp <> "" Then
    DriveSerialNumber = varTmp
  End If
End Function

Private Function GetDriveInfo(ByVal LogicalDriveNbr As Integer, _
                              ByVal eType As DriveInfoTypes) As Variant
  Dim strVolLabelBuffer As String
  Dim strFileSystemBuffer As String
  Dim lngVolumeSerialNumber As Long
  Dim lngMaximumComponentLength As Long
  Dim lngFileSystemFlags As Long
  Dim strDrive As String
  Dim fOK As Boolean

  ' Get the drive letter for the logical drive number
  strDrive = LogicalDrives(LogicalDriveNbr)

  ' Prepare the buffers
  strVolLabelBuffer = Space(255)
  strFileSystemBuffer = Space(255)

  ' Call the API to get what we want
  fOK = CBool(GetVolumeInformation(strDrive, _
              strVolLabelBuffer, Len(strVolLabelBuffer), _
              lngVolumeSerialNumber, _
              lngMaximumComponentLength, _
              lngFileSystemFlags, _
              strFileSystemBuffer, _

  ' Check to see the  API returned a valid return flag
  If fOK Then

    ' Return the requested value
    Select Case eType

      Case ditDriveLabel
        GetDriveInfo = TrimNulls(strVolLabelBuffer)

      Case ditDriveSerialNbr
        GetDriveInfo = Hex(lngVolumeSerialNumber)

      Case ditDriveMaxComponentLength
        GetDriveInfo = lngMaximumComponentLength

      Case ditDriveFlags
        GetDriveInfo = lngFileSystemFlags

      Case ditDriveFileSystemType
        GetDriveInfo = TrimNulls(strFileSystemBuffer)

      Case ditDriveType
        ' Do a different call
        GetDriveInfo = GetDriveType(CStr(strDrive))

    End Select

    ' Invalid return value from the API
    GetDriveInfo = ""
  End If
End Function

Private Function LogicalDrives(ByVal LogicalDriveNbr As Integer) As String
  Dim strTmpBuf As String
  Dim lngBufBytes As Long
  Dim intPos As Integer
  Dim intPos2 As Integer
  Dim strDrive As String
  Dim intCounter As Integer
  Dim strTmp As String

  intCounter = 0

  ' Create the buffer to pass to the API
  strTmpBuf = Space(255)

  ' Get all strings and the count
  lngBufBytes = GetLogicalDriveStrings(Len(strTmpBuf), strTmpBuf)

  ' Get the first drive letter position by looking for the
  ' null character
  intPos2 = 1
  intPos = InStr(intPos2, strTmpBuf, vbNullChar)

  ' Walk through the string to find our drive
  Do Until intPos = 0 Or intPos > lngBufBytes

    ' Get the drive letter from the buffer
    strDrive = Mid$(strTmpBuf, intPos2, intPos - intPos2)

    ' Its the one we want
    If intCounter = LogicalDriveNbr Then
      strTmp = strDrive

'Exit Do
      Exit Do
    End If

    ' Move onto the next one
    intPos2 = intPos + 1
    intPos = InStr(intPos2, strTmpBuf, Chr(0))

    intCounter = intCounter + 1


  LogicalDrives = strTmp
End Function

Private Function TrimNulls(ByVal strIn As String) As String
  Dim intPos As Integer

  intPos = InStr(strIn, vbNullChar)

  If intPos = 0 Then
    ' No nulls in the string, just return it as is
    TrimNulls = strIn
    If intPos = 1 Then
      ' If the null character is at the first position, the
      ' entire string is a null string, so return a zero-length string
      TrimNulls = ""
      ' Not at the first position, so return the contents up
      ' to the occurrence of the null character
      TrimNulls = Left$(strIn, intPos - 1)
    End If
  End If
End Function

Earl Damron, MCSD, MVP (Visual Basic)

Common Controls Replacement Project Member


>I would like to 'clip' an programm to an serialnumber of a (eg. cdrom) .
>how do you get the 'number' in MSVB 5.0????

>I know it's possible in DOS, but how to make a link OrSomethingLikeThat.

>Thank you
>Joris Janssen a starting programmer (15, the netherlands) (Hans is my

Mon, 06 Aug 2001 03:00:00 GMT  
 [ 2 post ] 

 Relevant Pages 

1. Extracting the hard disk serial number

2. Network card serial number or Bios Serial Number

3. Q: Extract system Serial #

4. extracting every number in a document

5. Extracting phone numbers

6. extracting a Phone number

7. Extract only number from a text

8. Easy, but need help - Extracting version numbers of DLL

9. Extracting line numbers from WebBrowser control

10. Extract numbers from string?

11. best way to extract network number from IP address

12. Extracting a number from within a text field


Powered by phpBB® Forum Software