怎么编程实现U盘插入,EXE自动运行(4分)

  • 主题发起人 主题发起人 冰力不足
  • 开始时间 开始时间

冰力不足

Unregistered / Unconfirmed
GUEST, unregistred user!
我在驱动论坛看到大侠都用C++ 而且他们都说delphi难实现 不知道这里的delphi的高手能够写出来么
问题太难 所以只给4分啦 因为这样的问题估计在大富翁没有高人能够回答了
检验您水平的时候到了 不管您学10年还是学1天 能够回答的就是牛人
所以 这4分弥足珍贵 [:D]
 
算了吧。那你用C++写一个?
灰色那里到现在几年过去了,想不到你还是这个样子
 
写一个系统监控程序,监控U盘插入,拔出状态
unit USBMonitor;
interface
uses
Windows, Messages, SysUtils, Classes, Forms;
type
TDeviceMonitorUSB = class(TComponent)
private
FWindowHandle: HWND;
FOnUSBArrival: TNotifyEvent;
FOnUSBRemove: TNotifyEvent;
procedure WndProc(var Msg: TMessage);
function USBRegister: Boolean;
protected
procedure WMDeviceChange(var Msg: TMessage);
dynamic;
public
constructor Create(AOwner: TComponent);
override;
destructor Destroy;
override;
published
property OnUSBArrival: TNotifyEvent read FOnUSBArrival write FOnUSBArrival;
property OnUSBRemove: TNotifyEvent read FOnUSBRemove write FOnUSBRemove;
end;

procedure Register;
implementation
procedure Register;
begin
RegisterComponents('FMA', [TDeviceMonitorUSB]);
end;

type
PDevBroadcastHdr = ^DEV_BROADCAST_HDR;
DEV_BROADCAST_HDR = packed record
dbch_size: DWORD;
dbch_devicetype: DWORD;
dbch_reserved: DWORD;
end;

PDevBroadcastDeviceInterface = ^DEV_BROADCAST_DEVICEINTERFACE;
DEV_BROADCAST_DEVICEINTERFACE = record
dbcc_size: DWORD;
dbcc_devicetype: DWORD;
dbcc_reserved: DWORD;
dbcc_classguid: TGUID;
dbcc_name: short;
end;

const
GUID_DEVINTERFACE_USB_DEVICE: TGUID = '{A5DCBF10-6530-11D2-901F-00C04FB951ED}';
DBT_DEVICEARRIVAL = $8000;
// system detected a new device
DBT_DEVICEREMOVECOMPLETE = $8004;
// device is gone
DBT_DEVTYP_DEVICEINTERFACE = $00000005;
// device interface class
constructor TDeviceMonitorUSB.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FWindowHandle := AllocateHWnd(WndProc);
USBRegister;
end;

destructor TDeviceMonitorUSB.Destroy;
begin
DeallocateHWnd(FWindowHandle);
inherited Destroy;
end;

procedure TDeviceMonitorUSB.WndProc(var Msg: TMessage);
begin
if (Msg.Msg = WM_DEVICECHANGE) then
begin
try
WMDeviceChange(Msg);
except
Application.HandleException(Self);
end;
end
else
Msg.Result := DefWindowProc(FWindowHandle, Msg.Msg, Msg.wParam, Msg.lParam);
end;

procedure TDeviceMonitorUSB.WMDeviceChange(var Msg: TMessage);
var
devType: Integer;
Datos: PDevBroadcastHdr;
begin
if (Msg.wParam = DBT_DEVICEARRIVAL) or (Msg.wParam = DBT_DEVICEREMOVECOMPLETE) then
begin
Datos := PDevBroadcastHdr(Msg.lParam);
devType := Datos^.dbch_devicetype;
if devType = DBT_DEVTYP_DEVICEINTERFACE then
begin
// USB Device
if Msg.wParam = DBT_DEVICEARRIVAL then
begin
if Assigned(FOnUSBArrival) then
FOnUSBArrival(Self);
end
else
if Msg.wParam = DBT_DEVICEREMOVECOMPLETE then
begin
if Assigned(FOnUSBRemove) then
FOnUSBRemove(Self);
end;
end;
end;
end;

function TDeviceMonitorUSB.USBRegister: Boolean;
var
dbi: DEV_BROADCAST_DEVICEINTERFACE;
Size: Integer;
begin
Size := SizeOf(DEV_BROADCAST_DEVICEINTERFACE);
ZeroMemory(@dbi, Size);
dbi.dbcc_size := Size;
dbi.dbcc_devicetype := DBT_DEVTYP_DEVICEINTERFACE;
dbi.dbcc_reserved := 0;
dbi.dbcc_classguid := GUID_DEVINTERFACE_USB_DEVICE;
dbi.dbcc_name := 0;
Result := Assigned(RegisterDeviceNotification(FWindowHandle, @dbi,
DEVICE_NOTIFY_WINDOW_HANDLE));
end;

end.
 
谢谢ZQFILE 分太少了 真不好意思撒
 
接受答案了.
 
这个代码只是查USB插入与拔出.并不是实现自动运行的.不管是DELPHI或VC都是查USB插入和拔出再加autorun.inf进去.写不出得出来和是不是用DELPHI和VC并没有关系.如果只是写AUTORUN.inf的话,能否成功要看系统设置而定.
 
后退
顶部