[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2009
  • Last Modified:

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

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
Ed Covney
Asked:
Ed Covney
  • 2
  • 2
  • 2
2 Solutions
 
aflarinCommented:
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
 
aflarinCommented:
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
 
Ed CovneyRetiredAuthor Commented:
@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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
flexiwebswCommented:
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
 
Ed CovneyRetiredAuthor Commented:
@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
 
flexiwebswCommented:
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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now