Solved

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

Posted on 2010-08-20
6
1,897 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 Covney
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
 
LVL 3

Author Comment

by:Ed Covney
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
LVL 3

Author Comment

by:Ed Covney
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

724 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