Results 1 to 5 of 5

Thread: Getting Disk Label from DVD-Drive

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2002
    Posts
    269

    Getting Disk Label from DVD-Drive

    Hi,

    I have been using this code for some time to get the name of the currenty disk in the DVD-Drive.

    VB Code:
    1. Dim VI As VolumeInfo
    2. 'getVolumeInfo
    3. Private Type VolumeInfo
    4.         Serial As Long
    5.         VolumeName As String
    6.         FileSystemName As String
    7. End Type
    8.  
    9.  
    10. Private Function GetVolumeInfo(Drive As String) As VolumeInfo
    11. Dim tmp As VolumeInfo
    12. tmp.VolumeName = Space(255)
    13. tmp.FileSystemName = Space(255)
    14.     GetVolumeInformation Drive & ":\", tmp.VolumeName, 255, tmp.Serial, 0, 0, tmp.FileSystemName, 255
    15.  
    16. tmp.VolumeName = Trim(tmp.VolumeName)
    17. tmp.FileSystemName = Trim(tmp.FileSystemName)
    18. GetVolumeInfo = tmp
    19. End Function

    Then to put the actual disk label into a text box I have...

    VB Code:
    1. vi2 = GetVolumeInfo(letter)
    2. Text23 = vi2.VolumeName

    It works and I put the above two lines into a timer in order to get the text box updated. However no matter how fast I set the timer, like 100ms, 200ms, 500ms, there is always a massive delay.

    Eg. Say I have AMERICAN PIE in at the minute. My text box shows it fine. Now I eject the tray and the text box will stay saying AMERICAN PIE for about 8 seconds. The whole program seems to freeze up for those few seconds.

    Has anyone got any better code for getting the disk name. Looking in "MY COMPUTER", windows updates the disk name in a split second.

    Any help would be greatly appreciated. I tried using windows own code to get notified when a disk was inserted but I couldn't get it to work if my program wasn't focused.

    Thanks!!!!!!
    KT
    Last edited by King_Tweaker; Jun 10th, 2003 at 01:59 PM.

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2002
    Posts
    269
    Anyone??

    Can it be that visual basic is just so slow that it can't get the disk name in 8 seconds?

    I will see if I can find a C++ example of this using the same getvolumeinformation.

    Please guys, can anyone get this to work in any sort of fast time at all, even less than 1 seconds would be good.

    KT

  3. #3
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    This should do it :

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function GetDriveType Lib "Kernel32" Alias "GetDriveTypeA" _
    4. (ByVal nDrive As String) As Long
    5.  
    6. Private Declare Function GetVolumeInformation Lib "Kernel32" Alias "GetVolumeInformationA" _
    7. (ByVal lpRootPathName As String, ByVal lpVolumeNameBuffer As String, ByVal nVolumeNameSize As Long, _
    8. lpVolumeSerialNumber As Long, lpMaximumComponentLength As Long, lpFileSystemFlags As Long, _
    9. ByVal lpFileSystemNameBuffer As String, ByVal nFileSystemNameSize As Long) As Long
    10.  
    11. Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" _
    12. (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, _
    13. ByVal hwndCallback As Long) As Long
    14.  
    15.  
    16. Private Sub Form_Load()
    17.  
    18.     Dim intLetterCounter As Integer
    19.     Dim intElementCounter As Integer
    20.     Dim lngSerial As Long
    21.     Dim strVolumeName As String
    22.     Dim lngRetVal As Long
    23.    
    24.     intElementCounter = 0
    25.    
    26.     For intLetterCounter = 65 To 90
    27.    
    28.         If (GetDriveType(Chr(intLetterCounter) & ":\") = 5) Then
    29.            
    30.             strVolumeName = String(255, Chr(0))
    31.            
    32.             GetVolumeInformation Chr(intLetterCounter) & ":\", strVolumeName, 255, lngSerial, 0, 0, 0, 255
    33.            
    34.             MsgBox strVolumeName
    35.            
    36.         End If
    37.        
    38.     Next
    39.  
    40. End Sub


    Has someone helped you? Then you can Rate their helpful post.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2002
    Posts
    269
    Thanks but it does the exact same thing. See I have a text box and when the text box is updated with a new disk name it does some stuff like disable autorun etc.

    But with this code and the one I was using earlier, it takes for ever for the text box to update itself.


    Here is how I modified your code to simply test it out on my drive "F:\"

    Again it works fine but when I eject the disk it takes a fair few seconds for the text box to get updated. Is it just text boxes or something. I will try again without using text boxes and just pure variables.

    I tried all timer intervals like 100, 500, 1000, doesnt make a diff?

    KT

    VB Code:
    1. Private Sub Timer1_Timer()
    2.     Dim intLetterCounter As Integer
    3.     Dim intElementCounter As Integer
    4.     Dim lngSerial As Long
    5.     Dim strVolumeName As String
    6.     Dim lngRetVal As Long
    7.    
    8.     intElementCounter = 0
    9.    
    10.  
    11.             strVolumeName = String(255, Chr(0))
    12.            
    13.             GetVolumeInformation "F:\", strVolumeName, 255, lngSerial, 0, 0, 0, 255
    14.            
    15.             Text1 = strVolumeName
    16.  
    17. End Sub

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2002
    Posts
    269
    OK

    I have narrowed it down to one thing!!

    The visual basic timers.

    I have tried all types of techniques, from using the fso to detect when a disk was in, using mci stuff, using getvolumeinformation and all had this massive delay. It usually takes around 7 seconds for basic to detect there is no disk in the drive.

    BUT simply putting the code in a button, and clicking that button 1 second after the drive is ejected correctly tells me "There is no disk in the drive"

    What is it with the vb timers. Its like as if the timers totally stall and there is this massive backlog of timercode that gets done after the disk is ejected. I need to come up with some way to make the timer check if a disk is in but not stall.

    Does anyone have a technique for doing this. I was thinking of using the windows time somehow to make the timer recognize if its in some stall period.

    Please guys, I need you experts to help me out.

    Try the code out for yourselves if you like. Here is a simple set of code if you want to see for yourself what I am talking about.

    All you need is this code, a timer and a textbox. You will see yourself the code works fine but no matter what interval you give the timer, upon ejecting a disk it takes forever to update the textbox. If you then put the timer code into a text box and disable the timer, you will see that manually clicking the button at any time works fine!

    VB Code:
    1. Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, _
    2. ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
    3.  
    4. Private Function IsCDInDrive(ByVal strCDDriveLetter As String) As Boolean
    5.    Dim strStatus As String * 128
    6.    'open the device
    7.    mciSendString "open " & strCDDriveLetter & ": type cdaudio alias cddrive wait", vbNullString, 0, 0
    8.    'see if there's something in the drive
    9.    mciSendString "status cddrive media present", strStatus, Len(strStatus), 0
    10.    'close the device
    11.    mciSendString "close cddrive", vbNullString, 0, 0
    12.    'the string will contain "true" or "false" which can be easily converted
    13.    IsCDInDrive = CBool(Left$(strStatus, InStr(strStatus, vbNullChar) - 1))
    14. End Function
    15.  
    16. Private Sub Timer1_Timer()
    17. Text1 = IsCDInDrive("F")
    18. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width