我有以下代码:
if (APartitionStyle = 0) then //mbr
begin
hDevice := CreateFile(
PChar(ADisk),
GENERIC_WRITE and GENERIC_READ,
FILE_SHARE_WRITE and FILE_SHARE_READ,
nil,
OPEN_EXISTING,
0,
0);
error := SysErrorMessage(GetLastError);
if (hDevice = INVALID_HANDLE_VALUE) then
begin
error := SysErrorMessage(GetLastError);
result := error;
end;
dwIoControlCode := IOCTL_DISK_CREATE_DISK;
dsk.PartitionStyle := PARTITION_STYLE_MBR;
dsk.mbr.Signature := Random(9999);
lpInBuffer := @dsk;
nInBufferSize := sizeof(CREATE_DISK);
lpOutBuffer := nil;
nOutBufferSize := 0;
lpOverlapped := nil;
bresult := DeviceIOControl(
hDevice,
dwIoControlCode,
lpInBuffer,
nInBufferSize,
lpOutBuffer,
nOutBufferSize,
lpBytesReturned,
lpOverlapped);
if not bresult then
begin
error := SysErrorMessage(GetLastError);
result := error;
end;
我已经以管理员或系统以及用户身份(使用admin privilegs)执行了代码。
我读过类似的文章:驱动程序已锁定。代码中缺少什么吗?
该句柄已成功创建。在DeviceIOControl上,出现错误“访问被拒绝”。
最佳答案
您正在将错误的值传递给CreateFile()
。您正在使用and
运算符组合位标志:
hDevice := CreateFile(
PChar(ADisk),
GENERIC_WRITE and GENERIC_READ, { = 0 ! }
FILE_SHARE_WRITE and FILE_SHARE_READ, { = 0 ! }
nil,
OPEN_EXISTING,
0,
0);
您需要使用
or
运算符:hDevice := CreateFile(
PChar(ADisk),
GENERIC_WRITE or GENERIC_READ, { = $C0000000 ! }
FILE_SHARE_WRITE or FILE_SHARE_READ, { = $00000003 ! }
nil,
OPEN_EXISTING,
0,
0);