禁止弹出光驱
下面的代码需要光驱支持才行,不一定对所有光驱起作用。
type
TPREVENT_MEDIA_REMOVAL = packed record
PreventMediaRemoval: LongBool;
end;
const
IOCTL_STORAGE_MEDIA_REMOVAL = $002d4804;
procedure PreventEjection(Drive: char; Prevent: boolean);
var
DeviceName: string;
Device: THandle;
b: boolean;
BufIn: TPREVENT_MEDIA_REMOVAL;
BytesReturned: DWORD;
begin
DeviceName := '\\.\' + Drive + ':';
Device := CreateFile(PChar(DeviceName),
GENERIC_READ,
FILE_SHARE_READ or FILE_SHARE_WRITE,
nil,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if Device <> INVALID_HANDLE_VALUE then
begin
BufIn.PreventMediaRemoval := Prevent;
b := DeviceIOControl(Device,
IOCTL_STORAGE_MEDIA_REMOVAL,
@BufIn,
SizeOf(BufIn),
nil,
0,
BytesReturned,
nil);
CloseHandle(Device);
if not b then RaiseLastWin32Error;
end;
end;