我在Lazarus编程论坛 how to open a physical disk上询问过.我想让用户在单击“选择磁盘”按钮时从系统中选择物理磁盘. Stack Overflow中有一些示例类似但不完全相同(例如 Delphi – Using DeviceIoCo
有很多使用CreateFile(in the documentation,特别是an example of calling DeviceIoControl)的C和C示例,但我找不到任何Free Pascal或Delphi,我还不够好,还没有找到如何做到这一点.
任何人都可以指向我解释它的链接的方向或更好仍然是用Delphi或Free Pascal编写的实际示例?任何人都可以帮我理解如何使用它吗?
您的C示例包含以下代码:/* LPWSTR wszPath */
hDevice = CreateFileW(wszPath, // drive to open
0, // no access to the drive
FILE_SHARE_READ | // share mode
FILE_SHARE_WRITE,
NULL, // default security attributes
OPEN_EXISTING, // disposition
0, // file attributes
NULL); // do not copy file attributes
将该函数调用转换为Delphi只需更改语法:
// wszPath: PWideChar
hDevice := CreateFileW(wszPath,
0,
FILE_SHARE_READ or
FILE_SHARE_WRITE,
nil,
OPEN_EXISTING,
0,
0);
也就是说,使用:=用于赋值,或用于组合位标志,nil用于空指针,0用于空文件句柄.
该函数调用如下:
#define wszDrive L"\\\\.\\PhysicalDrive0"
DISK_GEOMETRY pdg = { 0 }; // disk drive geometry structure
bResult = GetDriveGeometry (wszDrive, &pdg);
再次,只需将语法更改为Delphi:
const wszDrive = '\\.\PhysicalDrive0'; var pdg: DISK_GEOMETRY; ZeroMemory(@pdg, SizeOf(pdg)); bResult := GetDriveGeometry(wszDrive, @pdg);
Delphi无类型字符串常量自动是它们需要在上下文中的任何类型,因此我们不需要像C使用的任何L前缀.反斜杠在Delphi中并不特殊,因此不需要进行转义. Delphi不允许在声明中初始化局部变量,因此我们使用ZeroMemory将所有内容设置为零.使用@代替&获取指向变量的指针.
