Solved

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

Posted on 2010-08-20
6
1,805 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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.

757 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

19 Experts available now in Live!

Get 1:1 Help Now