(************************************************************* ?2004 yeahware software development http://www.yeahware.com If this code works, it was written by Markus Fuchs. If not, I don't know who wrote it. (Paul DiLascia) Module : eject Source File Name : ueject.pas Description : Ejects Firewire/USB HDDs and USB sticks Compiler : Delphi 5 Platforms : Windows 9x and NT platform Created : 08.05.2004 Revisions : 19.08.2006 - Update device list with every DBT_DEVNODES_CHANGED *************************************************************)unit ueject;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ImgList, ComCtrls, StdCtrls, SetupAPI, ExtCtrls, Menus; 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_VetoPendingClose, 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; MainMenu: TMainMenu; mFile: TMenuItem; mExit: TMenuItem; mChange: TMenuItem; mEnableDevice: TMenuItem; mDisableDevice: TMenuItem; mOptions: TMenuItem; mRefreshDisplay: TMenuItem; mShowHiddenDevices: TMenuItem; mN: TMenuItem; mRemoveDevice: TMenuItem; procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure mExitClick(Sender: TObject); procedure mRefreshDisplayClick(Sender: TObject); procedure mShowHiddenDevicesClick(Sender: TObject); procedure TreeViewChange(Sender: TObject; Node: TTreeNode); procedure TreeViewDblClick(Sender: TObject); procedure ChangeEnableDevice(Sender: TObject); procedure ChangeDisableDevice(Sender: TObject); procedure EjectDevice(Sender: TObject); private { Private-Deklarationen } DevInfo: hDevInfo; ClassImageListData: TSPClassImageListData; ShowHidden: Boolean; function CheckStatus(SelectedItem: DWord; hDevInfo: hDevInfo; StatusFlag: LongWord): Boolean; function IsDisabled(SelectedItem: DWord; hDevInfo: hDevInfo): Boolean; function StateChange(NewState: DWord; SelectedItem: DWord; hDevInfo: hDevInfo): 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 IsClassHidden(ClassGuid: TGuid): Boolean; function EnumAddDevices(ShowHidden: Boolean; hwndTree: TTreeView; DevInfo: hDevInfo): Boolean; function GetClassImageIndex(ClassGuid: TGuid; Index: PInt): Boolean; procedure WMDeviceChange(var Msg: TMessage); message WM_DEVICECHANGE; procedure GetDevInfo; public { Public-Deklarationen } end;var Form1: TForm1; implementation{$R *.DFM}function TForm1.CheckStatus(SelectedItem: DWord; hDevInfo: hDevInfo; StatusFlag: LongWord): Boolean; var DeviceInfoData: TSPDevInfoData; Status, Problem: DWord; begin DeviceInfoData.cbSize := SizeOf(TSPDevInfoData); // Get a handle to the Selected Item. if (not SetupDiEnumDeviceInfo(hDevInfo, SelectedItem, DeviceInfoData)) then begin Result := false; exit; end; if (CM_Get_DevNode_Status(@Status, @Problem, DeviceInfoData.DevInst, 0) <> CR_SUCCESS) then begin Result := false; exit; end; Result := ((Status and StatusFlag = StatusFlag) and not (CM_PROB_HARDWARE_DISABLED = Problem)); end;function TForm1.IsDisabled(SelectedItem: DWord; hDevInfo: hDevInfo): Boolean; var DeviceInfoData: TSPDevInfoData; Status, Problem: DWord; begin DeviceInfoData.cbSize := SizeOf(TSPDevInfoData); // Get a handle to the Selected Item. if (not SetupDiEnumDeviceInfo(hDevInfo, SelectedItem, DeviceInfoData)) then begin Result := false; exit; end; if (CM_Get_DevNode_Status(@Status, @Problem, DeviceInfoData.DevInst, 0) <> CR_SUCCESS) then begin Result := false; exit; end; Result := ((Status and DN_HAS_PROBLEM = DN_HAS_PROBLEM) and (CM_PROB_DISABLED = Problem)); end;function TForm1.StateChange(NewState: DWord; SelectedItem: DWord; hDevInfo: hDevInfo): Boolean; var PropChangeParams: TSPPropChangeParams; DeviceInfoData: TSPDevInfoData; begin PropChangeParams.ClassInstallHeader.cbSize := SizeOf(TSPClassInstallHeader); DeviceInfoData.cbSize := SizeOf(TSPDevInfoData); // Get a handle to the Selected Item. if (not SetupDiEnumDeviceInfo(hDevInfo, SelectedItem, DeviceInfoData)) then begin Result := false; ShowMessage('EnumDeviceInfo'); exit; end; // Set the PropChangeParams structure. PropChangeParams.ClassInstallHeader.InstallFunction := DIF_PROPERTYCHANGE; PropChangeParams.Scope := DICS_FLAG_GLOBAL; PropChangeParams.StateChange := NewState; if (not SetupDiSetClassInstallParams(hDevInfo, @DeviceInfoData, PSPClassInstallHeader(@PropChangeParams), SizeOf(PropChangeParams))) then begin Result := false; ShowMessage('SetClassInstallParams'); exit; end; // Call the ClassInstaller and perform the change. if (not SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, hDevInfo, @DeviceInfoData)) then begin Result := false; ShowMessage('SetClassInstallParams'); exit; end; Result := true; end;function TForm1.GetClassImageIndex(ClassGuid: TGuid; Index: PInt): Boolean; begin Result := SetupDiGetClassImageIndex(ClassImageListData, ClassGuid, Index^); end;function TForm1.GetRegistryProperty(PnPHandle: hDevInfo; DevData: TSPDevInfoData; Prop: DWord; Buffer: PChar; dwLength: DWord): Boolean; var aBuffer: array[0..256] of Char; begin dwLength := 0; aBuffer[0] := #0; SetupDiGetDeviceRegistryProperty(PnPHandle, DevData, Prop, Prop, PBYTE(@aBuffer[0]), SizeOf(aBuffer), dwLength); StrCopy(Buffer, aBuffer); Result := Buffer^ <> #0; end;
function TForm1.ConstructDeviceName(DeviceInfoSet: hDevInfo; DeviceInfoData: TSPDevInfoData; Buffer: PChar; dwLength: DWord): Boolean; const UnknownDevice = '<Unknown Device>'; begin if (not GetRegistryProperty(DeviceInfoSet, DeviceInfoData, SPDRP_FRIENDLYNAME, Buffer, dwLength)) then begin if (not GetRegistryProperty(DeviceInfoSet, DeviceInfoData, SPDRP_DEVICEDESC, Buffer, dwLength)) then begin if (not GetRegistryProperty(DeviceInfoSet, DeviceInfoData, SPDRP_CLASS, Buffer, dwLength)) then begin if (not GetRegistryProperty(DeviceInfoSet, DeviceInfoData, SPDRP_CLASSGUID, Buffer, dwLength)) then begin dwLength := DWord(SizeOf(UnknownDevice)); Buffer := Pointer(LocalAlloc(LPTR, Cardinal(dwLength))); StrCopy(Buffer, UnknownDevice); end; end; end; end; Result := true; end;function TForm1.IsClassHidden(ClassGuid: TGuid): Boolean; var bHidden: Boolean; hKeyClass: HKey; begin bHidden := false; hKeyClass := SetupDiOpenClassRegKey(@ClassGuid, KEY_READ); if (hKeyClass <> 0) then begin bHidden := (RegQueryValueEx(hKeyClass, REGSTR_VAL_NODISPLAYCLASS, nil, nil, nil, nil) = ERROR_SUCCESS); RegCloseKey(hKeyClass); end; Result := bHidden; end;function TForm1.EnumAddDevices(ShowHidden: Boolean; hwndTree: TTreeView; DevInfo: hDevInfo): Boolean; var i, Status, Problem: DWord; pszText: PChar; DeviceInfoData: TSPDevInfoData; iImage: Integer; begin TTreeView(hWndTree).Items.BeginUpdate; DeviceInfoData.cbSize := SizeOf(TSPDevInfoData); // Clean off all the items in a TreeView. TTreeView(hWndTree).Items.Clear; i := 0; // Enumerate though all the devices. while SetupDiEnumDeviceInfo(DevInfo, i, DeviceInfoData) do begin inc(i); // Should we display this device, or move onto the next one. if (CM_Get_DevNode_Status(@Status, @Problem, DeviceInfoData.DevInst, 0) <> CR_SUCCESS) then begin break; end; if (not (ShowHidden or not(Boolean(Status and DN_NO_SHOW_IN_DM) or IsClassHidden(DeviceInfoData.ClassGuid)))) then begin break; end; GetMem(pszText, 256); try // Get a friendly name for the device. ConstructDeviceName(DevInfo, DeviceInfoData, pszText, DWord(nil)); // Try to get an icon index for this device. if (GetClassImageIndex(DeviceInfoData.ClassGuid, @iImage)) then begin with TTreeView(hWndTree).Items.AddObject(nil, pszText, nil) do begin TTreeView(hWndTree).Items[i-1].ImageIndex := iImage; TTreeView(hWndTree).Items[i-1].SelectedIndex := iImage; end; if (Problem = CM_PROB_DISABLED) then // red (X) begin TTreeView(hWndTree).Items[i-1].OverlayIndex := IDI_DISABLED_OVL - IDI_CLASSICON_OVERLAYFIRST; end else begin if (Boolean(Problem)) then // yellow (!) begin TTreeView(hWndTree).Items[i-1].OverlayIndex := IDI_PROBLEM_OVL - IDI_CLASSICON_OVERLAYFIRST; end; end; if (Status and DN_NO_SHOW_IN_DM = DN_NO_SHOW_IN_DM) then // Greyed out begin TTreeView(hWndTree).Items[i-1].Cut := true; end; end; finally FreeMem(pszText); end; end; TTreeView(hWndTree).Items.EndUpdate; Result := true; end;procedure TForm1.GetDevInfo; begin if (assigned(DevInfo)) then begin SetupDiDestroyDeviceInfoList(DevInfo); SetupDiDestroyClassImageList(ClassImageListData); end; // Get a handle to all devices in all classes present on system DevInfo := SetupDiGetClassDevs(nil, nil, 0, DIGCF_PRESENT or DIGCF_ALLCLASSES); if (DevInfo = Pointer(INVALID_HANDLE_VALUE)) then begin ShowMessage('GetClassDevs'); exit; end; // Get the Images for all classes, and bind to the TreeView ClassImageListData.cbSize := SizeOf(TSPClassImageListData); if (not SetupDiGetClassImageList(ClassImageListData)) then begin ShowMessage('GetClassImageList'); exit; end; ImageList.Handle := ClassImageListData.ImageList; TreeView.Images := ImageList; end;procedure TForm1.FormCreate(Sender: TObject); begin if (not LoadSetupAPI) then begin ShowMessage('Could not load SetupAPI.dll'); exit; end; DevInfo := nil; ShowHidden := false; GetDevInfo; // Add the devices to the TreeView window. EnumAddDevices(ShowHidden, TreeView, DevInfo); end;procedure TForm1.FormDestroy(Sender: TObject); begin SetupDiDestroyDeviceInfoList(DevInfo); SetupDiDestroyClassImageList(ClassImageListData); UnloadSetupApi; end;procedure TForm1.mExitClick(Sender: TObject); begin Close; end;procedure TForm1.mRefreshDisplayClick(Sender: TObject); begin EnumAddDevices(ShowHidden, TreeView, DevInfo); end;procedure TForm1.mShowHiddenDevicesClick(Sender: TObject); begin ShowHidden := not ShowHidden; MainMenu.Items[2][2].Checked := ShowHidden; EnumAddDevices(ShowHidden, TreeView, DevInfo); end;
procedure TForm1.TreeViewChange(Sender: TObject; Node: TTreeNode); begin with MainMenu, TreeView.Selected do begin // Clear the Menu Items. Items[1][0].enabled := false; Items[1][1].enabled := false; Items[1][2].enabled := false; if (IsDisabled(Index, DevInfo)) then begin Items[1][0].enabled := true end else begin if (CheckStatus(Index, DevInfo, DN_DISABLEABLE)) then Items[1][1].enabled := true end; if (CheckStatus(Index, DevInfo, DN_REMOVABLE)) then begin Items[1][2].enabled := true; end; end; end;procedure TForm1.TreeViewDblClick(Sender: TObject); begin with TreeView.Selected do begin if (IsDisabled(Index, DevInfo)) then begin ChangeEnableDevice(Self); end else begin if (CheckStatus(Index, DevInfo, DN_DISABLEABLE and DN_REMOVABLE)) then begin MessageBox(Handle, 'Device is disableable and ejectable. Please use MainMenu.', 'Change unknown', MB_OK); end else begin if (CheckStatus(Index, DevInfo, DN_DISABLEABLE)) then ChangeDisableDevice(Self); if (CheckStatus(Index, DevInfo, DN_REMOVABLE)) then EjectDevice(Self); end; end; end; end;procedure TForm1.ChangeEnableDevice(Sender: TObject); begin if (MessageBox(Handle, 'Enable this Device?', 'Change Device Status', MB_OKCANCEL) = IDOK) then begin if (StateChange(DICS_ENABLE, TreeView.Selected.Index, DevInfo)) then EnumAddDevices(ShowHidden, TreeView, DevInfo); end; end;procedure TForm1.ChangeDisableDevice(Sender: TObject); begin if (MessageBox(Handle, 'Disable this Device?', 'Change Device Status', MB_OKCANCEL) = IDOK) then begin if (StateChange(DICS_DISABLE, TreeView.Selected.Index, DevInfo)) then EnumAddDevices(ShowHidden, TreeView, DevInfo); end; end;procedure TForm1.EjectDevice(Sender: TObject); var DeviceInfoData: TSPDevInfoData; Status, Problem: DWord; VetoType: TPNPVetoType; VetoName: array[0..256] of Char; begin if (MessageBox(Handle, 'Eject this Device?', 'Eject Device', MB_OKCANCEL) = IDOK) then begin DeviceInfoData.cbSize := SizeOf(TSPDevInfoData); // Get a handle to the Selected Item. if (not SetupDiEnumDeviceInfo(DevInfo, TreeView.Selected.Index, DeviceInfoData)) then begin exit; end; if (CM_Get_DevNode_Status(@Status, @Problem, DeviceInfoData.DevInst, 0) <> CR_SUCCESS) then begin exit; end; VetoName[0] := #0; case CM_Request_Device_Eject(DeviceInfoData.DevInst, VetoType, @VetoName, SizeOf(VetoName), 0) of CR_SUCCESS: begin EnumAddDevices(ShowHidden, TreeView, DevInfo); end; CR_REMOVE_VETOED: begin MessageBox(Handle, PChar('Failed to eject the Device (Veto: ' + VetoName + ')'), 'Vetoed', MB_OK); end; else begin MessageBox(Handle, PChar('Failed to eject the Device (' + SysErrorMessage(GetLastError) + ')'), 'Failure', MB_OK); end; end; end; end;procedure TForm1.WMDeviceChange(var Msg: TMessage); const DBT_DEVNODES_CHANGED = $0007; begin case Msg.wParam of DBT_DEVNODES_CHANGED: begin GetDevInfo; EnumAddDevices(ShowHidden, TreeView, DevInfo); end; end; end;end.
二楼写的也太多了吧,我给你个简单的 先在type后加上 procedure WMDeviceChange(var Msg: TMessage); message WM_DEVICECHANGE;实现部分如下 procedure TForm1.WMDeviceChange(var Msg: TMessage); var myMsg : String; begin Case Msg.WParam of 32768: begin myMsg :='U盘插入'; MessageBox(0,'注意!U盘已插入!!!','AutoCopy Information',MB_ICONASTERISK and MB_ICONINFORMATION); end; 32772: begin myMsg :='U盘拔出'; MessageBox(0,'注意!U盘已取走!!!','AutoCopy Information',MB_ICONASTERISK and MB_ICONINFORMATION); end; end; end;
//看哪个盘是U盘 procedure TForm1.Button1Click(Sender: TObject); var buf:array [0..MAX_PATH-1] of char; m_Result:Integer; i:Integer; str_temp:string; begin m_Result:=GetLogicalDriveStrings(MAX_PATH,buf); for i:=0 to (m_Result div 4) do begin str_temp:=string(buf[i*4]+buf[i*4+1]+buf[i*4+2]); if GetDriveType(pchar(str_temp)) = DRIVE_REMOVABLE then begin ShowMessage(str_temp+'盘为U盘'); ListBox1.Items.Add(str_temp); end; end;//检测磁盘容量用DiskSize,检测磁盘剩余空间用DiskFree
这个是我不久前发的帖子...
http://topic.csdn.net/u/20080317/15/e4c14af2-f057-4b94-bbbf-970328517739.html
源码还在我那个邮箱中 你可以看usbview得源码...比我写的清晰...
我已经基本上解决了...
?2004 yeahware software development
http://www.yeahware.com If this code works, it was written by Markus Fuchs.
If not, I don't know who wrote it. (Paul DiLascia) Module : eject
Source File Name : ueject.pas
Description : Ejects Firewire/USB HDDs and USB sticks
Compiler : Delphi 5
Platforms : Windows 9x and NT platform
Created : 08.05.2004
Revisions : 19.08.2006 - Update device list with
every DBT_DEVNODES_CHANGED
*************************************************************)unit ueject;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ImgList, ComCtrls, StdCtrls, SetupAPI, ExtCtrls, Menus;
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_VetoPendingClose,
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;
MainMenu: TMainMenu;
mFile: TMenuItem;
mExit: TMenuItem;
mChange: TMenuItem;
mEnableDevice: TMenuItem;
mDisableDevice: TMenuItem;
mOptions: TMenuItem;
mRefreshDisplay: TMenuItem;
mShowHiddenDevices: TMenuItem;
mN: TMenuItem;
mRemoveDevice: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure mExitClick(Sender: TObject);
procedure mRefreshDisplayClick(Sender: TObject);
procedure mShowHiddenDevicesClick(Sender: TObject);
procedure TreeViewChange(Sender: TObject; Node: TTreeNode);
procedure TreeViewDblClick(Sender: TObject);
procedure ChangeEnableDevice(Sender: TObject);
procedure ChangeDisableDevice(Sender: TObject);
procedure EjectDevice(Sender: TObject);
private
{ Private-Deklarationen }
DevInfo: hDevInfo;
ClassImageListData: TSPClassImageListData;
ShowHidden: Boolean;
function CheckStatus(SelectedItem: DWord; hDevInfo: hDevInfo; StatusFlag: LongWord): Boolean;
function IsDisabled(SelectedItem: DWord; hDevInfo: hDevInfo): Boolean;
function StateChange(NewState: DWord; SelectedItem: DWord; hDevInfo: hDevInfo): 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 IsClassHidden(ClassGuid: TGuid): Boolean;
function EnumAddDevices(ShowHidden: Boolean; hwndTree: TTreeView; DevInfo: hDevInfo): Boolean;
function GetClassImageIndex(ClassGuid: TGuid; Index: PInt): Boolean;
procedure WMDeviceChange(var Msg: TMessage); message WM_DEVICECHANGE;
procedure GetDevInfo;
public
{ Public-Deklarationen }
end;var
Form1: TForm1;
implementation{$R *.DFM}function TForm1.CheckStatus(SelectedItem: DWord; hDevInfo: hDevInfo; StatusFlag: LongWord): Boolean;
var
DeviceInfoData: TSPDevInfoData;
Status, Problem: DWord;
begin
DeviceInfoData.cbSize := SizeOf(TSPDevInfoData);
// Get a handle to the Selected Item.
if (not SetupDiEnumDeviceInfo(hDevInfo, SelectedItem, DeviceInfoData)) then
begin
Result := false;
exit;
end;
if (CM_Get_DevNode_Status(@Status, @Problem, DeviceInfoData.DevInst, 0) <> CR_SUCCESS) then
begin
Result := false;
exit;
end;
Result := ((Status and StatusFlag = StatusFlag) and not (CM_PROB_HARDWARE_DISABLED = Problem));
end;function TForm1.IsDisabled(SelectedItem: DWord; hDevInfo: hDevInfo): Boolean;
var
DeviceInfoData: TSPDevInfoData;
Status, Problem: DWord;
begin
DeviceInfoData.cbSize := SizeOf(TSPDevInfoData);
// Get a handle to the Selected Item.
if (not SetupDiEnumDeviceInfo(hDevInfo, SelectedItem, DeviceInfoData)) then
begin
Result := false;
exit;
end;
if (CM_Get_DevNode_Status(@Status, @Problem, DeviceInfoData.DevInst, 0) <> CR_SUCCESS) then
begin
Result := false;
exit;
end;
Result := ((Status and DN_HAS_PROBLEM = DN_HAS_PROBLEM) and (CM_PROB_DISABLED = Problem));
end;function TForm1.StateChange(NewState: DWord; SelectedItem: DWord; hDevInfo: hDevInfo): Boolean;
var
PropChangeParams: TSPPropChangeParams;
DeviceInfoData: TSPDevInfoData;
begin
PropChangeParams.ClassInstallHeader.cbSize := SizeOf(TSPClassInstallHeader);
DeviceInfoData.cbSize := SizeOf(TSPDevInfoData);
// Get a handle to the Selected Item.
if (not SetupDiEnumDeviceInfo(hDevInfo, SelectedItem, DeviceInfoData)) then
begin
Result := false;
ShowMessage('EnumDeviceInfo');
exit;
end;
// Set the PropChangeParams structure.
PropChangeParams.ClassInstallHeader.InstallFunction := DIF_PROPERTYCHANGE;
PropChangeParams.Scope := DICS_FLAG_GLOBAL;
PropChangeParams.StateChange := NewState;
if (not SetupDiSetClassInstallParams(hDevInfo, @DeviceInfoData,
PSPClassInstallHeader(@PropChangeParams), SizeOf(PropChangeParams))) then
begin
Result := false;
ShowMessage('SetClassInstallParams');
exit;
end;
// Call the ClassInstaller and perform the change.
if (not SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, hDevInfo, @DeviceInfoData)) then
begin
Result := false;
ShowMessage('SetClassInstallParams');
exit;
end;
Result := true;
end;function TForm1.GetClassImageIndex(ClassGuid: TGuid; Index: PInt): Boolean;
begin
Result := SetupDiGetClassImageIndex(ClassImageListData, ClassGuid, Index^);
end;function TForm1.GetRegistryProperty(PnPHandle: hDevInfo; DevData: TSPDevInfoData; Prop: DWord; Buffer: PChar; dwLength: DWord): Boolean;
var
aBuffer: array[0..256] of Char;
begin
dwLength := 0;
aBuffer[0] := #0;
SetupDiGetDeviceRegistryProperty(PnPHandle, DevData, Prop, Prop, PBYTE(@aBuffer[0]), SizeOf(aBuffer), dwLength);
StrCopy(Buffer, aBuffer);
Result := Buffer^ <> #0;
end;
DeviceInfoData: TSPDevInfoData; Buffer: PChar; dwLength: DWord): Boolean;
const
UnknownDevice = '<Unknown Device>';
begin
if (not GetRegistryProperty(DeviceInfoSet, DeviceInfoData, SPDRP_FRIENDLYNAME, Buffer, dwLength)) then
begin
if (not GetRegistryProperty(DeviceInfoSet, DeviceInfoData, SPDRP_DEVICEDESC, Buffer, dwLength)) then
begin
if (not GetRegistryProperty(DeviceInfoSet, DeviceInfoData, SPDRP_CLASS, Buffer, dwLength)) then
begin
if (not GetRegistryProperty(DeviceInfoSet, DeviceInfoData, SPDRP_CLASSGUID, Buffer, dwLength)) then
begin
dwLength := DWord(SizeOf(UnknownDevice));
Buffer := Pointer(LocalAlloc(LPTR, Cardinal(dwLength)));
StrCopy(Buffer, UnknownDevice);
end;
end;
end;
end;
Result := true;
end;function TForm1.IsClassHidden(ClassGuid: TGuid): Boolean;
var
bHidden: Boolean;
hKeyClass: HKey;
begin
bHidden := false;
hKeyClass := SetupDiOpenClassRegKey(@ClassGuid, KEY_READ);
if (hKeyClass <> 0) then
begin
bHidden := (RegQueryValueEx(hKeyClass, REGSTR_VAL_NODISPLAYCLASS, nil, nil, nil, nil) = ERROR_SUCCESS);
RegCloseKey(hKeyClass);
end;
Result := bHidden;
end;function TForm1.EnumAddDevices(ShowHidden: Boolean; hwndTree: TTreeView; DevInfo: hDevInfo): Boolean;
var
i, Status, Problem: DWord;
pszText: PChar;
DeviceInfoData: TSPDevInfoData;
iImage: Integer;
begin
TTreeView(hWndTree).Items.BeginUpdate;
DeviceInfoData.cbSize := SizeOf(TSPDevInfoData);
// Clean off all the items in a TreeView.
TTreeView(hWndTree).Items.Clear;
i := 0;
// Enumerate though all the devices.
while SetupDiEnumDeviceInfo(DevInfo, i, DeviceInfoData) do
begin
inc(i);
// Should we display this device, or move onto the next one.
if (CM_Get_DevNode_Status(@Status, @Problem, DeviceInfoData.DevInst, 0) <> CR_SUCCESS) then
begin
break;
end;
if (not (ShowHidden or not(Boolean(Status and DN_NO_SHOW_IN_DM) or IsClassHidden(DeviceInfoData.ClassGuid)))) then
begin
break;
end;
GetMem(pszText, 256);
try
// Get a friendly name for the device.
ConstructDeviceName(DevInfo, DeviceInfoData, pszText, DWord(nil));
// Try to get an icon index for this device.
if (GetClassImageIndex(DeviceInfoData.ClassGuid, @iImage)) then
begin
with TTreeView(hWndTree).Items.AddObject(nil, pszText, nil) do
begin
TTreeView(hWndTree).Items[i-1].ImageIndex := iImage;
TTreeView(hWndTree).Items[i-1].SelectedIndex := iImage;
end;
if (Problem = CM_PROB_DISABLED) then // red (X)
begin
TTreeView(hWndTree).Items[i-1].OverlayIndex := IDI_DISABLED_OVL - IDI_CLASSICON_OVERLAYFIRST;
end else
begin
if (Boolean(Problem)) then // yellow (!)
begin
TTreeView(hWndTree).Items[i-1].OverlayIndex := IDI_PROBLEM_OVL - IDI_CLASSICON_OVERLAYFIRST;
end;
end;
if (Status and DN_NO_SHOW_IN_DM = DN_NO_SHOW_IN_DM) then // Greyed out
begin
TTreeView(hWndTree).Items[i-1].Cut := true;
end;
end;
finally
FreeMem(pszText);
end;
end;
TTreeView(hWndTree).Items.EndUpdate;
Result := true;
end;procedure TForm1.GetDevInfo;
begin
if (assigned(DevInfo)) then
begin
SetupDiDestroyDeviceInfoList(DevInfo);
SetupDiDestroyClassImageList(ClassImageListData);
end;
// Get a handle to all devices in all classes present on system
DevInfo := SetupDiGetClassDevs(nil, nil, 0, DIGCF_PRESENT or DIGCF_ALLCLASSES);
if (DevInfo = Pointer(INVALID_HANDLE_VALUE)) then
begin
ShowMessage('GetClassDevs');
exit;
end;
// Get the Images for all classes, and bind to the TreeView
ClassImageListData.cbSize := SizeOf(TSPClassImageListData);
if (not SetupDiGetClassImageList(ClassImageListData)) then
begin
ShowMessage('GetClassImageList');
exit;
end;
ImageList.Handle := ClassImageListData.ImageList;
TreeView.Images := ImageList;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
if (not LoadSetupAPI) then
begin
ShowMessage('Could not load SetupAPI.dll');
exit;
end;
DevInfo := nil;
ShowHidden := false;
GetDevInfo;
// Add the devices to the TreeView window.
EnumAddDevices(ShowHidden, TreeView, DevInfo);
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
SetupDiDestroyDeviceInfoList(DevInfo);
SetupDiDestroyClassImageList(ClassImageListData);
UnloadSetupApi;
end;procedure TForm1.mExitClick(Sender: TObject);
begin
Close;
end;procedure TForm1.mRefreshDisplayClick(Sender: TObject);
begin
EnumAddDevices(ShowHidden, TreeView, DevInfo);
end;procedure TForm1.mShowHiddenDevicesClick(Sender: TObject);
begin
ShowHidden := not ShowHidden;
MainMenu.Items[2][2].Checked := ShowHidden;
EnumAddDevices(ShowHidden, TreeView, DevInfo);
end;
begin
with MainMenu, TreeView.Selected do
begin
// Clear the Menu Items.
Items[1][0].enabled := false;
Items[1][1].enabled := false;
Items[1][2].enabled := false;
if (IsDisabled(Index, DevInfo)) then
begin
Items[1][0].enabled := true
end else
begin
if (CheckStatus(Index, DevInfo, DN_DISABLEABLE)) then Items[1][1].enabled := true
end;
if (CheckStatus(Index, DevInfo, DN_REMOVABLE)) then
begin
Items[1][2].enabled := true;
end;
end;
end;procedure TForm1.TreeViewDblClick(Sender: TObject);
begin
with TreeView.Selected do
begin
if (IsDisabled(Index, DevInfo)) then
begin
ChangeEnableDevice(Self);
end else
begin
if (CheckStatus(Index, DevInfo, DN_DISABLEABLE and DN_REMOVABLE)) then
begin
MessageBox(Handle, 'Device is disableable and ejectable. Please use MainMenu.', 'Change unknown', MB_OK);
end
else
begin
if (CheckStatus(Index, DevInfo, DN_DISABLEABLE)) then ChangeDisableDevice(Self);
if (CheckStatus(Index, DevInfo, DN_REMOVABLE)) then EjectDevice(Self);
end;
end;
end;
end;procedure TForm1.ChangeEnableDevice(Sender: TObject);
begin
if (MessageBox(Handle, 'Enable this Device?', 'Change Device Status', MB_OKCANCEL) = IDOK) then
begin
if (StateChange(DICS_ENABLE, TreeView.Selected.Index, DevInfo)) then EnumAddDevices(ShowHidden, TreeView, DevInfo);
end;
end;procedure TForm1.ChangeDisableDevice(Sender: TObject);
begin
if (MessageBox(Handle, 'Disable this Device?', 'Change Device Status', MB_OKCANCEL) = IDOK) then
begin
if (StateChange(DICS_DISABLE, TreeView.Selected.Index, DevInfo)) then EnumAddDevices(ShowHidden, TreeView, DevInfo);
end;
end;procedure TForm1.EjectDevice(Sender: TObject);
var
DeviceInfoData: TSPDevInfoData;
Status, Problem: DWord;
VetoType: TPNPVetoType;
VetoName: array[0..256] of Char;
begin
if (MessageBox(Handle, 'Eject this Device?', 'Eject Device', MB_OKCANCEL) = IDOK) then
begin
DeviceInfoData.cbSize := SizeOf(TSPDevInfoData);
// Get a handle to the Selected Item.
if (not SetupDiEnumDeviceInfo(DevInfo, TreeView.Selected.Index, DeviceInfoData)) then
begin
exit;
end;
if (CM_Get_DevNode_Status(@Status, @Problem, DeviceInfoData.DevInst, 0) <> CR_SUCCESS) then
begin
exit;
end;
VetoName[0] := #0;
case CM_Request_Device_Eject(DeviceInfoData.DevInst, VetoType, @VetoName, SizeOf(VetoName), 0) of
CR_SUCCESS:
begin
EnumAddDevices(ShowHidden, TreeView, DevInfo);
end;
CR_REMOVE_VETOED:
begin
MessageBox(Handle, PChar('Failed to eject the Device (Veto: ' + VetoName + ')'), 'Vetoed', MB_OK);
end;
else
begin
MessageBox(Handle, PChar('Failed to eject the Device (' + SysErrorMessage(GetLastError) + ')'), 'Failure', MB_OK);
end;
end;
end;
end;procedure TForm1.WMDeviceChange(var Msg: TMessage);
const
DBT_DEVNODES_CHANGED = $0007;
begin
case Msg.wParam of
DBT_DEVNODES_CHANGED:
begin
GetDevInfo;
EnumAddDevices(ShowHidden, TreeView, DevInfo);
end;
end;
end;end.
To BrightLiuW3 ,能不能把你源码发给我一份[email protected]
先在type后加上
procedure WMDeviceChange(var Msg: TMessage); message WM_DEVICECHANGE;实现部分如下
procedure TForm1.WMDeviceChange(var Msg: TMessage);
var
myMsg : String;
begin
Case Msg.WParam of
32768:
begin
myMsg :='U盘插入';
MessageBox(0,'注意!U盘已插入!!!','AutoCopy Information',MB_ICONASTERISK and MB_ICONINFORMATION);
end;
32772:
begin
myMsg :='U盘拔出';
MessageBox(0,'注意!U盘已取走!!!','AutoCopy Information',MB_ICONASTERISK and MB_ICONINFORMATION);
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
buf:array [0..MAX_PATH-1] of char;
m_Result:Integer;
i:Integer;
str_temp:string;
begin
m_Result:=GetLogicalDriveStrings(MAX_PATH,buf);
for i:=0 to (m_Result div 4) do
begin
str_temp:=string(buf[i*4]+buf[i*4+1]+buf[i*4+2]);
if GetDriveType(pchar(str_temp)) = DRIVE_REMOVABLE then
begin
ShowMessage(str_temp+'盘为U盘');
ListBox1.Items.Add(str_temp);
end;
end;//检测磁盘容量用DiskSize,检测磁盘剩余空间用DiskFree
//=========================================//磁盘类型常量参考//DRIVE_UNKNOWN = 0;
//{$EXTERNALSYM DRIVE_UNKNOWN}
//DRIVE_NO_ROOT_DIR = 1;
//{$EXTERNALSYM DRIVE_NO_ROOT_DIR}
//DRIVE_REMOVABLE = 2;
//{$EXTERNALSYM DRIVE_REMOVABLE}
//DRIVE_FIXED = 3;
//{$EXTERNALSYM DRIVE_FIXED}
//DRIVE_REMOTE = 4;
//{$EXTERNALSYM DRIVE_REMOTE}
//DRIVE_CDROM = 5;
//{$EXTERNALSYM DRIVE_CDROM}
//DRIVE_RAMDISK = 6;
如何获取磁盘分区信息(包括卷标、序列号、类型、总大小、剩余大小)
{
文章名 : 如何获取磁盘分区信息(包括卷标、序列号、类型、总大小、剩余大小)
说明 : 无
作者 : JJony
QQ : 254706028
博客 : http://blog.csdn.net/jzj_jony
空间 : http://jonychen,ys168.com
测试环境 : WinXPSP2+Delphi7
声明 : 您可以任意转载,但请注明文章作者和出处
}最近看到有人在问"如何知道分区剩余空间大小",看看也没人回答或者回答的不太准确,
闲来无事就写了个小程序,当然获得的信息更全面一点,下面就是我的函数源代码:
//其中VolumeName,VolumeSerialNumber,PartitionType,TotalSpace,TotalFreeSpace为
//返回参数,包括分区的各种信息
Function GetHardDiskPartitionInfo(const DriveLetter:Char;
var VolumeName,VolumeSerialNumber,PartitionType:string;
var TotalSpace,TotalFreeSpace:string): string;
var
NotUsed: DWORD;
VolumeFlags: DWORD;
VolumeInfo: array[0..MAX_PATH] of Char;
VSNumber: DWORD;
PType: array[0..32] of Char;
VName:array[0..32] of Char;
FreeS,TotalS:Int64;
TotalF:Int64;
begin
if not GetVolumeInformation(PChar(DriveLetter + ':\'),
@VName, SizeOf(VolumeInfo), @VSNumber, NotUsed,
VolumeFlags, PType, 32) then
result:='卷信息未能完全返回'#13#10;
VolumeName:=strpas(VName);
VolumeSerialNumber:=InttoHex(VSNumber,8);
PartitionType:=StrPas(PType);
if not GetDiskFreeSpaceEx(PChar(DriveLetter + ':\'),FreeS,TotalS,@TotalF) then
result:=result+'获取卷空间大小失败';
TotalSpace:=FloatToStr(Round(TotalS / 1024 / 1024 /1024 * 100) / 100);
TotalFreeSpace:=FloatToStr(Round(TotalF / 1024 / 1024 /1024 * 100) / 100);
end;
//函数很简单,一看就明白
注意:根据MSDN的说明,如果分区大于2G一定要用GetDiskFreeSpaceEx
//以下是调用实例
procedure TForm1.Button1Click(Sender: TObject);
var
VName,VSNumber,PType:string;
TotalS,TotalF:string;
begin
GetHardDiskPartitionInfo('h',VName,VSNumber,PType, TotalS,TotalF);//[color=#FF0000]假设h盘为U盘
memo1.Lines.Add('卷标:'+VName);
memo1.Lines.Add('卷序列号:'+VSNumber);
memo1.Lines.Add('分区类型:'+PType);
memo1.Lines.Add('分区总空间大小:'+TotalS+' G');
memo1.Lines.Add('分区剩余空间大小:'+TotalF+' G');
end;
[/color]