DELPHI编程捕获U盘插入或者拔出
翻了下老帖子,月亮写的,根据LS发的MSDN翻译的
源地址:
topic.csdn/u/20090628/16/96e73f9e-5e74-4bed-bb75-2f18a0e9cb02.html
Delphi(Pascal)code
function OpenVolume(ADrive:char):THandle;
var
RootName,VolumeName:string;
AccessFlags:DWORD;
begin
RootName:=ADrive+':'+#134;//ADrive+':\'kills the syntax highlighting
瓷碗
ca GetDriveType(PChar(RootName))of
DRIVE_REMOVABLE:
AccessFlags:=GENERIC_READ or GENERIC_WRITE;
DRIVE_CDROM:
AccessFlags:=GENERIC_READ;
el
Result:=INVALID_HANDLE_VALUE;
exit;
end;
VolumeName:=Format('\\.\%s:',[ADrive]);
Result:=CreateFile(PChar(VolumeName),AccessFlags,
FILE_SHARE_READ or FILE_SHARE_WRITE,nil,OPEN_EXISTING,0,0);
if Result=INVALID_HANDLE_VALUE then
RaiLastWin32Error;
end;
function LockVolume(AV olumeHandle:THandle):boolean;
const
LOCK_TIMEOUT=10*1000;//10Seconds
LOCK_RETRIES=20;
LOCK_SLEEP=LOCK_TIMEOUT div LOCK_RETRIES;
//#define FSCTL_LOCK_VOLUME CTL_CODE(FILE_DEVICE_FILE_SYSTEM,6, METHOD_BUFFERED,FILE_ANY_ACCESS)
FSCTL_LOCK_VOLUME=(9shl16)or(0shl14)or(6shl2)or0;
var
Retries:integer;
BytesReturned:Cardinal;
begin
for Retries:=1to LOCK_RETRIES do begin
Result:=DeviceIoControl(AV olumeHandle,FSCTL_LOCK_VOLUME,nil,0,
nil,0,BytesReturned,nil);
if Result then
break;
Sleep(LOCK_SLEEP);
end;
end;
function DismountVolume(AV olumeHandle:THandle):boolean;
const
//#define FSCTL_DISMOUNT_VOLUME CTL_CODE(FILE_DEVICE_FILE_SYSTEM,8, METHOD_BUFFERED,FILE_ANY_ACCESS)
FSCTL_DISMOUNT_VOLUME=(9shl16)or(0shl14)or(8shl2)or0;
var
BytesReturned:Cardinal;
begin
Result:=DeviceIoControl(AV olumeHandle,FSCTL_DISMOUNT_VOLUME,nil,0,
nil,0,BytesReturned,nil);
if not Result then
RaiLastWin32Error;
end;
function PreventRemovalOfVolume(AV olumeHandle:THandle;
APreventRemoval:boolean):boolean;
const
//#define IOCTL_STORAGE_MEDIA_REMOVAL CTL_CODE(IOCTL_STORAGE_BASE, 0x0201,METHOD_BUFFERED,FILE_READ_ACCESS)
IOCTL_STORAGE_MEDIA_REMOVAL=($2d shl16)or(1shl14)or($201shl2)or0; type
TPreventMediaRemoval=record
PreventMediaRemoval:BOOL;
end;
var
吸收拼音BytesReturned:Cardinal;
PMRBuffer:TPreventMediaRemoval;
begin
PMRBuffer.PreventMediaRemoval:=APreventRemoval;
Result:=DeviceIoControl(AV olumeHandle,IOCTL_STORAGE_MEDIA_REMOVAL,
@PMRBuffer,SizeOf(TPreventMediaRemoval),nil,0,BytesReturned,nil);
if not Result then
RaiLastWin32Error;
end;
function AutoEjectVolume(AV olumeHandle:THandle):boolean;
const
/
/#define IOCTL_STORAGE_EJECT_MEDIA CTL_CODE(IOCTL_STORAGE_BASE,0x0202, METHOD_BUFFERED,FILE_READ_ACCESS)
IOCTL_STORAGE_EJECT_MEDIA=($2d shl16)or(1shl14)or($202shl2)or0;
var
BytesReturned:Cardinal;
begin
Result:=DeviceIoControl(AV olumeHandle,IOCTL_STORAGE_EJECT_MEDIA,nil,0,
nil,0,BytesReturned,nil);
if not Result then
RaiLastWin32Error;
end;
function EjectVolume(ADrive:char):boolean;
var
VolumeHandle:THandle;
begin
Result:=FALSE;
//Open the volume
VolumeHandle:=OpenVolume(ADrive);
if VolumeHandle=INVALID_HANDLE_VALUE then
exit;
try
//Lock and dismount the volume
if LockVolume(VolumeHandle)and DismountVolume(VolumeHandle)then begin
/
/Set prevent removal to fal and eject the volume
if PreventRemovalOfVolume(VolumeHandle,FALSE)then
AutoEjectVolume(VolumeHandle);
end;
后背脊柱中间疼痛
finally
//Clo the volume so other process can u the drive
CloHandle(VolumeHandle);
end;
end;
2010-07-0215:04
在USB端口port被热插后,操作系统会向所有程式正式发布WM_DEVICECHANGE消息,所以只要捕获此消息即可呈现对U盘的监视。
1、新建工程;
2、在form1上放唯一Label;
3、定义唯一私有过程procedure WMDeviceChange(var Msg:TMessage);message WM_DEVICECHANGE;
完整代码Code如下:
unit Unit1;
interface
us
Windows,Messages,SysUtils,Variants,Class,Graphics,Controls,Forms,
Dialogs,AppEvnts,ExtCtrls,StdCtrls;
type
TForm1=class(TForm)
Label1:TLabel;
private
{Private declarations}
procedure WMDeviceChange(var Msg:TMessage);message WM_DEVICECHANGE;
public
{Public declarations}
end;
var
Form1:TForm1;
implementation
{$R*.dfm}
procedure TForm1.WMDeviceChange(var Msg:TMessage);
var
myMsg:String;
begin
Ca Msg.WParam of
32768:
begin
myMsg:='U盘插入';
Label1.Caption:=myMsg
end;
32772:
begin
myMsg:='U盘拔出';
Label1.Caption:=myMsg;
end;
end;
end;
苏州时尚舞台
End.
此代码未经调试
const
CfgMgr32ModuleName='cfgmgr32.dll'; SetupApiModuleName='SetupApi.dll'; REGSTR_VAL_NODISPLAYCLASS='NoDisplayClass'; CR_SUCCESS=$00000000;
CR_REMOVE_VETOED=$00000017;
DN_HAS_PROBLEM=$00000400;
DN_DISABLEABLE=$00002000;
DN_REMOVABLE=$00004000;
DN_NO_SHOW_IN_DM=$40000000;
CM_PROB_DISABLED=$00000016;
CM_PROB_HARDWARE_DISABLED=$0000001D; type
_PNP_VETO_TYPE=(
PNP_VetoTypeUnknown,
PNP_VetoLegacyDevice,
PNP_VetoPendingClo,
PNP_VetoWindowsApp,
PNP_VetoWindowsService,
PNP_VetoOutstandingOpen,
PNP_VetoDevice,
PNP_VetoDriver,
PNP_VetoIllegalDeviceRequest,
PNP_VetoInsufficientPower,
PNP_VetoNonDisableable,
PNP_VetoLegacyDriver
);
PNP_VETO_TYPE=_PNP_VETO_TYPE;
PPNP_VETO_TYPE=^_PNP_VETO_TYPE; TPNPVetoType=_PNP_VETO_TYPE; PPNPVetoType=PPNP_VETO_TYPE;
function CM_Get_DevNode_Status(pulStatus:PULong;pulProblemNumber:PULong; dnDevInst:DWord;ulFlags:ULong):DWord;stdcall;
external CfgMgr32ModuleName name'CM_Get_DevNode_Status';
function CM_Request_Device_Eject(dnDevInst:DWord;out pVetoType:TPNPVetoType; pszVetoName:PChar;ulNameLength:ULong;ulFlags:ULong):DWord;stdcall;
external SetupApiModuleName name'CM_Request_Device_EjectA';
type
TForm1=class(TForm)
TreeView:TTreeView;
ImageList:TImageList;
MainMenu1:TMainMenu;
Eject1:TMenuItem;
Exit1:TMenuItem;
Change1:TMenuItem;
8级伤残ShowHidden1:TMenuItem;
EjectDriver1:TMenuItem;
Exit2:TMenuItem;
procedure FormCreate(Sender:TObject);
中华传奇
摘抄好句procedure ShowHidden1Click(Sender:TObject);
procedure FormCloQuery(Sender:TObject;var CanClo:Boolean);
procedure EjectDriver1Click(Sender:TObject);
procedure Exit2Click(Sender:TObject);
procedure TreeViewClick(Sender:TObject);
private
DevInfo:hDevInfo;
ClassImageListData:TSPClassImageListData;
ShowHidden:Boolean;
function EnumAddDevices(ShowHidden:Boolean;hwndTree:TTreeView;DevInfo: hDevInfo):Boolean;建设工程质量
function IsClassHidden(ClassGuid:TGuid):Boolean;
function GetRegistryProperty(PnPHandle:hDevInfo;DevData:TSPDevInfoData;
Prop:DWord;Buffer:PChar;dwLength:DWord):Boolean;
function ConstructDeviceName(DeviceInfoSet:hDevInfo;
DeviceInfoData:TSPDevInfoData;Buffer:PChar;dwLength:DWord):Boolean;
function GetClassImageIndex(ClassGuid:TGuid;Index:PInt):Boolean;
function GetDevInfo(var hDevInfo:hDevInfo):boolean;
{Private declarations}
public
{Public declarations}