Results 1 to 24 of 24

Thread: Detect if CD-ROM in drive?

  1. #1

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205

    Detect if CD-ROM in drive?

    I need the simplest, way of determining if a CD I have burned is in the CD-ROM drive when someone tries to run my application from their hard disk.

    Spare me the 'they'll copy it anyway' stuff...

    The reason I need this is because I have 165MB of audio files (.wavs - no time to create anything smaller, so spare me that lecture too... ), and I want a warning to appear when someone tries to run the program without the CD to say something like:

    "CD Not in drive, do you wish to proceed with audio deactivated?"

    No = Quit program.

    Simple.

    Cheers,
    RJ

    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  2. #2
    Hyperactive Member Blinky Bill's Avatar
    Join Date
    Mar 2002
    Location
    Happily munching on the greenery in your garden
    Posts
    349
    You can use the fso to try to connect to the C: drive and check its volume name. The fso gives an error if a cd isn't in the drive, so you can then give the message. If the cd does exist, you can check the volume name or something else if you wish and give the message that way.

    Hope this helps
    We don't know what's wrong. . . So the best bet might be to remove something surgically.

  3. #3

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    I'm not going to know which drive letter to check though. Could be D:, E:, F:, or anything else, I'm not sure.
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  4. #4
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    well.... when they first run the program ask them for their cd-drive. or maybe enum all drives and check each one.
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  5. #5

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    Thewse people are computer noobies to the max, or something...

    Some of them put the mouse on the computer screen, cause they just don't know how to use PCs, so can't ask them for the CD drive.

    I'd rather find something neater than checking every letter of the alphabet.
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  6. #6
    Hyperactive Member Blinky Bill's Avatar
    Join Date
    Mar 2002
    Location
    Happily munching on the greenery in your garden
    Posts
    349
    Do you install the program at all? Because if you do, they have to put your cd in the cd-rom right? So, use App.Path to determine the letter of the drive being used at install time, and write a text file containing that letter and read that text file every time the app starts to check for your cd.
    We don't know what's wrong. . . So the best bet might be to remove something surgically.

  7. #7
    Frenzied Member macai's Avatar
    Join Date
    Jul 2001
    Location
    Napanoch NY
    Posts
    1,228
    Put this code in a module:
    VB Code:
    1. Public Declare Function mciSendString Lib "winmm.dll" _
    2. Alias "mciSendStringA" (ByVal lpstrCommand As String, _
    3. ByVal lpstrReturnString As String, ByVal uReturnLength _
    4. As Long, ByVal hwndCallback As Long) As Long
    5.  
    6. Public Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long
    7. Const DRIVE_CDROM = 5
    8.  
    9. Public Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
    10.  
    11.  
    12.  
    13. Public Sub CloseDoor(ByVal DriveLetter As String)
    14.     Dim AliasName As String
    15.     AliasName = "Drive" & DriveLetter
    16.     mciSendString "Open " & DriveLetter & ": Alias " & AliasName _
    17.     & " Type CDAudio", 0, 0, 0
    18.     mciSendString "Set " & AliasName & " Door Closed", 0, 0, 0
    19. End Sub
    20.  
    21. Public Sub OpenDoor(ByVal DriveLetter As String)
    22.     Dim AliasName As String
    23.     AliasName = "Drive" & DriveLetter
    24.     mciSendString "Open " & DriveLetter & ": Alias " & AliasName _
    25.     & " Type CDAudio", 0, 0, 0
    26.     mciSendString "Set " & AliasName & " Door Open", 0, 0, 0
    27. End Sub
    28. Public Function CDLETTERS() As Variant()
    29.  'Nucleus
    30.  'Returns an array containing all cd rom drives Or Null of none found
    31.  Dim tmp&, CDsCount&, ret&
    32.  Dim tmpStr$, Drives$
    33.  Dim CDDrives(): ReDim CDDrives(0)
    34.  
    35.  'init Drives To 255 spaces
    36.  Drives = Space(255)
    37.  'get drives, Drives var will look like
    38.  'A:\<NULL>C:\<NULL>D:\<NULL>E:\<NULL> (note: letter of drive is every 4th char)
    39.  'ret is the length of the String containing all the drives
    40.  ret = GetLogicalDriveStrings(Len(Drives), Drives)
    41.  For tmp = 1 To ret Step 4
    42.     'extract drive from the String like "A:\"
    43.     tmpStr = Mid(Drives, tmp, 3)
    44.     'check If drive is a CD
    45.     If GetDriveType(tmpStr) = DRIVE_CDROM Then
    46.         CDsCount = CDsCount + 1
    47.         If CDDrives(0) <> "" Then ReDim Preserve CDDrives(UBound(CDDrives) + 1)
    48.         CDDrives(UBound(CDDrives)) = Left(tmpStr, 1)
    49.     End If
    50.  Next tmp
    51.  If CDDrives(0) = "" Then
    52.   CDLETTERS = Null
    53.  Else
    54.   CDLETTERS = CDDrives
    55.  End If
    56. End Function
    The CDLETTERS function returns an array of drive letters that ARE
    ALL CD ROMS! So just go through all of them!
    Luke

  8. #8

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    Thanks all. I have a working solution.

    Macai - your code seems a bit much. I used the API function GetDriveType to do this. I ended up just looping through the A-Z possible drive letters.

    Is all the rest necessary?

    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  9. #9
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    G'Day RJ,

    Just a thought; 'My Computer' can see the CDROM drive, so isn't
    there some API function that can access that system info?
    (instead of looping thru).



    Bruce.

  10. #10

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    It probably just does the same thing as this. It's very fast really. And the GetDriveType function can return about 8 different drive 'types', so it's a pretty nifty function I guess.

    It's all good though, this solution is fine.
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  11. #11
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    The simpler way to grab cd rom drives is to use the GetDriveType Api call, you're right on that one (bit less coding):
    http://www.vbforums.com/showthread.p...&highlight=rom

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  12. #12

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    That's almost exactly what I have done.
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  13. #13
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Now the second part, this is the shortest way I could think of to determine if a cd is in the drive:
    VB Code:
    1. [size="1"]Private Declare Function GetDiskFreeSpace Lib "kernel32" Alias _
    2.   "GetDiskFreeSpaceA" (ByVal lpRootPathName As String, _
    3.   lpSectorsPerCluster As Long, lpBytesPerSector As Long, _
    4.   lpNumberOfFreeClusters As Long, lpTtoalNumberOfClusters As Long) As Long
    5.  
    6. Private Sub Form_Load()
    7.     Dim lngTotalClusters As Long
    8.     If (GetDiskFreeSpace("D:\", ByVal 0&, ByVal 0&, ByVal 0&, _
    9.     lngTotalClusters) = 0) Then
    10.         MsgBox "No CD in drive"
    11.     End If
    12. End Sub[/size]
    Last edited by alex_read; Oct 17th, 2002 at 03:35 AM.

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  14. #14
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Finally, if you needed to check that it was your CD in the drive, you'd need to use the GetVolumeLabel Api call as mentined above somehow.

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  15. #15
    Frenzied Member macai's Avatar
    Join Date
    Jul 2001
    Location
    Napanoch NY
    Posts
    1,228
    Originally posted by rjlohan
    Thanks all. I have a working solution.

    Macai - your code seems a bit much. I used the API function GetDriveType to do this. I ended up just looping through the A-Z possible drive letters.

    Is all the rest necessary?

    RJ, i just had a module with this code in it. I didn't look at the
    code itself, but all i know is it works. In other words, its a module
    that i copied and pasted into the post. Sorry if i wasted your time,
    man.
    Luke

  16. #16
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    It wasn't a waste of time at all - it works! Just that he wanted a cut down version. If you know the answer & can help out on a post, then always reply

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  17. #17

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    Wasn't a waste of time.

    Just thought I'd suggest that your code could be simplified. Could help you too.


    Alex - I used some GetVolumeInformation API or somesuch to read the CD label, in the drive which returns a DRIVE_CDROM result from GetDriveType.

    Works, although I'm a bit unclear of what the parameters to that function are. Gotta love C&P.
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  18. #18
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    I haven't got the faintest idea what you're going on about !
    What's C&P?!?!?!

    Anyway, if you haven't downloaded the API Guide program (free one from www.allapi.net), grab it now it has the parameters explained as well as vb6 & vb.net sample code for most Windows API calls & it's where I start off when I write most of my samples!

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  19. #19

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    Cut & Paste.


    Not, don't have that one yet. Don't know why really...
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  20. #20
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Sorry it took a while to post this back, but here goes! This is a list explanation of the parameters for the GetVolumeInformation API call ....
    ByVal lpRootPathName As String
    String for the root directory of the path or file you want to grab the information for.
    In your sample, you'll want to pass in the CD-ROM drive i.e. "D:\"

    ByVal lpVolumeNameBuffer As String
    This is a returned value from the API call - rather than specifying/passing in a value parameter,
    you pass a string variable in which'll be populated with a value when the API call runs.
    Since you don't need this bit, you can pass a null string in. i.e. "vbNullString"

    ByVal nVolumeNameSize As Long
    This limit's the size of the above string returned. I.e. if the above string returned was "xyz " and
    you put a 3 in here, the actual value returned will be the first 3 trimmed characters i.e. "xyz"

    lpVolumeSerialNumber As Long
    Again, this is a value which is returned from the API call & returns the volume serial number of the
    drive. This one we do need, so pass in a long variable already declared in here. When the API call runs,
    this will be populated & your long variable can be used afterward in say, a messagebox.

    lpMaximumComponentLength As Long
    Won't go into this one, only as much as to say we don't need this parameter!

    Pass another blank value in for this one, like with the "vbnullstring" setting passed to a string parameter,
    you pass a 0 in for a blank/default value when dealing with long parameter types. To make it run a bit
    faster, enter this 0 value as "ByVal CLng(0)", specifying it as byval & converting it to a long value.

    lpFileSystemFlags As Long
    Again, not needed, this returns a value from the function for the flags of the file system, flags are items/notes
    about the file systems such as "does it support case sensitive filenames" or "is it compressed", pass another
    null value in. i.e. "Byval Clng(0)"

    ByVal lpFileSystemNameBuffer As String
    Returned value indicating if the volume (CD-ROM Drive) has a FAT or NTFS filesystem. Again, this we don't need,
    so pass in a blank string "vbNullString"

    ByVal nFileSystemNameSize As Long
    Works exactly the same as the "nVolumeNameSize" parameter, so deserves the same value - "Byval Clng(0)"

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  21. #21
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Api's aren't too scary once you get to know them, putting the above info into a sample, you end up with this one. Realistically, it's only 4 lines of code:
    VB Code:
    1. [size="1"]Private Declare Function GetVolumeInformation Lib "Kernel32" _
    2.   Alias "GetVolumeInformationA" (ByVal lpRootPathName As String, _
    3.   ByVal lpVolumeNameBuffer As String, ByVal nVolumeNameSize As Long, _
    4.   lpVolumeSerialNumber As Long, lpMaximumComponentLength As Long, _
    5.   lpFileSystemFlags As Long, ByVal lpFileSystemNameBuffer As String, _
    6.   ByVal nFileSystemNameSize As Long) As Long
    7.  
    8. Private Sub Form_Load()
    9.     Dim lngReturnedSerialNo As Long
    10.     GetVolumeInformation "C:\", vbNullString, ByVal CLng(0), lngReturnedSerialNo, _
    11.     ByVal CLng(0), ByVal CLng(0), vbNullString, ByVal CLng(0)
    12.  
    13.     MsgBox "The serial number of the C drive is: " & Trim(lngReturnedSerialNo)
    14. End Sub[/size]

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  22. #22
    Lively Member bumbala's Avatar
    Join Date
    Sep 2002
    Posts
    111
    Achieving your goal with APIs is cleaner and I would suggest you do that way but if you have already included Scripting in your project or you may include it, you can try this:

    Code:
        Dim fs As New FileSystemObject
        Dim d As Drive
        
        For Each d In fs.Drives
            If d.DriveType = CDRom Then
                'Check a file in the CD-ROM, to see whether it is the correct CD
                'or check its Label etc...
                If True Then
                    Exit For
                End If
            End If
        Next
        Set fs = Nothing

  23. #23
    Fanatic Member jian2587's Avatar
    Join Date
    Aug 2000
    Location
    I bet u need a fusion powered shuttle to reach my place...
    Posts
    963
    Well, if u use CD Volume to determine if its ur CD, then is it
    possible that someone is trying to fool ur program by formatting
    a CDROM with the Volume number of ur CD, and how he get ur
    CD's volume is by DIR it.
    ASM,C,C++,BASIC,VB,JAVA,VBS,HTML,ASP,PHP,mySQL,VB.NET,MATLAB
    Programming is fun, but only if you're not on a tight deadline
    So I consider all those working engineers sad people

    VB FTP class
    3 page PHP crash course
    Crash Course on DX9 Managed with VB.NET covering basics till terrain creation

  24. #24

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    As I said in the very first post, this isn't a security measure.

    I don't care if someone wants to do that.
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

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