如何使用CreateFile访问物理磁盘? [英] How do I use CreateFile to access a physical disk?

查看:573
本文介绍了如何使用CreateFile访问物理磁盘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Lazarus编程论坛上问过如何打开物理磁盘.我想允许用户在单击选择磁盘"按钮时从系统中选择物理磁盘.在Stack Overflow上有一些相似但不完全相同的示例(例如

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)).

有很多使用CreateFile的C和C ++示例(

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.

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

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?

推荐答案

您的C示例具有以下代码:

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

将函数调用转换为Delphi只需更改语法即可:

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

也就是说,将:=用于分配,将or用于组合位标志,将nil用于空指针,将0用于空文件句柄.

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

此函数被调用:

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

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

bResult = GetDriveGeometry (wszDrive, &pdg);

同样,只需将语法更改为Delphi:

Again, just change the syntax to Delphi:

const wszDrive = '\\.\PhysicalDrive0';

var pdg: DISK_GEOMETRY;

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

Delphi无类型字符串常量自动是上下文中所需的任何类型,因此我们不需要像C用法那样的任何L前缀.反斜杠在Delphi中并不特殊,因此不需要转义. Delphi不允许在声明中初始化局部变量,因此我们使用ZeroMemory将所有内容设置为零.使用@而不是&获取指向变量的指针.

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 Delp 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.

这篇关于如何使用CreateFile访问物理磁盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆