Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
639 views
in Technique[技术] by (71.8m points)

detect usb drive/device using delphi

i can't figure out the formatting rules here .. too many lines of code in my example to add 4 spaces to each line, so here is the link to the code i need help with

http://nitemsg.blogspot.com/2011/01/heres-unit-written-in-delphi-7-that-you.html

The problem I have is that I don't know enough about delphi to use this code with a form. I am a drag and drop programmer only.

An example with a showmessage('friendly name =' + ... ) when a USB device is detected is what I need.

cheers,

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you are only familiar with drag-and-drop programming, and don't know much about objects or other units, then you need to get yourself familiarized with using objects other than auto-created forms and the components you drop in them.

The code at this link is an entire unit. You need to create a new Unit in your project (File > New > Unit). It will look something like this:

unit Unit1;

interface

implementation

end.

Now when you save the unit, the name of the unit will automatically change to the filename (without the extension) like this:

unit MahUSB;

interface

implementation

end.

In this example, you should use the same unit name as that source you're trying to use. Save the unit as 'MahUSB.pas', and should be in the same folder as the rest of your project (or elsewhere, just a suggestion). Copy/Paste all the code from that website and replace everything in this unit now.

Now in order to actually use this, you need to create an instance of this object. ONLY ONE INSTANCE (I say that just because by the looks of this, there's no need for more than one).

Very important: Seeing as you are not familiar with objects, let me quickly explain something. Objects need to be created in order to work. At the same time, anything that's created also needs to be free'd when you're done with it. In this case, we will create this object when your application starts, and free the object when your application closes.

Now on your MAIN FORM (not any other forms) you need to put an event handler for both OnCreate and OnDestroy. You also need to declare a variable to represent this object. In the declaration of your main form, add a variable 'USB' with the type of this object. Make sure that goes under the 'private' or 'public' section, either one is ok. Also make sure you declare the "MahUSB" unit at the top of your main unit in the uses clause.

Declaring the object in your main form:

type
  TForm1 = class(TForm)
  private
    USB: TUsbClass;
  public

  end;

Creating/freeing object when your app starts/closes:

  procedure TForm1.FormCreate(Sender: TObject);
  begin
    USB:= TUsbClass.Create;
  end;

  procedure TForm1.FormDestroy(Sender: TObject);
  begin
    if assigned(USB) then USB.Free;
  end;

Now we're not done yet. Now we need to add the event handlers. Notice at the top of this unit you got, there are two types called TOnDevVolumeEvent and TOnUsbChangeEvent. These are event types. The parameters in the event handlers must be identical to the parameters declared in these types. So now in your main form, declare these event handler procedures...

  type
    TForm1 = class(TForm)
      procedure FormCreate(Sender: TObject);
      procedure FormDestroy(Sender: TObject);
    private
      USB: TUsbClass;
      procedure VolumeEvent(const bInserted : boolean; const sDrive : string);
      procedure ChangeEvent(const bInserted : boolean;
        const ADevType,ADriverName, AFriendlyName : string);
    public

    end;

Now just one more thing we have to do before this will work. The USB object needs to know what event handlers to use, therefore, we need to assign these procedures to the events. Upon your form's creation, we need to assign these events...

    procedure TForm1.FormCreate(Sender: TObject);
    begin
      USB:= TUsbClass.Create;
      USB.OnUsbChange:= Self.ChangeEvent;
      USB.OnDevVolume:= Self.VolumeEvent;
    end;

When all is said and done, your main form unit should look something like this:

unit uUSBTest;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, MahUSB;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    USB: TUsbClass;
    procedure VolumeEvent(const bInserted : boolean; const sDrive : string);
    procedure ChangeEvent(const bInserted : boolean;
      const ADevType,ADriverName, AFriendlyName : string);
  public

  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.ChangeEvent(const bInserted: boolean; const ADevType,
  ADriverName, AFriendlyName: string);
begin
  ShowMessage('Change event for "'+AFriendlyName+'"');
end;

procedure TForm1.VolumeEvent(const bInserted: boolean;
  const sDrive: string);
begin
  ShowMessage('Volume event for "'+sDrive+'"');
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  USB:= TUsbClass.Create;
  USB.OnUsbChange:= Self.ChangeEvent;
  USB.OnDevVolume:= Self.VolumeEvent;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  if assigned(USB) then USB.Free;
end;

end.

And there you are! You will have these two event handler procedures where you can further handle either of those two events.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...