使用delphi检测USB驱动器/设备 [英] detect usb drive/device using delphi

查看:305
本文介绍了使用delphi检测USB驱动器/设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚这里的格式规则..在我的示例中,太多的代码行无法在每行中添加4个空格,因此这是我需要帮助的代码链接

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-write-in-delphi-7-that-you.html

我遇到的问题是,我对delphi不太了解,无法将此代码与表单一起使用。
我只是一个拖放式程序员。

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.

检测到USB设备时显示showmessage('friendly name ='+ ...)的示例是我所需要的。

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

欢呼声,

推荐答案

如果仅熟悉拖放编程,对对象或其他单元了解不多,那么您需要熟悉使用自动创建的表单和放置在其中的组件之外的对象。

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.

在此示例中,您应使用与要使用的源相同的单位名称。将单元另存为 MahUSB.pas,并且应与项目的其余部分位于同一文件夹(或其他建议)。复制/粘贴该网站上的所有代码,并立即替换此单元中的所有内容。

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.

现在在您的主表单(没有任何其他表单)上,需要为OnCreate和OnDestroy放置一个事件处理程序。您还需要声明一个变量来表示该对象。在主表单的声明中,添加带有该对象类型的变量 USB。确保在私人或公开部分下都可以。还要确保在uses子句中的主单元顶部声明 MahUSB单元。

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.

以主形式声明对象:

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;

现在我们还没有完成。现在我们需要添加事件处理程序。请注意,在该单元的顶部,有两种类型,分别为 TOnDevVolumeEvent TOnUsbChangeEvent 。这些是事件类型。事件处理程序中的参数必须与在这些类型中声明的参数相同。因此,现在在您的主窗体中,声明这些事件处理程序过程...

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;

现在,在此之前,我们还要做一件事。 USB对象需要知道要使用哪些事件处理程序,因此,我们需要将这些过程分配给事件。创建您的表单后,我们需要分配这些事件...

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.

这篇关于使用delphi检测USB驱动器/设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆