如何使用CreateFile访问物理磁盘?

发布于 2024-12-21 00:00:29 字数 887 浏览 2 评论 0原文

我在 Lazarus 编程论坛上询问如何打开物理磁盘。我想允许用户在单击“选择磁盘”按钮时从系统中选择物理磁盘。 Stack Overflow 上有一些类似但不完全相同的示例(例如 Delphi - 使用 DeviceIoControl 传递 IOCTL_DISK_GET_LENGTH_INFO 来获取闪存介质物理大小(不是分区))。

有很多使用 CreateFile 的 C 和 C++ 示例 (在文档中,尤其是调用 DeviceIoControl 的示例),但我找不到 Free Pascal 或 Delphi 的任何内容,而且我还不够好,无法弄清楚如何做到这一点。

谁能给我指出一个解释它的链接的方向,或者更好的是用 Delphi 或 Free Pascal 编写的实际示例?谁能帮助我了解如何使用它?

I asked on the Lazarus programming forum how to open a physical disk. I want to allow the user to select physical disks from their system when they click a "Select Disk" button. There are some examples here at Stack Overflow that are similar but not quite the same (such as Delphi - Using DeviceIoControl passing IOCTL_DISK_GET_LENGTH_INFO to get flash media physical size (Not Partition)).

There are lots of C and C++ examples of using CreateFile (in the documentation and especially an example of calling DeviceIoControl) but I can't find any for Free Pascal or Delphi and I am not good enough yet to work out how to do it.

Can anyone point me in the direction of a link that explains it or better still an actual example written in Delphi or Free Pascal? Can anyone help me understand how to use it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谎言月老 2024-12-28 00:00:29

您的 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);

也就是说,使用 := 进行赋值,使用 or 组合位标志, 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 将所有内容设置为零。使用 @ 而不是 & 来获取指向变量的指针。

Your C example has this code:

/* 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

Converting that function call to Delphi is just a matter of changing the syntax:

// wszPath: PWideChar

hDevice := CreateFileW(wszPath,
                       0,
                       FILE_SHARE_READ or
                       FILE_SHARE_WRITE,
                       nil,
                       OPEN_EXISTING,
                       0,
                       0);

That is, use := for assignment, or for combining bit flags, nil for null pointers, and 0 for null file handles.

The function is called with this:

#define wszDrive L"\\\\.\\PhysicalDrive0"

DISK_GEOMETRY pdg = { 0 }; // disk drive geometry structure

bResult = GetDriveGeometry (wszDrive, &pdg);

Again, just change the syntax to Delphi:

const wszDrive = '\\.\PhysicalDrive0';

var pdg: DISK_GEOMETRY;

ZeroMemory(@pdg, SizeOf(pdg));
bResult := GetDriveGeometry(wszDrive, @pdg);

Delphi untyped string constants are automatically whatever type they need to be in context, so we don't need any L prefix like C uses. Backslashes aren't special in Delphi, so they don't need to be escaped. Delphi doesn't allow initializing local variables in the declaration, so we use ZeroMemory to set everything to zero. Use @ instead of & to get a pointer to a variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文