LPCTSTR lpFileName, // pointer to name of the file
DWORD dwDesiredAccess, // access (read-write) mode
DWORD dwShareMode, // share mode
LPSECURITY_ATTRIBUTES lpSecurityAttributes, // pointer to security attributes
DWORD dwCreationDistribution, // how to create
DWORD dwFlagsAndAttributes, // file attributes
HANDLE hTemplateFile // handle to file with attributes to copy
);
大家注意第一个参数的说明:
lpFileName
Points to a null-terminated string that specifies the name of the object (file, pipe, mailslot, communications resource, disk device, console, or directory) to create or open.
If *lpFileName is a path, there is a default string size limit of MAX_PATH characters. This limit is related to how the CreateFile function parses paths.
Windows NT: You can use paths longer than MAX_PATH characters by calling the wide (W) version of CreateFile and prepending "\\?\" to the path. The "\\?\" tells the function to turn off path parsing. This lets you use paths that are nearly 32,000 Unicode characters long. You must use fully-qualified paths with this technique. This also works with UNC names. The "\\?\" is ignored as part of the path. For example, "\\?\C:\myworld\private" is seen as "C:\myworld\private", and "\\?\UNC\tom_1\hotstuff\coolapps" is seen as "\\tom_1\hotstuff\coolapps".
上面的意思大概是说"\\?\"可以引用路径(英语垃圾,大家最好自己看)。
尤其是下面的这一段:
Disk Devices
Windows NT: You can use the CreateFile function to open a disk drive or a partition on a disk drive. The function returns a handle to the disk device; that handle can be used with the DeviceIOControl function. The following requirements must be met in order for such a call to succeed:
The caller must have administrative privileges for the operation to succeed on a hard disk drive.
The lpFileName string should be of the form \\.\PHYSICALDRIVEx to open the hard disk x. Hard disk numbers start at zero.For example:
String Meaning
\\.\PHYSICALDRIVE2 Obtains a handle to the third physical drive on the user's computer.
The lpFileName string should be \\.\x: to open a floppy drive x or a partition x on a hard disk.For example:
String Meaning
\\.\A: Obtains a handle to drive A on the user's computer.
\\.\C: Obtains a handle to drive C on the user's computer.
没有骗大家吧,上面说如果你拥有管理员权限,那么lpFileName ==\\.\PHYSICALDRIVE2就是表示打开第三块硬盘,\\.\PHYSICALDRIVE0指的就是第一块硬盘了。lpFileName ==\\.\C:就是打开C盘了。并且返回的句柄还可以用于DeviceIOControl函数,相信大家看到这里应该放下手中的鸡蛋了吧。
由此可以管中窥豹Windows文件管理这个核心是多么的强大。希望对大家有所帮助。
{另附上一段演示读硬盘第一扇区 保存为 Project1.dpr WinXP+Delphi7编译通过}
program Project1;
var
str :string;
p :pchar;
i :Cardinal;
hDeviceHandle :Thandle;
begin
hDeviceHandle := CreateFile(drive, GENERIC_READ,
FILE_SHARE_READ OR FILE_SHARE_WRITE, nil, OPEN_EXISTING,0, 0);
if (hDeviceHandle <> INVALID_HANDLE_VALUE) then
begin
p:=allocmem(SectorCount*BytesPerSector);
FileSeek(hDevicehandle,SectorStart*BytesPerSector,0);
if FileRead(hDevicehandle,p[0],SectorCount*BytesPerSector)<>SectorCount*BytesPerSector then
raise exception.create('读取错误');
str:='';
for i:=0 to 512-1 do
begin
if i mod 16=0 then
str:=str+format('0x%.8x ',[i]);
str:=str+format(' %.2x',[integer(p[i])]);
if i mod 16=15 then
str:=str+#13#10;
end;
ShowMessage( str);