为什么会执行错误?请高手指点一二!!! ( 积分: 50 )

  • 主题发起人 主题发起人 landina
  • 开始时间 开始时间
L

landina

Unregistered / Unconfirmed
GUEST, unregistred user!
以下代码为什么会返回错误:5?<br><br>unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, StdCtrls;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;Button1: TButton;<br> &nbsp; &nbsp;Label1: TLabel;<br> &nbsp; &nbsp;procedure Button1Click(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br> &nbsp;PPARTITION_INFORMATION=^PARTITION_INFORMATION;<br> &nbsp;PARTITION_INFORMATION = RECORD<br> &nbsp; &nbsp;StartingOffset: TLargeInteger;<br> &nbsp; &nbsp;PartitionLength: TLargeInteger;<br> &nbsp; &nbsp;HiddenSectors: DWORD;<br> &nbsp; &nbsp;PartitionNumber: DWORD;<br> &nbsp; &nbsp;PartitionType: BYTE;<br> &nbsp; &nbsp;BootIndicator: WordBool;<br> &nbsp; &nbsp;RecognizedPartition: WordBool;<br> &nbsp; &nbsp;RewritePartition: WordBool;<br> &nbsp;end;<br><br> &nbsp;PDRIVE_LAYOUT_INFORMATION=^DRIVE_LAYOUT_INFORMATION;<br> &nbsp;DRIVE_LAYOUT_INFORMATION = Record<br> &nbsp; PartitionCount &nbsp; : DWORD;<br> &nbsp; Signature &nbsp; &nbsp; &nbsp; &nbsp;: DWORD;<br> &nbsp; Partitioninfo: array[0..1] of PARTITION_INFORMATION;<br> &nbsp; end;<br><br>var<br> &nbsp;Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>Function GetDiskGeometry(DriveNum : Longint) : PARTITION_INFORMATION;<br>Var<br> &nbsp;RetBytes &nbsp;: DWORD;<br> &nbsp;hDevice &nbsp; : Longint;<br> &nbsp;Status &nbsp; &nbsp;: Longbool;<br> &nbsp;Drive &nbsp; &nbsp; : String;<br> &nbsp;Layout : DRIVE_LAYOUT_INFORMATION;<br> &nbsp;Size : Integer;<br> &nbsp;Success: Boolean;<br>begin<br> &nbsp; &nbsp;Drive := '//./PhysicalDrive' + inttostr(DriveNum);<br><br> &nbsp; &nbsp;FillChar(Layout,SizeOf(DRIVE_LAYOUT_INFORMATION),0);<br> &nbsp; &nbsp;RetBytes := 0;<br><br> &nbsp; &nbsp;hDevice := CreateFile(PChar(Drive),0, FILE_SHARE_READ Or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0);<br><br><br> &nbsp; &nbsp;If hDevice &lt;&gt; INVALID_HANDLE_VALUE Then<br> &nbsp; &nbsp; &nbsp;begin<br><br> &nbsp; &nbsp; &nbsp; &nbsp;Status := DeviceIoControl(hDevice, 475148,nil, 0, @Layout, Sizeof(DRIVE_LAYOUT_INFORMATION), RetBytes, nil);<br> &nbsp; &nbsp; &nbsp; &nbsp;if (status = false) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;showmessage('Failed : '+inttostr(getLastError()));<br> &nbsp; &nbsp; &nbsp; &nbsp;showmessage(inttostr(Layout.PartitionCount));<br> &nbsp; &nbsp; &nbsp; &nbsp;CloseHandle(hDevice);<br> &nbsp; &nbsp; end;<br>End;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> &nbsp;layout: PARTITION_INFORMATION;<br>begin<br> &nbsp;layout := GetDiskGeometry(1);<br><br>end;<br><br>end.
 
以下代码为什么会返回错误:5?<br><br>unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, StdCtrls;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;Button1: TButton;<br> &nbsp; &nbsp;Label1: TLabel;<br> &nbsp; &nbsp;procedure Button1Click(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br> &nbsp;PPARTITION_INFORMATION=^PARTITION_INFORMATION;<br> &nbsp;PARTITION_INFORMATION = RECORD<br> &nbsp; &nbsp;StartingOffset: TLargeInteger;<br> &nbsp; &nbsp;PartitionLength: TLargeInteger;<br> &nbsp; &nbsp;HiddenSectors: DWORD;<br> &nbsp; &nbsp;PartitionNumber: DWORD;<br> &nbsp; &nbsp;PartitionType: BYTE;<br> &nbsp; &nbsp;BootIndicator: WordBool;<br> &nbsp; &nbsp;RecognizedPartition: WordBool;<br> &nbsp; &nbsp;RewritePartition: WordBool;<br> &nbsp;end;<br><br> &nbsp;PDRIVE_LAYOUT_INFORMATION=^DRIVE_LAYOUT_INFORMATION;<br> &nbsp;DRIVE_LAYOUT_INFORMATION = Record<br> &nbsp; PartitionCount &nbsp; : DWORD;<br> &nbsp; Signature &nbsp; &nbsp; &nbsp; &nbsp;: DWORD;<br> &nbsp; Partitioninfo: array[0..1] of PARTITION_INFORMATION;<br> &nbsp; end;<br><br>var<br> &nbsp;Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>Function GetDiskGeometry(DriveNum : Longint) : PARTITION_INFORMATION;<br>Var<br> &nbsp;RetBytes &nbsp;: DWORD;<br> &nbsp;hDevice &nbsp; : Longint;<br> &nbsp;Status &nbsp; &nbsp;: Longbool;<br> &nbsp;Drive &nbsp; &nbsp; : String;<br> &nbsp;Layout : DRIVE_LAYOUT_INFORMATION;<br> &nbsp;Size : Integer;<br> &nbsp;Success: Boolean;<br>begin<br> &nbsp; &nbsp;Drive := '//./PhysicalDrive' + inttostr(DriveNum);<br><br> &nbsp; &nbsp;FillChar(Layout,SizeOf(DRIVE_LAYOUT_INFORMATION),0);<br> &nbsp; &nbsp;RetBytes := 0;<br><br> &nbsp; &nbsp;hDevice := CreateFile(PChar(Drive),0, FILE_SHARE_READ Or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0);<br><br><br> &nbsp; &nbsp;If hDevice &lt;&gt; INVALID_HANDLE_VALUE Then<br> &nbsp; &nbsp; &nbsp;begin<br><br> &nbsp; &nbsp; &nbsp; &nbsp;Status := DeviceIoControl(hDevice, 475148,nil, 0, @Layout, Sizeof(DRIVE_LAYOUT_INFORMATION), RetBytes, nil);<br> &nbsp; &nbsp; &nbsp; &nbsp;if (status = false) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;showmessage('Failed : '+inttostr(getLastError()));<br> &nbsp; &nbsp; &nbsp; &nbsp;showmessage(inttostr(Layout.PartitionCount));<br> &nbsp; &nbsp; &nbsp; &nbsp;CloseHandle(hDevice);<br> &nbsp; &nbsp; end;<br>End;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> &nbsp;layout: PARTITION_INFORMATION;<br>begin<br> &nbsp;layout := GetDiskGeometry(1);<br><br>end;<br><br>end.
 
hDevice声明错了,应该这样声明:<br> &nbsp;hDevice &nbsp; : THandle;
 
to thx1180:<br><br>把申明改成THandle也出现一样的错误:5<br>5:拒绝访问
 
你这段代码的运行环境是什么?查一下msdn吧。
 
我是在2003 server和XP下都不能执行
 
到98下试试,代码是可以编译通过的对吧?
 
DeviceIoControl的获取版本的控制码能够执行啊,还有即使是VC能执行的代码翻译成delphi后也出现同样的错误,运行环境都一样的。
 
to:newsmile<br><br>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/ioctl_disk_get_drive_layout.asp<br><br>Client: Requires Windows XP, Windows 2000 Professional, or Windows NT Workstation. <br>Server: Requires Windows Server 2003, Windows 2000 Server, or Windows NT Server. <br>Header: Declared in Winioctl.h.
 
单步调试吧,看看那步出了问题,另外特别注意参数!尤其是后面那种#0之类,borland特色啊!
 
我也请台湾的一个高手调试过了,他也没有找出原因来啊,不信你帮我调试一下看看,看不出来那个地方有错误的
 
是createfile时候出错?
 
不是CreateFile错误,是<br>Status := DeviceIoControl(hDevice, 475148,nil, 0, @Layout, Sizeof(DRIVE_LAYOUT_INFORMATION), RetBytes, nil);<br>执行不成功。
 
参考: http://dev.csdn.net/article/62/62312.shtm<br>特别注意:需要JEDI的Win32支持库才能编译!JEDI好象是一个开源项目,免费的。
 
有结果了,来了告诉我一声。
 
问题已经解决了,谢谢newsmile
 
后退
顶部