Solved

How to get the USB drie letter

Posted on 2004-04-08
12
479 Views
Last Modified: 2007-12-19
Hello all

I want to write a utility whcih finds the drive letters of the USB flash drive inserted into the system.Kindly help me out in this problem.
0
Comment
Question by:talhaamin
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 3

Expert Comment

by:pamboo
Comment Utility
The below ex program is written in VB 6.0 uisng win32 APIs. Hope this helps u .

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

Private Sub Form_Load()
   
    Dim strSave As String
    Dim driveLetter As String
   
    'Set the graphic mode to persistent
    Me.AutoRedraw = True
   
    'Create a buffer to store all the drives
    strSave = String(255, Chr$(0))
   
    'Get all the drives
    ret& = GetLogicalDriveStrings(255, strSave)
   
    'Extract the drives from the buffer and check if it removable
    For keer = 1 To 100
        If Left$(strSave, InStr(1, strSave, Chr$(0))) = Chr$(0) Then Exit For
        driveLetter = Left$(strSave, InStr(1, strSave, Chr$(0)) - 1)
       
        'Get information about the drive
       Select Case GetDriveType(driveLetter)
         
         Case 2
           
            ' USB flash drive is a removable type.
            Me.Print driveLetter & " Type: Removable"
           
            'But floppy drives also comes under this category .
            'Since they r not dynamic drives like USB , the USB drive
            'letter can be predicted by neglecting the know floppy drive letters.

'         Case 3
'            Me.Print "Drive Fixed" 'Hard disk
'         Case Is = 4
'            Me.Print "Remote" 'network map drive
'         Case Is = 5
'            Me.Print "Cd-Rom"
'         Case Is = 6
'            Me.Print "Ram disk"
'         Case Else
'            Me.Print "Unrecognized"

       End Select

       strSave = Right$(strSave, Len(strSave) - InStr(1, strSave, Chr$(0)))
       
    Next keer

   
    'Thanks to KPD-Team 1998 - Code extracted from allapi.net
   
End Sub


- vm
0
 

Expert Comment

by:adamee
Comment Utility
Essentialy what is above but in C

#include <stdio.h>

int main(){

int i;
char c[4] = {'a',':','\\','\0'};
for(i = 65;i < 91; i++){ // run through all possible drive letters
      c[0] = i;
      switch (GetDriveType(c)){
            
      case 0:
              {
                  //printf("DRIVE_UNKNOWN\n");
                  break;
              }
      case 1:
              {
                  //printf("DRIVE_NO_ROOT_DIR\n");
                  break;
              }
      case 2:
              {
                                               //printf("DRIVE_REMOVABLE\n");
                                                /* USB FLASH DISK WILL BE HERE */
                  break;
              }
      case 3:
              {
                  //printf("DRIVE_FIXED\n");
                  break;
              }
      case 4:
              {
                  //printf("DRIVE_REMOTE\n");
                  break;
              }
      case 5:
              {
                  //printf("DRIVE_CDROM\n");
                  break;
              }
      case 6:
              {
                  //printf("DRIVE_RAMDISK\n");
                  break;
              }
      }
      }

}
0
 

Expert Comment

by:adamee
Comment Utility
might want to return something from main as well not to raise an error :)
0
 
LVL 2

Author Comment

by:talhaamin
Comment Utility
But guys when i use this code, my usb type return is fixed drive .Iam confused why
0
 
LVL 3

Expert Comment

by:pamboo
Comment Utility
Just to check . R u properly eding the case statement . I mean taking care of break properly .

JAlso just check this below and reply us

1.After instering the USB flash drive , get the drive name using the windows explorer and pass that drive name/letter to
GetDriveType API and check if what type it returns .

eg: GetDriveType("G:\")

2. Also check in the explorer itself , (it should say removable disk on drive G:\ etc.,) . Post us what it shows in the explorer.

Finally , what OS u r using ?

- vm
0
 
LVL 3

Expert Comment

by:pamboo
Comment Utility
correction in my prev post
>>R u properly eding the case statement
R u properly ending the case statement ?

0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 2

Author Comment

by:talhaamin
Comment Utility
In windows explorer My drive type is 'disk drives' and this drive type is also of my C:\ too.Iam using Windows XP and win2k but in both OS iam getting the same drive type Fixed.Iam ending the case statements correctly.
0
 
LVL 2

Author Comment

by:talhaamin
Comment Utility
I have a TwinMos usb drive of 16MB capacity
0
 
LVL 1

Expert Comment

by:hal3000
Comment Utility

When using the getDriveType function "removable" refers to the fact that you can remove the media from the drive and replace. It does not mean the drive is removable.

A "removable" drive would be something lile the ZIP drive.

The floppy drive has it's own category, as does cd-roms.

It is possible to get the drive capacity from the drive letter and considering the orders of magnitude difference between your usb drive and your hard drives it might be an easy way to detect the difference. Look into the function "GetDiskFreeSpaceEx". This returns a structure where one of the fields is the number of bytes on the drive.
0
 
LVL 2

Author Comment

by:talhaamin
Comment Utility
Dear hal3000 thanks for your reply but the above code is working properly in win2k but not working in winxp.
I want a solution in winxp.Can any body help me.
0
 
LVL 1

Accepted Solution

by:
hal3000 earned 125 total points
Comment Utility
I'm confused.

At 9:48 you said that both Win2k and winxp were returning the same result "fixed drive"

That is what the MS documentation says it should return since you cannot change the media in the flash drive (even if it is removable from the system).

I therefore offered an alternative solution.

But your 2:51 message says you are getting different results in XP and 2k.

Does win2k now indicate that the drive is "removable" instead of fixed?
0
 

Expert Comment

by:WilliamSolberg
Comment Utility
I would suggest just putting a file on the flash drive called flashdrive.txt and look for the file..  if it finds it then you got the right drive..
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now