Solved

Drivecombobox (Delphi), what's the proper way to test if removeable media is present

Posted on 2010-08-20
6
1,823 Views
Last Modified: 2013-11-23
What's the proper way to test whether removeable media is inserted? Whether it's ready for writing (formatted)? Empty floppy drives and memory card readers are causing problems.

Thanks, Ed
0
Comment
Question by:Ed-SSA
  • 2
  • 2
  • 2
6 Comments
 
LVL 13

Expert Comment

by:aflarin
ID: 33490383
you should handle the WM_DEVICECHANGE message to rebuild list of drives.

Also you can use TJvDriveCombo from JVCL: http://jvcl.delphi-jedi.org/ 
0
 
LVL 13

Assisted Solution

by:aflarin
aflarin earned 100 total points
ID: 33490394
here is the example

type
  TWMDeviceChange = record
   Msg:    Cardinal;
   Event:  UINT;
   dwData: Pointer;
   Result: LongInt;
  end;

  PDevBroadcastVolume = ^TDevBroadcastVolume;
  DEV_BROADCAST_VOLUME = record
    dbcv_size: DWORD;
    dbcv_devicetype: DWORD;
    dbcv_reserved: DWORD;
    dbcv_unitmask: DWORD;
    dbcv_flags: Word;
  end;
  TDevBroadcastVolume = DEV_BROADCAST_VOLUME;


  TForm1 = class(TForm)
  ....
  private
    { Private declarations }
    procedure WMDeviceChange(var Msg: TWMDeviceChange); message WM_DEVICECHANGE;
  public
    { Public declarations }
  end;

procedure TForm1.WMDeviceChange(var Msg: TWMDeviceChange);
const
  DBT_DEVICEARRIVAL = $8000;                   { system detected a new device }
  DBT_DEVICEREMOVECOMPLETE = $8004;            { device is gone }
  DBT_DEVTYP_VOLUME = $00000002;               { logical volume }
begin
    if ((Msg.Event = DBT_DEVICEARRIVAL) or
      (Msg.Event = DBT_DEVICEREMOVECOMPLETE)) and
      (PDevBroadcastVolume(Msg.dwData)^.dbcv_devicetype = DBT_DEVTYP_VOLUME) then
    begin
      ShowMessage( 'Device was added or removed' );
    end;

end;
0
 

Author Comment

by:Ed-SSA
ID: 33492439
@Aflarin:

I may not have made myself clear.  On start-up, I need to check removable drives for a specific file. If the removable drive is a flash drive, there's is no problem - windows doesn't give it a drive letter assignment until its installed. If its a floppy or memory card drive, windows has assigned it a drive letters, whether media exists in the drive or not. So if I check A: drive and there's no floppy inserted, I get an error which asks to [Abort, Retry, or Cancel]. Want I need is a way to determine if A: has a floppy in it before stating the test.

Here's a specific example. A user has an empty floppy drive A:\,  C:\ & D:\ local hard drives, E:, F;, G: are empty memory card reader drives, H: is a USB flash drive (which hosts the file I'm looking for), and I: and J: are 2 more empty memory card drives. So the question is upon start-up, how can I be determine which removable drives realy exist?

Thanks

Ed  
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Accepted Solution

by:
flexiwebsw earned 400 total points
ID: 33492618
Here you go, try this:
unit Unit1;



interface



uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls, ExtCtrls;



type

  TForm1 = class(TForm)

    ComboBox1: TComboBox;

    procedure FormCreate(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

  end;



var

  Form1: TForm1;



implementation



{$R *.dfm}



function DiskInDrive(Drive: Char): Boolean;

var

  ErrorMode: Word;

begin

  { make it upper case }

  if Drive in ['a'..'z'] then Dec(Drive, $20);

  { make sure it's a letter }

  if not (Drive in ['A'..'Z']) then

    raise EConvertError.Create('Not a valid drive ID');

  { turn off critical errors }

  ErrorMode := SetErrorMode(SEM_FailCriticalErrors);

  try

    { drive 1 = a, 2 = b, 3 = c, etc. }

    if DiskSize(Ord(Drive) - $40) = -1 then

      Result := False

    else

      Result := True;

  finally

    { Restore old error mode }

    SetErrorMode(ErrorMode);

  end;

end;



procedure TForm1.FormCreate(Sender: TObject);

var

  Drive : Char;

begin

for Drive := 'A' to 'Z' do

if DiskInDrive(Drive) then

if FileExists(Drive + ':\MyFile.txt') then

ComboBox1.Items.Add(Drive + ':\');

end;





end.

Open in new window

0
 

Author Comment

by:Ed-SSA
ID: 33496137
@flexiwebsw:
Your "if DiskSize(Ord(Drive) - $40) = -1 then"  was like magic !! - THANK YOU.

@aflarin
I know at some point in the future, if I don't find the file, I'll ask the user to insert the flash drive. In the mean time, I'll play with your code as I insert and remove flash drives.  Thanks

0
 
LVL 4

Expert Comment

by:flexiwebsw
ID: 33496244
Thankyou, it was taken from a script i developed a while ago, it was to tell the user the type of drive, e.g removable, network, cd-rom or fixed, and others. but i got errors when it searched through drives that had letters but no disks. this piece of code has saved my life a few times.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Newbie Thread Programming 1 138
eclipse luna javeEE perspecive missing 5 95
Connection between libmysql.dll and MySQL Versions 7 87
CheckListBox usage 3 48
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

943 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

8 Experts available now in Live!

Get 1:1 Help Now