Function SetCDRomDoorStatus(CDRomNo:Word;Open:Boolean):Boolean;
Var
CDR:THandle;
C:DWORD;
DevName:String;
PMR:PREVENT_MEDIA_REMOVAL;
Begin
Result:=False;
DevName:=Format('\\.\CdRom%d',[CDRomNo]);
CDR:=CreateFile(PChar(DevName),GENERIC_READ,FILE_SHARE_READ,Nil,OPEN_EXISTING,0,0);
//如果无法打开设备,就退出
If CDR=INVALID_HANDLE_VALUE Then Exit;
Try
If Open Then
Begin
If DeviceIoControl(CDR,FSCTL_LOCK_VOLUME,Nil,0,Nil,0,C,Nil) Then
Begin
If DeviceIoControl(CDR,FSCTL_DISMOUNT_VOLUME,Nil,0,Nil,0,C,Nil) Then
Begin
PMR.PreventMediaRemoval:=False;
If DeviceIoControl(CDR,IOCTL_STORAGE_MEDIA_REMOVAL,@PMR,SizeOf(PMR),Nil,0,C,Nil) Then
Begin
Result:=DeviceIoControl(CDR,IOCTL_STORAGE_EJECT_MEDIA,Nil,0,Nil,0,C,Nil);
End;
End;
DeviceIoControl(CDR,FSCTL_UNLOCK_VOLUME,Nil,0,Nil,0,C,Nil);
End;
End
Else
Begin
Result:=DeviceIoControl(CDR,IOCTL_STORAGE_LOAD_MEDIA,Nil,0,Nil,0,C,Nil);
End;
Finally
CloseHandle(CDR);
End;
End;
获得光驱个数
Function GetCDRomCount:LongWord;
Var
P:PChar;//临时缓冲区
S:PChar;//当前字符串指针
BufLen:LongWord;//临时缓冲区的大小
RealBufLen:LongWord;//缓冲区中数据的大小
Retry:Boolean;//是否继续尝试获取DOS设备列表
Begin
Retry:=True;
Result:=0;
//首先分配1000字节的Buffer并清0
BufLen:=1000;
GetMem(P,BufLen);
FillChar(P^,BufLen,0);
Repeat
//尝试QueryDosDevice
RealBufLen:=QueryDosDevice(Nil,P,BufLen);
If RealBufLen=0 Then
Begin
//若返回0则说明函数调用失败
//释放Buffer,以1000字节的增量递增Buffer
FreeMem(P);
Inc(BufLen,1000);
GetMem(P,BufLen);
FillChar(P^,BufLen,0);
End
Else
Begin
Retry:=False;
End;
Until (Not Retry) Or (BufLen>=16000);//最大分配16000字节
S:=P;//指向PChar序列的开头
While Byte(S^)<>0 Do//PChar序列由一个#0结尾
Begin
If Pos('cdrom',AnsiLowerCase(S))=1 Then//CDROM设备开头是CdRom
Begin
Inc(Result);
End;
//跳到下一个PChar
While Byte(S^)<>0 Do Inc(S);
Inc(S);
End;
FreeMem(P);//释放缓冲区
End;